summaryrefslogtreecommitdiffstats
path: root/win8/metro_driver/ime/text_service.cc
blob: 1ad071b3e30a0c1351a8d99cdeef61b9da27aa1f (plain)
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
485
486
487
488
489
490
// Copyright 2013 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 "win8/metro_driver/ime/text_service.h"

#include <msctf.h>
#include <stddef.h>
#include <stdint.h>

#include "base/logging.h"
#include "base/macros.h"
#include "base/win/scoped_variant.h"
#include "ui/metro_viewer/ime_types.h"
#include "win8/metro_driver/ime/text_service_delegate.h"
#include "win8/metro_driver/ime/text_store.h"
#include "win8/metro_driver/ime/text_store_delegate.h"

// Architecture overview of input method support on Ash mode:
//
// Overview:
// On Ash mode, the system keyboard focus is owned by the metro_driver process
// while most of event handling are still implemented in the browser process.
// Thus the metro_driver basically works as a proxy that simply forwards
// keyevents to the metro_driver process. IME support must be involved somewhere
// in this flow.
//
// In short, we need to interact with an IME in the metro_driver process since
// TSF (Text Services Framework) runtime wants to processes keyevents while
// (and only while) the attached UI thread owns keyboard focus.
//
// Due to this limitation, we need to split IME handling into two parts, one
// is in the metro_driver process and the other is in the browser process.
// The metro_driver process is responsible for implementing the primary data
// store for the composition text and wiring it up with an IME via TSF APIs.
// On the other hand, the browser process is responsible for calculating
// character position in the composition text whenever the composition text
// is updated.
//
// IPC overview:
// Fortunately, we don't need so many IPC messages to support IMEs. In fact,
// only 4 messages are required to enable basic IME functionality.
//
//   metro_driver process -> browser process
//     Message Type:
//       - MetroViewerHostMsg_ImeCompositionChanged
//       - MetroViewerHostMsg_ImeTextCommitted
//     Message Routing:
//       TextServiceImpl
//         -> ChromeAppViewAsh
//         -- (process boundary) --
//         -> RemoteWindowTreeHostWin
//         -> RemoteInputMethodWin
//
//   browser process -> metro_driver process
//     Message Type:
//       - MetroViewerHostMsg_ImeCancelComposition
//       - MetroViewerHostMsg_ImeTextInputClientUpdated
//     Message Routing:
//       RemoteInputMethodWin
//         -> RemoteWindowTreeHostWin
//         -- (process boundary) --
//         -> ChromeAppViewAsh
//         -> TextServiceImpl
//
// Note that a keyevent may be forwarded through a different path. When a
// keyevent is not handled by an IME, such keyevent and subsequent character
// events will be sent from the metro_driver process to the browser process as
// following IPC messages.
//  - MetroViewerHostMsg_KeyDown
//  - MetroViewerHostMsg_KeyUp
//  - MetroViewerHostMsg_Character
//
// How TextServiceImpl works:
// Here is the list of the major tasks that are handled in TextServiceImpl.
//  - Manages a session object obtained from TSF runtime. We need them to call
//    most of TSF APIs.
//  - Handles OnDocumentChanged event. Whenever the document type is changed,
//    TextServiceImpl destroyes the current document and initializes new one
//    according to the given |input_scopes|.
//  - Stores the |composition_character_bounds_| passed from OnDocumentChanged
//    event so that an IME or on-screen keyboard can query the character
//    position synchronously.
// The most complicated part is the OnDocumentChanged handler. Since some IMEs
// such as Japanese IMEs drastically change their behavior depending on
// properties exposed from the virtual document, we need to set up a lot
// properties carefully and correctly. See DocumentBinding class in this file
// about what will be involved in this multi-phase construction. See also
// text_store.cc and input_scope.cc for more underlying details.

