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"
12 #include "nsAccUtils.h"
14 #include "RemoteAccessible.h"
15 #include "mozilla/a11y/DocAccessibleParent.h"
16 #include "mozilla/Likely.h"
18 using namespace mozilla::a11y
;
20 static const char* const kDocUrlName
= "DocURL";
21 static const char* const kMimeTypeName
= "MimeType";
23 // below functions are vfuncs on an ATK interface so they need to be C call
26 static const gchar
* getDocumentLocaleCB(AtkDocument
* aDocument
);
27 static AtkAttributeSet
* getDocumentAttributesCB(AtkDocument
* aDocument
);
28 static const gchar
* getDocumentAttributeValueCB(AtkDocument
* aDocument
,
29 const gchar
* aAttrName
);
31 void documentInterfaceInitCB(AtkDocumentIface
* aIface
) {
32 NS_ASSERTION(aIface
, "Invalid Interface");
33 if (MOZ_UNLIKELY(!aIface
)) return;
36 * We don't support get_document or set_attribute right now.
38 aIface
->get_document_attributes
= getDocumentAttributesCB
;
39 aIface
->get_document_attribute_value
= getDocumentAttributeValueCB
;
40 aIface
->get_document_locale
= getDocumentLocaleCB
;
43 const gchar
* getDocumentLocaleCB(AtkDocument
* aDocument
) {
45 Accessible
* acc
= GetInternalObj(ATK_OBJECT(aDocument
));
47 acc
->Language(locale
);
50 return locale
.IsEmpty() ? nullptr : AccessibleWrap::ReturnString(locale
);
53 static inline GSList
* prependToList(GSList
* aList
, const char* const aName
,
54 const nsAutoString
& aValue
) {
55 if (aValue
.IsEmpty()) {
59 // libspi will free these
60 AtkAttribute
* atkAttr
= (AtkAttribute
*)g_malloc(sizeof(AtkAttribute
));
61 atkAttr
->name
= g_strdup(aName
);
62 atkAttr
->value
= g_strdup(NS_ConvertUTF16toUTF8(aValue
).get());
63 return g_slist_prepend(aList
, atkAttr
);
66 AtkAttributeSet
* getDocumentAttributesCB(AtkDocument
* aDocument
) {
68 nsAutoString mimeType
;
69 Accessible
* acc
= GetInternalObj(ATK_OBJECT(aDocument
));
71 if (!acc
|| !acc
->IsDoc()) {
75 nsAccUtils::DocumentURL(acc
, url
);
76 nsAccUtils::DocumentMimeType(acc
, mimeType
);
78 // according to atkobject.h, AtkAttributeSet is a GSList
79 GSList
* attributes
= nullptr;
80 attributes
= prependToList(attributes
, kDocUrlName
, url
);
81 attributes
= prependToList(attributes
, kMimeTypeName
, mimeType
);
86 const gchar
* getDocumentAttributeValueCB(AtkDocument
* aDocument
,
87 const gchar
* aAttrName
) {
88 Accessible
* acc
= GetInternalObj(ATK_OBJECT(aDocument
));
90 if (!acc
|| !acc
->IsDoc()) {
94 nsAutoString attrValue
;
95 if (!strcasecmp(aAttrName
, kDocUrlName
)) {
96 nsAccUtils::DocumentURL(acc
, attrValue
);
97 } else if (!strcasecmp(aAttrName
, kMimeTypeName
)) {
98 nsAccUtils::DocumentMimeType(acc
, attrValue
);
103 return attrValue
.IsEmpty() ? nullptr
104 : AccessibleWrap::ReturnString(attrValue
);