Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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"
12 #include "nsMai.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
22 extern "C" {
24 static const gchar* getDocumentLocaleCB(AtkDocument* aDocument);
25 static AtkAttributeSet* getDocumentAttributesCB(AtkDocument* aDocument);
26 static const gchar* getDocumentAttributeValueCB(AtkDocument* aDocument,
27 const gchar* aAttrName);
29 void
30 documentInterfaceInitCB(AtkDocumentIface *aIface)
31 {
32 NS_ASSERTION(aIface, "Invalid Interface");
33 if(MOZ_UNLIKELY(!aIface))
34 return;
36 /*
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.
40 */
41 aIface->get_document_attributes = getDocumentAttributesCB;
42 aIface->get_document_attribute_value = getDocumentAttributeValueCB;
43 aIface->get_document_locale = getDocumentLocaleCB;
44 }
46 const gchar *
47 getDocumentLocaleCB(AtkDocument *aDocument)
48 {
49 AccessibleWrap* accWrap = GetAccessibleWrap(ATK_OBJECT(aDocument));
50 if (!accWrap)
51 return nullptr;
53 nsAutoString locale;
54 accWrap->Language(locale);
55 return locale.IsEmpty() ? nullptr : AccessibleWrap::ReturnString(locale);
56 }
58 static inline GSList *
59 prependToList(GSList *aList, const char *const aName, const nsAutoString &aValue)
60 {
61 if (aValue.IsEmpty())
62 return aList;
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);
69 }
71 AtkAttributeSet *
72 getDocumentAttributesCB(AtkDocument *aDocument)
73 {
74 AccessibleWrap* accWrap = GetAccessibleWrap(ATK_OBJECT(aDocument));
75 if (!accWrap || !accWrap->IsDoc())
76 return nullptr;
78 // according to atkobject.h, AtkAttributeSet is a GSList
79 GSList* attributes = nullptr;
80 DocAccessible* document = accWrap->AsDoc();
81 nsAutoString aURL;
82 nsresult rv = document->GetURL(aURL);
83 if (NS_SUCCEEDED(rv))
84 attributes = prependToList(attributes, kDocUrlName, aURL);
86 nsAutoString aW3CDocType;
87 rv = document->GetDocType(aW3CDocType);
88 if (NS_SUCCEEDED(rv))
89 attributes = prependToList(attributes, kDocTypeName, aW3CDocType);
91 nsAutoString aMimeType;
92 rv = document->GetMimeType(aMimeType);
93 if (NS_SUCCEEDED(rv))
94 attributes = prependToList(attributes, kMimeTypeName, aMimeType);
96 return attributes;
97 }
99 const gchar *
100 getDocumentAttributeValueCB(AtkDocument *aDocument,
101 const gchar *aAttrName)
102 {
103 AccessibleWrap* accWrap = GetAccessibleWrap(ATK_OBJECT(aDocument));
104 if (!accWrap || !accWrap->IsDoc())
105 return nullptr;
107 DocAccessible* document = accWrap->AsDoc();
108 nsresult rv;
109 nsAutoString attrValue;
110 if (!strcasecmp(aAttrName, kDocTypeName))
111 rv = document->GetDocType(attrValue);
112 else if (!strcasecmp(aAttrName, kDocUrlName))
113 rv = document->GetURL(attrValue);
114 else if (!strcasecmp(aAttrName, kMimeTypeName))
115 rv = document->GetMimeType(attrValue);
116 else
117 return nullptr;
119 NS_ENSURE_SUCCESS(rv, nullptr);
120 return attrValue.IsEmpty() ? nullptr : AccessibleWrap::ReturnString(attrValue);
121 }
122 }