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 "LocalAccessible-inl.h"
10 #include "AccessibleWrap.h"
11 #include "DocAccessible.h"
13 #include "RemoteAccessible.h"
14 #include "mozilla/Likely.h"
16 using namespace mozilla::a11y
;
18 static const char* const kDocTypeName
= "W3C-doctype";
19 static const char* const kDocUrlName
= "DocURL";
20 static const char* const kMimeTypeName
= "MimeType";
22 // below functions are vfuncs on an ATK interface so they need to be C call
25 static const gchar
* getDocumentLocaleCB(AtkDocument
* aDocument
);
26 static AtkAttributeSet
* getDocumentAttributesCB(AtkDocument
* aDocument
);
27 static const gchar
* getDocumentAttributeValueCB(AtkDocument
* aDocument
,
28 const gchar
* aAttrName
);
30 void documentInterfaceInitCB(AtkDocumentIface
* aIface
) {
31 NS_ASSERTION(aIface
, "Invalid Interface");
32 if (MOZ_UNLIKELY(!aIface
)) return;
35 * We don't support get_document or set_attribute right now.
36 * get_document_type is deprecated, we return DocType in
37 * get_document_attribute_value and get_document_attributes instead.
39 aIface
->get_document_attributes
= getDocumentAttributesCB
;
40 aIface
->get_document_attribute_value
= getDocumentAttributeValueCB
;
41 aIface
->get_document_locale
= getDocumentLocaleCB
;
44 const gchar
* getDocumentLocaleCB(AtkDocument
* aDocument
) {
46 AccessibleWrap
* accWrap
= GetAccessibleWrap(ATK_OBJECT(aDocument
));
48 accWrap
->Language(locale
);
49 } else if (RemoteAccessible
* proxy
= GetProxy(ATK_OBJECT(aDocument
))) {
50 proxy
->Language(locale
);
53 return locale
.IsEmpty() ? nullptr : AccessibleWrap::ReturnString(locale
);
56 static inline GSList
* prependToList(GSList
* aList
, const char* const aName
,
57 const nsAutoString
& aValue
) {
58 if (aValue
.IsEmpty()) {
62 // libspi will free these
63 AtkAttribute
* atkAttr
= (AtkAttribute
*)g_malloc(sizeof(AtkAttribute
));
64 atkAttr
->name
= g_strdup(aName
);
65 atkAttr
->value
= g_strdup(NS_ConvertUTF16toUTF8(aValue
).get());
66 return g_slist_prepend(aList
, atkAttr
);
69 AtkAttributeSet
* getDocumentAttributesCB(AtkDocument
* aDocument
) {
71 nsAutoString w3cDocType
;
72 nsAutoString mimeType
;
73 AccessibleWrap
* accWrap
= GetAccessibleWrap(ATK_OBJECT(aDocument
));
75 if (!accWrap
->IsDoc()) {
79 DocAccessible
* document
= accWrap
->AsDoc();
81 document
->DocType(w3cDocType
);
82 document
->MimeType(mimeType
);
83 } else if (RemoteAccessible
* proxy
= GetProxy(ATK_OBJECT(aDocument
))) {
84 proxy
->URLDocTypeMimeType(url
, w3cDocType
, mimeType
);
89 // according to atkobject.h, AtkAttributeSet is a GSList
90 GSList
* attributes
= nullptr;
91 attributes
= prependToList(attributes
, kDocUrlName
, url
);
92 attributes
= prependToList(attributes
, kDocTypeName
, w3cDocType
);
93 attributes
= prependToList(attributes
, kMimeTypeName
, mimeType
);
98 const gchar
* getDocumentAttributeValueCB(AtkDocument
* aDocument
,
99 const gchar
* aAttrName
) {
100 RemoteAccessible
* proxy
= nullptr;
101 DocAccessible
* document
= nullptr;
102 AccessibleWrap
* accWrap
= GetAccessibleWrap(ATK_OBJECT(aDocument
));
104 if (!accWrap
->IsDoc()) {
108 document
= accWrap
->AsDoc();
110 proxy
= GetProxy(ATK_OBJECT(aDocument
));
116 nsAutoString attrValue
;
117 if (!strcasecmp(aAttrName
, kDocTypeName
)) {
119 document
->DocType(attrValue
);
121 proxy
->DocType(attrValue
);
123 } else if (!strcasecmp(aAttrName
, kDocUrlName
)) {
125 document
->URL(attrValue
);
127 proxy
->URL(attrValue
);
129 } else if (!strcasecmp(aAttrName
, kMimeTypeName
)) {
131 document
->MimeType(attrValue
);
133 proxy
->MimeType(attrValue
);
139 return attrValue
.IsEmpty() ? nullptr
140 : AccessibleWrap::ReturnString(attrValue
);