File: tsf_event_router.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (294 lines) | stat: -rw-r--r-- 9,709 bytes parent folder | download | duplicates (9)
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
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/base/ime/win/tsf_event_router.h"

#include <msctf.h>

#include <set>
#include <utility>

#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/win/atl.h"
#include "ui/base/win/atl_module.h"
#include "ui/gfx/range/range.h"

namespace ui {

// TSFEventRouter::Delegate  ------------------------------------

// The implementation class of ITfUIElementSink, whose member functions will be
// called back by TSF when the UI element status is changed, for example when
// the candidate window is opened or closed. This class also implements
// ITfTextEditSink, whose member function is called back by TSF when the text
// editting session is finished.
class ATL_NO_VTABLE TSFEventRouter::Delegate
    : public ATL::CComObjectRootEx<CComSingleThreadModel>,
      public ITfUIElementSink,
      public ITfTextEditSink {
 public:
  BEGIN_COM_MAP(Delegate)
  COM_INTERFACE_ENTRY(ITfUIElementSink)
  COM_INTERFACE_ENTRY(ITfTextEditSink)
  END_COM_MAP()

  Delegate();

  Delegate(const Delegate&) = delete;
  Delegate& operator=(const Delegate&) = delete;

  ~Delegate();

  // ITfTextEditSink:
  IFACEMETHODIMP OnEndEdit(ITfContext* context,
                           TfEditCookie read_only_cookie,
                           ITfEditRecord* edit_record) override;

  // ITfUiElementSink:
  IFACEMETHODIMP BeginUIElement(DWORD element_id, BOOL* is_show) override;
  IFACEMETHODIMP UpdateUIElement(DWORD element_id) override;
  IFACEMETHODIMP EndUIElement(DWORD element_id) override;

  // Sets |thread_manager| to be monitored. |thread_manager| can be nullptr.
  void SetManager(ITfThreadMgr* thread_manager);

  // Returns true if the IME is composing text.
  bool IsImeComposing();

  // Sets |router| to be forwarded TSF-related events.
  void SetRouter(TSFEventRouter* router);

 private:
  // Returns current composition range. Returns gfx::Range::InvalidRange if
  // there is no composition.
  static gfx::Range GetCompositionRange(ITfContext* context);

  // Returns true if the given |element_id| represents the candidate window.
  bool IsCandidateWindowInternal(DWORD element_id);

  // A context associated with this class.
  Microsoft::WRL::ComPtr<ITfContext> context_;

  // The ITfSource associated with |context_|.
  Microsoft::WRL::ComPtr<ITfSource> context_source_;

  // The cookie for |context_source_|.
  DWORD context_source_cookie_;

  // A UIElementMgr associated with this class.
  Microsoft::WRL::ComPtr<ITfUIElementMgr> ui_element_manager_;

  // The ITfSouce associated with |ui_element_manager_|.
  Microsoft::WRL::ComPtr<ITfSource> ui_source_;

  // The set of currently opened candidate window ids.
  std::set<DWORD> open_candidate_window_ids_;

  // The cookie for |ui_source_|.
  DWORD ui_source_cookie_ = TF_INVALID_COOKIE;

  raw_ptr<TSFEventRouter> router_ = nullptr;
  gfx::Range previous_composition_range_;
};

TSFEventRouter::Delegate::Delegate()
    : previous_composition_range_(gfx::Range::InvalidRange()) {}

TSFEventRouter::Delegate::~Delegate() = default;

void TSFEventRouter::Delegate::SetRouter(TSFEventRouter* router) {
  router_ = router;
}

HRESULT TSFEventRouter::Delegate::OnEndEdit(ITfContext* context,
                                            TfEditCookie read_only_cookie,
                                            ITfEditRecord* edit_record) {
  if (!edit_record || !context)
    return E_INVALIDARG;
  if (!router_)
    return S_OK;

  // |edit_record| can be used to obtain updated ranges in terms of text
  // contents and/or text attributes. Here we are interested only in text update
  // so we use TF_GTP_INCL_TEXT and check if there is any range which contains
  // updated text.
  Microsoft::WRL::ComPtr<IEnumTfRanges> ranges;
  if (FAILED(edit_record->GetTextAndPropertyUpdates(TF_GTP_INCL_TEXT, nullptr,
                                                    0, &ranges)))
    return S_OK;  // Don't care about failures.

  ULONG fetched_count = 0;
  Microsoft::WRL::ComPtr<ITfRange> range;
  if (FAILED(ranges->Next(1, &range, &fetched_count)))
    return S_OK;  // Don't care about failures.

  const gfx::Range composition_range = GetCompositionRange(context);

  if (!previous_composition_range_.IsValid() && composition_range.IsValid())
    router_->OnTSFStartComposition();

  // |fetched_count| != 0 means there is at least one range that contains
  // updated text.
  if (fetched_count != 0)
    router_->OnTextUpdated(composition_range);

  if (previous_composition_range_.IsValid() && !composition_range.IsValid())
    router_->OnTSFEndComposition();

  previous_composition_range_ = composition_range;
  return S_OK;
}

HRESULT TSFEventRouter::Delegate::BeginUIElement(DWORD element_id,
                                                 BOOL* is_show) {
  if (is_show)
    *is_show = TRUE;  // Without this the UI element will not be shown.

  if (!IsCandidateWindowInternal(element_id))
    return S_OK;

  std::pair<std::set<DWORD>::iterator, bool> insert_result =
      open_candidate_window_ids_.insert(element_id);
  // Don't call if |router_| is null or |element_id| is already handled.
  if (router_ && insert_result.second)
    router_->OnCandidateWindowCountChanged(open_candidate_window_ids_.size());

  return S_OK;
}

HRESULT TSFEventRouter::Delegate::UpdateUIElement(DWORD element_id) {
  return S_OK;
}

HRESULT TSFEventRouter::Delegate::EndUIElement(DWORD element_id) {
  if ((open_candidate_window_ids_.erase(element_id) != 0) && router_)
    router_->OnCandidateWindowCountChanged(open_candidate_window_ids_.size());
  return S_OK;
}

void TSFEventRouter::Delegate::SetManager(ITfThreadMgr* thread_manager) {
  context_.Reset();

  if (context_source_) {
    context_source_->UnadviseSink(context_source_cookie_);
    context_source_.Reset();
  }
  context_source_cookie_ = TF_INVALID_COOKIE;

  ui_element_manager_.Reset();
  if (ui_source_) {
    ui_source_->UnadviseSink(ui_source_cookie_);
    ui_source_.Reset();
  }
  ui_source_cookie_ = TF_INVALID_COOKIE;

  if (!thread_manager)
    return;

  Microsoft::WRL::ComPtr<ITfDocumentMgr> document_manager;
  if (FAILED(thread_manager->GetFocus(&document_manager)) ||
      !document_manager.Get() || FAILED(document_manager->GetBase(&context_)) ||
      FAILED(context_.As(&context_source_)))
    return;
  context_source_->AdviseSink(IID_ITfTextEditSink,
                              static_cast<ITfTextEditSink*>(this),
                              &context_source_cookie_);

  if (FAILED(
          thread_manager->QueryInterface(IID_PPV_ARGS(&ui_element_manager_))) ||
      FAILED(ui_element_manager_.As(&ui_source_)))
    return;
  ui_source_->AdviseSink(IID_ITfUIElementSink,
                         static_cast<ITfUIElementSink*>(this),
                         &ui_source_cookie_);
}

bool TSFEventRouter::Delegate::IsImeComposing() {
  return context_ && GetCompositionRange(context_.Get()).IsValid();
}

// static
gfx::Range TSFEventRouter::Delegate::GetCompositionRange(ITfContext* context) {
  DCHECK(context);
  Microsoft::WRL::ComPtr<ITfContextComposition> context_composition;
  if (FAILED(context->QueryInterface(IID_PPV_ARGS(&context_composition))))
    return gfx::Range::InvalidRange();
  Microsoft::WRL::ComPtr<IEnumITfCompositionView> enum_composition_view;
  if (FAILED(context_composition->EnumCompositions(&enum_composition_view)))
    return gfx::Range::InvalidRange();
  Microsoft::WRL::ComPtr<ITfCompositionView> composition_view;
  if (enum_composition_view->Next(1, &composition_view, nullptr) != S_OK)
    return gfx::Range::InvalidRange();

  Microsoft::WRL::ComPtr<ITfRange> range;
  if (FAILED(composition_view->GetRange(&range)))
    return gfx::Range::InvalidRange();

  Microsoft::WRL::ComPtr<ITfRangeACP> range_acp;
  if (FAILED(range.As(&range_acp)))
    return gfx::Range::InvalidRange();

  LONG start = 0;
  LONG length = 0;
  if (FAILED(range_acp->GetExtent(&start, &length)))
    return gfx::Range::InvalidRange();

  return gfx::Range(start, start + length);
}

bool TSFEventRouter::Delegate::IsCandidateWindowInternal(DWORD element_id) {
  DCHECK(ui_element_manager_.Get());
  Microsoft::WRL::ComPtr<ITfUIElement> ui_element;
  if (FAILED(ui_element_manager_->GetUIElement(element_id, &ui_element)))
    return false;
  Microsoft::WRL::ComPtr<ITfCandidateListUIElement> candidate_list_ui_element;
  return SUCCEEDED(ui_element.As(&candidate_list_ui_element));
}

// TSFEventRouter  ------------------------------------------------------------

TSFEventRouter::TSFEventRouter(TSFEventRouterObserver* observer)
    : observer_(observer) {
  DCHECK(observer_);
  CComObject<Delegate>* delegate;
  ui::win::CreateATLModuleIfNeeded();
  if (SUCCEEDED(CComObject<Delegate>::CreateInstance(&delegate))) {
    delegate_ = delegate;
    delegate_->SetRouter(this);
  }
}

TSFEventRouter::~TSFEventRouter() {
  if (delegate_) {
    delegate_->SetManager(nullptr);
    delegate_->SetRouter(nullptr);
  }
}

bool TSFEventRouter::IsImeComposing() {
  return delegate_->IsImeComposing();
}

void TSFEventRouter::OnCandidateWindowCountChanged(size_t window_count) {
  observer_->OnCandidateWindowCountChanged(window_count);
}

void TSFEventRouter::OnTSFStartComposition() {
  observer_->OnTSFStartComposition();
}

void TSFEventRouter::OnTextUpdated(const gfx::Range& composition_range) {
  observer_->OnTextUpdated(composition_range);
}

void TSFEventRouter::OnTSFEndComposition() {
  observer_->OnTSFEndComposition();
}

void TSFEventRouter::SetManager(ITfThreadMgr* thread_manager) {
  delegate_->SetManager(thread_manager);
}

}  // namespace ui