ProxyAccessibleBase.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. #ifndef mozilla_a11y_ProxyAccessibleBase_h
  6. #define mozilla_a11y_ProxyAccessibleBase_h
  7. #include "mozilla/a11y/Role.h"
  8. #include "nsIAccessibleText.h"
  9. #include "nsIAccessibleTypes.h"
  10. #include "Accessible.h"
  11. #include "nsString.h"
  12. #include "nsTArray.h"
  13. #include "nsRect.h"
  14. #include "Accessible.h"
  15. namespace mozilla {
  16. namespace a11y {
  17. class Accessible;
  18. class Attribute;
  19. class DocAccessibleParent;
  20. class ProxyAccessible;
  21. enum class RelationType;
  22. enum Interfaces
  23. {
  24. HYPERTEXT = 1,
  25. HYPERLINK = 1 << 1,
  26. IMAGE = 1 << 2,
  27. VALUE = 1 << 3,
  28. TABLE = 1 << 4,
  29. TABLECELL = 1 << 5,
  30. DOCUMENT = 1 << 6,
  31. SELECTION = 1 << 7,
  32. ACTION = 1 << 8,
  33. };
  34. template <class Derived>
  35. class ProxyAccessibleBase
  36. {
  37. public:
  38. ~ProxyAccessibleBase()
  39. {
  40. MOZ_ASSERT(!mWrapper);
  41. }
  42. void AddChildAt(uint32_t aIdx, Derived* aChild)
  43. { mChildren.InsertElementAt(aIdx, aChild); }
  44. uint32_t ChildrenCount() const { return mChildren.Length(); }
  45. Derived* ChildAt(uint32_t aIdx) const { return mChildren[aIdx]; }
  46. Derived* FirstChild() const
  47. { return mChildren.Length() ? mChildren[0] : nullptr; }
  48. Derived* LastChild() const
  49. { return mChildren.Length() ? mChildren[mChildren.Length() - 1] : nullptr; }
  50. Derived* PrevSibling() const
  51. {
  52. size_t idx = IndexInParent();
  53. return idx > 0 ? Parent()->mChildren[idx - 1] : nullptr;
  54. }
  55. Derived* NextSibling() const
  56. {
  57. size_t idx = IndexInParent();
  58. return idx + 1 < Parent()->mChildren.Length() ? Parent()->mChildren[idx + 1]
  59. : nullptr;
  60. }
  61. // XXX evaluate if this is fast enough.
  62. size_t IndexInParent() const { return
  63. Parent()->mChildren.IndexOf(static_cast<const Derived*>(this)); }
  64. uint32_t EmbeddedChildCount() const;
  65. int32_t IndexOfEmbeddedChild(const Derived* aChild);
  66. Derived* EmbeddedChildAt(size_t aChildIdx);
  67. bool MustPruneChildren() const;
  68. void Shutdown();
  69. void SetChildDoc(DocAccessibleParent* aChildDoc);
  70. void ClearChildDoc(DocAccessibleParent* aChildDoc);
  71. /**
  72. * Remove The given child.
  73. */
  74. void RemoveChild(Derived* aChild)
  75. { mChildren.RemoveElement(aChild); }
  76. /**
  77. * Return the proxy for the parent of the wrapped accessible.
  78. */
  79. Derived* Parent() const { return mParent; }
  80. Accessible* OuterDocOfRemoteBrowser() const;
  81. /**
  82. * Get the role of the accessible we're proxying.
  83. */
  84. role Role() const { return mRole; }
  85. /**
  86. * Return true if this is an embedded object.
  87. */
  88. bool IsEmbeddedObject() const
  89. {
  90. role role = Role();
  91. return role != roles::TEXT_LEAF &&
  92. role != roles::WHITESPACE &&
  93. role != roles::STATICTEXT;
  94. }
  95. /**
  96. * Allow the platform to store a pointers worth of data on us.
  97. */
  98. uintptr_t GetWrapper() const { return mWrapper; }
  99. void SetWrapper(uintptr_t aWrapper) { mWrapper = aWrapper; }
  100. /*
  101. * Return the ID of the accessible being proxied.
  102. */
  103. uint64_t ID() const { return mID; }
  104. /**
  105. * Return the document containing this proxy, or the proxy itself if it is a
  106. * document.
  107. */
  108. DocAccessibleParent* Document() const { return mDoc; }
  109. /**
  110. * Return true if this proxy is a DocAccessibleParent.
  111. */
  112. bool IsDoc() const { return mIsDoc; }
  113. DocAccessibleParent* AsDoc() const { return IsDoc() ? mDoc : nullptr; }
  114. // XXX checking mRole alone may not result in same behavior as Accessibles
  115. // due to ARIA roles. See bug 1210477.
  116. inline bool IsTable() const
  117. {
  118. return mRole == roles::TABLE || mRole == roles::MATHML_TABLE;
  119. }
  120. inline bool IsTableRow() const
  121. {
  122. return (mRole == roles::ROW ||
  123. mRole == roles::MATHML_TABLE_ROW ||
  124. mRole == roles::MATHML_LABELED_ROW);
  125. }
  126. inline bool IsTableCell() const
  127. {
  128. return (mRole == roles::CELL ||
  129. mRole == roles::COLUMNHEADER ||
  130. mRole == roles::ROWHEADER ||
  131. mRole == roles::GRID_CELL ||
  132. mRole == roles::MATHML_CELL);
  133. }
  134. protected:
  135. ProxyAccessibleBase(uint64_t aID, Derived* aParent,
  136. DocAccessibleParent* aDoc, role aRole,
  137. uint32_t aInterfaces)
  138. : mParent(aParent)
  139. , mDoc(aDoc)
  140. , mWrapper(0)
  141. , mID(aID)
  142. , mRole(aRole)
  143. , mOuterDoc(false)
  144. , mIsDoc(false)
  145. , mHasValue(aInterfaces & Interfaces::VALUE)
  146. , mIsHyperLink(aInterfaces & Interfaces::HYPERLINK)
  147. , mIsHyperText(aInterfaces & Interfaces::HYPERTEXT)
  148. {
  149. }
  150. explicit ProxyAccessibleBase(DocAccessibleParent* aThisAsDoc) :
  151. mParent(nullptr), mDoc(aThisAsDoc), mWrapper(0), mID(0),
  152. mRole(roles::DOCUMENT), mOuterDoc(false), mIsDoc(true), mHasValue(false),
  153. mIsHyperLink(false), mIsHyperText(false)
  154. {}
  155. protected:
  156. Derived* mParent;
  157. private:
  158. friend Derived;
  159. nsTArray<Derived*> mChildren;
  160. DocAccessibleParent* mDoc;
  161. uintptr_t mWrapper;
  162. uint64_t mID;
  163. protected:
  164. // XXX DocAccessibleParent gets to change this to change the role of
  165. // documents.
  166. role mRole : 27;
  167. private:
  168. bool mOuterDoc : 1;
  169. public:
  170. const bool mIsDoc: 1;
  171. const bool mHasValue: 1;
  172. const bool mIsHyperLink: 1;
  173. const bool mIsHyperText: 1;
  174. };
  175. extern template class ProxyAccessibleBase<ProxyAccessible>;
  176. }
  177. }
  178. #endif