OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include <msctf.h> | 5 #include <msctf.h> |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 DISALLOW_COPY_AND_ASSIGN(TSFBridgeDelegate); | 134 DISALLOW_COPY_AND_ASSIGN(TSFBridgeDelegate); |
135 }; | 135 }; |
136 | 136 |
137 TSFBridgeDelegate::TSFBridgeDelegate() | 137 TSFBridgeDelegate::TSFBridgeDelegate() |
138 : client_id_(TF_CLIENTID_NULL), | 138 : client_id_(TF_CLIENTID_NULL), |
139 client_(NULL), | 139 client_(NULL), |
140 attached_window_handle_(NULL) { | 140 attached_window_handle_(NULL) { |
141 } | 141 } |
142 | 142 |
143 TSFBridgeDelegate::~TSFBridgeDelegate() { | 143 TSFBridgeDelegate::~TSFBridgeDelegate() { |
144 DCHECK_EQ(base::MessageLoop::TYPE_UI, base::MessageLoop::current()->type()); | 144 DCHECK(base::MessageLoopForUI::IsCurrent()); |
145 if (!IsInitialized()) | 145 if (!IsInitialized()) |
146 return; | 146 return; |
147 for (TSFDocumentMap::iterator it = tsf_document_map_.begin(); | 147 for (TSFDocumentMap::iterator it = tsf_document_map_.begin(); |
148 it != tsf_document_map_.end(); ++it) { | 148 it != tsf_document_map_.end(); ++it) { |
149 base::win::ScopedComPtr<ITfContext> context; | 149 base::win::ScopedComPtr<ITfContext> context; |
150 base::win::ScopedComPtr<ITfSource> source; | 150 base::win::ScopedComPtr<ITfSource> source; |
151 if (it->second.cookie != TF_INVALID_COOKIE && | 151 if (it->second.cookie != TF_INVALID_COOKIE && |
152 SUCCEEDED(it->second.document_manager->GetBase(context.Receive())) && | 152 SUCCEEDED(it->second.document_manager->GetBase(context.Receive())) && |
153 SUCCEEDED(source.QueryFrom(context))) { | 153 SUCCEEDED(source.QueryFrom(context))) { |
154 source->UnadviseSink(it->second.cookie); | 154 source->UnadviseSink(it->second.cookie); |
155 } | 155 } |
156 } | 156 } |
157 tsf_document_map_.clear(); | 157 tsf_document_map_.clear(); |
158 | 158 |
159 client_id_ = TF_CLIENTID_NULL; | 159 client_id_ = TF_CLIENTID_NULL; |
160 } | 160 } |
161 | 161 |
162 bool TSFBridgeDelegate::Initialize() { | 162 bool TSFBridgeDelegate::Initialize() { |
163 DCHECK_EQ(base::MessageLoop::TYPE_UI, base::MessageLoop::current()->type()); | 163 DCHECK(base::MessageLoopForUI::IsCurrent()); |
164 if (client_id_ != TF_CLIENTID_NULL) { | 164 if (client_id_ != TF_CLIENTID_NULL) { |
165 DVLOG(1) << "Already initialized."; | 165 DVLOG(1) << "Already initialized."; |
166 return false; | 166 return false; |
167 } | 167 } |
168 | 168 |
169 if (FAILED(thread_manager_.CreateInstance(CLSID_TF_ThreadMgr))) { | 169 if (FAILED(thread_manager_.CreateInstance(CLSID_TF_ThreadMgr))) { |
170 DVLOG(1) << "Failed to create ThreadManager instance."; | 170 DVLOG(1) << "Failed to create ThreadManager instance."; |
171 return false; | 171 return false; |
172 } | 172 } |
173 | 173 |
(...skipping 27 matching lines...) Expand all Loading... |
201 sentence_variant.Set(TF_SENTENCEMODE_PHRASEPREDICT); | 201 sentence_variant.Set(TF_SENTENCEMODE_PHRASEPREDICT); |
202 if (FAILED(sentence_compartment->SetValue(client_id_, &sentence_variant))) { | 202 if (FAILED(sentence_compartment->SetValue(client_id_, &sentence_variant))) { |
203 DVLOG(1) << "Failed to change the sentence mode."; | 203 DVLOG(1) << "Failed to change the sentence mode."; |
204 return false; | 204 return false; |
205 } | 205 } |
206 | 206 |
207 return true; | 207 return true; |
208 } | 208 } |
209 | 209 |
210 void TSFBridgeDelegate::OnTextInputTypeChanged(const TextInputClient* client) { | 210 void TSFBridgeDelegate::OnTextInputTypeChanged(const TextInputClient* client) { |
211 DCHECK_EQ(base::MessageLoop::TYPE_UI, base::MessageLoop::current()->type()); | 211 DCHECK(base::MessageLoopForUI::IsCurrent()); |
212 DCHECK(IsInitialized()); | 212 DCHECK(IsInitialized()); |
213 | 213 |
214 if (client != client_) { | 214 if (client != client_) { |
215 // Called from not focusing client. Do nothing. | 215 // Called from not focusing client. Do nothing. |
216 return; | 216 return; |
217 } | 217 } |
218 | 218 |
219 UpdateAssociateFocus(); | 219 UpdateAssociateFocus(); |
220 | 220 |
221 TSFDocument* document = GetAssociatedDocument(); | 221 TSFDocument* document = GetAssociatedDocument(); |
222 if (!document) | 222 if (!document) |
223 return; | 223 return; |
224 thread_manager_->SetFocus(document->document_manager.get()); | 224 thread_manager_->SetFocus(document->document_manager.get()); |
225 OnTextLayoutChanged(); | 225 OnTextLayoutChanged(); |
226 } | 226 } |
227 | 227 |
228 void TSFBridgeDelegate::OnTextLayoutChanged() { | 228 void TSFBridgeDelegate::OnTextLayoutChanged() { |
229 TSFDocument* document = GetAssociatedDocument(); | 229 TSFDocument* document = GetAssociatedDocument(); |
230 if (!document) | 230 if (!document) |
231 return; | 231 return; |
232 if (!document->text_store) | 232 if (!document->text_store) |
233 return; | 233 return; |
234 document->text_store->SendOnLayoutChange(); | 234 document->text_store->SendOnLayoutChange(); |
235 } | 235 } |
236 | 236 |
237 bool TSFBridgeDelegate::CancelComposition() { | 237 bool TSFBridgeDelegate::CancelComposition() { |
238 DCHECK_EQ(base::MessageLoop::TYPE_UI, base::MessageLoop::current()->type()); | 238 DCHECK(base::MessageLoopForUI::IsCurrent()); |
239 DCHECK(IsInitialized()); | 239 DCHECK(IsInitialized()); |
240 | 240 |
241 TSFDocument* document = GetAssociatedDocument(); | 241 TSFDocument* document = GetAssociatedDocument(); |
242 if (!document) | 242 if (!document) |
243 return false; | 243 return false; |
244 if (!document->text_store) | 244 if (!document->text_store) |
245 return false; | 245 return false; |
246 | 246 |
247 return document->text_store->CancelComposition(); | 247 return document->text_store->CancelComposition(); |
248 } | 248 } |
249 | 249 |
250 bool TSFBridgeDelegate::ConfirmComposition() { | 250 bool TSFBridgeDelegate::ConfirmComposition() { |
251 DCHECK_EQ(base::MessageLoop::TYPE_UI, base::MessageLoop::current()->type()); | 251 DCHECK(base::MessageLoopForUI::IsCurrent()); |
252 DCHECK(IsInitialized()); | 252 DCHECK(IsInitialized()); |
253 | 253 |
254 TSFDocument* document = GetAssociatedDocument(); | 254 TSFDocument* document = GetAssociatedDocument(); |
255 if (!document) | 255 if (!document) |
256 return false; | 256 return false; |
257 if (!document->text_store) | 257 if (!document->text_store) |
258 return false; | 258 return false; |
259 | 259 |
260 return document->text_store->ConfirmComposition(); | 260 return document->text_store->ConfirmComposition(); |
261 } | 261 } |
262 | 262 |
263 void TSFBridgeDelegate::SetFocusedClient(HWND focused_window, | 263 void TSFBridgeDelegate::SetFocusedClient(HWND focused_window, |
264 TextInputClient* client) { | 264 TextInputClient* client) { |
265 DCHECK_EQ(base::MessageLoop::TYPE_UI, base::MessageLoop::current()->type()); | 265 DCHECK(base::MessageLoopForUI::IsCurrent()); |
266 DCHECK(client); | 266 DCHECK(client); |
267 DCHECK(IsInitialized()); | 267 DCHECK(IsInitialized()); |
268 if (attached_window_handle_ != focused_window) | 268 if (attached_window_handle_ != focused_window) |
269 ClearAssociateFocus(); | 269 ClearAssociateFocus(); |
270 client_ = client; | 270 client_ = client; |
271 attached_window_handle_ = focused_window; | 271 attached_window_handle_ = focused_window; |
272 | 272 |
273 for (TSFDocumentMap::iterator it = tsf_document_map_.begin(); | 273 for (TSFDocumentMap::iterator it = tsf_document_map_.begin(); |
274 it != tsf_document_map_.end(); ++it) { | 274 it != tsf_document_map_.end(); ++it) { |
275 if (it->second.text_store.get() == NULL) | 275 if (it->second.text_store.get() == NULL) |
276 continue; | 276 continue; |
277 it->second.text_store->SetFocusedTextInputClient(focused_window, | 277 it->second.text_store->SetFocusedTextInputClient(focused_window, |
278 client); | 278 client); |
279 } | 279 } |
280 | 280 |
281 // Synchronize text input type state. | 281 // Synchronize text input type state. |
282 OnTextInputTypeChanged(client); | 282 OnTextInputTypeChanged(client); |
283 } | 283 } |
284 | 284 |
285 void TSFBridgeDelegate::RemoveFocusedClient(TextInputClient* client) { | 285 void TSFBridgeDelegate::RemoveFocusedClient(TextInputClient* client) { |
286 DCHECK_EQ(base::MessageLoop::TYPE_UI, base::MessageLoop::current()->type()); | 286 DCHECK(base::MessageLoopForUI::IsCurrent()); |
287 DCHECK(IsInitialized()); | 287 DCHECK(IsInitialized()); |
288 if (client_ != client) | 288 if (client_ != client) |
289 return; | 289 return; |
290 ClearAssociateFocus(); | 290 ClearAssociateFocus(); |
291 client_ = NULL; | 291 client_ = NULL; |
292 attached_window_handle_ = NULL; | 292 attached_window_handle_ = NULL; |
293 for (TSFDocumentMap::iterator it = tsf_document_map_.begin(); | 293 for (TSFDocumentMap::iterator it = tsf_document_map_.begin(); |
294 it != tsf_document_map_.end(); ++it) { | 294 it != tsf_document_map_.end(); ++it) { |
295 if (it->second.text_store.get() == NULL) | 295 if (it->second.text_store.get() == NULL) |
296 continue; | 296 continue; |
297 it->second.text_store->SetFocusedTextInputClient(NULL, NULL); | 297 it->second.text_store->SetFocusedTextInputClient(NULL, NULL); |
298 } | 298 } |
299 } | 299 } |
300 | 300 |
301 TextInputClient* TSFBridgeDelegate::GetFocusedTextInputClient() const { | 301 TextInputClient* TSFBridgeDelegate::GetFocusedTextInputClient() const { |
302 return client_; | 302 return client_; |
303 } | 303 } |
304 | 304 |
305 base::win::ScopedComPtr<ITfThreadMgr> TSFBridgeDelegate::GetThreadManager() { | 305 base::win::ScopedComPtr<ITfThreadMgr> TSFBridgeDelegate::GetThreadManager() { |
306 DCHECK_EQ(base::MessageLoop::TYPE_UI, base::MessageLoop::current()->type()); | 306 DCHECK(base::MessageLoopForUI::IsCurrent()); |
307 DCHECK(IsInitialized()); | 307 DCHECK(IsInitialized()); |
308 return thread_manager_; | 308 return thread_manager_; |
309 } | 309 } |
310 | 310 |
311 bool TSFBridgeDelegate::CreateDocumentManager(TSFTextStore* text_store, | 311 bool TSFBridgeDelegate::CreateDocumentManager(TSFTextStore* text_store, |
312 ITfDocumentMgr** document_manager, | 312 ITfDocumentMgr** document_manager, |
313 ITfContext** context, | 313 ITfContext** context, |
314 DWORD* source_cookie) { | 314 DWORD* source_cookie) { |
315 if (FAILED(thread_manager_->CreateDocumentMgr(document_manager))) { | 315 if (FAILED(thread_manager_->CreateDocumentMgr(document_manager))) { |
316 DVLOG(1) << "Failed to create Document Manager."; | 316 DVLOG(1) << "Failed to create Document Manager."; |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
490 // TsfBridge ----------------------------------------------------------------- | 490 // TsfBridge ----------------------------------------------------------------- |
491 | 491 |
492 TSFBridge::TSFBridge() { | 492 TSFBridge::TSFBridge() { |
493 } | 493 } |
494 | 494 |
495 TSFBridge::~TSFBridge() { | 495 TSFBridge::~TSFBridge() { |
496 } | 496 } |
497 | 497 |
498 // static | 498 // static |
499 bool TSFBridge::Initialize() { | 499 bool TSFBridge::Initialize() { |
500 if (base::MessageLoop::current()->type() != base::MessageLoop::TYPE_UI) { | 500 if (!base::MessageLoopForUI::IsCurrent()) { |
501 DVLOG(1) << "Do not use TSFBridge without UI thread."; | 501 DVLOG(1) << "Do not use TSFBridge without UI thread."; |
502 return false; | 502 return false; |
503 } | 503 } |
504 if (!tls_tsf_bridge.initialized()) { | 504 if (!tls_tsf_bridge.initialized()) { |
505 tls_tsf_bridge.Initialize(TSFBridge::Finalize); | 505 tls_tsf_bridge.Initialize(TSFBridge::Finalize); |
506 } | 506 } |
507 TSFBridgeDelegate* delegate = | 507 TSFBridgeDelegate* delegate = |
508 static_cast<TSFBridgeDelegate*>(tls_tsf_bridge.Get()); | 508 static_cast<TSFBridgeDelegate*>(tls_tsf_bridge.Get()); |
509 if (delegate) | 509 if (delegate) |
510 return true; | 510 return true; |
511 delegate = new TSFBridgeDelegate(); | 511 delegate = new TSFBridgeDelegate(); |
512 tls_tsf_bridge.Set(delegate); | 512 tls_tsf_bridge.Set(delegate); |
513 return delegate->Initialize(); | 513 return delegate->Initialize(); |
514 } | 514 } |
515 | 515 |
516 // static | 516 // static |
517 TSFBridge* TSFBridge::ReplaceForTesting(TSFBridge* bridge) { | 517 TSFBridge* TSFBridge::ReplaceForTesting(TSFBridge* bridge) { |
518 if (base::MessageLoop::current()->type() != base::MessageLoop::TYPE_UI) { | 518 if (!base::MessageLoopForUI::IsCurrent()) { |
519 DVLOG(1) << "Do not use TSFBridge without UI thread."; | 519 DVLOG(1) << "Do not use TSFBridge without UI thread."; |
520 return NULL; | 520 return NULL; |
521 } | 521 } |
522 TSFBridge* old_bridge = TSFBridge::GetInstance(); | 522 TSFBridge* old_bridge = TSFBridge::GetInstance(); |
523 tls_tsf_bridge.Set(bridge); | 523 tls_tsf_bridge.Set(bridge); |
524 return old_bridge; | 524 return old_bridge; |
525 } | 525 } |
526 | 526 |
527 // static | 527 // static |
528 void TSFBridge::Shutdown() { | 528 void TSFBridge::Shutdown() { |
529 if (base::MessageLoop::current()->type() != base::MessageLoop::TYPE_UI) { | 529 if (!base::MessageLoopForUI::IsCurrent()) { |
530 DVLOG(1) << "Do not use TSFBridge without UI thread."; | 530 DVLOG(1) << "Do not use TSFBridge without UI thread."; |
531 } | 531 } |
532 if (tls_tsf_bridge.initialized()) { | 532 if (tls_tsf_bridge.initialized()) { |
533 TSFBridgeDelegate* delegate = | 533 TSFBridgeDelegate* delegate = |
534 static_cast<TSFBridgeDelegate*>(tls_tsf_bridge.Get()); | 534 static_cast<TSFBridgeDelegate*>(tls_tsf_bridge.Get()); |
535 tls_tsf_bridge.Set(NULL); | 535 tls_tsf_bridge.Set(NULL); |
536 delete delegate; | 536 delete delegate; |
537 } | 537 } |
538 } | 538 } |
539 | 539 |
540 // static | 540 // static |
541 TSFBridge* TSFBridge::GetInstance() { | 541 TSFBridge* TSFBridge::GetInstance() { |
542 if (base::MessageLoop::current()->type() != base::MessageLoop::TYPE_UI) { | 542 if (!base::MessageLoopForUI::IsCurrent()) { |
543 DVLOG(1) << "Do not use TSFBridge without UI thread."; | 543 DVLOG(1) << "Do not use TSFBridge without UI thread."; |
544 return NULL; | 544 return NULL; |
545 } | 545 } |
546 TSFBridgeDelegate* delegate = | 546 TSFBridgeDelegate* delegate = |
547 static_cast<TSFBridgeDelegate*>(tls_tsf_bridge.Get()); | 547 static_cast<TSFBridgeDelegate*>(tls_tsf_bridge.Get()); |
548 DCHECK(delegate) << "Do no call GetInstance before TSFBridge::Initialize."; | 548 DCHECK(delegate) << "Do no call GetInstance before TSFBridge::Initialize."; |
549 return delegate; | 549 return delegate; |
550 } | 550 } |
551 | 551 |
552 // static | 552 // static |
553 void TSFBridge::Finalize(void* data) { | 553 void TSFBridge::Finalize(void* data) { |
554 TSFBridgeDelegate* delegate = static_cast<TSFBridgeDelegate*>(data); | 554 TSFBridgeDelegate* delegate = static_cast<TSFBridgeDelegate*>(data); |
555 delete delegate; | 555 delete delegate; |
556 } | 556 } |
557 | 557 |
558 } // namespace ui | 558 } // namespace ui |
OLD | NEW |