summaryrefslogtreecommitdiff
path: root/accessible/ipc/win/DocAccessibleChild.h
blob: 7a3da0172538d8e600e7d53b74e1cbde4361388d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_a11y_DocAccessibleChild_h
#define mozilla_a11y_DocAccessibleChild_h

#include "mozilla/a11y/COMPtrTypes.h"
#include "mozilla/a11y/DocAccessibleChildBase.h"
#include "mozilla/dom/TabChild.h"
#include "mozilla/mscom/Ptr.h"

namespace mozilla {
namespace a11y {

/*
 * These objects handle content side communication for an accessible document,
 * and their lifetime is the same as the document they represent.
 */
class DocAccessibleChild : public DocAccessibleChildBase
{
public:
  explicit DocAccessibleChild(DocAccessible* aDoc);
  ~DocAccessibleChild();

  virtual void Shutdown() override;

  virtual bool
  RecvParentCOMProxy(const IAccessibleHolder& aParentCOMProxy) override;

  IAccessible* GetParentIAccessible() const { return mParentProxy.get(); }

  bool SendEvent(const uint64_t& aID, const uint32_t& type);
  bool SendHideEvent(const uint64_t& aRootID, const bool& aFromUser);
  bool SendStateChangeEvent(const uint64_t& aID, const uint64_t& aState,
                            const bool& aEnabled);
  bool SendCaretMoveEvent(const uint64_t& aID, const int32_t& aOffset);
  bool SendTextChangeEvent(const uint64_t& aID, const nsString& aStr,
                           const int32_t& aStart, const uint32_t& aLen,
                           const bool& aIsInsert, const bool& aFromUser);
  bool SendSelectionEvent(const uint64_t& aID, const uint64_t& aWidgetID,
                          const uint32_t& aType);
  bool SendRoleChangedEvent(const uint32_t& aRole);

  bool ConstructChildDocInParentProcess(DocAccessibleChild* aNewChildDoc,
                                        uint64_t aUniqueID, uint32_t aMsaaID);

  bool SendBindChildDoc(DocAccessibleChild* aChildDoc,
                        const uint64_t& aNewParentID);

protected:
  virtual void MaybeSendShowEvent(ShowEventData& aData, bool aFromUser) override;

private:
  void RemoveDeferredConstructor();

  bool IsConstructedInParentProcess() const { return mIsRemoteConstructed; }
  void SetConstructedInParentProcess() { mIsRemoteConstructed = true; }

  /**
   * DocAccessibleChild should not fire events until it has asynchronously
   * received the COM proxy for its parent. OTOH, content a11y may still be
   * attempting to fire events during this window of time. If this object does
   * not yet have its parent proxy, instead of immediately sending the events to
   * our parent, we enqueue them to mDeferredEvents. As soon as
   * RecvParentCOMProxy is called, we play back mDeferredEvents.
   */
  struct DeferredEvent
  {
    void Dispatch()
    {
      Dispatch(mTarget);
    }

    virtual ~DeferredEvent() {}

  protected:
    explicit DeferredEvent(DocAccessibleChild* aTarget)
      : mTarget(aTarget)
    {}

    virtual void Dispatch(DocAccessibleChild* aIPCDoc) = 0;

  private:
    DocAccessibleChild* mTarget;
  };

  void PushDeferredEvent(UniquePtr<DeferredEvent> aEvent);

  struct SerializedShow final : public DeferredEvent
  {
    SerializedShow(DocAccessibleChild* aTarget,
                   ShowEventData& aEventData, bool aFromUser)
      : DeferredEvent(aTarget)
      , mEventData(aEventData.ID(), aEventData.Idx(), nsTArray<AccessibleData>())
      , mFromUser(aFromUser)
    {
      // Since IPDL doesn't generate a move constructor for ShowEventData,
      // we move NewTree manually (ugh). We still construct with an empty
      // NewTree above so that the compiler catches any changes made to the
      // ShowEventData structure in IPDL.
      mEventData.NewTree() = Move(aEventData.NewTree());
    }

    void Dispatch(DocAccessibleChild* aIPCDoc) override
    {
      Unused << aIPCDoc->SendShowEvent(mEventData, mFromUser);
    }

    ShowEventData mEventData;
    bool          mFromUser;
  };

  struct SerializedHide final : public DeferredEvent
  {
    SerializedHide(DocAccessibleChild* aTarget, uint64_t aRootID, bool aFromUser)
      : DeferredEvent(aTarget)
      , mRootID(aRootID)
      , mFromUser(aFromUser)
    {}

    void Dispatch(DocAccessibleChild* aIPCDoc) override
    {
      Unused << aIPCDoc->SendHideEvent(mRootID, mFromUser);
    }

    uint64_t  mRootID;
    bool      mFromUser;
  };

  struct SerializedStateChange final : public DeferredEvent
  {
    SerializedStateChange(DocAccessibleChild* aTarget, uint64_t aID,
                          uint64_t aState, bool aEnabled)
      : DeferredEvent(aTarget)
      , mID(aID)
      , mState(aState)
      , mEnabled(aEnabled)
    {}

    void Dispatch(DocAccessibleChild* aIPCDoc) override
    {
      Unused << aIPCDoc->SendStateChangeEvent(mID, mState, mEnabled);
    }

    uint64_t  mID;
    uint64_t  mState;
    bool      mEnabled;
  };

