File: TSFUtils.h

package info (click to toggle)
firefox 147.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,484 kB
  • sloc: cpp: 7,607,246; javascript: 6,533,185; ansic: 3,775,227; python: 1,415,393; xml: 634,561; asm: 438,951; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (484 lines) | stat: -rw-r--r-- 15,186 bytes parent folder | download | duplicates (12)
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 TSFUtils_h
#define TSFUtils_h

#include <ostream>

#include <msctf.h>
#include <textstor.h>

#include "mozilla/Maybe.h"
#include "mozilla/RefPtr.h"
#include "mozilla/Result.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/TextRange.h"
#include "mozilla/ToString.h"
#include "mozilla/widget/IMEData.h"
#include "nsPrintfCString.h"
#include "nsString.h"
#include "nsTArray.h"

// GUID_PROP_INPUTSCOPE is declared in inputscope.h using INIT_GUID.
// With initguid.h, we get its instance instead of extern declaration.
#ifdef INPUTSCOPE_INIT_GUID
#  include <initguid.h>
#endif
#ifdef TEXTATTRS_INIT_GUID
#  include <tsattrs.h>
#endif
#include <inputscope.h>

// TSF InputScope, for earlier SDK 8
#define IS_SEARCH static_cast<InputScope>(50)

class nsWindow;

namespace mozilla::widget {
class TSFEmptyTextStore;
class TSFTextStore;
class TSFTextStoreBase;
struct IMENotificationRequests;
struct InputContext;
struct InputContextAction;

class TSFUtils final {
 public:
  TSFUtils() = delete;
  ~TSFUtils() = delete;

  static void Initialize();
  static void Shutdown();

  /**
   * Return true while TSF is available.
   */
  [[nodiscard]] static bool IsAvailable() { return sThreadMgr; }

  [[nodiscard]] static IMENotificationRequests GetIMENotificationRequests();

  enum class GotFocus : bool { No, Yes };
  /**
   * Called when focus changed in the DOM level and aContext has the details of
   * the new focused element.
   */
  static nsresult OnFocusChange(GotFocus aGotFocus, nsWindow* aFocusedWindow,
                                const InputContext& aContext);

  /**
   * Called when input context for aWindow is set.  aWindow should have focus
   * when this is called.
   */
  static void OnSetInputContext(nsWindow* aWindow, const InputContext& aContext,
                                const InputContextAction& aAction);

  // FIXME: Simplify the following APIs with TSFTextStoreBase::IsEditable().

  /**
   * Active TextStore is a TSFTextStoreBase instance which is for editable
   * content.
   * Note that it may disable IME, e.g., when the editable content is a password
   * field or `ime-mode: disabled`.
   */
  [[nodiscard]] static TSFTextStore* GetActiveTextStore() {
    MOZ_ASSERT_IF(sActiveTextStore, (void*)sActiveTextStore.get() ==
                                        (void*)sCurrentTextStore.get());
    return sActiveTextStore.get();
  }

  /**
   * Current TextStore ia TSFTextStoreBase instance for either the content is
   * editable nor not editable.
   */
  [[nodiscard]] static TSFTextStoreBase* GetCurrentTextStore() {
    MOZ_ASSERT_IF(sActiveTextStore, (void*)sActiveTextStore.get() ==
                                        (void*)sCurrentTextStore.get());
    return sCurrentTextStore.get();
  }

  /**
   * Check whether current TextStore is for editable one.  I.e., when there is
   * an active TextStore.  This is just an alias to make the caller easier to
   * read.
   */
  [[nodiscard]] static bool CurrentTextStoreIsEditable() {
    MOZ_ASSERT_IF(sActiveTextStore, (void*)sActiveTextStore.get() ==
                                        (void*)sCurrentTextStore.get());
    return sActiveTextStore;
  }

  template <typename TSFTextStoreClass>
  static void ClearStoringTextStoresIf(
      const RefPtr<TSFTextStoreClass>& aTextStore);

  [[nodiscard]] static ITfThreadMgr* GetThreadMgr() { return sThreadMgr; }
  [[nodiscard]] static ITfMessagePump* GetMessagePump() {
    if (!sMessagePump) {
      EnsureMessagePump();
    }
    return sMessagePump;
  }
  [[nodiscard]] static ITfKeystrokeMgr* GetKeystrokeMgr() {
    if (!sKeystrokeMgr) {
      EnsureKeystrokeMgr();
    }
    return sKeystrokeMgr;
  }
  [[nodiscard]] static ITfInputProcessorProfiles* GetInputProcessorProfiles();
  [[nodiscard]] static ITfDisplayAttributeMgr* GetDisplayAttributeMgr();
  [[nodiscard]] static ITfCategoryMgr* GetCategoryMgr();
  [[nodiscard]] static ITfCompartment* GetCompartmentForOpenClose();

