summaryrefslogtreecommitdiffstats
path: root/win8/metro_driver/ime/input_source.cc
blob: 17d36bfb4bd80cbdd98342fb4ba64daf07149a90 (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
// 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/input_source.h"

#include <atlbase.h>
#include <atlcom.h>
#include <msctf.h>

#include "base/bind.h"
#include "base/callback.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/observer_list.h"
#include "base/win/scoped_comptr.h"
#include "ui/base/win/atl_module.h"
#include "win8/metro_driver/ime/input_source_observer.h"

namespace metro_driver {
namespace {

// An implementation of ITfLanguageProfileNotifySink interface, which will be
// used to receive notifications when the text input source is changed.
class ATL_NO_VTABLE InputSourceMonitor
    : public CComObjectRootEx<CComMultiThreadModel>,
      public ITfLanguageProfileNotifySink {
 public:
  InputSourceMonitor()
      : cookie_(TF_INVALID_COOKIE) {
  }

  BEGIN_COM_MAP(InputSourceMonitor)
    COM_INTERFACE_ENTRY(ITfLanguageProfileNotifySink)
  END_COM_MAP()

  bool Initialize(ITfSource* source) {
    DWORD cookie = TF_INVALID_COOKIE;
    HRESULT hr = source->AdviseSink(IID_ITfLanguageProfileNotifySink,
                                    this,
                                    &cookie);
    if (FAILED(hr)) {
      LOG(ERROR) << "ITfSource::AdviseSink failed. hr = " << hr;
      return false;
    }
    cookie_ = cookie;
    source_ = source;
    return true;
  }

  void SetCallback(base::Closure on_language_chanaged) {
    on_language_chanaged_ = on_language_chanaged;
  }

  void Unadvise() {
    if (cookie_ == TF_INVALID_COOKIE || !source_.get())
      return;
    if (FAILED(source_->UnadviseSink(cookie_)))
      return;
    cookie_ = TF_INVALID_COOKIE;
    source_.Release();
  }

 private:
  // ITfLanguageProfileNotifySink overrides:
  STDMETHOD(OnLanguageChange)(LANGID langid, BOOL *accept) override {
    if (!accept)
      return E_INVALIDARG;
    *accept = TRUE;
    return S_OK;
  }

  STDMETHOD(OnLanguageChanged)() override {
    if (!on_language_chanaged_.is_null())
      on_language_chanaged_.Run();
    return S_OK;
  }

  base::Closure on_language_chanaged_;
  base::win::ScopedComPtr<ITfSource> source_;
  DWORD cookie_;

  DISALLOW_COPY_AND_ASSIGN(InputSourceMonitor);
};

class InputSourceImpl : public InputSource {
 public:
  InputSourceImpl(ITfInputProcessorProfileMgr* profile_manager,
                  InputSourceMonitor* monitor)
      : profile_manager_(profile_manager),
        monitor_(monitor) {
    monitor_->SetCallback(base::Bind(&InputSourceImpl::OnLanguageChanged,
                                     base::Unretained(this)));
  }
  ~InputSourceImpl() override {
    monitor_->SetCallback(base::Closure());
    monitor_->Unadvise();
  }

 private:
  // InputSource overrides.
  bool GetActiveSource(LANGID* langid, bool* is_ime) override {
    TF_INPUTPROCESSORPROFILE profile = {};
    HRESULT hr = profile_manager_->GetActiveProfile(GUID_TFCAT_TIP_KEYBOARD,
                                                    &profile);
    if (FAILED(hr)) {
      LOG(ERROR) << "ITfInputProcessorProfileMgr::GetActiveProfile failed."
                 << " hr = " << hr;
      return false;
    }
    *langid = profile.langid;
    *is_ime = profile.dwProfileType == TF_PROFILETYPE_INPUTPROCESSOR;
    return true;
  }
  void AddObserver(InputSourceObserver* observer) override {
    observer_list_.AddObserver(observer);
  }
  void RemoveObserver(InputSourceObserver* observer) override {
    observer_list_.RemoveObserver(observer);
  }
  void OnLanguageChanged() {
    FOR_EACH_OBSERVER(InputSourceObserver,
                      observer_list_,
                      OnInputSourceChanged());
  }

  base::win::ScopedComPtr<ITfInputProcessorProfileMgr> profile_manager_;
  scoped_refptr<InputSourceMonitor> monitor_;
  base::ObserverList<InputSourceObserver> observer_list_;

  DISALLOW_COPY_AND_ASSIGN(InputSourceImpl);
};

}  // namespace

// static
scoped_ptr<InputSource> InputSource::Create() {
  ui::win::CreateATLModuleIfNeeded();

  base::win::ScopedComPtr<ITfInputProcessorProfileMgr> profile_manager;
  HRESULT hr = profile_manager.CreateInstance(CLSID_TF_InputProcessorProfiles);
  if (FAILED(hr)) {
    LOG(ERROR) << "Failed to instantiate CLSID_TF_InputProcessorProfiles."
               << " hr = " << hr;
    return scoped_ptr<InputSource>();
  }
  base::win::ScopedComPtr<ITfSource> profiles_source;
  hr = profiles_source.QueryFrom(profile_manager.get());
  if (FAILED(hr)) {
    LOG(ERROR) << "QueryFrom to ITfSource failed. hr = " << hr;
    return scoped_ptr<InputSource>();
  }

  CComObject<InputSourceMonitor>* monitor = NULL;
  hr = CComObject<InputSourceMonitor>::CreateInstance(&monitor);
  if (FAILED(hr)) {
    LOG(ERROR) << "CComObject<InputSourceMonitor>::CreateInstance failed."
               << " hr = " << hr;
    return scoped_ptr<InputSource>();
  }
  if (!monitor->Initialize(profiles_source.get())) {
    LOG(ERROR) << "Failed to initialize the monitor.";
    return scoped_ptr<InputSource>();
  }

  // Transfer the ownership.
  return scoped_ptr<InputSource>(
      new InputSourceImpl(profile_manager.get(), monitor));
}

}  // namespace metro_driver