ApplicationAccessible.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim:expandtab:shiftwidth=2:tabstop=2:
  3. */
  4. /* This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  7. #include "ApplicationAccessible.h"
  8. #include "nsAccessibilityService.h"
  9. #include "nsAccUtils.h"
  10. #include "Relation.h"
  11. #include "Role.h"
  12. #include "States.h"
  13. #include "nsIComponentManager.h"
  14. #include "nsIDOMDocument.h"
  15. #include "nsIWindowMediator.h"
  16. #include "nsServiceManagerUtils.h"
  17. #include "mozilla/Services.h"
  18. #include "nsIStringBundle.h"
  19. using namespace mozilla::a11y;
  20. ApplicationAccessible::ApplicationAccessible() :
  21. AccessibleWrap(nullptr, nullptr)
  22. {
  23. mType = eApplicationType;
  24. mAppInfo = do_GetService("@mozilla.org/xre/app-info;1");
  25. MOZ_ASSERT(mAppInfo, "no application info");
  26. }
  27. NS_IMPL_ISUPPORTS_INHERITED0(ApplicationAccessible, Accessible)
  28. ////////////////////////////////////////////////////////////////////////////////
  29. // nsIAccessible
  30. ENameValueFlag
  31. ApplicationAccessible::Name(nsString& aName)
  32. {
  33. aName.Truncate();
  34. nsCOMPtr<nsIStringBundleService> bundleService =
  35. mozilla::services::GetStringBundleService();
  36. NS_ASSERTION(bundleService, "String bundle service must be present!");
  37. if (!bundleService)
  38. return eNameOK;
  39. nsCOMPtr<nsIStringBundle> bundle;
  40. nsresult rv = bundleService->CreateBundle("chrome://branding/locale/brand.properties",
  41. getter_AddRefs(bundle));
  42. if (NS_FAILED(rv))
  43. return eNameOK;
  44. nsXPIDLString appName;
  45. rv = bundle->GetStringFromName(u"brandShortName",
  46. getter_Copies(appName));
  47. if (NS_FAILED(rv) || appName.IsEmpty()) {
  48. NS_WARNING("brandShortName not found, using default app name");
  49. appName.AssignLiteral("Gecko based application");
  50. }
  51. aName.Assign(appName);
  52. return eNameOK;
  53. }
  54. void
  55. ApplicationAccessible::Description(nsString& aDescription)
  56. {
  57. aDescription.Truncate();
  58. }
  59. void
  60. ApplicationAccessible::Value(nsString& aValue)
  61. {
  62. aValue.Truncate();
  63. }
  64. uint64_t
  65. ApplicationAccessible::State()
  66. {
  67. return IsDefunct() ? states::DEFUNCT : 0;
  68. }
  69. already_AddRefed<nsIPersistentProperties>
  70. ApplicationAccessible::NativeAttributes()
  71. {
  72. return nullptr;
  73. }
  74. GroupPos
  75. ApplicationAccessible::GroupPosition()
  76. {
  77. return GroupPos();
  78. }
  79. Accessible*
  80. ApplicationAccessible::ChildAtPoint(int32_t aX, int32_t aY,
  81. EWhichChildAtPoint aWhichChild)
  82. {
  83. return nullptr;
  84. }
  85. Accessible*
  86. ApplicationAccessible::FocusedChild()
  87. {
  88. Accessible* focus = FocusMgr()->FocusedAccessible();
  89. if (focus && focus->Parent() == this)
  90. return focus;
  91. return nullptr;
  92. }
  93. Relation
  94. ApplicationAccessible::RelationByType(RelationType aRelationType)
  95. {
  96. return Relation();
  97. }
  98. nsIntRect
  99. ApplicationAccessible::Bounds() const
  100. {
  101. return nsIntRect();
  102. }
  103. ////////////////////////////////////////////////////////////////////////////////
  104. // Accessible public methods
  105. void
  106. ApplicationAccessible::Shutdown()
  107. {
  108. mAppInfo = nullptr;
  109. }
  110. void
  111. ApplicationAccessible::ApplyARIAState(uint64_t* aState) const
  112. {
  113. }
  114. role
  115. ApplicationAccessible::NativeRole()
  116. {
  117. return roles::APP_ROOT;
  118. }
  119. uint64_t
  120. ApplicationAccessible::NativeState()
  121. {
  122. return 0;
  123. }
  124. KeyBinding
  125. ApplicationAccessible::AccessKey() const
  126. {
  127. return KeyBinding();
  128. }
  129. void
  130. ApplicationAccessible::Init()
  131. {
  132. // Basically children are kept updated by Append/RemoveChild method calls.
  133. // However if there are open windows before accessibility was started
  134. // then we need to make sure root accessibles for open windows are created so
  135. // that all root accessibles are stored in application accessible children
  136. // array.
  137. nsCOMPtr<nsIWindowMediator> windowMediator =
  138. do_GetService(NS_WINDOWMEDIATOR_CONTRACTID);
  139. nsCOMPtr<nsISimpleEnumerator> windowEnumerator;
  140. nsresult rv = windowMediator->GetEnumerator(nullptr,
  141. getter_AddRefs(windowEnumerator));
  142. if (NS_FAILED(rv))
  143. return;
  144. bool hasMore = false;
  145. windowEnumerator->HasMoreElements(&hasMore);
  146. while (hasMore) {
  147. nsCOMPtr<nsISupports> window;
  148. windowEnumerator->GetNext(getter_AddRefs(window));
  149. nsCOMPtr<nsPIDOMWindowOuter> DOMWindow = do_QueryInterface(window);
  150. if (DOMWindow) {
  151. nsCOMPtr<nsIDocument> docNode = DOMWindow->GetDoc();
  152. if (docNode) {
  153. GetAccService()->GetDocAccessible(docNode); // ensure creation
  154. }
  155. }
  156. windowEnumerator->HasMoreElements(&hasMore);
  157. }
  158. }
  159. Accessible*
  160. ApplicationAccessible::GetSiblingAtOffset(int32_t aOffset,
  161. nsresult* aError) const
  162. {
  163. if (aError)
  164. *aError = NS_OK; // fail peacefully
  165. return nullptr;
  166. }