  [[nodiscard]] static DWORD ClientId() { return sClientId; }

  // TODO: GUID_PROP_URL has not been declared in the SDK yet.  We should drop
  // this after it's released by a new SDK and it becomes the minimum supported
  // SDK version.
  constexpr static const GUID sGUID_PROP_URL = {
      0xd5138268,
      0xa1bf,
      0x4308,
      {0xbc, 0xbf, 0x2e, 0x73, 0x93, 0x98, 0xe2, 0x34}};

  constexpr static TsViewCookie sDefaultView = 1;

  /**
   * Returns true if the Windows may have a crash bug at handling
   * ITfTextStoreACP::GetSelection() returns error.
   */
  [[nodiscard]] static bool DoNotReturnErrorFromGetSelection();

  struct MOZ_STACK_CLASS AutoRangeExtant {
    explicit AutoRangeExtant(ITfRange* aRange);

    [[nodiscard]] bool isErr() const { return FAILED(mHR); }
    [[nodiscard]] bool isOk() const { return SUCCEEDED(mHR); }
    [[nodiscard]] LONG End() const { return mStart + mLength; }

    LONG mStart = 0;
    LONG mLength = 0;
    HRESULT mHR = E_NOT_SET;
  };

  /**
   * Get TextRangeType corresponding to aDisplayAttr.
   */
  [[nodiscard]] static TextRangeType GetTextRangeType(
      TF_DISPLAYATTRIBUTE& aDisplayAttr);

  /**
   * Get nscolor corresponding to aTSFColor.
   */
  [[nodiscard]] static Maybe<nscolor> GetColor(const TF_DA_COLOR& aTSFColor);

  /**
   * Get LineStyle corresponding to aTSFLineStyle.
   */
  [[nodiscard]] static Maybe<TextRangeStyle::LineStyle> GetLineStyle(
      TF_DA_LINESTYLE aTSFLineStyle);

  /**
   * Returns true if active TIP or IME is a black listed one and we should
   * set input scope of URL bar to IS_DEFAULT rather than IS_URL.
   */
  [[nodiscard]] static bool ShouldSetInputScopeOfURLBarToDefault();

  // Support retrieving attributes.
  // TODO: We should support RightToLeft, perhaps.
  enum AttrIndex {
    // Used for result of GetRequestedAttrIndex()
    NotSupported = -1,

    // Supported attributes even in TSFEmptyTextStore.
    InputScope = 0,
    DocumentURL,

    // Count of the supported attrs in empty text store
    NUM_OF_SUPPORTED_ATTRS_IN_EMPTY_TEXT_STORE,

    // Supported attributes in any TextStores.
    TextVerticalWriting = NUM_OF_SUPPORTED_ATTRS_IN_EMPTY_TEXT_STORE,
    TextOrientation,

    // Count of the supported attributes
    NUM_OF_SUPPORTED_ATTRS,
  };

  /**
   * Return AttrIndex fo aAttrID.
   */
  [[nodiscard]] static AttrIndex GetRequestedAttrIndex(
      const TS_ATTRID& aAttrID);

  /**
   * Return TS_ATTRID for aIndex.
   */
  [[nodiscard]] static TS_ATTRID GetAttrID(AttrIndex aIndex);

  /**
   * Get compartment instance.
   */
  [[nodiscard]] static Result<RefPtr<ITfCompartment>, bool> GetCompartment(
      IUnknown* pUnk, const GUID& aID);

  /**
   * Mark aContent as keyboard disabled.
   *
   * @return true if succeeded, otherwise, false.
   */
  static bool MarkContextAsKeyboardDisabled(ITfContext* aContext);

  /**
   * Mark aContext as empty.
   *
   * @return true if succeeded, otherwise, false.
   */
  static bool MarkContextAsEmpty(ITfContext* aContext);

  static const char* BoolToChar(bool aValue) {
    return aValue ? "true" : "false";
  }
  static const char* MouseButtonToChar(int16_t aButton);
  static const char* CommonHRESULTToChar(HRESULT);
  static const char* HRESULTToChar(HRESULT);

  static TS_SELECTION_ACP EmptySelectionACP() {
    return TS_SELECTION_ACP{
        .acpStart = 0,
        .acpEnd = 0,
        .style = {.ase = TS_AE_NONE, .fInterimChar = FALSE}};
  }

