1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/accessible/src/atk/nsMaiInterfaceDocument.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,122 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "InterfaceInitFuncs.h" 1.11 + 1.12 +#include "Accessible-inl.h" 1.13 +#include "AccessibleWrap.h" 1.14 +#include "DocAccessible.h" 1.15 +#include "nsMai.h" 1.16 +#include "mozilla/Likely.h" 1.17 + 1.18 +using namespace mozilla::a11y; 1.19 + 1.20 +static const char* const kDocTypeName = "W3C-doctype"; 1.21 +static const char* const kDocUrlName = "DocURL"; 1.22 +static const char* const kMimeTypeName = "MimeType"; 1.23 + 1.24 +// below functions are vfuncs on an ATK interface so they need to be C call 1.25 +extern "C" { 1.26 + 1.27 +static const gchar* getDocumentLocaleCB(AtkDocument* aDocument); 1.28 +static AtkAttributeSet* getDocumentAttributesCB(AtkDocument* aDocument); 1.29 +static const gchar* getDocumentAttributeValueCB(AtkDocument* aDocument, 1.30 + const gchar* aAttrName); 1.31 + 1.32 +void 1.33 +documentInterfaceInitCB(AtkDocumentIface *aIface) 1.34 +{ 1.35 + NS_ASSERTION(aIface, "Invalid Interface"); 1.36 + if(MOZ_UNLIKELY(!aIface)) 1.37 + return; 1.38 + 1.39 + /* 1.40 + * We don't support get_document or set_attribute right now. 1.41 + * get_document_type is deprecated, we return DocType in 1.42 + * get_document_attribute_value and get_document_attributes instead. 1.43 + */ 1.44 + aIface->get_document_attributes = getDocumentAttributesCB; 1.45 + aIface->get_document_attribute_value = getDocumentAttributeValueCB; 1.46 + aIface->get_document_locale = getDocumentLocaleCB; 1.47 +} 1.48 + 1.49 +const gchar * 1.50 +getDocumentLocaleCB(AtkDocument *aDocument) 1.51 +{ 1.52 + AccessibleWrap* accWrap = GetAccessibleWrap(ATK_OBJECT(aDocument)); 1.53 + if (!accWrap) 1.54 + return nullptr; 1.55 + 1.56 + nsAutoString locale; 1.57 + accWrap->Language(locale); 1.58 + return locale.IsEmpty() ? nullptr : AccessibleWrap::ReturnString(locale); 1.59 +} 1.60 + 1.61 +static inline GSList * 1.62 +prependToList(GSList *aList, const char *const aName, const nsAutoString &aValue) 1.63 +{ 1.64 + if (aValue.IsEmpty()) 1.65 + return aList; 1.66 + 1.67 + // libspi will free these 1.68 + AtkAttribute *atkAttr = (AtkAttribute *)g_malloc(sizeof(AtkAttribute)); 1.69 + atkAttr->name = g_strdup(aName); 1.70 + atkAttr->value = g_strdup(NS_ConvertUTF16toUTF8(aValue).get()); 1.71 + return g_slist_prepend(aList, atkAttr); 1.72 +} 1.73 + 1.74 +AtkAttributeSet * 1.75 +getDocumentAttributesCB(AtkDocument *aDocument) 1.76 +{ 1.77 + AccessibleWrap* accWrap = GetAccessibleWrap(ATK_OBJECT(aDocument)); 1.78 + if (!accWrap || !accWrap->IsDoc()) 1.79 + return nullptr; 1.80 + 1.81 + // according to atkobject.h, AtkAttributeSet is a GSList 1.82 + GSList* attributes = nullptr; 1.83 + DocAccessible* document = accWrap->AsDoc(); 1.84 + nsAutoString aURL; 1.85 + nsresult rv = document->GetURL(aURL); 1.86 + if (NS_SUCCEEDED(rv)) 1.87 + attributes = prependToList(attributes, kDocUrlName, aURL); 1.88 + 1.89 + nsAutoString aW3CDocType; 1.90 + rv = document->GetDocType(aW3CDocType); 1.91 + if (NS_SUCCEEDED(rv)) 1.92 + attributes = prependToList(attributes, kDocTypeName, aW3CDocType); 1.93 + 1.94 + nsAutoString aMimeType; 1.95 + rv = document->GetMimeType(aMimeType); 1.96 + if (NS_SUCCEEDED(rv)) 1.97 + attributes = prependToList(attributes, kMimeTypeName, aMimeType); 1.98 + 1.99 + return attributes; 1.100 +} 1.101 + 1.102 +const gchar * 1.103 +getDocumentAttributeValueCB(AtkDocument *aDocument, 1.104 + const gchar *aAttrName) 1.105 +{ 1.106 + AccessibleWrap* accWrap = GetAccessibleWrap(ATK_OBJECT(aDocument)); 1.107 + if (!accWrap || !accWrap->IsDoc()) 1.108 + return nullptr; 1.109 + 1.110 + DocAccessible* document = accWrap->AsDoc(); 1.111 + nsresult rv; 1.112 + nsAutoString attrValue; 1.113 + if (!strcasecmp(aAttrName, kDocTypeName)) 1.114 + rv = document->GetDocType(attrValue); 1.115 + else if (!strcasecmp(aAttrName, kDocUrlName)) 1.116 + rv = document->GetURL(attrValue); 1.117 + else if (!strcasecmp(aAttrName, kMimeTypeName)) 1.118 + rv = document->GetMimeType(attrValue); 1.119 + else 1.120 + return nullptr; 1.121 + 1.122 + NS_ENSURE_SUCCESS(rv, nullptr); 1.123 + return attrValue.IsEmpty() ? nullptr : AccessibleWrap::ReturnString(attrValue); 1.124 +} 1.125 +}