DocAccessibleParent.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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_DocAccessibleParent_h
  6. #define mozilla_a11y_DocAccessibleParent_h
  7. #include "nsAccessibilityService.h"
  8. #include "mozilla/a11y/PDocAccessibleParent.h"
  9. #include "mozilla/a11y/ProxyAccessible.h"
  10. #include "nsClassHashtable.h"
  11. #include "nsHashKeys.h"
  12. #include "nsISupportsImpl.h"
  13. namespace mozilla {
  14. namespace a11y {
  15. class xpcAccessibleGeneric;
  16. /*
  17. * These objects live in the main process and comunicate with and represent
  18. * an accessible document in a content process.
  19. */
  20. class DocAccessibleParent : public ProxyAccessible,
  21. public PDocAccessibleParent
  22. {
  23. public:
  24. DocAccessibleParent() :
  25. ProxyAccessible(this), mParentDoc(nullptr),
  26. mTopLevel(false), mShutdown(false)
  27. { MOZ_COUNT_CTOR_INHERITED(DocAccessibleParent, ProxyAccessible); }
  28. ~DocAccessibleParent()
  29. {
  30. MOZ_COUNT_DTOR_INHERITED(DocAccessibleParent, ProxyAccessible);
  31. MOZ_ASSERT(mChildDocs.Length() == 0);
  32. MOZ_ASSERT(!ParentDoc());
  33. }
  34. void SetTopLevel() { mTopLevel = true; }
  35. bool IsTopLevel() const { return mTopLevel; }
  36. bool IsShutdown() const { return mShutdown; }
  37. /*
  38. * Called when a message from a document in a child process notifies the main
  39. * process it is firing an event.
  40. */
  41. virtual bool RecvEvent(const uint64_t& aID, const uint32_t& aType)
  42. override;
  43. virtual bool RecvShowEvent(const ShowEventData& aData, const bool& aFromUser)
  44. override;
  45. virtual bool RecvHideEvent(const uint64_t& aRootID, const bool& aFromUser)
  46. override;
  47. virtual bool RecvStateChangeEvent(const uint64_t& aID,
  48. const uint64_t& aState,
  49. const bool& aEnabled) override final;
  50. virtual bool RecvCaretMoveEvent(const uint64_t& aID, const int32_t& aOffset)
  51. override final;
  52. virtual bool RecvTextChangeEvent(const uint64_t& aID, const nsString& aStr,
  53. const int32_t& aStart, const uint32_t& aLen,
  54. const bool& aIsInsert,
  55. const bool& aFromUser) override;
  56. virtual bool RecvSelectionEvent(const uint64_t& aID,
  57. const uint64_t& aWidgetID,
  58. const uint32_t& aType) override;
  59. virtual bool RecvRoleChangedEvent(const uint32_t& aRole) override final;
  60. virtual bool RecvBindChildDoc(PDocAccessibleParent* aChildDoc, const uint64_t& aID) override;
  61. void Unbind()
  62. {
  63. if (DocAccessibleParent* parent = ParentDoc()) {
  64. parent->RemoveChildDoc(this);
  65. }
  66. mParent = nullptr;
  67. }
  68. virtual bool RecvShutdown() override;
  69. void Destroy();
  70. virtual void ActorDestroy(ActorDestroyReason aWhy) override
  71. {
  72. MOZ_DIAGNOSTIC_ASSERT(CheckDocTree());
  73. if (!mShutdown)
  74. Destroy();
  75. }
  76. /*
  77. * Return the main processes representation of the parent document (if any)
  78. * of the document this object represents.
  79. */
  80. DocAccessibleParent* ParentDoc() const { return mParentDoc; }
  81. /*
  82. * Called when a document in a content process notifies the main process of a
  83. * new child document.
  84. */
  85. bool AddChildDoc(DocAccessibleParent* aChildDoc, uint64_t aParentID,
  86. bool aCreating = true);
  87. /*
  88. * Called when the document in the content process this object represents
  89. * notifies the main process a child document has been removed.
  90. */
  91. void RemoveChildDoc(DocAccessibleParent* aChildDoc)
  92. {
  93. aChildDoc->Parent()->ClearChildDoc(aChildDoc);
  94. mChildDocs.RemoveElement(aChildDoc);
  95. aChildDoc->mParentDoc = nullptr;
  96. MOZ_ASSERT(aChildDoc->mChildDocs.Length() == 0);
  97. }
  98. void RemoveAccessible(ProxyAccessible* aAccessible)
  99. {
  100. MOZ_DIAGNOSTIC_ASSERT(mAccessibles.GetEntry(aAccessible->ID()));
  101. mAccessibles.RemoveEntry(aAccessible->ID());
  102. }
  103. /**
  104. * Return the accessible for given id.
  105. */
  106. ProxyAccessible* GetAccessible(uintptr_t aID)
  107. {
  108. if (!aID)
  109. return this;
  110. ProxyEntry* e = mAccessibles.GetEntry(aID);
  111. return e ? e->mProxy : nullptr;
  112. }
  113. const ProxyAccessible* GetAccessible(uintptr_t aID) const
  114. { return const_cast<DocAccessibleParent*>(this)->GetAccessible(aID); }
  115. size_t ChildDocCount() const { return mChildDocs.Length(); }
  116. const DocAccessibleParent* ChildDocAt(size_t aIdx) const
  117. { return mChildDocs[aIdx]; }
  118. #if defined(XP_WIN)
  119. void SetCOMProxy(const RefPtr<IAccessible>& aCOMProxy);
  120. virtual bool RecvGetWindowedPluginIAccessible(
  121. const WindowsHandle& aHwnd, IAccessibleHolder* aPluginCOMProxy) override;
  122. #endif
  123. private:
  124. class ProxyEntry : public PLDHashEntryHdr
  125. {
  126. public:
  127. explicit ProxyEntry(const void*) : mProxy(nullptr) {}
  128. ProxyEntry(ProxyEntry&& aOther) :
  129. mProxy(aOther.mProxy) { aOther.mProxy = nullptr; }
  130. ~ProxyEntry() { delete mProxy; }
  131. typedef uint64_t KeyType;
  132. typedef const void* KeyTypePointer;
  133. bool KeyEquals(const void* aKey) const
  134. { return mProxy->ID() == (uint64_t)aKey; }
  135. static const void* KeyToPointer(uint64_t aKey) { return (void*)aKey; }
  136. static PLDHashNumber HashKey(const void* aKey) { return (uint64_t)aKey; }
  137. enum { ALLOW_MEMMOVE = true };
  138. ProxyAccessible* mProxy;
  139. };
  140. uint32_t AddSubtree(ProxyAccessible* aParent,
  141. const nsTArray<AccessibleData>& aNewTree, uint32_t aIdx,
  142. uint32_t aIdxInParent);
  143. MOZ_MUST_USE bool CheckDocTree() const;
  144. xpcAccessibleGeneric* GetXPCAccessible(ProxyAccessible* aProxy);
  145. nsTArray<DocAccessibleParent*> mChildDocs;
  146. DocAccessibleParent* mParentDoc;
  147. /*
  148. * Conceptually this is a map from IDs to proxies, but we store the ID in the
  149. * proxy object so we can't use a real map.
  150. */
  151. nsTHashtable<ProxyEntry> mAccessibles;
  152. bool mTopLevel;
  153. bool mShutdown;
  154. };
  155. }
  156. }
  157. #endif