  struct SerializedCaretMove final : public DeferredEvent
  {
    SerializedCaretMove(DocAccessibleChild* aTarget, uint64_t aID,
                        int32_t aOffset)
      : DeferredEvent(aTarget)
      , mID(aID)
      , mOffset(aOffset)
    {}

    void Dispatch(DocAccessibleChild* aIPCDoc) override
    {
      Unused << aIPCDoc->SendCaretMoveEvent(mID, mOffset);
    }

    uint64_t  mID;
    int32_t   mOffset;
  };

  struct SerializedTextChange final : public DeferredEvent
  {
    SerializedTextChange(DocAccessibleChild* aTarget, uint64_t aID,
                         const nsString& aStr, int32_t aStart, uint32_t aLen,
                         bool aIsInsert, bool aFromUser)
      : DeferredEvent(aTarget)
      , mID(aID)
      , mStr(aStr)
      , mStart(aStart)
      , mLen(aLen)
      , mIsInsert(aIsInsert)
      , mFromUser(aFromUser)
    {}

    void Dispatch(DocAccessibleChild* aIPCDoc) override
    {
      Unused << aIPCDoc->SendTextChangeEvent(mID, mStr, mStart, mLen, mIsInsert,
                                             mFromUser);
    }

    uint64_t  mID;
    nsString  mStr;
    int32_t   mStart;
    uint32_t  mLen;
    bool      mIsInsert;
    bool      mFromUser;
  };

  struct SerializedSelection final : public DeferredEvent
  {
    SerializedSelection(DocAccessibleChild* aTarget, uint64_t aID,
                        uint64_t aWidgetID, uint32_t aType)
      : DeferredEvent(aTarget)
      , mID(aID)
      , mWidgetID(aWidgetID)
      , mType(aType)
    {}

    void Dispatch(DocAccessibleChild* aIPCDoc) override
    {
      Unused << aIPCDoc->SendSelectionEvent(mID, mWidgetID, mType);
    }

    uint64_t  mID;
    uint64_t  mWidgetID;
    uint32_t  mType;
  };

  struct SerializedRoleChanged final : public DeferredEvent
  {
    explicit SerializedRoleChanged(DocAccessibleChild* aTarget, uint32_t aRole)
      : DeferredEvent(aTarget)
      , mRole(aRole)
    {}

    void Dispatch(DocAccessibleChild* aIPCDoc) override
    {
      Unused << aIPCDoc->SendRoleChangedEvent(mRole);
    }

    uint32_t mRole;
  };

  struct SerializedEvent final : public DeferredEvent
  {
    SerializedEvent(DocAccessibleChild* aTarget, uint64_t aID, uint32_t aType)
      : DeferredEvent(aTarget)
      , mID(aID)
      , mType(aType)
    {}

    void Dispatch(DocAccessibleChild* aIPCDoc) override
    {
      Unused << aIPCDoc->SendEvent(mID, mType);
    }

    uint64_t  mID;
    uint32_t  mType;
  };

  struct SerializedChildDocConstructor final : public DeferredEvent
  {
    SerializedChildDocConstructor(DocAccessibleChild* aIPCDoc,
                                  DocAccessibleChild* aParentIPCDoc,
                                  uint64_t aUniqueID, uint32_t aMsaaID)
      : DeferredEvent(aParentIPCDoc)
      , mIPCDoc(aIPCDoc)
      , mUniqueID(aUniqueID)
      , mMsaaID(aMsaaID)
    {}

    void Dispatch(DocAccessibleChild* aParentIPCDoc) override
    {
      auto tabChild = static_cast<dom::TabChild*>(aParentIPCDoc->Manager());
      MOZ_ASSERT(tabChild);
      Unused << tabChild->SendPDocAccessibleConstructor(mIPCDoc, aParentIPCDoc,
                                                        mUniqueID, mMsaaID,
                                                        IAccessibleHolder());
      mIPCDoc->SetConstructedInParentProcess();
    }

    DocAccessibleChild* mIPCDoc;
    uint64_t            mUniqueID;
    uint32_t            mMsaaID;
  };

  friend struct SerializedChildDocConstructor;

  struct SerializedBindChildDoc final : public DeferredEvent
  {
    SerializedBindChildDoc(DocAccessibleChild* aParentDoc,
                           DocAccessibleChild* aChildDoc, uint64_t aNewParentID)
      : DeferredEvent(aParentDoc)
      , mChildDoc(aChildDoc)
      , mNewParentID(aNewParentID)
    {}

    void Dispatch(DocAccessibleChild* aParentIPCDoc) override
    {
      Unused << aParentIPCDoc->SendBindChildDoc(mChildDoc, mNewParentID);
    }

    DocAccessibleChild* mChildDoc;
    uint64_t            mNewParentID;
  };

  struct SerializedShutdown final : public DeferredEvent
  {
    explicit SerializedShutdown(DocAccessibleChild* aTarget)
      : DeferredEvent(aTarget)
    {
    }

    void Dispatch(DocAccessibleChild* aIPCDoc) override
    {
      aIPCDoc->Shutdown();
    }
  };

  bool mIsRemoteConstructed;
  mscom::ProxyUniquePtr<IAccessible> mParentProxy;
  nsTArray<UniquePtr<DeferredEvent>> mDeferredEvents;
};

} // namespace a11y
} // namespace mozilla

#endif // mozilla_a11y_DocAccessibleChild_h