namespace metro_driver {
namespace {

// Japanese IME expects the default value of this compartment is
// TF_SENTENCEMODE_PHRASEPREDICT to emulate IMM32 behavior. This value is
// managed per thread, thus setting this value at once is sufficient. This
// value never affects non-Japanese IMEs.
bool InitializeSentenceMode(ITfThreadMgr* thread_manager,
                            TfClientId client_id) {
  base::win::ScopedComPtr<ITfCompartmentMgr> thread_compartment_manager;
  HRESULT hr = thread_compartment_manager.QueryFrom(thread_manager);
  if (FAILED(hr)) {
    LOG(ERROR) << "QueryFrom failed. hr = " << hr;
    return false;
  }
  base::win::ScopedComPtr<ITfCompartment> sentence_compartment;
  hr = thread_compartment_manager->GetCompartment(
      GUID_COMPARTMENT_KEYBOARD_INPUTMODE_SENTENCE,
      sentence_compartment.Receive());
  if (FAILED(hr)) {
    LOG(ERROR) << "ITfCompartment::GetCompartment failed. hr = " << hr;
    return false;
  }

  base::win::ScopedVariant sentence_variant;
  sentence_variant.Set(TF_SENTENCEMODE_PHRASEPREDICT);
  hr = sentence_compartment->SetValue(client_id, sentence_variant.ptr());
  if (FAILED(hr)) {
    LOG(ERROR) << "ITfCompartment::SetValue failed. hr = " << hr;
    return false;
  }
  return true;
}

// Initializes |context| as disabled context where IMEs will be disabled.
bool InitializeDisabledContext(ITfContext* context, TfClientId client_id) {
  base::win::ScopedComPtr<ITfCompartmentMgr> compartment_mgr;
  HRESULT hr = compartment_mgr.QueryFrom(context);
  if (FAILED(hr)) {
    LOG(ERROR) << "QueryFrom failed. hr = " << hr;
    return false;
  }

  base::win::ScopedComPtr<ITfCompartment> disabled_compartment;
  hr = compartment_mgr->GetCompartment(GUID_COMPARTMENT_KEYBOARD_DISABLED,
                                       disabled_compartment.Receive());
  if (FAILED(hr)) {
    LOG(ERROR) << "ITfCompartment::GetCompartment failed. hr = " << hr;
    return false;
  }

  base::win::ScopedVariant variant;
  variant.Set(1);
  hr = disabled_compartment->SetValue(client_id, variant.ptr());
  if (FAILED(hr)) {
    LOG(ERROR) << "ITfCompartment::SetValue failed. hr = " << hr;
    return false;
  }

  base::win::ScopedComPtr<ITfCompartment> empty_context;
  hr = compartment_mgr->GetCompartment(GUID_COMPARTMENT_EMPTYCONTEXT,
                                       empty_context.Receive());
  if (FAILED(hr)) {
    LOG(ERROR) << "ITfCompartment::GetCompartment failed. hr = " << hr;
    return false;
  }

  base::win::ScopedVariant empty_context_variant;
  empty_context_variant.Set(static_cast<int32_t>(1));
  hr = empty_context->SetValue(client_id, empty_context_variant.ptr());
  if (FAILED(hr)) {
    LOG(ERROR) << "ITfCompartment::SetValue failed. hr = " << hr;
    return false;
  }

  return true;
}

bool IsPasswordField(const std::vector<InputScope>& input_scopes) {
  return std::find(input_scopes.begin(), input_scopes.end(), IS_PASSWORD) !=
      input_scopes.end();
}

// A class that manages the lifetime of the event callback registration. When
// this object is destroyed, corresponding event callback will be unregistered.
class EventSink {
 public:
  EventSink(DWORD cookie, base::win::ScopedComPtr<ITfSource> source)
      : cookie_(cookie),
        source_(source) {}
  ~EventSink() {
    if (!source_.get() || cookie_ != TF_INVALID_COOKIE)
      return;
    source_->UnadviseSink(cookie_);
    cookie_ = TF_INVALID_COOKIE;
    source_.Release();
  }

 private:
  DWORD cookie_;
  base::win::ScopedComPtr<ITfSource> source_;
  DISALLOW_COPY_AND_ASSIGN(EventSink);
};

scoped_ptr<EventSink> CreateTextEditSink(ITfContext* context,
                                         ITfTextEditSink* text_store) {
  DCHECK(text_store);
  base::win::ScopedComPtr<ITfSource> source;
  DWORD cookie = TF_INVALID_EDIT_COOKIE;
  HRESULT hr = source.QueryFrom(context);
  if (FAILED(hr)) {
    LOG(ERROR) << "QueryFrom failed, hr = " << hr;
    return scoped_ptr<EventSink>();
  }
  hr = source->AdviseSink(IID_ITfTextEditSink, text_store, &cookie);
  if (FAILED(hr)) {
    LOG(ERROR) << "AdviseSink failed, hr = " << hr;
    return scoped_ptr<EventSink>();
  }
  return scoped_ptr<EventSink>(new EventSink(cookie, source));
}

// A set of objects that should have the same lifetime. Following things
// are maintained.
//  - TextStore: a COM object that abstracts text buffer. This object is
//      actually implemented by us in text_store.cc
//  - ITfDocumentMgr: a focusable unit in TSF. This object is implemented by
//      TSF runtime and works as a container of TextStore.
//  - EventSink: an object that ensures that the event callback between
//      TSF runtime and TextStore is unregistered when this object is destroyed.
class DocumentBinding {
 public:
  ~DocumentBinding() {
    if (!document_manager_.get())
      return;
    document_manager_->Pop(TF_POPF_ALL);
  }

