Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/TextInputProcessor.h
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#ifndef mozilla_dom_textinputprocessor_h_
8
#define mozilla_dom_textinputprocessor_h_
9
10
#include "mozilla/Attributes.h"
11
#include "mozilla/EventForwards.h"
12
#include "mozilla/TextEventDispatcher.h"
13
#include "mozilla/TextEventDispatcherListener.h"
14
#include "nsITextInputProcessor.h"
15
#include "nsITextInputProcessorCallback.h"
16
#include "nsTArray.h"
17
18
namespace mozilla {
19
20
namespace dom {
21
class KeyboardEvent;
22
} // namespace dom
23
24
class TextInputProcessor final : public nsITextInputProcessor
25
                               , public widget::TextEventDispatcherListener
26
{
27
  typedef mozilla::widget::IMENotification IMENotification;
28
  typedef mozilla::widget::IMENotificationRequests IMENotificationRequests;
29
  typedef mozilla::widget::TextEventDispatcher TextEventDispatcher;
30
31
public:
32
  TextInputProcessor();
33
34
  NS_DECL_ISUPPORTS
35
  NS_DECL_NSITEXTINPUTPROCESSOR
36
37
  // TextEventDispatcherListener
38
  NS_IMETHOD NotifyIME(TextEventDispatcher* aTextEventDispatcher,
39
                       const IMENotification& aNotification) override;
40
41
  NS_IMETHOD_(IMENotificationRequests) GetIMENotificationRequests() override;
42
43
  NS_IMETHOD_(void)
44
    OnRemovedFrom(TextEventDispatcher* aTextEventDispatcher) override;
45
46
  NS_IMETHOD_(void) WillDispatchKeyboardEvent(
47
                      TextEventDispatcher* aTextEventDispatcher,
48
                      WidgetKeyboardEvent& aKeyboardEvent,
49
                      uint32_t aIndexOfKeypress,
50
                      void* aData) override;
51
52
protected:
53
  virtual ~TextInputProcessor();
54
55
private:
56
  bool IsComposing() const;
57
  nsresult BeginInputTransactionInternal(
58
             mozIDOMWindow* aWindow,
59
             nsITextInputProcessorCallback* aCallback,
60
             bool aForTests,
61
             bool& aSucceeded);
62
  nsresult CommitCompositionInternal(
63
             const WidgetKeyboardEvent* aKeyboardEvent = nullptr,
64
             uint32_t aKeyFlags = 0,
65
             const nsAString* aCommitString = nullptr,
66
             bool* aSucceeded = nullptr);
67
  nsresult CancelCompositionInternal(
68
             const WidgetKeyboardEvent* aKeyboardEvent = nullptr,
69
             uint32_t aKeyFlags = 0);
70
  nsresult KeydownInternal(const WidgetKeyboardEvent& aKeyboardEvent,
71
                           uint32_t aKeyFlags,
72
                           bool aAllowToDispatchKeypress,
73
                           uint32_t& aConsumedFlags);
74
  nsresult KeyupInternal(const WidgetKeyboardEvent& aKeyboardEvent,
75
                         uint32_t aKeyFlags,
76
                         bool& aDoDefault);
77
  nsresult IsValidStateForComposition();
78
  void UnlinkFromTextEventDispatcher();
79
  nsresult PrepareKeyboardEventToDispatch(WidgetKeyboardEvent& aKeyboardEvent,
80
                                          uint32_t aKeyFlags);
81
  bool IsValidEventTypeForComposition(
82
         const WidgetKeyboardEvent& aKeyboardEvent) const;
83
  nsresult PrepareKeyboardEventForComposition(
84
             dom::KeyboardEvent* aDOMKeyEvent,
85
             uint32_t& aKeyFlags,
86
             uint8_t aOptionalArgc,
87
             WidgetKeyboardEvent*& aKeyboardEvent);
88
89
  struct EventDispatcherResult
90
  {
91
    nsresult mResult;
92
    bool     mDoDefault;
93
    bool     mCanContinue;
94
95
    EventDispatcherResult()
96
      : mResult(NS_OK)
97
      , mDoDefault(true)
98
      , mCanContinue(true)
99
0
    {
100
0
    }
101
  };
102
  EventDispatcherResult MaybeDispatchKeydownForComposition(
103
                          const WidgetKeyboardEvent* aKeyboardEvent,
104
                          uint32_t aKeyFlags);
105
  EventDispatcherResult MaybeDispatchKeyupForComposition(
106
                          const WidgetKeyboardEvent* aKeyboardEvent,
107
                          uint32_t aKeyFlags);
108
109
  /**
110
   * AutoPendingCompositionResetter guarantees to clear all pending composition
111
   * data in its destructor.
112
   */
113
  class MOZ_STACK_CLASS AutoPendingCompositionResetter
114
  {
115
  public:
116
    explicit AutoPendingCompositionResetter(TextInputProcessor* aTIP);
117
    ~AutoPendingCompositionResetter();
118
119
  private:
120
    RefPtr<TextInputProcessor> mTIP;
121
  };
122
123
  /**
124
   * TextInputProcessor manages modifier state both with .key and .code.
125
   * For example, left shift key up shouldn't cause inactivating shift state
126
   * while right shift key is being pressed.
127
   */
128
  struct ModifierKeyData
129
  {
130
    // One of modifier key name
131
    KeyNameIndex mKeyNameIndex;
132
    // Any code name is allowed.
133
    CodeNameIndex mCodeNameIndex;
134
    // A modifier key flag which is activated by the key.
135
    Modifiers mModifier;
136
137
    explicit ModifierKeyData(const WidgetKeyboardEvent& aKeyboardEvent);
138
139
    bool operator==(const ModifierKeyData& aOther) const
140
0
    {
141
0
      return mKeyNameIndex == aOther.mKeyNameIndex &&
142
0
             mCodeNameIndex == aOther.mCodeNameIndex;
143
0
    }
144
  };
145
146
  class ModifierKeyDataArray : public nsTArray<ModifierKeyData>
147
  {
148
    NS_INLINE_DECL_REFCOUNTING(ModifierKeyDataArray)
149
150
  public:
151
    Modifiers GetActiveModifiers() const;
152
    void ActivateModifierKey(const ModifierKeyData& aModifierKeyData);
153
    void InactivateModifierKey(const ModifierKeyData& aModifierKeyData);
154
    void ToggleModifierKey(const ModifierKeyData& aModifierKeyData);
155
156
  private:
157
0
    virtual ~ModifierKeyDataArray() { }
158
  };
159
160
  Modifiers GetActiveModifiers() const
161
0
  {
162
0
    return mModifierKeyDataArray ?
163
0
      mModifierKeyDataArray->GetActiveModifiers() : 0;
164
0
  }
165
  void EnsureModifierKeyDataArray()
166
0
  {
167
0
    if (mModifierKeyDataArray) {
168
0
      return;
169
0
    }
170
0
    mModifierKeyDataArray = new ModifierKeyDataArray();
171
0
  }
172
  void ActivateModifierKey(const ModifierKeyData& aModifierKeyData)
173
0
  {
174
0
    EnsureModifierKeyDataArray();
175
0
    mModifierKeyDataArray->ActivateModifierKey(aModifierKeyData);
176
0
  }
177
  void InactivateModifierKey(const ModifierKeyData& aModifierKeyData)
178
0
  {
179
0
    if (!mModifierKeyDataArray) {
180
0
      return;
181
0
    }
182
0
    mModifierKeyDataArray->InactivateModifierKey(aModifierKeyData);
183
0
  }
184
  void ToggleModifierKey(const ModifierKeyData& aModifierKeyData)
185
0
  {
186
0
    EnsureModifierKeyDataArray();
187
0
    mModifierKeyDataArray->ToggleModifierKey(aModifierKeyData);
188
0
  }
189
190
  TextEventDispatcher* mDispatcher; // [Weak]
191
  nsCOMPtr<nsITextInputProcessorCallback> mCallback;
192
  RefPtr<ModifierKeyDataArray> mModifierKeyDataArray;
193
194
  bool mForTests;
195
};
196
197
} // namespace mozilla
198
199
#endif // #ifndef mozilla_dom_textinputprocessor_h_