1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "InterfaceInitFuncs.h"
9 #include "Accessible-inl.h"
10 #include "AccessibleWrap.h"
11 #include "DocAccessible.h"
13 #include "mozilla/Likely.h"
15 using namespace mozilla::a11y
;
17 static const char* const kDocTypeName
= "W3C-doctype";
18 static const char* const kDocUrlName
= "DocURL";
19 static const char* const kMimeTypeName
= "MimeType";
21 // below functions are vfuncs on an ATK interface so they need to be C call
24 static const gchar
* getDocumentLocaleCB(AtkDocument
* aDocument
);
25 static AtkAttributeSet
* getDocumentAttributesCB(AtkDocument
* aDocument
);
26 static const gchar
* getDocumentAttributeValueCB(AtkDocument
* aDocument
,
27 const gchar
* aAttrName
);
30 documentInterfaceInitCB(AtkDocumentIface
*aIface
)
32 NS_ASSERTION(aIface
, "Invalid Interface");
33 if(MOZ_UNLIKELY(!aIface
))
37 * We don't support get_document or set_attribute right now.
38 * get_document_type is deprecated, we return DocType in
39 * get_document_attribute_value and get_document_attributes instead.
41 aIface
->get_document_attributes
= getDocumentAttributesCB
;
42 aIface
->get_document_attribute_value
= getDocumentAttributeValueCB
;
43 aIface
->get_document_locale
= getDocumentLocaleCB
;
47 getDocumentLocaleCB(AtkDocument
*aDocument
)
49 AccessibleWrap
* accWrap
= GetAccessibleWrap(ATK_OBJECT(aDocument
));
54 accWrap
->Language(locale
);
55 return locale
.IsEmpty() ? nullptr : AccessibleWrap::ReturnString(locale
);
58 static inline GSList
*
59 prependToList(GSList
*aList
, const char *const aName
, const nsAutoString
&aValue
)
64 // libspi will free these
65 AtkAttribute
*atkAttr
= (AtkAttribute
*)g_malloc(sizeof(AtkAttribute
));
66 atkAttr
->name
= g_strdup(aName
);
67 atkAttr
->value
= g_strdup(NS_ConvertUTF16toUTF8(aValue
).get());
68 return g_slist_prepend(aList
, atkAttr
);
72 getDocumentAttributesCB(AtkDocument
*aDocument
)
74 AccessibleWrap
* accWrap
= GetAccessibleWrap(ATK_OBJECT(aDocument
));
75 if (!accWrap
|| !accWrap
->IsDoc())
78 // according to atkobject.h, AtkAttributeSet is a GSList
79 GSList
* attributes
= nullptr;
80 DocAccessible
* document
= accWrap
->AsDoc();
83 attributes
= prependToList(attributes
, kDocUrlName
, aURL
);
85 nsAutoString aW3CDocType
;
86 document
->DocType(aW3CDocType
);
87 attributes
= prependToList(attributes
, kDocTypeName
, aW3CDocType
);
89 nsAutoString aMimeType
;
90 document
->MimeType(aMimeType
);
91 attributes
= prependToList(attributes
, kMimeTypeName
, aMimeType
);
97 getDocumentAttributeValueCB(AtkDocument
*aDocument
,
98 const gchar
*aAttrName
)
100 AccessibleWrap
* accWrap
= GetAccessibleWrap(ATK_OBJECT(aDocument
));
101 if (!accWrap
|| !accWrap
->IsDoc())
104 DocAccessible
* document
= accWrap
->AsDoc();
105 nsAutoString attrValue
;
106 if (!strcasecmp(aAttrName
, kDocTypeName
))
107 document
->DocType(attrValue
);
108 else if (!strcasecmp(aAttrName
, kDocUrlName
))
109 document
->URL(attrValue
);
110 else if (!strcasecmp(aAttrName
, kMimeTypeName
))
111 document
->MimeType(attrValue
);
115 return attrValue
.IsEmpty() ? nullptr : AccessibleWrap::ReturnString(attrValue
);