  static scoped_ptr<DocumentBinding> Create(
      ITfThreadMgr* thread_manager,
      TfClientId client_id,
      const std::vector<InputScope>& input_scopes,
      HWND window_handle,
      TextStoreDelegate* delegate) {
    base::win::ScopedComPtr<ITfDocumentMgr> document_manager;
    HRESULT hr = thread_manager->CreateDocumentMgr(document_manager.Receive());
    if (FAILED(hr)) {
      LOG(ERROR) << "ITfThreadMgr::CreateDocumentMgr failed. hr = " << hr;
      return scoped_ptr<DocumentBinding>();
    }

    // Note: In our IPC protocol, an empty |input_scopes| is used to indicate
    // that an IME must be disabled in this context. In such case, we need not
    // instantiate TextStore.
    const bool use_null_text_store = input_scopes.empty();

    scoped_refptr<TextStore> text_store;
    if (!use_null_text_store) {
      text_store = TextStore::Create(window_handle, input_scopes, delegate);
      if (!text_store.get()) {
        LOG(ERROR) << "Failed to create TextStore.";
        return scoped_ptr<DocumentBinding>();
      }
    }

    base::win::ScopedComPtr<ITfContext> context;
    DWORD edit_cookie = TF_INVALID_EDIT_COOKIE;
    hr = document_manager->CreateContext(
        client_id,
        0,
        static_cast<ITextStoreACP*>(text_store.get()),
        context.Receive(),
        &edit_cookie);
    if (FAILED(hr)) {
      LOG(ERROR) << "ITfDocumentMgr::CreateContext failed. hr = " << hr;
      return scoped_ptr<DocumentBinding>();
    }

    // If null-TextStore is used or |input_scopes| looks like a password field,
    // set special properties to tell IMEs to be disabled.
    if ((use_null_text_store || IsPasswordField(input_scopes)) &&
        !InitializeDisabledContext(context.get(), client_id)) {
      LOG(ERROR) << "InitializeDisabledContext failed.";
      return scoped_ptr<DocumentBinding>();
    }

    scoped_ptr<EventSink> text_edit_sink;
    if (!use_null_text_store) {
      text_edit_sink = CreateTextEditSink(context.get(), text_store.get());
      if (!text_edit_sink) {
        LOG(ERROR) << "CreateTextEditSink failed.";
        return scoped_ptr<DocumentBinding>();
      }
    }
    hr = document_manager->Push(context.get());
    if (FAILED(hr)) {
      LOG(ERROR) << "ITfDocumentMgr::Push failed. hr = " << hr;
      return scoped_ptr<DocumentBinding>();
    }
    return scoped_ptr<DocumentBinding>(
        new DocumentBinding(text_store,
                            document_manager,
                            text_edit_sink.Pass()));
  }

  ITfDocumentMgr* document_manager() const { return document_manager_.get(); }

  scoped_refptr<TextStore> text_store() const {
    return text_store_;
  }

 private:
  DocumentBinding(scoped_refptr<TextStore> text_store,
                  base::win::ScopedComPtr<ITfDocumentMgr> document_manager,
                  scoped_ptr<EventSink> text_edit_sink)
      : text_store_(text_store),
        document_manager_(document_manager),
        text_edit_sink_(text_edit_sink.Pass()) {}

  scoped_refptr<TextStore> text_store_;
  base::win::ScopedComPtr<ITfDocumentMgr> document_manager_;
  scoped_ptr<EventSink> text_edit_sink_;

