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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <msctf.h>
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/singleton.h"
#include "base/message_loop.h"
#include "base/threading/thread_local_storage.h"
#include "base/win/scoped_comptr.h"
#include "base/win/scoped_variant.h"
#include "ui/base/win/tsf_bridge.h"
#include "ui/base/win/tsf_text_store.h"
namespace ui {
namespace {
// A TLS implementation of TsfBridge.
class TsfBridgeDelegate : public TsfBridge {
public:
TsfBridgeDelegate()
: source_cookie_(TF_INVALID_COOKIE),
client_id_(TF_CLIENTID_NULL),
client_(NULL) {
}
virtual ~TsfBridgeDelegate() {
}
// TsfBridge override.
bool Initialize() {
DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type());
if (client_id_ != TF_CLIENTID_NULL) {
VLOG(1) << "Already initialized.";
return false;
}
text_store_.reset(new scoped_refptr<TsfTextStore>(new TsfTextStore()));
if (FAILED(thread_manager_.CreateInstance(CLSID_TF_ThreadMgr))) {
VLOG(1) << "Failed to create ThreadManager instance.";
return false;
}
if (!InitializeForEnabledDocumentManager())
return false;
if (!InitializeForDisabledDocumentManager())
return false;
// Japanese IME expectes the default value of this compartment is
// TF_SENTENCEMODE_PHRASEPREDICT like IMM32 implementation. This value is
// managed per thread, so that it is enough to set this value at once. This
// value does not affect other language's IME behaviors.
base::win::ScopedComPtr<ITfCompartmentMgr> thread_compartment_manager;
if (FAILED(thread_compartment_manager.QueryFrom(thread_manager_))) {
VLOG(1) << "Failed to get ITfCompartmentMgr.";
return false;
}
base::win::ScopedComPtr<ITfCompartment> sentence_compartment;
if (FAILED(thread_compartment_manager->GetCompartment(
GUID_COMPARTMENT_KEYBOARD_INPUTMODE_SENTENCE,
sentence_compartment.Receive()))) {
VLOG(1) << "Failed to get sentence compartment.";
return false;
}
base::win::ScopedVariant sentence_variant;
sentence_variant.Set(TF_SENTENCEMODE_PHRASEPREDICT);
if (FAILED(sentence_compartment->SetValue(client_id_, &sentence_variant))) {
VLOG(1) << "Failed to change the sentence mode.";
return false;
}
return true;
}
// TsfBridge override.
virtual void Shutdown() OVERRIDE {
DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type());
if (!IsInitialized())
return;
base::win::ScopedComPtr<ITfContext> context;
if (FAILED(document_manager_->GetTop(context.Receive())))
return;
base::win::ScopedComPtr<ITfSource> source;
if (FAILED(source.QueryFrom(context)))
return;
if (source_cookie_ == TF_INVALID_COOKIE)
return;
if (FAILED(source->UnadviseSink(source_cookie_)))
return;
DCHECK(text_store_.get());
text_store_.reset();
client_id_ = TF_CLIENTID_NULL;
}
// TsfBridge override.
virtual bool EnableIME() OVERRIDE {
DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type());
DCHECK(IsInitialized());
if (SUCCEEDED(thread_manager_->SetFocus(document_manager_))) {
return true;
}
return false;
}
// TsfBridge override.
virtual bool DisableIME() OVERRIDE {
DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type());
DCHECK(IsInitialized());
if (SUCCEEDED(thread_manager_->SetFocus(disabled_document_manager_))) {
return true;
}
return false;
}
// TsfBridge override.
virtual bool CancelComposition() OVERRIDE {
DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type());
DCHECK(IsInitialized());
// If current focused document manager is not |document_manager_|, do
// nothing here.
base::win::ScopedComPtr<ITfDocumentMgr> focused_document_mangaer;
if (FAILED(thread_manager_->GetFocus(focused_document_mangaer.Receive())))
return false;
if (!focused_document_mangaer.IsSameObject(document_manager_))
return false;
base::win::ScopedComPtr<ITfContext> context;
if (FAILED(document_manager_->GetTop(context.Receive()))) {
VLOG(1) << "Failed to get top context.";
return false;
}
base::win::ScopedComPtr<ITfContextOwnerCompositionServices> owner;
if (FAILED(owner.QueryFrom(context))) {
VLOG(1) << "Failed to get ITfContextOwnerCompositionService.";
return false;
}
// Cancel all composition.
owner->TerminateComposition(NULL);
return true;
}
// TsfBridge override.
virtual bool AssociateFocus(HWND window) {
DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type());
DCHECK(IsInitialized());
base::win::ScopedComPtr<ITfDocumentMgr> previous_manager;
return SUCCEEDED(thread_manager_->AssociateFocus(
window,
document_manager_,
previous_manager.Receive()));
}
// TsfBridge override.
virtual void SetFocusedClient(HWND focused_window,
TextInputClient* client) OVERRIDE {
DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type());
DCHECK(client);
DCHECK(IsInitialized());
client_ = client;
text_store_->get()->SetFocusedTextInputClient(focused_window, client);
}
// TsfBridge override.
virtual void RemoveFocusedClient(TextInputClient* client) OVERRIDE {
DCHECK_EQ(MessageLoop::TYPE_UI, MessageLoop::current()->type());
DCHECK(IsInitialized());
if (client_ == client)
text_store_->get()->SetFocusedTextInputClient(NULL, NULL);
}
private:
friend struct DefaultSingletonTraits<TsfBridgeDelegate>;
// Initializes |document_manager_|.
bool InitializeForEnabledDocumentManager() {
if (FAILED(thread_manager_->Activate(&client_id_))) {
VLOG(1) << "Failed to activate Thread Manager.";
return false;
}
if (FAILED(thread_manager_->CreateDocumentMgr(
document_manager_.Receive()))) {
VLOG(1) << "Failed to create Document Manager.";
return false;
}
TfEditCookie unused_edit_cookie = TF_INVALID_EDIT_COOKIE;
base::win::ScopedComPtr<ITfContext> context;
if (FAILED(document_manager_->CreateContext(client_id_,
0,
static_cast<ITextStoreACP*>(
text_store_->get()),
context.Receive(),
&unused_edit_cookie))) {
VLOG(1) << "Failed to create Context.";
return false;
}
if (FAILED(document_manager_->Push(context))) {
VLOG(1) << "Failed to push context.";
return false;
}
if (FAILED(thread_manager_->CreateDocumentMgr(
disabled_document_manager_.Receive()))) {
VLOG(1) << "Failed to create Document Manager.";
return false;
}
base::win::ScopedComPtr<ITfSource> source;
if (FAILED(source.QueryFrom(context))) {
VLOG(1) << "Failed to get source.";
return false;
}
if (FAILED(source->AdviseSink(IID_ITfTextEditSink,
static_cast<ITfTextEditSink*>(
text_store_->get()),
&source_cookie_))) {
VLOG(1) << "AdviseSink failed.";
return false;
}
if (source_cookie_ == TF_INVALID_COOKIE) {
VLOG(1) << "The result of cookie is invalid.";
return false;
}
return true;
}
// Initializes |disabled_document_manager_|.
bool InitializeForDisabledDocumentManager() {
base::win::ScopedComPtr<ITfContext> disabled_context;
DWORD disabled_edit_cookie;
if (document_manager_->CreateContext(client_id_,
0,
NULL,
disabled_context.Receive(),
&disabled_edit_cookie)) {
VLOG(1) << "Failed to create disabled Context";
return false;
}
base::win::ScopedComPtr<ITfCompartmentMgr> compartment_mgr;
if (FAILED(compartment_mgr.QueryFrom(disabled_context))) {
VLOG(1) << "Failed to get CompartmentMgr.";
return false;
}
base::win::ScopedComPtr<ITfCompartment> disabled_compartment;
if (FAILED(compartment_mgr->GetCompartment(
GUID_COMPARTMENT_KEYBOARD_DISABLED,
disabled_compartment.Receive()))) {
VLOG(1) << "Failed to get keyboard disabled compartment.";
return false;
}
base::win::ScopedVariant variant;
variant.Set(static_cast<int32>(1));
if (FAILED(disabled_compartment->SetValue(client_id_, &variant))) {
VLOG(1) << "Failed to disable the DocumentMgr.";
return false;
}
base::win::ScopedComPtr<ITfCompartment> empty_context;
if (FAILED(compartment_mgr->GetCompartment(GUID_COMPARTMENT_EMPTYCONTEXT,
empty_context.Receive()))) {
VLOG(1) << "Failed to get empty context compartment.";
return false;
}
base::win::ScopedVariant empty_context_variant;
empty_context_variant.Set(static_cast<int32>(1));
if (FAILED(empty_context->SetValue(client_id_, &empty_context_variant))) {
VLOG(1) << "Failed to set empty context.";
return false;
}
if (FAILED(disabled_document_manager_->Push(disabled_context))) {
VLOG(1) << "Failed to push disabled context.";
return false;
}
return true;
}
// Returns true if already initialized.
bool IsInitialized() {
return client_id_ != TF_CLIENTID_NULL;
}
// An ITfContext object to be used in normal text field.
base::win::ScopedComPtr<ITfDocumentMgr> document_manager_;
// Altough TSF support IME enable/disable switching without context changing,
// some IMEs don't change their state. So we should switch a focus to a
// |disabled_document_manager_| which contains deactivated context for
// disabling IMEs.
base::win::ScopedComPtr<ITfDocumentMgr> disabled_document_manager_;
// An ITfThreadMgr object to be used in focus and document management.
base::win::ScopedComPtr<ITfThreadMgr> thread_manager_;
// A TextStore object to be used in communicating with IME.
scoped_ptr<scoped_refptr<TsfTextStore> > text_store_;
DWORD source_cookie_;
TfClientId client_id_;
// Current focused text input client. Do not free |client_|.
TextInputClient* client_;
DISALLOW_COPY_AND_ASSIGN(TsfBridgeDelegate);
};
// We use thread local storage for TsfBridge lifespan management.
base::ThreadLocalStorage::StaticSlot tls_tsf_bridge = TLS_INITIALIZER;
// Used for releasing TLS object.
void FreeTlsTsfBridgeDelegate(void* data) {
TsfBridgeDelegate* delegate = static_cast<TsfBridgeDelegate*>(data);
delete delegate;
}
} // namespace
TsfBridge::TsfBridge() {
}
TsfBridge::~TsfBridge() {
}
// static
bool TsfBridge::Initialize() {
if (MessageLoop::current()->type() != MessageLoop::TYPE_UI) {
VLOG(1) << "Do not use TsfBridge without UI thread.";
return false;
}
tls_tsf_bridge.Initialize(FreeTlsTsfBridgeDelegate);
TsfBridgeDelegate* delegate = new TsfBridgeDelegate();
tls_tsf_bridge.Set(delegate);
return delegate->Initialize();
}
// static
TsfBridge* TsfBridge::GetInstance() {
if (MessageLoop::current()->type() != MessageLoop::TYPE_UI) {
VLOG(1) << "Do not use TsfBridge without UI thread.";
return NULL;
}
TsfBridgeDelegate* delegate =
static_cast<TsfBridgeDelegate*>(tls_tsf_bridge.Get());
DCHECK(delegate) << "Do no call GetInstance before TsfBridge::Initialize.";
return delegate;
}
} // namespace ui
|