 private:
  static void EnsureMessagePump();
  static void EnsureKeystrokeMgr();

  // TSF thread manager object for the current application
  static StaticRefPtr<ITfThreadMgr> sThreadMgr;
  // sMessagePump is QI'ed from sThreadMgr
  static StaticRefPtr<ITfMessagePump> sMessagePump;
  // sKeystrokeMgr is QI'ed from sThreadMgr
  static StaticRefPtr<ITfKeystrokeMgr> sKeystrokeMgr;

  // TSF display attribute manager
  static StaticRefPtr<ITfDisplayAttributeMgr> sDisplayAttrMgr;
  // TSF category manager
  static StaticRefPtr<ITfCategoryMgr> sCategoryMgr;
  // Compartment for (Get|Set)IMEOpenState()
  static StaticRefPtr<ITfCompartment> sCompartmentForOpenClose;

  static StaticRefPtr<ITfInputProcessorProfiles> sInputProcessorProfiles;

  // Current active text store which is managing a keyboard enabled editor
  // (i.e., editable editor).  Currently only ONE TSFTextStore instance is ever
  // used, although Create is called when an editor is focused and Destroy
  // called when the focused editor is blurred.
  static StaticRefPtr<TSFTextStore> sActiveTextStore;

  // Current text store which may be an empty one for disabled state.
  static StaticRefPtr<TSFTextStoreBase> sCurrentTextStore;

  // Global instance for non-editable state.
  static StaticRefPtr<TSFEmptyTextStore> sEmptyTextStore;

  // TSF client ID for the current application
  static DWORD sClientId;
};

class AutoFlagsCString : public nsAutoCString {
 protected:
  void AppendFlag(const nsCString& aFlagStr) { AppendFlag(aFlagStr.get()); }
  void AppendFlag(const char* aFlagStr) {
    if (!IsEmpty()) {
      AppendLiteral(" | ");
    }
    Append(aFlagStr);
  }
};

class AutoFindFlagsCString final : public AutoFlagsCString {
 public:
  explicit AutoFindFlagsCString(DWORD);
};
class AutoACPFromPointFlagsCString final : public AutoFlagsCString {
 public:
  explicit AutoACPFromPointFlagsCString(DWORD);
};
class AutoSinkMasksCString final : public AutoFlagsCString {
 public:
  explicit AutoSinkMasksCString(DWORD);
};
class AutoLockFlagsCString final : public AutoFlagsCString {
 public:
  explicit AutoLockFlagsCString(DWORD);
};

class AutoClsidCString final : public nsAutoCString {
 public:
  explicit AutoClsidCString(REFGUID);
};
class AutoRawGuidCString : public nsAutoCString {
 public:
  explicit AutoRawGuidCString(REFGUID aGUID) { AssignRawGuid(aGUID); }

 protected:
  AutoRawGuidCString() = default;
  void AssignRawGuid(REFGUID);
};
class AutoGuidCString final : public AutoRawGuidCString {
 public:
  explicit AutoGuidCString(REFGUID);
};
class AutoRiidCString final : public nsAutoCString {
 public:
  explicit AutoRiidCString(REFIID);
};

class AutoMouseButtonsCString final : public AutoFlagsCString {
 public:
  explicit AutoMouseButtonsCString(int16_t aButtons);
};

class AutoInputScopesCString : public AutoFlagsCString {
 public:
  explicit AutoInputScopesCString(const nsTArray<InputScope>& aList);
};

class AutoEscapedUTF8String final : public NS_ConvertUTF16toUTF8 {
 public:
  explicit AutoEscapedUTF8String(const nsAString& aString)
      : NS_ConvertUTF16toUTF8(aString) {
    Escape();
  }
  explicit AutoEscapedUTF8String(const char16ptr_t aString)
      : NS_ConvertUTF16toUTF8(aString) {
    Escape();
  }
  AutoEscapedUTF8String(const char16ptr_t aString, uint32_t aLength)
      : NS_ConvertUTF16toUTF8(aString, aLength) {
    Escape();
  }

 private:
  void Escape() {
    ReplaceSubstring("\r", "\\r");
    ReplaceSubstring("\n", "\\n");
    ReplaceSubstring("\t", "\\t");
  }
};

}  // namespace mozilla::widget

