nsMaiInterfaceDocument.cpp (3379B)
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/. */ 6 7 #include "InterfaceInitFuncs.h" 8 9 #include "LocalAccessible-inl.h" 10 #include "AccessibleWrap.h" 11 #include "DocAccessible.h" 12 #include "nsAccUtils.h" 13 #include "nsMai.h" 14 #include "RemoteAccessible.h" 15 #include "mozilla/a11y/DocAccessibleParent.h" 16 #include "mozilla/Likely.h" 17 18 using namespace mozilla::a11y; 19 20 static const char* const kDocUrlName = "DocURL"; 21 static const char* const kMimeTypeName = "MimeType"; 22 23 // below functions are vfuncs on an ATK interface so they need to be C call 24 extern "C" { 25 26 static const gchar* getDocumentLocaleCB(AtkDocument* aDocument); 27 static AtkAttributeSet* getDocumentAttributesCB(AtkDocument* aDocument); 28 static const gchar* getDocumentAttributeValueCB(AtkDocument* aDocument, 29 const gchar* aAttrName); 30 31 void documentInterfaceInitCB(AtkDocumentIface* aIface) { 32 NS_ASSERTION(aIface, "Invalid Interface"); 33 if (MOZ_UNLIKELY(!aIface)) return; 34 35 /* 36 * We don't support get_document or set_attribute right now. 37 */ 38 aIface->get_document_attributes = getDocumentAttributesCB; 39 aIface->get_document_attribute_value = getDocumentAttributeValueCB; 40 aIface->get_document_locale = getDocumentLocaleCB; 41 } 42 43 const gchar* getDocumentLocaleCB(AtkDocument* aDocument) { 44 nsAutoString locale; 45 Accessible* acc = GetInternalObj(ATK_OBJECT(aDocument)); 46 if (acc) { 47 acc->Language(locale); 48 } 49 50 return locale.IsEmpty() ? nullptr : AccessibleWrap::ReturnString(locale); 51 } 52 53 static inline GSList* prependToList(GSList* aList, const char* const aName, 54 const nsAutoString& aValue) { 55 if (aValue.IsEmpty()) { 56 return aList; 57 } 58 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); 64 } 65 66 AtkAttributeSet* getDocumentAttributesCB(AtkDocument* aDocument) { 67 nsAutoString url; 68 nsAutoString mimeType; 69 Accessible* acc = GetInternalObj(ATK_OBJECT(aDocument)); 70 71 if (!acc || !acc->IsDoc()) { 72 return nullptr; 73 } 74 75 nsAccUtils::DocumentURL(acc, url); 76 nsAccUtils::DocumentMimeType(acc, mimeType); 77 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); 82 83 return attributes; 84 } 85 86 const gchar* getDocumentAttributeValueCB(AtkDocument* aDocument, 87 const gchar* aAttrName) { 88 Accessible* acc = GetInternalObj(ATK_OBJECT(aDocument)); 89 90 if (!acc || !acc->IsDoc()) { 91 return nullptr; 92 } 93 94 nsAutoString attrValue; 95 if (!strcasecmp(aAttrName, kDocUrlName)) { 96 nsAccUtils::DocumentURL(acc, attrValue); 97 } else if (!strcasecmp(aAttrName, kMimeTypeName)) { 98 nsAccUtils::DocumentMimeType(acc, attrValue); 99 } else { 100 return nullptr; 101 } 102 103 return attrValue.IsEmpty() ? nullptr 104 : AccessibleWrap::ReturnString(attrValue); 105 } 106 }