  DISALLOW_COPY_AND_ASSIGN(DocumentBinding);
};

class TextServiceImpl : public TextService,
                        public TextStoreDelegate {
 public:
  TextServiceImpl(ITfThreadMgr* thread_manager,
                  TfClientId client_id,
                  HWND window_handle,
                  TextServiceDelegate* delegate)
      : client_id_(client_id),
        window_handle_(window_handle),
        delegate_(delegate),
        thread_manager_(thread_manager) {
    DCHECK_NE(TF_CLIENTID_NULL, client_id);
    DCHECK(window_handle != NULL);
    DCHECK(thread_manager_.get());
  }
  ~TextServiceImpl() override {
    thread_manager_->Deactivate();
  }

 private:
  // TextService overrides:
  void CancelComposition() override {
    if (!current_document_) {
      VLOG(0) << "|current_document_| is NULL due to the previous error.";
      return;
    }
    scoped_refptr<TextStore> text_store = current_document_->text_store();
    if (!text_store.get())
      return;
    text_store->CancelComposition();
  }

  void OnDocumentChanged(const std::vector<int32_t>& input_scopes,
                         const std::vector<metro_viewer::CharacterBounds>&
                             character_bounds) override {
    bool document_type_changed = input_scopes_ != input_scopes;
    input_scopes_ = input_scopes;
    composition_character_bounds_ = character_bounds;
    if (document_type_changed)
      OnDocumentTypeChanged(input_scopes);
  }

  void OnWindowActivated() override {
    if (!current_document_) {
      VLOG(0) << "|current_document_| is NULL due to the previous error.";
      return;
    }
    ITfDocumentMgr* document_manager = current_document_->document_manager();
    if (!document_manager) {
      VLOG(0) << "|document_manager| is NULL due to the previous error.";
      return;
    }
    HRESULT hr = thread_manager_->SetFocus(document_manager);
    if (FAILED(hr)) {
      LOG(ERROR) << "ITfThreadMgr::SetFocus failed. hr = " << hr;
      return;
    }
  }

  void OnCompositionChanged(
      const base::string16& text,
      int32_t selection_start,
      int32_t selection_end,
      const std::vector<metro_viewer::UnderlineInfo>& underlines) override {
    if (!delegate_)
      return;
    delegate_->OnCompositionChanged(text,
                                    selection_start,
                                    selection_end,
                                    underlines);
  }

  void OnTextCommitted(const base::string16& text) override {
    if (!delegate_)
      return;
    delegate_->OnTextCommitted(text);
  }

  RECT GetCaretBounds() override {
    if (composition_character_bounds_.empty()) {
      const RECT rect = {};
      return rect;
    }
    const metro_viewer::CharacterBounds& bounds =
        composition_character_bounds_[0];
    POINT left_top = { bounds.left, bounds.top };
    POINT right_bottom = { bounds.right, bounds.bottom };
    ClientToScreen(window_handle_, &left_top);
    ClientToScreen(window_handle_, &right_bottom);
    const RECT rect = {
      left_top.x,
      left_top.y,
      right_bottom.x,
      right_bottom.y,
    };
    return rect;
  }

  bool GetCompositionCharacterBounds(uint32_t index, RECT* rect) override {
    if (index >= composition_character_bounds_.size()) {
      return false;
    }
    const metro_viewer::CharacterBounds& bounds =
        composition_character_bounds_[index];
    POINT left_top = { bounds.left, bounds.top };
    POINT right_bottom = { bounds.right, bounds.bottom };
    ClientToScreen(window_handle_, &left_top);
    ClientToScreen(window_handle_, &right_bottom);
    SetRect(rect, left_top.x, left_top.y, right_bottom.x, right_bottom.y);
    return true;
  }

  void OnDocumentTypeChanged(const std::vector<int32_t>& input_scopes) {
    std::vector<InputScope> native_input_scopes(input_scopes.size());
    for (size_t i = 0; i < input_scopes.size(); ++i)
      native_input_scopes[i] = static_cast<InputScope>(input_scopes[i]);
    scoped_ptr<DocumentBinding> new_document =
        DocumentBinding::Create(thread_manager_.get(),
                                client_id_,
                                native_input_scopes,
                                window_handle_,
                                this);
    LOG_IF(ERROR, !new_document) << "Failed to create a new document.";
    current_document_.swap(new_document);
    OnWindowActivated();
  }

  TfClientId client_id_;
  HWND window_handle_;
  TextServiceDelegate* delegate_;
  scoped_ptr<DocumentBinding> current_document_;
  base::win::ScopedComPtr<ITfThreadMgr> thread_manager_;

  // A vector of InputScope enumeration, which represents the document type of
  // the focused text field. Note that in our IPC message protocol, an empty
  // |input_scopes_| has special meaning that IMEs must be disabled on this
  // document.
  std::vector<int32_t> input_scopes_;
  // Character bounds of the composition. When there is no composition but this
  // vector is not empty, the first element contains the caret bounds.
  std::vector<metro_viewer::CharacterBounds> composition_character_bounds_;

  DISALLOW_COPY_AND_ASSIGN(TextServiceImpl);
};

}  // namespace

scoped_ptr<TextService>
CreateTextService(TextServiceDelegate* delegate, HWND window_handle) {
  if (!delegate)
    return scoped_ptr<TextService>();
  base::win::ScopedComPtr<ITfThreadMgr> thread_manager;
  HRESULT hr = thread_manager.CreateInstance(CLSID_TF_ThreadMgr);
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to create instance of CLSID_TF_ThreadMgr. hr = "
               << hr;
    return scoped_ptr<TextService>();
  }
  TfClientId client_id = TF_CLIENTID_NULL;
  hr = thread_manager->Activate(&client_id);
  if (FAILED(hr)) {
    LOG(ERROR) << "ITfThreadMgr::Activate failed. hr = " << hr;
    return scoped_ptr<TextService>();
  }
  if (!InitializeSentenceMode(thread_manager.get(), client_id)) {
    LOG(ERROR) << "InitializeSentenceMode failed.";
    thread_manager->Deactivate();
    return scoped_ptr<TextService>();
  }
  return scoped_ptr<TextService>(new TextServiceImpl(
      thread_manager.get(), client_id, window_handle, delegate));
}

}  // namespace metro_driver