inline std::ostream& operator<<(std::ostream& aStream,
                                const TS_SELECTIONSTYLE& aSelectionStyle) {
  const char* ase = "Unknown";
  switch (aSelectionStyle.ase) {
    case TS_AE_START:
      ase = "TS_AE_START";
      break;
    case TS_AE_END:
      ase = "TS_AE_END";
      break;
    case TS_AE_NONE:
      ase = "TS_AE_NONE";
      break;
  }
  return aStream << "{ ase=" << ase << ", fInterimChar="
                 << (aSelectionStyle.fInterimChar ? "TRUE" : "FALSE") << " }";
}

inline std::ostream& operator<<(std::ostream& aStream,
                                const TS_SELECTION_ACP& aACP) {
  return aStream << "{ acpStart=" << aACP.acpStart << ", acpEnd=" << aACP.acpEnd
                 << ", style=" << mozilla::ToString(aACP.style).c_str() << " }";
}

inline std::ostream& operator<<(std::ostream& aStream,
                                const TsRunType& aRunType) {
  switch (aRunType) {
    case TS_RT_PLAIN:
      return aStream << "TS_RT_PLAIN";
    case TS_RT_HIDDEN:
      return aStream << "TS_RT_HIDDEN";
    case TS_RT_OPAQUE:
      return aStream << "TS_RT_OPAQUE";
    default:
      return aStream << nsPrintfCString("Unknown(%08X)",
                                        static_cast<int32_t>(aRunType));
  }
}

inline std::ostream& operator<<(std::ostream& aStream,
                                const TF_DA_COLOR& aColor) {
  switch (aColor.type) {
    case TF_CT_NONE:
      return aStream << "TF_CT_NONE";
    case TF_CT_SYSCOLOR:
      return aStream << nsPrintfCString("TF_CT_SYSCOLOR, nIndex:0x%08X",
                                        static_cast<int32_t>(aColor.nIndex));
    case TF_CT_COLORREF:
      return aStream << nsPrintfCString("TF_CT_COLORREF, cr:0x%08X",
                                        static_cast<int32_t>(aColor.cr));
      break;
    default:
      return aStream << nsPrintfCString("Unknown(%08X)",
                                        static_cast<int32_t>(aColor.type));
  }
}

inline std::ostream& operator<<(std::ostream& aStream,
                                const TF_DA_LINESTYLE& aLineStyle) {
  switch (aLineStyle) {
    case TF_LS_NONE:
      return aStream << "TF_LS_NONE";
    case TF_LS_SOLID:
      return aStream << "TF_LS_SOLID";
    case TF_LS_DOT:
      return aStream << "TF_LS_DOT";
    case TF_LS_DASH:
      return aStream << "TF_LS_DASH";
    case TF_LS_SQUIGGLE:
      return aStream << "TF_LS_SQUIGGLE";
    default:
      return aStream << nsPrintfCString("Unknown(%08X)",
                                        static_cast<int32_t>(aLineStyle));
  }
}

inline std::ostream& operator<<(std::ostream& aStream,
                                const TF_DA_ATTR_INFO& aAttr) {
  switch (aAttr) {
    case TF_ATTR_INPUT:
      return aStream << "TF_ATTR_INPUT";
    case TF_ATTR_TARGET_CONVERTED:
      return aStream << "TF_ATTR_TARGET_CONVERTED";
    case TF_ATTR_CONVERTED:
      return aStream << "TF_ATTR_CONVERTED";
    case TF_ATTR_TARGET_NOTCONVERTED:
      return aStream << "TF_ATTR_TARGET_NOTCONVERTED";
    case TF_ATTR_INPUT_ERROR:
      return aStream << "TF_ATTR_INPUT_ERROR";
    case TF_ATTR_FIXEDCONVERTED:
      return aStream << "TF_ATTR_FIXEDCONVERTED";
    case TF_ATTR_OTHER:
      return aStream << "TF_ATTR_OTHER";
    default:
      return aStream << nsPrintfCString("Unknown(%08X)",
                                        static_cast<int32_t>(aAttr));
  }
}

inline std::ostream& operator<<(std::ostream& aStream,
                                const TF_DISPLAYATTRIBUTE& aDispAttr) {
  return aStream << "{ crText:{" << aDispAttr.crText << " }, crBk:{ "
                 << aDispAttr.crBk << " }, lsStyle: " << aDispAttr.lsStyle
                 << ", fBoldLine: "
                 << mozilla::widget::TSFUtils::BoolToChar(aDispAttr.fBoldLine)
                 << ", crLine:{ " << aDispAttr.crLine
                 << " }, bAttr: " << aDispAttr.bAttr << " }";
}

#endif  // #ifndef TSFUtils_h