summaryrefslogtreecommitdiffstats
path: root/dom/events/TextComposition.h
blob: 7199171068cdbae5b2929de8626541a99dcba0ca (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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=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_TextComposition_h
#define mozilla_TextComposition_h

#include "nsCOMPtr.h"
#include "nsINode.h"
#include "nsIWidget.h"
#include "nsTArray.h"
#include "nsThreadUtils.h"
#include "nsPresContext.h"
#include "mozilla/AlreadyAddRefed.h"
#include "mozilla/Attributes.h"
#include "mozilla/EventForwards.h"
#include "mozilla/RangeBoundary.h"
#include "mozilla/TextRange.h"
#include "mozilla/dom/BrowserParent.h"
#include "mozilla/dom/Text.h"

class nsRange;

struct CharacterDataChangeInfo;

namespace mozilla {

class EditorBase;
class EventDispatchingCallback;
class IMEStateManager;

/**
 * TextComposition represents a text composition.  This class stores the
 * composition event target and its presContext.  At dispatching the event via
 * this class, the instances use the stored event target.
 */

class TextComposition final {
  friend class IMEStateManager;

  NS_INLINE_DECL_REFCOUNTING(TextComposition)

 public:
  typedef dom::BrowserParent BrowserParent;
  typedef dom::Text Text;

  static bool IsHandlingSelectionEvent() { return sHandlingSelectionEvent; }

  TextComposition(nsPresContext* aPresContext, nsINode* aNode,
                  BrowserParent* aBrowserParent,
                  WidgetCompositionEvent* aCompositionEvent);

  bool Destroyed() const { return !mPresContext; }
  nsPresContext* GetPresContext() const { return mPresContext; }
  nsINode* GetEventTargetNode() const { return mNode; }
  // The text node which includes composition string.
  Text* GetContainerTextNode() const { return mContainerTextNode; }
  // The latest CompositionEvent.data value except compositionstart event.
  // This value is modified at dispatching compositionupdate.
  const nsString& LastData() const { return mLastData; }
  // Returns commit string if it'll be commited as-is.
  nsString CommitStringIfCommittedAsIs() const;
  // The composition string which is already handled by the focused editor.
  // I.e., this value must be same as the composition string on the focused
  // editor.  This value is modified at a call of
  // EditorDidHandleCompositionChangeEvent().
  // Note that mString and mLastData are different between dispatcing
  // compositionupdate and compositionchange event handled by focused editor.
  const nsString& String() const { return mString; }
  // The latest clauses range of the composition string.
  // During compositionupdate event, GetRanges() returns old ranges.
  // So if getting on compositionupdate, Use GetLastRange instead of GetRange().
  TextRangeArray* GetLastRanges() const { return mLastRanges; }
  // Returns the clauses and/or caret range of the composition string.
  // This is modified at a call of EditorWillHandleCompositionChangeEvent().
  // This may return null if there is no clauses and caret.
  // XXX We should return |const TextRangeArray*| here, but it causes compile
  //     error due to inaccessible Release() method.
  TextRangeArray* GetRanges() const { return mRanges; }
  // Returns the widget which is proper to call NotifyIME().
  already_AddRefed<nsIWidget> GetWidget() const {
    if (!mPresContext) {
      return nullptr;
    }
    return do_AddRef(mPresContext->GetRootWidget());
  }
  // Returns the tab parent which has this composition in its remote process.
  BrowserParent* GetBrowserParent() const { return mBrowserParent; }
  // Returns true if the composition is started with synthesized event which
  // came from nsDOMWindowUtils.
  bool IsSynthesizedForTests() const { return mIsSynthesizedForTests; }

  const widget::NativeIMEContext& GetNativeIMEContext() const {
    return mNativeContext;
  }

  /**
   * This is called when IMEStateManager stops managing the instance.
   */
  void Destroy();

  /**
   * Request to commit (or cancel) the composition to IME.  This method should
   * be called only by IMEStateManager::NotifyIME().
   */
  nsresult RequestToCommit(nsIWidget* aWidget, bool aDiscard);

  /**
   * IsRequestingCommitOrCancelComposition() returns true if the instance is
   * requesting widget to commit or cancel composition.
   */
  bool IsRequestingCommitOrCancelComposition() const {
    return mIsRequestingCancel || mIsRequestingCommit;
  }

  /**
   * Send a notification to IME.  It depends on the IME or platform spec what
   * will occur (or not occur).
   */
  nsresult NotifyIME(widget::IMEMessage aMessage);

  /**
   * the offset of first composition string
   */
  uint32_t NativeOffsetOfStartComposition() const {
    return mCompositionStartOffset;
  }

  /**
   * the offset of first selected clause or start of composition
   */
  uint32_t NativeOffsetOfTargetClause() const {
    return mCompositionStartOffset + mTargetClauseOffsetInComposition;
  }

  /**
   * Return current composition start and end point in the DOM tree.
   * Note that one of or both of those result container may be different
   * from GetContainerTextNode() if the DOM tree was modified by the web
   * app.  If there is no composition string the DOM tree, these return
   * unset range boundaries.
   */
  RawRangeBoundary FirstIMESelectionStartRef() const;
  RawRangeBoundary LastIMESelectionEndRef() const;

  /**
   * The offset of composition string in the text node.  If composition string
   * hasn't been inserted in any text node yet, this returns UINT32_MAX.
   */
  uint32_t XPOffsetInTextNode() const {
    return mCompositionStartOffsetInTextNode;
  }

  /**
   * The length of composition string in the text node.  If composition string
   * hasn't been inserted in any text node yet, this returns 0.
   */
  uint32_t XPLengthInTextNode() const {
    return mCompositionLengthInTextNode == UINT32_MAX
               ? 0
               : mCompositionLengthInTextNode;
  }

  /**
   * The end offset of composition string in the text node.  If composition
   * string hasn't been inserted in any text node yet, this returns UINT32_MAX.
   */
  uint32_t XPEndOffsetInTextNode() const {
    if (mCompositionStartOffsetInTextNode == UINT32_MAX ||
        mCompositionLengthInTextNode == UINT32_MAX) {
      return UINT32_MAX;
    }
    return mCompositionStartOffsetInTextNode + mCompositionLengthInTextNode;
  }

  /**
   * Returns true if there is non-empty composition string and it's not fixed.
   * Otherwise, false.
   */
  bool IsComposing() const { return mIsComposing; }

  /**
   * Returns true while editor is handling an event which is modifying the
   * composition string.
   */
  bool IsEditorHandlingEvent() const { return mIsEditorHandlingEvent; }

  /**
   * IsMovingToNewTextNode() returns true if editor detects the text node
   * has been removed and still not insert the composition string into
   * new text node.
   */
  bool IsMovingToNewTextNode() const {
    return !mContainerTextNode && mCompositionLengthInTextNode &&
           mCompositionLengthInTextNode != UINT32_MAX;
  }

  /**
   * StartHandlingComposition() and EndHandlingComposition() are called by
   * editor when it holds a TextComposition instance and release it.
   */
  void StartHandlingComposition(EditorBase* aEditorBase);
  void EndHandlingComposition(EditorBase* aEditorBase);

  /**
   * OnEditorDestroyed() is called when the editor is destroyed but there is
   * active composition.
   */
  void OnEditorDestroyed();

  /**
   * CompositionChangeEventHandlingMarker class should be created at starting
   * to handle text event in focused editor.  This calls
   * EditorWillHandleCompositionChangeEvent() and
   * EditorDidHandleCompositionChangeEvent() automatically.
   */
  class MOZ_STACK_CLASS CompositionChangeEventHandlingMarker {
   public:
    CompositionChangeEventHandlingMarker(
        TextComposition* aComposition,
        const WidgetCompositionEvent* aCompositionChangeEvent)
        : mComposition(aComposition) {
      mComposition->EditorWillHandleCompositionChangeEvent(
          aCompositionChangeEvent);
    }

    ~CompositionChangeEventHandlingMarker() {
      mComposition->EditorDidHandleCompositionChangeEvent();
    }

   private:
    RefPtr<TextComposition> mComposition;
    CompositionChangeEventHandlingMarker();
    CompositionChangeEventHandlingMarker(
        const CompositionChangeEventHandlingMarker& aOther);
  };

  /**
   * OnUpdateCompositionInEditor() is called when editor updates composition
   * string in the DOM tree.
   *
   * @param aStringToInsert     The string to insert the text node actually.
   *                            This may be different from the data of
   *                            dispatching composition event because it may
   *                            be replaced with different character for
   *                            passwords, or truncated due to maxlength.
   * @param aTextNode           The text node which includes composition string.
   * @param aOffset             The offset of composition string in aTextNode.
   */
  void OnUpdateCompositionInEditor(const nsAString& aStringToInsert,
                                   Text& aTextNode, uint32_t aOffset) {
    mContainerTextNode = &aTextNode;
    mCompositionStartOffsetInTextNode = aOffset;
    NS_WARNING_ASSERTION(mCompositionStartOffsetInTextNode != UINT32_MAX,
                         "The text node is really too long.");
    mCompositionLengthInTextNode = aStringToInsert.Length();
    NS_WARNING_ASSERTION(mCompositionLengthInTextNode != UINT32_MAX,
                         "The string to insert is really too long.");
  }

  /**
   * OnTextNodeRemoved() is called when focused editor is reframed and
   * mContainerTextNode may be (or have been) replaced with different text
   * node, or just removes the text node due to empty.
   */
  void OnTextNodeRemoved() {
    mContainerTextNode = nullptr;
    // Don't reset mCompositionStartOffsetInTextNode nor
    // mCompositionLengthInTextNode because editor needs them to restore
    // composition in new text node.
  }

  /**
   * OnCharacterDataChanged() is called when IMEContentObserver receives
   * character data change notifications.
   */
  void OnCharacterDataChanged(Text& aText,
                              const CharacterDataChangeInfo& aInfo);

 private:
  // Private destructor, to discourage deletion outside of Release():
  ~TextComposition() {
    // WARNING: mPresContext may be destroying, so, be careful if you touch it.
  }

  // sHandlingSelectionEvent is true while TextComposition sends a selection
  // event to ContentEventHandler.
  static bool sHandlingSelectionEvent;

  // This class holds nsPresContext weak.  This instance shouldn't block
  // destroying it.  When the presContext is being destroyed, it's notified to
  // IMEStateManager::OnDestroyPresContext(), and then, it destroy
  // this instance.
  nsPresContext* mPresContext;
  RefPtr<nsINode> mNode;
  RefPtr<BrowserParent> mBrowserParent;

  // The text node which includes the composition string.
  RefPtr<Text> mContainerTextNode;

  // This is the clause and caret range information which is managed by
  // the focused editor.  This may be null if there is no clauses or caret.
  RefPtr<TextRangeArray> mRanges;
  // Same as mRange, but mRange will have old data during compositionupdate.
  // So this will be valied during compositionupdate.
  RefPtr<TextRangeArray> mLastRanges;

  // mNativeContext stores a opaque pointer.  This works as the "ID" for this
  // composition.  Don't access the instance, it may not be available.
  widget::NativeIMEContext mNativeContext;

  // mEditorBaseWeak is a weak reference to the focused editor handling
  // composition.
  nsWeakPtr mEditorBaseWeak;

  // mLastData stores the data attribute of the latest composition event (except
  // the compositionstart event).
  nsString mLastData;

  // mString stores the composition text which has been handled by the focused
  // editor.
  nsString mString;

  // Offset of the composition string from start of the editor
  uint32_t mCompositionStartOffset;
  // Offset of the selected clause of the composition string from
  // mCompositionStartOffset
  uint32_t mTargetClauseOffsetInComposition;
  // Offset of the composition string in mContainerTextNode.
  // NOTE: This is NOT valid in the main process if focused editor is in a
  //       remote process.
  uint32_t mCompositionStartOffsetInTextNode;
  // Length of the composition string in mContainerTextNode.  If this instance
  // has already dispatched eCompositionCommit(AsIs) and
  // EditorDidHandleCompositionChangeEvent() has already been called,
  // this may be different from length of mString because committed string
  // may be truncated by maxlength attribute of <input> or <textarea>.
  // NOTE: This is NOT valid in the main process if focused editor is in a
  //       remote process.
  uint32_t mCompositionLengthInTextNode;

  // See the comment for IsSynthesizedForTests().
  bool mIsSynthesizedForTests;

  // See the comment for IsComposing().
  bool mIsComposing;

  // mIsEditorHandlingEvent is true while editor is modifying the composition
  // string.
  bool mIsEditorHandlingEvent;

  // mIsRequestingCommit or mIsRequestingCancel is true *only* while we're
  // requesting commit or canceling the composition.  In other words, while
  // one of these values is true, we're handling the request.
  bool mIsRequestingCommit;
  bool mIsRequestingCancel;

  // mRequestedToCommitOrCancel is true *after* we requested IME to commit or
  // cancel the composition.  In other words, we already requested of IME that
  // it commits or cancels current composition.
  // NOTE: Before this is set to true, both mIsRequestingCommit and
  //       mIsRequestingCancel are set to false.
  bool mRequestedToCommitOrCancel;

  // Set to true if the instance dispatches an eCompositionChange event.
  bool mHasDispatchedDOMTextEvent;

  // Before this dispatches commit event into the tree, this is set to true.
  // So, this means if native IME already commits the composition.
  bool mHasReceivedCommitEvent;

  // mWasNativeCompositionEndEventDiscarded is true if this composition was
  // requested commit or cancel itself but native compositionend event is
  // discarded by PresShell due to not safe to dispatch events.
  bool mWasNativeCompositionEndEventDiscarded;

  // Allow control characters appear in composition string.
  // When this is false, control characters except
  // CHARACTER TABULATION (horizontal tab) are removed from
  // both composition string and data attribute of compositionupdate
  // and compositionend events.
  bool mAllowControlCharacters;

  // mWasCompositionStringEmpty is true if the composition string was empty
  // when DispatchCompositionEvent() is called.
  bool mWasCompositionStringEmpty;

  // Hide the default constructor and copy constructor.
  TextComposition()
      : mPresContext(nullptr),
        mNativeContext(nullptr),
        mCompositionStartOffset(0),
        mTargetClauseOffsetInComposition(0),
        mCompositionStartOffsetInTextNode(UINT32_MAX),
        mCompositionLengthInTextNode(UINT32_MAX),
        mIsSynthesizedForTests(false),
        mIsComposing(false),
        mIsEditorHandlingEvent(false),
        mIsRequestingCommit(false),
        mIsRequestingCancel(false),
        mRequestedToCommitOrCancel(false),
        mHasReceivedCommitEvent(false),
        mWasNativeCompositionEndEventDiscarded(false),
        mAllowControlCharacters(false),
        mWasCompositionStringEmpty(true) {}
  TextComposition(const TextComposition& aOther);

  /**
   * If we're requesting IME to commit or cancel composition, or we've already
   * requested it, or we've already known this composition has been ended in
   * IME, we don't need to request commit nor cancel composition anymore and
   * shouldn't do so if we're in content process for not committing/canceling
   * "current" composition in native IME.  So, when this returns true,
   * RequestIMEToCommit() does nothing.
   */
  bool CanRequsetIMEToCommitOrCancelComposition() const {
    return !mIsRequestingCommit && !mIsRequestingCancel &&
           !mRequestedToCommitOrCancel && !mHasReceivedCommitEvent;
  }

  /**
   * GetEditorBase() returns EditorBase pointer of mEditorBaseWeak.
   */
  already_AddRefed<EditorBase> GetEditorBase() const;

  /**
   * HasEditor() returns true if mEditorBaseWeak holds EditorBase instance
   * which is alive.  Otherwise, false.
   */
  bool HasEditor() const;

  /**
   * EditorWillHandleCompositionChangeEvent() must be called before the focused
   * editor handles the compositionchange event.
   */
  void EditorWillHandleCompositionChangeEvent(
      const WidgetCompositionEvent* aCompositionChangeEvent);

  /**
   * EditorDidHandleCompositionChangeEvent() must be called after the focused
   * editor handles a compositionchange event.
   */
  void EditorDidHandleCompositionChangeEvent();

  /**
   * IsValidStateForComposition() returns true if it's safe to dispatch an event
   * to the DOM tree.  Otherwise, false.
   * WARNING: This doesn't check script blocker state.  It should be checked
   *          before dispatching the first event.
   */
  bool IsValidStateForComposition(nsIWidget* aWidget) const;

  /**
   * DispatchCompositionEvent() dispatches the aCompositionEvent to the mContent
   * synchronously. The caller must ensure that it's safe to dispatch the event.
   */
  MOZ_CAN_RUN_SCRIPT void DispatchCompositionEvent(
      WidgetCompositionEvent* aCompositionEvent, nsEventStatus* aStatus,
      EventDispatchingCallback* aCallBack, bool aIsSynthesized);

  /**
   * Simply calling EventDispatcher::Dispatch() with plugin event.
   * If dispatching event has no orginal clone, aOriginalEvent can be null.
   */
  MOZ_CAN_RUN_SCRIPT void DispatchEvent(
      WidgetCompositionEvent* aDispatchEvent, nsEventStatus* aStatus,
      EventDispatchingCallback* aCallback,
      const WidgetCompositionEvent* aOriginalEvent = nullptr);

  /**
   * HandleSelectionEvent() sends the selection event to ContentEventHandler
   * or dispatches it to the focused child process.
   */
  MOZ_CAN_RUN_SCRIPT
  void HandleSelectionEvent(WidgetSelectionEvent* aSelectionEvent) {
    RefPtr<nsPresContext> presContext(mPresContext);
    RefPtr<BrowserParent> browserParent(mBrowserParent);
    HandleSelectionEvent(presContext, browserParent, aSelectionEvent);
  }
  MOZ_CAN_RUN_SCRIPT
  static void HandleSelectionEvent(nsPresContext* aPresContext,
                                   BrowserParent* aBrowserParent,
                                   WidgetSelectionEvent* aSelectionEvent);

  /**
   * MaybeDispatchCompositionUpdate() may dispatch a compositionupdate event
   * if aCompositionEvent changes composition string.
   * @return Returns false if dispatching the compositionupdate event caused
   *         destroying this composition.
   */
  MOZ_CAN_RUN_SCRIPT bool MaybeDispatchCompositionUpdate(
      const WidgetCompositionEvent* aCompositionEvent);

  /**
   * CloneAndDispatchAs() dispatches a composition event which is
   * duplicateed from aCompositionEvent and set the aMessage.
   *
   * @return Returns BaseEventFlags which is the result of dispatched event.
   */
  MOZ_CAN_RUN_SCRIPT BaseEventFlags
  CloneAndDispatchAs(const WidgetCompositionEvent* aCompositionEvent,
                     EventMessage aMessage, nsEventStatus* aStatus = nullptr,
                     EventDispatchingCallback* aCallBack = nullptr);

  /**
   * If IME has already dispatched compositionend event but it was discarded
   * by PresShell due to not safe to dispatch, this returns true.
   */
  bool WasNativeCompositionEndEventDiscarded() const {
    return mWasNativeCompositionEndEventDiscarded;
  }

  /**
   * OnCompositionEventDiscarded() is called when PresShell discards
   * compositionupdate, compositionend or compositionchange event due to not
   * safe to dispatch event.
   */
  void OnCompositionEventDiscarded(WidgetCompositionEvent* aCompositionEvent);

  /**
   * OnCompositionEventDispatched() is called after a composition event is
   * dispatched.
   */
  MOZ_CAN_RUN_SCRIPT void OnCompositionEventDispatched(
      const WidgetCompositionEvent* aDispatchEvent);

  /**
   * MaybeNotifyIMEOfCompositionEventHandled() notifies IME of composition
   * event handled.  This should be called after dispatching a composition
   * event which came from widget.
   */
  void MaybeNotifyIMEOfCompositionEventHandled(
      const WidgetCompositionEvent* aCompositionEvent);

  /**
   * GetSelectionStartOffset() returns normal selection start offset in the
   * editor which has this composition.
   * If it failed or lost focus, this would return 0.
   */
  MOZ_CAN_RUN_SCRIPT uint32_t GetSelectionStartOffset();

  /**
   * OnStartOffsetUpdatedInChild() is called when composition start offset
   * is updated in the child process.  I.e., this is called and never called
   * if the composition is in this process.
   * @param aStartOffset        New composition start offset with native
   *                            linebreaks.
   */
  void OnStartOffsetUpdatedInChild(uint32_t aStartOffset);

  /**
   * CompositionEventDispatcher dispatches the specified composition (or text)
   * event.
   */
  class CompositionEventDispatcher : public Runnable {
   public:
    CompositionEventDispatcher(TextComposition* aTextComposition,
                               nsINode* aEventTarget,
                               EventMessage aEventMessage,
                               const nsAString& aData,
                               bool aIsSynthesizedEvent = false);
    MOZ_CAN_RUN_SCRIPT_BOUNDARY NS_IMETHOD Run() override;

   private:
    RefPtr<TextComposition> mTextComposition;
    nsCOMPtr<nsINode> mEventTarget;
    nsString mData;
    EventMessage mEventMessage;
    bool mIsSynthesizedEvent;

    CompositionEventDispatcher()
        : Runnable("TextComposition::CompositionEventDispatcher"),
          mEventMessage(eVoidEvent),
          mIsSynthesizedEvent(false){};
  };

  /**
   * DispatchCompositionEventRunnable() dispatches a composition event to the
   * content.  Be aware, if you use this method, nsPresShellEventCB isn't used.
   * That means that nsIFrame::HandleEvent() is never called.
   * WARNING: The instance which is managed by IMEStateManager may be
   *          destroyed by this method call.
   *
   * @param aEventMessage       Must be one of composition events.
   * @param aData               Used for mData value.
   * @param aIsSynthesizingCommit   true if this is called for synthesizing
   *                                commit or cancel composition.  Otherwise,
   *                                false.
   */
  void DispatchCompositionEventRunnable(EventMessage aEventMessage,
                                        const nsAString& aData,
                                        bool aIsSynthesizingCommit = false);
};

/**
 * TextCompositionArray manages the instances of TextComposition class.
 * Managing with array is enough because only one composition is typically
 * there.  Even if user switches native IME context, it's very rare that
 * second or more composition is started.
 * It's assumed that this is used by IMEStateManager for storing all active
 * compositions in the process.  If the instance is it, each TextComposition
 * in the array can be destroyed by calling some methods of itself.
 */

class TextCompositionArray final
    : public AutoTArray<RefPtr<TextComposition>, 2> {
 public:
  // Looking for per native IME context.
  index_type IndexOf(const widget::NativeIMEContext& aNativeIMEContext);
  index_type IndexOf(nsIWidget* aWidget);

  TextComposition* GetCompositionFor(nsIWidget* aWidget);
  TextComposition* GetCompositionFor(
      const WidgetCompositionEvent* aCompositionEvent);

  // Looking for per nsPresContext
  index_type IndexOf(nsPresContext* aPresContext);
  index_type IndexOf(nsPresContext* aPresContext, nsINode* aNode);

  TextComposition* GetCompositionFor(nsPresContext* aPresContext);
  TextComposition* GetCompositionFor(nsPresContext* aPresContext,
                                     nsINode* aNode);
  TextComposition* GetCompositionInContent(nsPresContext* aPresContext,
                                           nsIContent* aContent);
};

}  // namespace mozilla

#endif  // #ifndef mozilla_TextComposition_h