nsAccessibilityService.cpp 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsAccessibilityService.h"
  6. // NOTE: alphabetically ordered
  7. #include "ApplicationAccessibleWrap.h"
  8. #include "ARIAGridAccessibleWrap.h"
  9. #include "ARIAMap.h"
  10. #include "DocAccessible-inl.h"
  11. #include "FocusManager.h"
  12. #include "HTMLCanvasAccessible.h"
  13. #include "HTMLElementAccessibles.h"
  14. #include "HTMLImageMapAccessible.h"
  15. #include "HTMLLinkAccessible.h"
  16. #include "HTMLListAccessible.h"
  17. #include "HTMLSelectAccessible.h"
  18. #include "HTMLTableAccessibleWrap.h"
  19. #include "HyperTextAccessibleWrap.h"
  20. #include "RootAccessible.h"
  21. #include "nsAccUtils.h"
  22. #include "nsArrayUtils.h"
  23. #include "nsAttrName.h"
  24. #include "nsEventShell.h"
  25. #include "nsIURI.h"
  26. #include "OuterDocAccessible.h"
  27. #include "Platform.h"
  28. #include "Role.h"
  29. #ifdef MOZ_ACCESSIBILITY_ATK
  30. #include "RootAccessibleWrap.h"
  31. #endif
  32. #include "States.h"
  33. #include "TextLeafAccessibleWrap.h"
  34. #include "TreeWalker.h"
  35. #include "xpcAccessibleApplication.h"
  36. #include "xpcAccessibleDocument.h"
  37. #ifdef MOZ_ACCESSIBILITY_ATK
  38. #include "AtkSocketAccessible.h"
  39. #endif
  40. #ifdef XP_WIN
  41. #include "mozilla/a11y/Compatibility.h"
  42. #include "mozilla/dom/ContentChild.h"
  43. #include "HTMLWin32ObjectAccessible.h"
  44. #include "mozilla/StaticPtr.h"
  45. #endif
  46. #ifdef A11Y_LOG
  47. #include "Logging.h"
  48. #endif
  49. #include "nsImageFrame.h"
  50. #include "nsIObserverService.h"
  51. #include "nsLayoutUtils.h"
  52. #include "nsPluginFrame.h"
  53. #include "nsSVGPathGeometryFrame.h"
  54. #include "nsTreeBodyFrame.h"
  55. #include "nsTreeColumns.h"
  56. #include "nsTreeUtils.h"
  57. #include "nsXBLPrototypeBinding.h"
  58. #include "nsXBLBinding.h"
  59. #include "mozilla/ArrayUtils.h"
  60. #include "mozilla/dom/DOMStringList.h"
  61. #include "mozilla/Preferences.h"
  62. #include "mozilla/Services.h"
  63. #include "nsDeckFrame.h"
  64. #ifdef MOZ_XUL
  65. #include "XULAlertAccessible.h"
  66. #include "XULColorPickerAccessible.h"
  67. #include "XULComboboxAccessible.h"
  68. #include "XULElementAccessibles.h"
  69. #include "XULFormControlAccessible.h"
  70. #include "XULListboxAccessibleWrap.h"
  71. #include "XULMenuAccessibleWrap.h"
  72. #include "XULSliderAccessible.h"
  73. #include "XULTabAccessible.h"
  74. #include "XULTreeGridAccessibleWrap.h"
  75. #endif
  76. #if defined(XP_WIN) || defined(MOZ_ACCESSIBILITY_ATK)
  77. #include "nsNPAPIPluginInstance.h"
  78. #endif
  79. using namespace mozilla;
  80. using namespace mozilla::a11y;
  81. using namespace mozilla::dom;
  82. ////////////////////////////////////////////////////////////////////////////////
  83. // Statics
  84. ////////////////////////////////////////////////////////////////////////////////
  85. /**
  86. * Return true if the element must be accessible.
  87. */
  88. static bool
  89. MustBeAccessible(nsIContent* aContent, DocAccessible* aDocument)
  90. {
  91. if (aContent->GetPrimaryFrame()->IsFocusable())
  92. return true;
  93. uint32_t attrCount = aContent->GetAttrCount();
  94. for (uint32_t attrIdx = 0; attrIdx < attrCount; attrIdx++) {
  95. const nsAttrName* attr = aContent->GetAttrNameAt(attrIdx);
  96. if (attr->NamespaceEquals(kNameSpaceID_None)) {
  97. nsIAtom* attrAtom = attr->Atom();
  98. nsDependentAtomString attrStr(attrAtom);
  99. if (!StringBeginsWith(attrStr, NS_LITERAL_STRING("aria-")))
  100. continue; // not ARIA
  101. // A global state or a property and in case of token defined.
  102. uint8_t attrFlags = aria::AttrCharacteristicsFor(attrAtom);
  103. if ((attrFlags & ATTR_GLOBAL) && (!(attrFlags & ATTR_VALTOKEN) ||
  104. nsAccUtils::HasDefinedARIAToken(aContent, attrAtom))) {
  105. return true;
  106. }
  107. }
  108. }
  109. // If the given ID is referred by relation attribute then create an accessible
  110. // for it.
  111. nsAutoString id;
  112. if (nsCoreUtils::GetID(aContent, id) && !id.IsEmpty())
  113. return aDocument->IsDependentID(id);
  114. return false;
  115. }
  116. ////////////////////////////////////////////////////////////////////////////////
  117. // Accessible constructors
  118. static Accessible*
  119. New_HTMLLink(nsIContent* aContent, Accessible* aContext)
  120. {
  121. // Only some roles truly enjoy life as HTMLLinkAccessibles, for details
  122. // see closed bug 494807.
  123. const nsRoleMapEntry* roleMapEntry = aria::GetRoleMap(aContent->AsElement());
  124. if (roleMapEntry && roleMapEntry->role != roles::NOTHING &&
  125. roleMapEntry->role != roles::LINK) {
  126. return new HyperTextAccessibleWrap(aContent, aContext->Document());
  127. }
  128. return new HTMLLinkAccessible(aContent, aContext->Document());
  129. }
  130. static Accessible* New_HyperText(nsIContent* aContent, Accessible* aContext)
  131. { return new HyperTextAccessibleWrap(aContent, aContext->Document()); }
  132. static Accessible* New_HTMLFigcaption(nsIContent* aContent, Accessible* aContext)
  133. { return new HTMLFigcaptionAccessible(aContent, aContext->Document()); }
  134. static Accessible* New_HTMLFigure(nsIContent* aContent, Accessible* aContext)
  135. { return new HTMLFigureAccessible(aContent, aContext->Document()); }
  136. static Accessible* New_HTMLLegend(nsIContent* aContent, Accessible* aContext)
  137. { return new HTMLLegendAccessible(aContent, aContext->Document()); }
  138. static Accessible* New_HTMLOption(nsIContent* aContent, Accessible* aContext)
  139. { return new HTMLSelectOptionAccessible(aContent, aContext->Document()); }
  140. static Accessible* New_HTMLOptgroup(nsIContent* aContent, Accessible* aContext)
  141. { return new HTMLSelectOptGroupAccessible(aContent, aContext->Document()); }
  142. static Accessible* New_HTMLList(nsIContent* aContent, Accessible* aContext)
  143. { return new HTMLListAccessible(aContent, aContext->Document()); }
  144. static Accessible*
  145. New_HTMLListitem(nsIContent* aContent, Accessible* aContext)
  146. {
  147. // If list item is a child of accessible list then create an accessible for
  148. // it unconditionally by tag name. nsBlockFrame creates the list item
  149. // accessible for other elements styled as list items.
  150. if (aContext->IsList() && aContext->GetContent() == aContent->GetParent())
  151. return new HTMLLIAccessible(aContent, aContext->Document());
  152. return nullptr;
  153. }
  154. static Accessible*
  155. New_HTMLDefinition(nsIContent* aContent, Accessible* aContext)
  156. {
  157. if (aContext->IsList())
  158. return new HyperTextAccessibleWrap(aContent, aContext->Document());
  159. return nullptr;
  160. }
  161. static Accessible* New_HTMLLabel(nsIContent* aContent, Accessible* aContext)
  162. { return new HTMLLabelAccessible(aContent, aContext->Document()); }
  163. static Accessible* New_HTMLOutput(nsIContent* aContent, Accessible* aContext)
  164. { return new HTMLOutputAccessible(aContent, aContext->Document()); }
  165. static Accessible* New_HTMLProgress(nsIContent* aContent, Accessible* aContext)
  166. { return new HTMLProgressMeterAccessible(aContent, aContext->Document()); }
  167. static Accessible* New_HTMLSummary(nsIContent* aContent, Accessible* aContext)
  168. { return new HTMLSummaryAccessible(aContent, aContext->Document()); }
  169. static Accessible*
  170. New_HTMLTableAccessible(nsIContent* aContent, Accessible* aContext)
  171. { return new HTMLTableAccessible(aContent, aContext->Document()); }
  172. static Accessible*
  173. New_HTMLTableRowAccessible(nsIContent* aContent, Accessible* aContext)
  174. { return new HTMLTableRowAccessible(aContent, aContext->Document()); }
  175. static Accessible*
  176. New_HTMLTableCellAccessible(nsIContent* aContent, Accessible* aContext)
  177. { return new HTMLTableCellAccessible(aContent, aContext->Document()); }
  178. static Accessible*
  179. New_HTMLTableHeaderCell(nsIContent* aContent, Accessible* aContext)
  180. {
  181. if (aContext->IsTableRow() && aContext->GetContent() == aContent->GetParent())
  182. return new HTMLTableHeaderCellAccessibleWrap(aContent, aContext->Document());
  183. return nullptr;
  184. }
  185. static Accessible*
  186. New_HTMLTableHeaderCellIfScope(nsIContent* aContent, Accessible* aContext)
  187. {
  188. if (aContext->IsTableRow() && aContext->GetContent() == aContent->GetParent() &&
  189. aContent->HasAttr(kNameSpaceID_None, nsGkAtoms::scope))
  190. return new HTMLTableHeaderCellAccessibleWrap(aContent, aContext->Document());
  191. return nullptr;
  192. }
  193. ////////////////////////////////////////////////////////////////////////////////
  194. // Markup maps array.
  195. #define Attr(name, value) \
  196. { &nsGkAtoms::name, &nsGkAtoms::value }
  197. #define AttrFromDOM(name, DOMAttrName) \
  198. { &nsGkAtoms::name, nullptr, &nsGkAtoms::DOMAttrName }
  199. #define AttrFromDOMIf(name, DOMAttrName, DOMAttrValue) \
  200. { &nsGkAtoms::name, nullptr, &nsGkAtoms::DOMAttrName, &nsGkAtoms::DOMAttrValue }
  201. #define MARKUPMAP(atom, new_func, r, ... ) \
  202. { &nsGkAtoms::atom, new_func, static_cast<a11y::role>(r), { __VA_ARGS__ } },
  203. static const MarkupMapInfo sMarkupMapList[] = {
  204. #include "MarkupMap.h"
  205. };
  206. #undef Attr
  207. #undef AttrFromDOM
  208. #undef AttrFromDOMIf
  209. #undef MARKUPMAP
  210. ////////////////////////////////////////////////////////////////////////////////
  211. // nsAccessibilityService
  212. ////////////////////////////////////////////////////////////////////////////////
  213. nsAccessibilityService *nsAccessibilityService::gAccessibilityService = nullptr;
  214. ApplicationAccessible* nsAccessibilityService::gApplicationAccessible = nullptr;
  215. xpcAccessibleApplication* nsAccessibilityService::gXPCApplicationAccessible = nullptr;
  216. uint32_t nsAccessibilityService::gConsumers = 0;
  217. nsAccessibilityService::nsAccessibilityService() :
  218. DocManager(), FocusManager(), mMarkupMaps(ArrayLength(sMarkupMapList))
  219. {
  220. }
  221. nsAccessibilityService::~nsAccessibilityService()
  222. {
  223. NS_ASSERTION(IsShutdown(), "Accessibility wasn't shutdown!");
  224. gAccessibilityService = nullptr;
  225. }
  226. ////////////////////////////////////////////////////////////////////////////////
  227. // nsIListenerChangeListener
  228. NS_IMETHODIMP
  229. nsAccessibilityService::ListenersChanged(nsIArray* aEventChanges)
  230. {
  231. uint32_t targetCount;
  232. nsresult rv = aEventChanges->GetLength(&targetCount);
  233. NS_ENSURE_SUCCESS(rv, rv);
  234. for (uint32_t i = 0 ; i < targetCount ; i++) {
  235. nsCOMPtr<nsIEventListenerChange> change = do_QueryElementAt(aEventChanges, i);
  236. nsCOMPtr<nsIDOMEventTarget> target;
  237. change->GetTarget(getter_AddRefs(target));
  238. nsCOMPtr<nsIContent> node(do_QueryInterface(target));
  239. if (!node || !node->IsHTMLElement()) {
  240. continue;
  241. }
  242. nsCOMPtr<nsIArray> listenerNames;
  243. change->GetChangedListenerNames(getter_AddRefs(listenerNames));
  244. uint32_t changeCount;
  245. rv = listenerNames->GetLength(&changeCount);
  246. NS_ENSURE_SUCCESS(rv, rv);
  247. for (uint32_t i = 0 ; i < changeCount ; i++) {
  248. nsCOMPtr<nsIAtom> listenerName = do_QueryElementAt(listenerNames, i);
  249. // We are only interested in event listener changes which may
  250. // make an element accessible or inaccessible.
  251. if (listenerName != nsGkAtoms::onclick &&
  252. listenerName != nsGkAtoms::onmousedown &&
  253. listenerName != nsGkAtoms::onmouseup) {
  254. continue;
  255. }
  256. nsIDocument* ownerDoc = node->OwnerDoc();
  257. DocAccessible* document = GetExistingDocAccessible(ownerDoc);
  258. // Create an accessible for a inaccessible element having click event
  259. // handler.
  260. if (document && !document->HasAccessible(node) &&
  261. nsCoreUtils::HasClickListener(node)) {
  262. nsIContent* parentEl = node->GetFlattenedTreeParent();
  263. if (parentEl) {
  264. document->ContentInserted(parentEl, node, node->GetNextSibling());
  265. }
  266. break;
  267. }
  268. }
  269. }
  270. return NS_OK;
  271. }
  272. ////////////////////////////////////////////////////////////////////////////////
  273. // nsISupports
  274. NS_IMPL_ISUPPORTS_INHERITED(nsAccessibilityService,
  275. DocManager,
  276. nsIObserver,
  277. nsIListenerChangeListener,
  278. nsISelectionListener) // from SelectionManager
  279. ////////////////////////////////////////////////////////////////////////////////
  280. // nsIObserver
  281. NS_IMETHODIMP
  282. nsAccessibilityService::Observe(nsISupports *aSubject, const char *aTopic,
  283. const char16_t *aData)
  284. {
  285. if (!nsCRT::strcmp(aTopic, NS_XPCOM_SHUTDOWN_OBSERVER_ID))
  286. Shutdown();
  287. return NS_OK;
  288. }
  289. void
  290. nsAccessibilityService::NotifyOfAnchorJumpTo(nsIContent* aTargetNode)
  291. {
  292. nsIDocument* documentNode = aTargetNode->GetUncomposedDoc();
  293. if (documentNode) {
  294. DocAccessible* document = GetDocAccessible(documentNode);
  295. if (document)
  296. document->SetAnchorJump(aTargetNode);
  297. }
  298. }
  299. void
  300. nsAccessibilityService::FireAccessibleEvent(uint32_t aEvent,
  301. Accessible* aTarget)
  302. {
  303. nsEventShell::FireEvent(aEvent, aTarget);
  304. }
  305. Accessible*
  306. nsAccessibilityService::GetRootDocumentAccessible(nsIPresShell* aPresShell,
  307. bool aCanCreate)
  308. {
  309. nsIPresShell* ps = aPresShell;
  310. nsIDocument* documentNode = aPresShell->GetDocument();
  311. if (documentNode) {
  312. nsCOMPtr<nsIDocShellTreeItem> treeItem(documentNode->GetDocShell());
  313. if (treeItem) {
  314. nsCOMPtr<nsIDocShellTreeItem> rootTreeItem;
  315. treeItem->GetRootTreeItem(getter_AddRefs(rootTreeItem));
  316. if (treeItem != rootTreeItem) {
  317. nsCOMPtr<nsIDocShell> docShell(do_QueryInterface(rootTreeItem));
  318. ps = docShell->GetPresShell();
  319. }
  320. return aCanCreate ? GetDocAccessible(ps) : ps->GetDocAccessible();
  321. }
  322. }
  323. return nullptr;
  324. }
  325. #ifdef XP_WIN
  326. static StaticAutoPtr<nsTArray<nsCOMPtr<nsIContent> > > sPendingPlugins;
  327. static StaticAutoPtr<nsTArray<nsCOMPtr<nsITimer> > > sPluginTimers;
  328. class PluginTimerCallBack final : public nsITimerCallback
  329. {
  330. ~PluginTimerCallBack() {}
  331. public:
  332. PluginTimerCallBack(nsIContent* aContent) : mContent(aContent) {}
  333. NS_DECL_ISUPPORTS
  334. NS_IMETHOD Notify(nsITimer* aTimer) final
  335. {
  336. if (!mContent->IsInUncomposedDoc())
  337. return NS_OK;
  338. nsIPresShell* ps = mContent->OwnerDoc()->GetShell();
  339. if (ps) {
  340. DocAccessible* doc = ps->GetDocAccessible();
  341. if (doc) {
  342. // Make sure that if we created an accessible for the plugin that wasn't
  343. // a plugin accessible we remove it before creating the right accessible.
  344. doc->RecreateAccessible(mContent);
  345. sPluginTimers->RemoveElement(aTimer);
  346. return NS_OK;
  347. }
  348. }
  349. // We couldn't get a doc accessible so presumably the document went away.
  350. // In this case don't leak our ref to the content or timer.
  351. sPendingPlugins->RemoveElement(mContent);
  352. sPluginTimers->RemoveElement(aTimer);
  353. return NS_OK;
  354. }
  355. private:
  356. nsCOMPtr<nsIContent> mContent;
  357. };
  358. NS_IMPL_ISUPPORTS(PluginTimerCallBack, nsITimerCallback)
  359. #endif
  360. already_AddRefed<Accessible>
  361. nsAccessibilityService::CreatePluginAccessible(nsPluginFrame* aFrame,
  362. nsIContent* aContent,
  363. Accessible* aContext)
  364. {
  365. // nsPluginFrame means a plugin, so we need to use the accessibility support
  366. // of the plugin.
  367. if (aFrame->GetRect().IsEmpty())
  368. return nullptr;
  369. #if defined(XP_WIN) || defined(MOZ_ACCESSIBILITY_ATK)
  370. RefPtr<nsNPAPIPluginInstance> pluginInstance;
  371. if (NS_SUCCEEDED(aFrame->GetPluginInstance(getter_AddRefs(pluginInstance))) &&
  372. pluginInstance) {
  373. #ifdef XP_WIN
  374. if (!sPendingPlugins->Contains(aContent) &&
  375. (Preferences::GetBool("accessibility.delay_plugins") ||
  376. Compatibility::IsJAWS() || Compatibility::IsWE())) {
  377. nsCOMPtr<nsITimer> timer = do_CreateInstance(NS_TIMER_CONTRACTID);
  378. RefPtr<PluginTimerCallBack> cb = new PluginTimerCallBack(aContent);
  379. timer->InitWithCallback(cb, Preferences::GetUint("accessibility.delay_plugin_time"),
  380. nsITimer::TYPE_ONE_SHOT);
  381. sPluginTimers->AppendElement(timer);
  382. sPendingPlugins->AppendElement(aContent);
  383. return nullptr;
  384. }
  385. // We need to remove aContent from the pending plugins here to avoid
  386. // reentrancy. When the timer fires it calls
  387. // DocAccessible::ContentInserted() which does the work async.
  388. sPendingPlugins->RemoveElement(aContent);
  389. // Note: pluginPort will be null if windowless.
  390. HWND pluginPort = nullptr;
  391. aFrame->GetPluginPort(&pluginPort);
  392. RefPtr<Accessible> accessible =
  393. new HTMLWin32ObjectOwnerAccessible(aContent, aContext->Document(),
  394. pluginPort);
  395. return accessible.forget();
  396. #elif MOZ_ACCESSIBILITY_ATK
  397. if (!AtkSocketAccessible::gCanEmbed)
  398. return nullptr;
  399. // Note this calls into the plugin, so crazy things may happen and aFrame
  400. // may go away.
  401. nsCString plugId;
  402. nsresult rv = pluginInstance->GetValueFromPlugin(
  403. NPPVpluginNativeAccessibleAtkPlugId, &plugId);
  404. if (NS_SUCCEEDED(rv) && !plugId.IsEmpty()) {
  405. RefPtr<AtkSocketAccessible> socketAccessible =
  406. new AtkSocketAccessible(aContent, aContext->Document(), plugId);
  407. return socketAccessible.forget();
  408. }
  409. #endif
  410. }
  411. #endif
  412. return nullptr;
  413. }
  414. void
  415. nsAccessibilityService::DeckPanelSwitched(nsIPresShell* aPresShell,
  416. nsIContent* aDeckNode,
  417. nsIFrame* aPrevBoxFrame,
  418. nsIFrame* aCurrentBoxFrame)
  419. {
  420. // Ignore tabpanels elements (a deck having an accessible) since their
  421. // children are accessible not depending on selected tab.
  422. DocAccessible* document = GetDocAccessible(aPresShell);
  423. if (!document || document->HasAccessible(aDeckNode))
  424. return;
  425. if (aPrevBoxFrame) {
  426. nsIContent* panelNode = aPrevBoxFrame->GetContent();
  427. #ifdef A11Y_LOG
  428. if (logging::IsEnabled(logging::eTree)) {
  429. logging::MsgBegin("TREE", "deck panel unselected");
  430. logging::Node("container", panelNode);
  431. logging::Node("content", aDeckNode);
  432. logging::MsgEnd();
  433. }
  434. #endif
  435. document->ContentRemoved(aDeckNode, panelNode);
  436. }
  437. if (aCurrentBoxFrame) {
  438. nsIContent* panelNode = aCurrentBoxFrame->GetContent();
  439. #ifdef A11Y_LOG
  440. if (logging::IsEnabled(logging::eTree)) {
  441. logging::MsgBegin("TREE", "deck panel selected");
  442. logging::Node("container", panelNode);
  443. logging::Node("content", aDeckNode);
  444. logging::MsgEnd();
  445. }
  446. #endif
  447. document->ContentInserted(aDeckNode, panelNode, panelNode->GetNextSibling());
  448. }
  449. }
  450. void
  451. nsAccessibilityService::ContentRangeInserted(nsIPresShell* aPresShell,
  452. nsIContent* aContainer,
  453. nsIContent* aStartChild,
  454. nsIContent* aEndChild)
  455. {
  456. DocAccessible* document = GetDocAccessible(aPresShell);
  457. #ifdef A11Y_LOG
  458. if (logging::IsEnabled(logging::eTree)) {
  459. logging::MsgBegin("TREE", "content inserted; doc: %p", document);
  460. logging::Node("container", aContainer);
  461. for (nsIContent* child = aStartChild; child != aEndChild;
  462. child = child->GetNextSibling()) {
  463. logging::Node("content", child);
  464. }
  465. logging::MsgEnd();
  466. logging::Stack();
  467. }
  468. #endif
  469. if (document) {
  470. document->ContentInserted(aContainer, aStartChild, aEndChild);
  471. }
  472. }
  473. void
  474. nsAccessibilityService::ContentRemoved(nsIPresShell* aPresShell,
  475. nsIContent* aChildNode)
  476. {
  477. DocAccessible* document = GetDocAccessible(aPresShell);
  478. #ifdef A11Y_LOG
  479. if (logging::IsEnabled(logging::eTree)) {
  480. logging::MsgBegin("TREE", "content removed; doc: %p", document);
  481. logging::Node("container node", aChildNode->GetFlattenedTreeParent());
  482. logging::Node("content node", aChildNode);
  483. logging::MsgEnd();
  484. }
  485. #endif
  486. if (document) {
  487. // Flatten hierarchy may be broken at this point so we cannot get a true
  488. // container by traversing up the DOM tree. Find a parent of first accessible
  489. // from the subtree of the given DOM node, that'll be a container. If no
  490. // accessibles in subtree then we don't care about the change.
  491. Accessible* child = document->GetAccessible(aChildNode);
  492. if (!child) {
  493. Accessible* container = document->GetContainerAccessible(aChildNode);
  494. a11y::TreeWalker walker(container ? container : document, aChildNode,
  495. a11y::TreeWalker::eWalkCache);
  496. child = walker.Next();
  497. }
  498. if (child) {
  499. document->ContentRemoved(child->Parent(), aChildNode);
  500. #ifdef A11Y_LOG
  501. if (logging::IsEnabled(logging::eTree))
  502. logging::AccessibleNNode("real container", child->Parent());
  503. #endif
  504. }
  505. }
  506. #ifdef A11Y_LOG
  507. if (logging::IsEnabled(logging::eTree)) {
  508. logging::MsgEnd();
  509. logging::Stack();
  510. }
  511. #endif
  512. }
  513. void
  514. nsAccessibilityService::UpdateText(nsIPresShell* aPresShell,
  515. nsIContent* aContent)
  516. {
  517. DocAccessible* document = GetDocAccessible(aPresShell);
  518. if (document)
  519. document->UpdateText(aContent);
  520. }
  521. void
  522. nsAccessibilityService::TreeViewChanged(nsIPresShell* aPresShell,
  523. nsIContent* aContent,
  524. nsITreeView* aView)
  525. {
  526. DocAccessible* document = GetDocAccessible(aPresShell);
  527. if (document) {
  528. Accessible* accessible = document->GetAccessible(aContent);
  529. if (accessible) {
  530. XULTreeAccessible* treeAcc = accessible->AsXULTree();
  531. if (treeAcc)
  532. treeAcc->TreeViewChanged(aView);
  533. }
  534. }
  535. }
  536. void
  537. nsAccessibilityService::RangeValueChanged(nsIPresShell* aPresShell,
  538. nsIContent* aContent)
  539. {
  540. DocAccessible* document = GetDocAccessible(aPresShell);
  541. if (document) {
  542. Accessible* accessible = document->GetAccessible(aContent);
  543. if (accessible) {
  544. document->FireDelayedEvent(nsIAccessibleEvent::EVENT_VALUE_CHANGE,
  545. accessible);
  546. }
  547. }
  548. }
  549. void
  550. nsAccessibilityService::UpdateListBullet(nsIPresShell* aPresShell,
  551. nsIContent* aHTMLListItemContent,
  552. bool aHasBullet)
  553. {
  554. DocAccessible* document = GetDocAccessible(aPresShell);
  555. if (document) {
  556. Accessible* accessible = document->GetAccessible(aHTMLListItemContent);
  557. if (accessible) {
  558. HTMLLIAccessible* listItem = accessible->AsHTMLListItem();
  559. if (listItem)
  560. listItem->UpdateBullet(aHasBullet);
  561. }
  562. }
  563. }
  564. void
  565. nsAccessibilityService::UpdateImageMap(nsImageFrame* aImageFrame)
  566. {
  567. nsIPresShell* presShell = aImageFrame->PresContext()->PresShell();
  568. DocAccessible* document = GetDocAccessible(presShell);
  569. if (document) {
  570. Accessible* accessible =
  571. document->GetAccessible(aImageFrame->GetContent());
  572. if (accessible) {
  573. HTMLImageMapAccessible* imageMap = accessible->AsImageMap();
  574. if (imageMap) {
  575. imageMap->UpdateChildAreas();
  576. return;
  577. }
  578. // If image map was initialized after we created an accessible (that'll
  579. // be an image accessible) then recreate it.
  580. RecreateAccessible(presShell, aImageFrame->GetContent());
  581. }
  582. }
  583. }
  584. void
  585. nsAccessibilityService::UpdateLabelValue(nsIPresShell* aPresShell,
  586. nsIContent* aLabelElm,
  587. const nsString& aNewValue)
  588. {
  589. DocAccessible* document = GetDocAccessible(aPresShell);
  590. if (document) {
  591. Accessible* accessible = document->GetAccessible(aLabelElm);
  592. if (accessible) {
  593. XULLabelAccessible* xulLabel = accessible->AsXULLabel();
  594. NS_ASSERTION(xulLabel,
  595. "UpdateLabelValue was called for wrong accessible!");
  596. if (xulLabel)
  597. xulLabel->UpdateLabelValue(aNewValue);
  598. }
  599. }
  600. }
  601. void
  602. nsAccessibilityService::PresShellActivated(nsIPresShell* aPresShell)
  603. {
  604. DocAccessible* document = aPresShell->GetDocAccessible();
  605. if (document) {
  606. RootAccessible* rootDocument = document->RootAccessible();
  607. NS_ASSERTION(rootDocument, "Entirely broken tree: no root document!");
  608. if (rootDocument)
  609. rootDocument->DocumentActivated(document);
  610. }
  611. }
  612. void
  613. nsAccessibilityService::RecreateAccessible(nsIPresShell* aPresShell,
  614. nsIContent* aContent)
  615. {
  616. DocAccessible* document = GetDocAccessible(aPresShell);
  617. if (document)
  618. document->RecreateAccessible(aContent);
  619. }
  620. void
  621. nsAccessibilityService::GetStringRole(uint32_t aRole, nsAString& aString)
  622. {
  623. #define ROLE(geckoRole, stringRole, atkRole, \
  624. macRole, msaaRole, ia2Role, nameRule) \
  625. case roles::geckoRole: \
  626. CopyUTF8toUTF16(stringRole, aString); \
  627. return;
  628. switch (aRole) {
  629. #include "RoleMap.h"
  630. default:
  631. aString.AssignLiteral("unknown");
  632. return;
  633. }
  634. #undef ROLE
  635. }
  636. void
  637. nsAccessibilityService::GetStringStates(uint32_t aState, uint32_t aExtraState,
  638. nsISupports **aStringStates)
  639. {
  640. RefPtr<DOMStringList> stringStates = new DOMStringList();
  641. uint64_t state = nsAccUtils::To64State(aState, aExtraState);
  642. // states
  643. if (state & states::UNAVAILABLE) {
  644. stringStates->Add(NS_LITERAL_STRING("unavailable"));
  645. }
  646. if (state & states::SELECTED) {
  647. stringStates->Add(NS_LITERAL_STRING("selected"));
  648. }
  649. if (state & states::FOCUSED) {
  650. stringStates->Add(NS_LITERAL_STRING("focused"));
  651. }
  652. if (state & states::PRESSED) {
  653. stringStates->Add(NS_LITERAL_STRING("pressed"));
  654. }
  655. if (state & states::CHECKED) {
  656. stringStates->Add(NS_LITERAL_STRING("checked"));
  657. }
  658. if (state & states::MIXED) {
  659. stringStates->Add(NS_LITERAL_STRING("mixed"));
  660. }
  661. if (state & states::READONLY) {
  662. stringStates->Add(NS_LITERAL_STRING("readonly"));
  663. }
  664. if (state & states::HOTTRACKED) {
  665. stringStates->Add(NS_LITERAL_STRING("hottracked"));
  666. }
  667. if (state & states::DEFAULT) {
  668. stringStates->Add(NS_LITERAL_STRING("default"));
  669. }
  670. if (state & states::EXPANDED) {
  671. stringStates->Add(NS_LITERAL_STRING("expanded"));
  672. }
  673. if (state & states::COLLAPSED) {
  674. stringStates->Add(NS_LITERAL_STRING("collapsed"));
  675. }
  676. if (state & states::BUSY) {
  677. stringStates->Add(NS_LITERAL_STRING("busy"));
  678. }
  679. if (state & states::FLOATING) {
  680. stringStates->Add(NS_LITERAL_STRING("floating"));
  681. }
  682. if (state & states::ANIMATED) {
  683. stringStates->Add(NS_LITERAL_STRING("animated"));
  684. }
  685. if (state & states::INVISIBLE) {
  686. stringStates->Add(NS_LITERAL_STRING("invisible"));
  687. }
  688. if (state & states::OFFSCREEN) {
  689. stringStates->Add(NS_LITERAL_STRING("offscreen"));
  690. }
  691. if (state & states::SIZEABLE) {
  692. stringStates->Add(NS_LITERAL_STRING("sizeable"));
  693. }
  694. if (state & states::MOVEABLE) {
  695. stringStates->Add(NS_LITERAL_STRING("moveable"));
  696. }
  697. if (state & states::SELFVOICING) {
  698. stringStates->Add(NS_LITERAL_STRING("selfvoicing"));
  699. }
  700. if (state & states::FOCUSABLE) {
  701. stringStates->Add(NS_LITERAL_STRING("focusable"));
  702. }
  703. if (state & states::SELECTABLE) {
  704. stringStates->Add(NS_LITERAL_STRING("selectable"));
  705. }
  706. if (state & states::LINKED) {
  707. stringStates->Add(NS_LITERAL_STRING("linked"));
  708. }
  709. if (state & states::TRAVERSED) {
  710. stringStates->Add(NS_LITERAL_STRING("traversed"));
  711. }
  712. if (state & states::MULTISELECTABLE) {
  713. stringStates->Add(NS_LITERAL_STRING("multiselectable"));
  714. }
  715. if (state & states::EXTSELECTABLE) {
  716. stringStates->Add(NS_LITERAL_STRING("extselectable"));
  717. }
  718. if (state & states::PROTECTED) {
  719. stringStates->Add(NS_LITERAL_STRING("protected"));
  720. }
  721. if (state & states::HASPOPUP) {
  722. stringStates->Add(NS_LITERAL_STRING("haspopup"));
  723. }
  724. if (state & states::REQUIRED) {
  725. stringStates->Add(NS_LITERAL_STRING("required"));
  726. }
  727. if (state & states::ALERT) {
  728. stringStates->Add(NS_LITERAL_STRING("alert"));
  729. }
  730. if (state & states::INVALID) {
  731. stringStates->Add(NS_LITERAL_STRING("invalid"));
  732. }
  733. if (state & states::CHECKABLE) {
  734. stringStates->Add(NS_LITERAL_STRING("checkable"));
  735. }
  736. // extraStates
  737. if (state & states::SUPPORTS_AUTOCOMPLETION) {
  738. stringStates->Add(NS_LITERAL_STRING("autocompletion"));
  739. }
  740. if (state & states::DEFUNCT) {
  741. stringStates->Add(NS_LITERAL_STRING("defunct"));
  742. }
  743. if (state & states::SELECTABLE_TEXT) {
  744. stringStates->Add(NS_LITERAL_STRING("selectable text"));
  745. }
  746. if (state & states::EDITABLE) {
  747. stringStates->Add(NS_LITERAL_STRING("editable"));
  748. }
  749. if (state & states::ACTIVE) {
  750. stringStates->Add(NS_LITERAL_STRING("active"));
  751. }
  752. if (state & states::MODAL) {
  753. stringStates->Add(NS_LITERAL_STRING("modal"));
  754. }
  755. if (state & states::MULTI_LINE) {
  756. stringStates->Add(NS_LITERAL_STRING("multi line"));
  757. }
  758. if (state & states::HORIZONTAL) {
  759. stringStates->Add(NS_LITERAL_STRING("horizontal"));
  760. }
  761. if (state & states::OPAQUE1) {
  762. stringStates->Add(NS_LITERAL_STRING("opaque"));
  763. }
  764. if (state & states::SINGLE_LINE) {
  765. stringStates->Add(NS_LITERAL_STRING("single line"));
  766. }
  767. if (state & states::TRANSIENT) {
  768. stringStates->Add(NS_LITERAL_STRING("transient"));
  769. }
  770. if (state & states::VERTICAL) {
  771. stringStates->Add(NS_LITERAL_STRING("vertical"));
  772. }
  773. if (state & states::STALE) {
  774. stringStates->Add(NS_LITERAL_STRING("stale"));
  775. }
  776. if (state & states::ENABLED) {
  777. stringStates->Add(NS_LITERAL_STRING("enabled"));
  778. }
  779. if (state & states::SENSITIVE) {
  780. stringStates->Add(NS_LITERAL_STRING("sensitive"));
  781. }
  782. if (state & states::EXPANDABLE) {
  783. stringStates->Add(NS_LITERAL_STRING("expandable"));
  784. }
  785. //unknown states
  786. if (!stringStates->Length()) {
  787. stringStates->Add(NS_LITERAL_STRING("unknown"));
  788. }
  789. stringStates.forget(aStringStates);
  790. }
  791. void
  792. nsAccessibilityService::GetStringEventType(uint32_t aEventType,
  793. nsAString& aString)
  794. {
  795. NS_ASSERTION(nsIAccessibleEvent::EVENT_LAST_ENTRY == ArrayLength(kEventTypeNames),
  796. "nsIAccessibleEvent constants are out of sync to kEventTypeNames");
  797. if (aEventType >= ArrayLength(kEventTypeNames)) {
  798. aString.AssignLiteral("unknown");
  799. return;
  800. }
  801. CopyUTF8toUTF16(kEventTypeNames[aEventType], aString);
  802. }
  803. void
  804. nsAccessibilityService::GetStringRelationType(uint32_t aRelationType,
  805. nsAString& aString)
  806. {
  807. NS_ENSURE_TRUE_VOID(aRelationType <= static_cast<uint32_t>(RelationType::LAST));
  808. #define RELATIONTYPE(geckoType, geckoTypeName, atkType, msaaType, ia2Type) \
  809. case RelationType::geckoType: \
  810. aString.AssignLiteral(geckoTypeName); \
  811. return;
  812. RelationType relationType = static_cast<RelationType>(aRelationType);
  813. switch (relationType) {
  814. #include "RelationTypeMap.h"
  815. default:
  816. aString.AssignLiteral("unknown");
  817. return;
  818. }
  819. #undef RELATIONTYPE
  820. }
  821. ////////////////////////////////////////////////////////////////////////////////
  822. // nsAccessibilityService public
  823. Accessible*
  824. nsAccessibilityService::CreateAccessible(nsINode* aNode,
  825. Accessible* aContext,
  826. bool* aIsSubtreeHidden)
  827. {
  828. MOZ_ASSERT(aContext, "No context provided");
  829. MOZ_ASSERT(aNode, "No node to create an accessible for");
  830. MOZ_ASSERT(gConsumers, "No creation after shutdown");
  831. if (aIsSubtreeHidden)
  832. *aIsSubtreeHidden = false;
  833. DocAccessible* document = aContext->Document();
  834. MOZ_ASSERT(!document->GetAccessible(aNode),
  835. "We already have an accessible for this node.");
  836. if (aNode->IsNodeOfType(nsINode::eDOCUMENT)) {
  837. // If it's document node then ask accessible document loader for
  838. // document accessible, otherwise return null.
  839. nsCOMPtr<nsIDocument> document(do_QueryInterface(aNode));
  840. return GetDocAccessible(document);
  841. }
  842. // We have a content node.
  843. if (!aNode->GetComposedDoc()) {
  844. NS_WARNING("Creating accessible for node with no document");
  845. return nullptr;
  846. }
  847. if (aNode->OwnerDoc() != document->DocumentNode()) {
  848. NS_ERROR("Creating accessible for wrong document");
  849. return nullptr;
  850. }
  851. if (!aNode->IsContent())
  852. return nullptr;
  853. nsIContent* content = aNode->AsContent();
  854. nsIFrame* frame = content->GetPrimaryFrame();
  855. // Check frame and its visibility. Note, hidden frame allows visible
  856. // elements in subtree.
  857. if (!frame || !frame->StyleVisibility()->IsVisible()) {
  858. if (aIsSubtreeHidden && !frame)
  859. *aIsSubtreeHidden = true;
  860. return nullptr;
  861. }
  862. if (frame->GetContent() != content) {
  863. // Not the main content for this frame. This happens because <area>
  864. // elements return the image frame as their primary frame. The main content
  865. // for the image frame is the image content. If the frame is not an image
  866. // frame or the node is not an area element then null is returned.
  867. // This setup will change when bug 135040 is fixed. Make sure we don't
  868. // create area accessible here. Hopefully assertion below will handle that.
  869. #ifdef DEBUG
  870. nsImageFrame* imageFrame = do_QueryFrame(frame);
  871. NS_ASSERTION(imageFrame && content->IsHTMLElement(nsGkAtoms::area),
  872. "Unknown case of not main content for the frame!");
  873. #endif
  874. return nullptr;
  875. }
  876. #ifdef DEBUG
  877. nsImageFrame* imageFrame = do_QueryFrame(frame);
  878. NS_ASSERTION(!imageFrame || !content->IsHTMLElement(nsGkAtoms::area),
  879. "Image map manages the area accessible creation!");
  880. #endif
  881. // Attempt to create an accessible based on what we know.
  882. RefPtr<Accessible> newAcc;
  883. // Create accessible for visible text frames.
  884. if (content->IsNodeOfType(nsINode::eTEXT)) {
  885. nsIFrame::RenderedText text = frame->GetRenderedText(0,
  886. UINT32_MAX, nsIFrame::TextOffsetType::OFFSETS_IN_CONTENT_TEXT,
  887. nsIFrame::TrailingWhitespace::DONT_TRIM_TRAILING_WHITESPACE);
  888. // Ignore not rendered text nodes and whitespace text nodes between table
  889. // cells.
  890. if (text.mString.IsEmpty() ||
  891. (aContext->IsTableRow() && nsCoreUtils::IsWhitespaceString(text.mString))) {
  892. if (aIsSubtreeHidden)
  893. *aIsSubtreeHidden = true;
  894. return nullptr;
  895. }
  896. newAcc = CreateAccessibleByFrameType(frame, content, aContext);
  897. document->BindToDocument(newAcc, nullptr);
  898. newAcc->AsTextLeaf()->SetText(text.mString);
  899. return newAcc;
  900. }
  901. if (content->IsHTMLElement(nsGkAtoms::map)) {
  902. // Create hyper text accessible for HTML map if it is used to group links
  903. // (see http://www.w3.org/TR/WCAG10-HTML-TECHS/#group-bypass). If the HTML
  904. // map rect is empty then it is used for links grouping. Otherwise it should
  905. // be used in conjunction with HTML image element and in this case we don't
  906. // create any accessible for it and don't walk into it. The accessibles for
  907. // HTML area (HTMLAreaAccessible) the map contains are attached as
  908. // children of the appropriate accessible for HTML image
  909. // (ImageAccessible).
  910. if (nsLayoutUtils::GetAllInFlowRectsUnion(frame,
  911. frame->GetParent()).IsEmpty()) {
  912. if (aIsSubtreeHidden)
  913. *aIsSubtreeHidden = true;
  914. return nullptr;
  915. }
  916. newAcc = new HyperTextAccessibleWrap(content, document);
  917. document->BindToDocument(newAcc, aria::GetRoleMap(content->AsElement()));
  918. return newAcc;
  919. }
  920. const nsRoleMapEntry* roleMapEntry = aria::GetRoleMap(content->AsElement());
  921. // If the element is focusable or global ARIA attribute is applied to it or
  922. // it is referenced by ARIA relationship then treat role="presentation" on
  923. // the element as the role is not there.
  924. if (roleMapEntry &&
  925. (roleMapEntry->Is(nsGkAtoms::presentation) ||
  926. roleMapEntry->Is(nsGkAtoms::none))) {
  927. if (!MustBeAccessible(content, document))
  928. return nullptr;
  929. roleMapEntry = nullptr;
  930. }
  931. if (!newAcc && content->IsHTMLElement()) { // HTML accessibles
  932. bool isARIATablePart = roleMapEntry &&
  933. (roleMapEntry->accTypes & (eTableCell | eTableRow | eTable));
  934. if (!isARIATablePart ||
  935. frame->AccessibleType() == eHTMLTableCellType ||
  936. frame->AccessibleType() == eHTMLTableRowType ||
  937. frame->AccessibleType() == eHTMLTableType) {
  938. // Prefer to use markup to decide if and what kind of accessible to create,
  939. const MarkupMapInfo* markupMap =
  940. mMarkupMaps.Get(content->NodeInfo()->NameAtom());
  941. if (markupMap && markupMap->new_func)
  942. newAcc = markupMap->new_func(content, aContext);
  943. if (!newAcc) // try by frame accessible type.
  944. newAcc = CreateAccessibleByFrameType(frame, content, aContext);
  945. }
  946. // In case of ARIA grid or table use table-specific classes if it's not
  947. // native table based.
  948. if (isARIATablePart && (!newAcc || newAcc->IsGenericHyperText())) {
  949. if ((roleMapEntry->accTypes & eTableCell)) {
  950. if (aContext->IsTableRow())
  951. newAcc = new ARIAGridCellAccessibleWrap(content, document);
  952. } else if (roleMapEntry->IsOfType(eTableRow)) {
  953. if (aContext->IsTable())
  954. newAcc = new ARIARowAccessible(content, document);
  955. } else if (roleMapEntry->IsOfType(eTable)) {
  956. newAcc = new ARIAGridAccessibleWrap(content, document);
  957. }
  958. }
  959. // If table has strong ARIA role then all table descendants shouldn't
  960. // expose their native roles.
  961. if (!roleMapEntry && newAcc && aContext->HasStrongARIARole()) {
  962. if (frame->AccessibleType() == eHTMLTableRowType) {
  963. const nsRoleMapEntry* contextRoleMap = aContext->ARIARoleMap();
  964. if (!contextRoleMap->IsOfType(eTable))
  965. roleMapEntry = &aria::gEmptyRoleMap;
  966. } else if (frame->AccessibleType() == eHTMLTableCellType &&
  967. aContext->ARIARoleMap() == &aria::gEmptyRoleMap) {
  968. roleMapEntry = &aria::gEmptyRoleMap;
  969. } else if (content->IsAnyOfHTMLElements(nsGkAtoms::dt,
  970. nsGkAtoms::li,
  971. nsGkAtoms::dd) ||
  972. frame->AccessibleType() == eHTMLLiType) {
  973. const nsRoleMapEntry* contextRoleMap = aContext->ARIARoleMap();
  974. if (!contextRoleMap->IsOfType(eList))
  975. roleMapEntry = &aria::gEmptyRoleMap;
  976. }
  977. }
  978. }
  979. // Accessible XBL types and deck stuff are used in XUL only currently.
  980. if (!newAcc && content->IsXULElement()) {
  981. // No accessible for not selected deck panel and its children.
  982. if (!aContext->IsXULTabpanels()) {
  983. nsDeckFrame* deckFrame = do_QueryFrame(frame->GetParent());
  984. if (deckFrame && deckFrame->GetSelectedBox() != frame) {
  985. if (aIsSubtreeHidden)
  986. *aIsSubtreeHidden = true;
  987. return nullptr;
  988. }
  989. }
  990. // XBL bindings may use @role attribute to point the accessible type
  991. // they belong to.
  992. newAcc = CreateAccessibleByType(content, document);
  993. // Any XUL box can be used as tabpanel, make sure we create a proper
  994. // accessible for it.
  995. if (!newAcc && aContext->IsXULTabpanels() &&
  996. content->GetParent() == aContext->GetContent()) {
  997. nsIAtom* frameType = frame->GetType();
  998. if (frameType == nsGkAtoms::boxFrame ||
  999. frameType == nsGkAtoms::scrollFrame) {
  1000. newAcc = new XULTabpanelAccessible(content, document);
  1001. }
  1002. }
  1003. }
  1004. if (!newAcc) {
  1005. if (content->IsSVGElement()) {
  1006. nsSVGPathGeometryFrame* pathGeometryFrame = do_QueryFrame(frame);
  1007. if (pathGeometryFrame) {
  1008. // A graphic elements: rect, circle, ellipse, line, path, polygon,
  1009. // polyline and image. A 'use' and 'text' graphic elements require
  1010. // special support.
  1011. newAcc = new EnumRoleAccessible<roles::GRAPHIC>(content, document);
  1012. } else if (content->IsSVGElement(nsGkAtoms::svg)) {
  1013. newAcc = new EnumRoleAccessible<roles::DIAGRAM>(content, document);
  1014. }
  1015. } else if (content->IsMathMLElement()) {
  1016. const MarkupMapInfo* markupMap =
  1017. mMarkupMaps.Get(content->NodeInfo()->NameAtom());
  1018. if (markupMap && markupMap->new_func)
  1019. newAcc = markupMap->new_func(content, aContext);
  1020. // Fall back to text when encountering Content MathML.
  1021. if (!newAcc && !content->IsAnyOfMathMLElements(nsGkAtoms::annotation_,
  1022. nsGkAtoms::annotation_xml_,
  1023. nsGkAtoms::mpadded_,
  1024. nsGkAtoms::mphantom_,
  1025. nsGkAtoms::maligngroup_,
  1026. nsGkAtoms::malignmark_,
  1027. nsGkAtoms::mspace_,
  1028. nsGkAtoms::semantics_)) {
  1029. newAcc = new HyperTextAccessible(content, document);
  1030. }
  1031. }
  1032. }
  1033. // If no accessible, see if we need to create a generic accessible because
  1034. // of some property that makes this object interesting
  1035. // We don't do this for <body>, <html>, <window>, <dialog> etc. which
  1036. // correspond to the doc accessible and will be created in any case
  1037. if (!newAcc && !content->IsHTMLElement(nsGkAtoms::body) &&
  1038. content->GetParent() &&
  1039. (roleMapEntry || MustBeAccessible(content, document) ||
  1040. (content->IsHTMLElement() &&
  1041. nsCoreUtils::HasClickListener(content)))) {
  1042. // This content is focusable or has an interesting dynamic content accessibility property.
  1043. // If it's interesting we need it in the accessibility hierarchy so that events or
  1044. // other accessibles can point to it, or so that it can hold a state, etc.
  1045. if (content->IsHTMLElement()) {
  1046. // Interesting HTML container which may have selectable text and/or embedded objects
  1047. newAcc = new HyperTextAccessibleWrap(content, document);
  1048. } else { // XUL, SVG, MathML etc.
  1049. // Interesting generic non-HTML container
  1050. newAcc = new AccessibleWrap(content, document);
  1051. }
  1052. }
  1053. if (newAcc) {
  1054. document->BindToDocument(newAcc, roleMapEntry);
  1055. }
  1056. return newAcc;
  1057. }
  1058. ////////////////////////////////////////////////////////////////////////////////
  1059. // nsAccessibilityService private
  1060. bool
  1061. nsAccessibilityService::Init()
  1062. {
  1063. // Initialize accessible document manager.
  1064. if (!DocManager::Init())
  1065. return false;
  1066. // Add observers.
  1067. nsCOMPtr<nsIObserverService> observerService =
  1068. mozilla::services::GetObserverService();
  1069. if (!observerService)
  1070. return false;
  1071. observerService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
  1072. static const char16_t kInitIndicator[] = { '1', 0 };
  1073. observerService->NotifyObservers(nullptr, "a11y-init-or-shutdown", kInitIndicator);
  1074. // Subscribe to EventListenerService.
  1075. nsCOMPtr<nsIEventListenerService> eventListenerService =
  1076. do_GetService("@mozilla.org/eventlistenerservice;1");
  1077. if (!eventListenerService)
  1078. return false;
  1079. eventListenerService->AddListenerChangeListener(this);
  1080. for (uint32_t i = 0; i < ArrayLength(sMarkupMapList); i++)
  1081. mMarkupMaps.Put(*sMarkupMapList[i].tag, &sMarkupMapList[i]);
  1082. #ifdef A11Y_LOG
  1083. logging::CheckEnv();
  1084. #endif
  1085. gAccessibilityService = this;
  1086. NS_ADDREF(gAccessibilityService); // will release in Shutdown()
  1087. if (XRE_IsParentProcess()) {
  1088. gApplicationAccessible = new ApplicationAccessibleWrap();
  1089. } else {
  1090. #if defined(XP_WIN)
  1091. dom::ContentChild* contentChild = dom::ContentChild::GetSingleton();
  1092. MOZ_ASSERT(contentChild);
  1093. // If we were instantiated by the chrome process, GetMsaaID() will return
  1094. // a non-zero value and we may safely continue with initialization.
  1095. if (!contentChild->GetMsaaID()) {
  1096. // Since we were not instantiated by chrome, we need to synchronously
  1097. // obtain a MSAA content process id.
  1098. contentChild->SendGetA11yContentId();
  1099. }
  1100. #endif // defined(XP_WIN)
  1101. gApplicationAccessible = new ApplicationAccessible();
  1102. }
  1103. NS_ADDREF(gApplicationAccessible); // will release in Shutdown()
  1104. gApplicationAccessible->Init();
  1105. #ifdef XP_WIN
  1106. sPendingPlugins = new nsTArray<nsCOMPtr<nsIContent> >;
  1107. sPluginTimers = new nsTArray<nsCOMPtr<nsITimer> >;
  1108. #endif
  1109. // Now its safe to start platform accessibility.
  1110. if (XRE_IsParentProcess())
  1111. PlatformInit();
  1112. return true;
  1113. }
  1114. void
  1115. nsAccessibilityService::Shutdown()
  1116. {
  1117. // Application is going to be closed, shutdown accessibility and mark
  1118. // accessibility service as shutdown to prevent calls of its methods.
  1119. // Don't null accessibility service static member at this point to be safe
  1120. // if someone will try to operate with it.
  1121. MOZ_ASSERT(gConsumers, "Accessibility was shutdown already");
  1122. gConsumers = 0;
  1123. // Remove observers.
  1124. nsCOMPtr<nsIObserverService> observerService =
  1125. mozilla::services::GetObserverService();
  1126. if (observerService) {
  1127. observerService->RemoveObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID);
  1128. static const char16_t kShutdownIndicator[] = { '0', 0 };
  1129. observerService->NotifyObservers(nullptr, "a11y-init-or-shutdown", kShutdownIndicator);
  1130. }
  1131. // Stop accessible document loader.
  1132. DocManager::Shutdown();
  1133. SelectionManager::Shutdown();
  1134. #ifdef XP_WIN
  1135. sPendingPlugins = nullptr;
  1136. uint32_t timerCount = sPluginTimers->Length();
  1137. for (uint32_t i = 0; i < timerCount; i++)
  1138. sPluginTimers->ElementAt(i)->Cancel();
  1139. sPluginTimers = nullptr;
  1140. #endif
  1141. if (XRE_IsParentProcess())
  1142. PlatformShutdown();
  1143. gApplicationAccessible->Shutdown();
  1144. NS_RELEASE(gApplicationAccessible);
  1145. gApplicationAccessible = nullptr;
  1146. NS_IF_RELEASE(gXPCApplicationAccessible);
  1147. gXPCApplicationAccessible = nullptr;
  1148. NS_RELEASE(gAccessibilityService);
  1149. gAccessibilityService = nullptr;
  1150. }
  1151. already_AddRefed<Accessible>
  1152. nsAccessibilityService::CreateAccessibleByType(nsIContent* aContent,
  1153. DocAccessible* aDoc)
  1154. {
  1155. nsAutoString role;
  1156. nsCoreUtils::XBLBindingRole(aContent, role);
  1157. if (role.IsEmpty() || role.EqualsLiteral("none"))
  1158. return nullptr;
  1159. if (role.EqualsLiteral("outerdoc")) {
  1160. RefPtr<Accessible> accessible = new OuterDocAccessible(aContent, aDoc);
  1161. return accessible.forget();
  1162. }
  1163. RefPtr<Accessible> accessible;
  1164. #ifdef MOZ_XUL
  1165. // XUL controls
  1166. if (role.EqualsLiteral("xul:alert")) {
  1167. accessible = new XULAlertAccessible(aContent, aDoc);
  1168. } else if (role.EqualsLiteral("xul:button")) {
  1169. accessible = new XULButtonAccessible(aContent, aDoc);
  1170. } else if (role.EqualsLiteral("xul:checkbox")) {
  1171. accessible = new XULCheckboxAccessible(aContent, aDoc);
  1172. } else if (role.EqualsLiteral("xul:colorpicker")) {
  1173. accessible = new XULColorPickerAccessible(aContent, aDoc);
  1174. } else if (role.EqualsLiteral("xul:colorpickertile")) {
  1175. accessible = new XULColorPickerTileAccessible(aContent, aDoc);
  1176. } else if (role.EqualsLiteral("xul:combobox")) {
  1177. accessible = new XULComboboxAccessible(aContent, aDoc);
  1178. } else if (role.EqualsLiteral("xul:tabpanels")) {
  1179. accessible = new XULTabpanelsAccessible(aContent, aDoc);
  1180. } else if (role.EqualsLiteral("xul:dropmarker")) {
  1181. accessible = new XULDropmarkerAccessible(aContent, aDoc);
  1182. } else if (role.EqualsLiteral("xul:groupbox")) {
  1183. accessible = new XULGroupboxAccessible(aContent, aDoc);
  1184. } else if (role.EqualsLiteral("xul:image")) {
  1185. if (aContent->HasAttr(kNameSpaceID_None, nsGkAtoms::onclick)) {
  1186. accessible = new XULToolbarButtonAccessible(aContent, aDoc);
  1187. } else {
  1188. // Don't include nameless images in accessible tree.
  1189. if (!aContent->HasAttr(kNameSpaceID_None,
  1190. nsGkAtoms::tooltiptext))
  1191. return nullptr;
  1192. accessible = new ImageAccessibleWrap(aContent, aDoc);
  1193. }
  1194. } else if (role.EqualsLiteral("xul:link")) {
  1195. accessible = new XULLinkAccessible(aContent, aDoc);
  1196. } else if (role.EqualsLiteral("xul:listbox")) {
  1197. accessible = new XULListboxAccessibleWrap(aContent, aDoc);
  1198. } else if (role.EqualsLiteral("xul:listcell")) {
  1199. // Only create cells if there's more than one per row.
  1200. nsIContent* listItem = aContent->GetParent();
  1201. if (!listItem)
  1202. return nullptr;
  1203. for (nsIContent* child = listItem->GetFirstChild(); child;
  1204. child = child->GetNextSibling()) {
  1205. if (child->IsXULElement(nsGkAtoms::listcell) && child != aContent) {
  1206. accessible = new XULListCellAccessibleWrap(aContent, aDoc);
  1207. break;
  1208. }
  1209. }
  1210. } else if (role.EqualsLiteral("xul:listhead")) {
  1211. accessible = new XULColumAccessible(aContent, aDoc);
  1212. } else if (role.EqualsLiteral("xul:listheader")) {
  1213. accessible = new XULColumnItemAccessible(aContent, aDoc);
  1214. } else if (role.EqualsLiteral("xul:listitem")) {
  1215. accessible = new XULListitemAccessible(aContent, aDoc);
  1216. } else if (role.EqualsLiteral("xul:menubar")) {
  1217. accessible = new XULMenubarAccessible(aContent, aDoc);
  1218. } else if (role.EqualsLiteral("xul:menulist")) {
  1219. accessible = new XULComboboxAccessible(aContent, aDoc);
  1220. } else if (role.EqualsLiteral("xul:menuitem")) {
  1221. accessible = new XULMenuitemAccessibleWrap(aContent, aDoc);
  1222. } else if (role.EqualsLiteral("xul:menupopup")) {
  1223. #ifdef MOZ_ACCESSIBILITY_ATK
  1224. // ATK considers this node to be redundant when within menubars, and it makes menu
  1225. // navigation with assistive technologies more difficult
  1226. // XXX In the future we will should this for consistency across the nsIAccessible
  1227. // implementations on each platform for a consistent scripting environment, but
  1228. // then strip out redundant accessibles in the AccessibleWrap class for each platform.
  1229. nsIContent *parent = aContent->GetParent();
  1230. if (parent && parent->IsXULElement(nsGkAtoms::menu))
  1231. return nullptr;
  1232. #endif
  1233. accessible = new XULMenupopupAccessible(aContent, aDoc);
  1234. } else if(role.EqualsLiteral("xul:menuseparator")) {
  1235. accessible = new XULMenuSeparatorAccessible(aContent, aDoc);
  1236. } else if(role.EqualsLiteral("xul:pane")) {
  1237. accessible = new EnumRoleAccessible<roles::PANE>(aContent, aDoc);
  1238. } else if (role.EqualsLiteral("xul:panel")) {
  1239. if (aContent->AttrValueIs(kNameSpaceID_None, nsGkAtoms::noautofocus,
  1240. nsGkAtoms::_true, eCaseMatters))
  1241. accessible = new XULAlertAccessible(aContent, aDoc);
  1242. else
  1243. accessible = new EnumRoleAccessible<roles::PANE>(aContent, aDoc);
  1244. } else if (role.EqualsLiteral("xul:progressmeter")) {
  1245. accessible = new XULProgressMeterAccessible(aContent, aDoc);
  1246. } else if (role.EqualsLiteral("xul:statusbar")) {
  1247. accessible = new XULStatusBarAccessible(aContent, aDoc);
  1248. } else if (role.EqualsLiteral("xul:scale")) {
  1249. accessible = new XULSliderAccessible(aContent, aDoc);
  1250. } else if (role.EqualsLiteral("xul:radiobutton")) {
  1251. accessible = new XULRadioButtonAccessible(aContent, aDoc);
  1252. } else if (role.EqualsLiteral("xul:radiogroup")) {
  1253. accessible = new XULRadioGroupAccessible(aContent, aDoc);
  1254. } else if (role.EqualsLiteral("xul:tab")) {
  1255. accessible = new XULTabAccessible(aContent, aDoc);
  1256. } else if (role.EqualsLiteral("xul:tabs")) {
  1257. accessible = new XULTabsAccessible(aContent, aDoc);
  1258. } else if (role.EqualsLiteral("xul:text")) {
  1259. accessible = new XULLabelAccessible(aContent, aDoc);
  1260. } else if (role.EqualsLiteral("xul:textbox")) {
  1261. accessible = new EnumRoleAccessible<roles::SECTION>(aContent, aDoc);
  1262. } else if (role.EqualsLiteral("xul:thumb")) {
  1263. accessible = new XULThumbAccessible(aContent, aDoc);
  1264. } else if (role.EqualsLiteral("xul:tree")) {
  1265. accessible = CreateAccessibleForXULTree(aContent, aDoc);
  1266. } else if (role.EqualsLiteral("xul:treecolumns")) {
  1267. accessible = new XULTreeColumAccessible(aContent, aDoc);
  1268. } else if (role.EqualsLiteral("xul:treecolumnitem")) {
  1269. accessible = new XULColumnItemAccessible(aContent, aDoc);
  1270. } else if (role.EqualsLiteral("xul:toolbar")) {
  1271. accessible = new XULToolbarAccessible(aContent, aDoc);
  1272. } else if (role.EqualsLiteral("xul:toolbarseparator")) {
  1273. accessible = new XULToolbarSeparatorAccessible(aContent, aDoc);
  1274. } else if (role.EqualsLiteral("xul:tooltip")) {
  1275. accessible = new XULTooltipAccessible(aContent, aDoc);
  1276. } else if (role.EqualsLiteral("xul:toolbarbutton")) {
  1277. accessible = new XULToolbarButtonAccessible(aContent, aDoc);
  1278. }
  1279. #endif // MOZ_XUL
  1280. return accessible.forget();
  1281. }
  1282. already_AddRefed<Accessible>
  1283. nsAccessibilityService::CreateAccessibleByFrameType(nsIFrame* aFrame,
  1284. nsIContent* aContent,
  1285. Accessible* aContext)
  1286. {
  1287. DocAccessible* document = aContext->Document();
  1288. RefPtr<Accessible> newAcc;
  1289. switch (aFrame->AccessibleType()) {
  1290. case eNoType:
  1291. return nullptr;
  1292. case eHTMLBRType:
  1293. newAcc = new HTMLBRAccessible(aContent, document);
  1294. break;
  1295. case eHTMLButtonType:
  1296. newAcc = new HTMLButtonAccessible(aContent, document);
  1297. break;
  1298. case eHTMLCanvasType:
  1299. newAcc = new HTMLCanvasAccessible(aContent, document);
  1300. break;
  1301. case eHTMLCaptionType:
  1302. if (aContext->IsTable() &&
  1303. aContext->GetContent() == aContent->GetParent()) {
  1304. newAcc = new HTMLCaptionAccessible(aContent, document);
  1305. }
  1306. break;
  1307. case eHTMLCheckboxType:
  1308. newAcc = new HTMLCheckboxAccessible(aContent, document);
  1309. break;
  1310. case eHTMLComboboxType:
  1311. newAcc = new HTMLComboboxAccessible(aContent, document);
  1312. break;
  1313. case eHTMLFileInputType:
  1314. newAcc = new HTMLFileInputAccessible(aContent, document);
  1315. break;
  1316. case eHTMLGroupboxType:
  1317. newAcc = new HTMLGroupboxAccessible(aContent, document);
  1318. break;
  1319. case eHTMLHRType:
  1320. newAcc = new HTMLHRAccessible(aContent, document);
  1321. break;
  1322. case eHTMLImageMapType:
  1323. newAcc = new HTMLImageMapAccessible(aContent, document);
  1324. break;
  1325. case eHTMLLiType:
  1326. if (aContext->IsList() &&
  1327. aContext->GetContent() == aContent->GetParent()) {
  1328. newAcc = new HTMLLIAccessible(aContent, document);
  1329. } else {
  1330. // Otherwise create a generic text accessible to avoid text jamming.
  1331. newAcc = new HyperTextAccessibleWrap(aContent, document);
  1332. }
  1333. break;
  1334. case eHTMLSelectListType:
  1335. newAcc = new HTMLSelectListAccessible(aContent, document);
  1336. break;
  1337. case eHTMLMediaType:
  1338. newAcc = new EnumRoleAccessible<roles::GROUPING>(aContent, document);
  1339. break;
  1340. case eHTMLRadioButtonType:
  1341. newAcc = new HTMLRadioButtonAccessible(aContent, document);
  1342. break;
  1343. case eHTMLRangeType:
  1344. newAcc = new HTMLRangeAccessible(aContent, document);
  1345. break;
  1346. case eHTMLSpinnerType:
  1347. newAcc = new HTMLSpinnerAccessible(aContent, document);
  1348. break;
  1349. case eHTMLTableType:
  1350. if (aContent->IsHTMLElement(nsGkAtoms::table))
  1351. newAcc = new HTMLTableAccessibleWrap(aContent, document);
  1352. else
  1353. newAcc = new HyperTextAccessibleWrap(aContent, document);
  1354. break;
  1355. case eHTMLTableCellType:
  1356. // Accessible HTML table cell should be a child of accessible HTML table
  1357. // or its row (CSS HTML tables are polite to the used markup at
  1358. // certain degree).
  1359. // Otherwise create a generic text accessible to avoid text jamming
  1360. // when reading by AT.
  1361. if (aContext->IsHTMLTableRow() || aContext->IsHTMLTable())
  1362. newAcc = new HTMLTableCellAccessibleWrap(aContent, document);
  1363. else
  1364. newAcc = new HyperTextAccessibleWrap(aContent, document);
  1365. break;
  1366. case eHTMLTableRowType: {
  1367. // Accessible HTML table row may be a child of tbody/tfoot/thead of
  1368. // accessible HTML table or a direct child of accessible of HTML table.
  1369. Accessible* table = aContext->IsTable() ? aContext : nullptr;
  1370. if (!table && aContext->Parent() && aContext->Parent()->IsTable())
  1371. table = aContext->Parent();
  1372. if (table) {
  1373. nsIContent* parentContent = aContent->GetParent();
  1374. nsIFrame* parentFrame = parentContent->GetPrimaryFrame();
  1375. if (parentFrame->GetType() != nsGkAtoms::tableWrapperFrame) {
  1376. parentContent = parentContent->GetParent();
  1377. parentFrame = parentContent->GetPrimaryFrame();
  1378. }
  1379. if (parentFrame->GetType() == nsGkAtoms::tableWrapperFrame &&
  1380. table->GetContent() == parentContent) {
  1381. newAcc = new HTMLTableRowAccessible(aContent, document);
  1382. }
  1383. }
  1384. break;
  1385. }
  1386. case eHTMLTextFieldType:
  1387. newAcc = new HTMLTextFieldAccessible(aContent, document);
  1388. break;
  1389. case eHyperTextType:
  1390. if (!aContent->IsAnyOfHTMLElements(nsGkAtoms::dt, nsGkAtoms::dd))
  1391. newAcc = new HyperTextAccessibleWrap(aContent, document);
  1392. break;
  1393. case eImageType:
  1394. newAcc = new ImageAccessibleWrap(aContent, document);
  1395. break;
  1396. case eOuterDocType:
  1397. newAcc = new OuterDocAccessible(aContent, document);
  1398. break;
  1399. case ePluginType: {
  1400. nsPluginFrame* pluginFrame = do_QueryFrame(aFrame);
  1401. newAcc = CreatePluginAccessible(pluginFrame, aContent, aContext);
  1402. break;
  1403. }
  1404. case eTextLeafType:
  1405. newAcc = new TextLeafAccessibleWrap(aContent, document);
  1406. break;
  1407. default:
  1408. MOZ_ASSERT(false);
  1409. break;
  1410. }
  1411. return newAcc.forget();
  1412. }
  1413. void
  1414. nsAccessibilityService::MarkupAttributes(const nsIContent* aContent,
  1415. nsIPersistentProperties* aAttributes) const
  1416. {
  1417. const mozilla::a11y::MarkupMapInfo* markupMap =
  1418. mMarkupMaps.Get(aContent->NodeInfo()->NameAtom());
  1419. if (!markupMap)
  1420. return;
  1421. for (uint32_t i = 0; i < ArrayLength(markupMap->attrs); i++) {
  1422. const MarkupAttrInfo* info = markupMap->attrs + i;
  1423. if (!info->name)
  1424. break;
  1425. if (info->DOMAttrName) {
  1426. if (info->DOMAttrValue) {
  1427. if (aContent->AttrValueIs(kNameSpaceID_None, *info->DOMAttrName,
  1428. *info->DOMAttrValue, eCaseMatters)) {
  1429. nsAccUtils::SetAccAttr(aAttributes, *info->name, *info->DOMAttrValue);
  1430. }
  1431. continue;
  1432. }
  1433. nsAutoString value;
  1434. aContent->GetAttr(kNameSpaceID_None, *info->DOMAttrName, value);
  1435. if (!value.IsEmpty())
  1436. nsAccUtils::SetAccAttr(aAttributes, *info->name, value);
  1437. continue;
  1438. }
  1439. nsAccUtils::SetAccAttr(aAttributes, *info->name, *info->value);
  1440. }
  1441. }
  1442. Accessible*
  1443. nsAccessibilityService::AddNativeRootAccessible(void* aAtkAccessible)
  1444. {
  1445. #ifdef MOZ_ACCESSIBILITY_ATK
  1446. ApplicationAccessible* applicationAcc = ApplicationAcc();
  1447. if (!applicationAcc)
  1448. return nullptr;
  1449. GtkWindowAccessible* nativeWnd =
  1450. new GtkWindowAccessible(static_cast<AtkObject*>(aAtkAccessible));
  1451. if (applicationAcc->AppendChild(nativeWnd))
  1452. return nativeWnd;
  1453. #endif
  1454. return nullptr;
  1455. }
  1456. void
  1457. nsAccessibilityService::RemoveNativeRootAccessible(Accessible* aAccessible)
  1458. {
  1459. #ifdef MOZ_ACCESSIBILITY_ATK
  1460. ApplicationAccessible* applicationAcc = ApplicationAcc();
  1461. if (applicationAcc)
  1462. applicationAcc->RemoveChild(aAccessible);
  1463. #endif
  1464. }
  1465. bool
  1466. nsAccessibilityService::HasAccessible(nsIDOMNode* aDOMNode)
  1467. {
  1468. nsCOMPtr<nsINode> node(do_QueryInterface(aDOMNode));
  1469. if (!node)
  1470. return false;
  1471. DocAccessible* document = GetDocAccessible(node->OwnerDoc());
  1472. if (!document)
  1473. return false;
  1474. return document->HasAccessible(node);
  1475. }
  1476. ////////////////////////////////////////////////////////////////////////////////
  1477. // nsAccessibilityService private (DON'T put methods here)
  1478. #ifdef MOZ_XUL
  1479. already_AddRefed<Accessible>
  1480. nsAccessibilityService::CreateAccessibleForXULTree(nsIContent* aContent,
  1481. DocAccessible* aDoc)
  1482. {
  1483. nsIContent* child = nsTreeUtils::GetDescendantChild(aContent,
  1484. nsGkAtoms::treechildren);
  1485. if (!child)
  1486. return nullptr;
  1487. nsTreeBodyFrame* treeFrame = do_QueryFrame(child->GetPrimaryFrame());
  1488. if (!treeFrame)
  1489. return nullptr;
  1490. RefPtr<nsTreeColumns> treeCols = treeFrame->Columns();
  1491. int32_t count = 0;
  1492. treeCols->GetCount(&count);
  1493. // Outline of list accessible.
  1494. if (count == 1) {
  1495. RefPtr<Accessible> accessible =
  1496. new XULTreeAccessible(aContent, aDoc, treeFrame);
  1497. return accessible.forget();
  1498. }
  1499. // Table or tree table accessible.
  1500. RefPtr<Accessible> accessible =
  1501. new XULTreeGridAccessibleWrap(aContent, aDoc, treeFrame);
  1502. return accessible.forget();
  1503. }
  1504. #endif
  1505. nsAccessibilityService*
  1506. GetOrCreateAccService(uint32_t aNewConsumer)
  1507. {
  1508. if (!nsAccessibilityService::gAccessibilityService) {
  1509. RefPtr<nsAccessibilityService> service = new nsAccessibilityService();
  1510. if (!service->Init()) {
  1511. service->Shutdown();
  1512. return nullptr;
  1513. }
  1514. }
  1515. MOZ_ASSERT(nsAccessibilityService::gAccessibilityService,
  1516. "Accessible service is not initialized.");
  1517. nsAccessibilityService::gConsumers |= aNewConsumer;
  1518. return nsAccessibilityService::gAccessibilityService;
  1519. }
  1520. void
  1521. MaybeShutdownAccService(uint32_t aFormerConsumer)
  1522. {
  1523. nsAccessibilityService* accService =
  1524. nsAccessibilityService::gAccessibilityService;
  1525. if (!accService || accService->IsShutdown()) {
  1526. return;
  1527. }
  1528. if (nsCoreUtils::AccEventObserversExist() ||
  1529. xpcAccessibilityService::IsInUse()) {
  1530. // Still used by XPCOM
  1531. nsAccessibilityService::gConsumers =
  1532. (nsAccessibilityService::gConsumers & ~aFormerConsumer) |
  1533. nsAccessibilityService::eXPCOM;
  1534. return;
  1535. }
  1536. if (nsAccessibilityService::gConsumers & ~aFormerConsumer) {
  1537. nsAccessibilityService::gConsumers &= ~aFormerConsumer;
  1538. } else {
  1539. accService->Shutdown(); // Will unset all nsAccessibilityService::gConsumers
  1540. }
  1541. }
  1542. ////////////////////////////////////////////////////////////////////////////////
  1543. // Services
  1544. ////////////////////////////////////////////////////////////////////////////////
  1545. namespace mozilla {
  1546. namespace a11y {
  1547. FocusManager*
  1548. FocusMgr()
  1549. {
  1550. return nsAccessibilityService::gAccessibilityService;
  1551. }
  1552. SelectionManager*
  1553. SelectionMgr()
  1554. {
  1555. return nsAccessibilityService::gAccessibilityService;
  1556. }
  1557. ApplicationAccessible*
  1558. ApplicationAcc()
  1559. {
  1560. return nsAccessibilityService::gApplicationAccessible;
  1561. }
  1562. xpcAccessibleApplication*
  1563. XPCApplicationAcc()
  1564. {
  1565. if (!nsAccessibilityService::gXPCApplicationAccessible &&
  1566. nsAccessibilityService::gApplicationAccessible) {
  1567. nsAccessibilityService::gXPCApplicationAccessible =
  1568. new xpcAccessibleApplication(nsAccessibilityService::gApplicationAccessible);
  1569. NS_ADDREF(nsAccessibilityService::gXPCApplicationAccessible);
  1570. }
  1571. return nsAccessibilityService::gXPCApplicationAccessible;
  1572. }
  1573. EPlatformDisabledState
  1574. PlatformDisabledState()
  1575. {
  1576. static int disabledState = 0xff;
  1577. if (disabledState == 0xff) {
  1578. disabledState = Preferences::GetInt("accessibility.force_disabled", 0);
  1579. if (disabledState < ePlatformIsForceEnabled)
  1580. disabledState = ePlatformIsForceEnabled;
  1581. else if (disabledState > ePlatformIsDisabled)
  1582. disabledState = ePlatformIsDisabled;
  1583. }
  1584. return (EPlatformDisabledState)disabledState;
  1585. }
  1586. }
  1587. }