Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / accessible / atk / nsMaiInterfaceDocument.cpp
blob6ce1e8ff4ccaefb7f706b17384460f1b8f5569c1
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)
32 NS_ASSERTION(aIface, "Invalid Interface");
33 if(MOZ_UNLIKELY(!aIface))
34 return;
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;
46 const gchar *
47 getDocumentLocaleCB(AtkDocument *aDocument)
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);
58 static inline GSList *
59 prependToList(GSList *aList, const char *const aName, const nsAutoString &aValue)
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);
71 AtkAttributeSet *
72 getDocumentAttributesCB(AtkDocument *aDocument)
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 document->URL(aURL);
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);
93 return attributes;
96 const gchar *
97 getDocumentAttributeValueCB(AtkDocument *aDocument,
98 const gchar *aAttrName)
100 AccessibleWrap* accWrap = GetAccessibleWrap(ATK_OBJECT(aDocument));
101 if (!accWrap || !accWrap->IsDoc())
102 return nullptr;
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);
112 else
113 return nullptr;
115 return attrValue.IsEmpty() ? nullptr : AccessibleWrap::ReturnString(attrValue);