summaryrefslogtreecommitdiff
path: root/chromium/ui/base/ime/win/on_screen_keyboard_display_manager_tab_tip.cc
blob: f1fcd63e3c1b32e6108a082c609d59e964aae9a6 (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
// Copyright 2016 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 "ui/base/ime/win/on_screen_keyboard_display_manager_tab_tip.h"

#include <windows.h>

#include <shellapi.h>
#include <shlobj.h>
#include <shobjidl.h>  // Must be before propkey.

#include "base/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/single_thread_task_runner.h"
#include "base/strings/string_util.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/win/registry.h"
#include "base/win/scoped_co_mem.h"
#include "base/win/win_util.h"
#include "base/win/windows_version.h"
#include "ui/base/ime/win/osk_display_observer.h"
#include "ui/base/win/hidden_window.h"
#include "ui/display/win/screen_win.h"
#include "ui/gfx/geometry/dip_util.h"

namespace {

constexpr base::TimeDelta kCheckOSKDelay =
    base::TimeDelta::FromMilliseconds(1000);
constexpr base::TimeDelta kDismissKeyboardRetryTimeout =
    base::TimeDelta::FromMilliseconds(100);
constexpr int kDismissKeyboardMaxRetries = 5;

constexpr wchar_t kOSKClassName[] = L"IPTip_Main_Window";

constexpr wchar_t kWindows8OSKRegPath[] =
    L"Software\\Classes\\CLSID\\{054AAE20-4BEA-4347-8A35-64A533254A9D}"
    L"\\LocalServer32";

}  // namespace

namespace ui {

// This class provides functionality to detect when the on screen keyboard
// is displayed and move the main window up if it is obscured by the keyboard.
class OnScreenKeyboardDetector {
 public:
  OnScreenKeyboardDetector();
  ~OnScreenKeyboardDetector();

  // Schedules a delayed task which detects if the on screen keyboard was
  // displayed.
  void DetectKeyboard(HWND main_window);

  // Dismisses the on screen keyboard. If a call to display the keyboard was
  // made, this function waits for the keyboard to become visible by retrying
  // upto a maximum of kDismissKeyboardMaxRetries.
  bool DismissKeyboard();

  // Add/Remove keyboard observers.
  // Please note that this class does not track the |observer| destruction. It
  // is upto the classes which set up these observers to remove them when they
  // are destroyed.
  void AddObserver(OnScreenKeyboardObserver* observer);
  void RemoveObserver(OnScreenKeyboardObserver* observer);

  // Returns true if the osk is visible.
  static bool IsKeyboardVisible();

 private:
  // Returns the occluded rect in dips.
  gfx::Rect GetOccludedRect();

  // Executes as a task and detects if the on screen keyboard is displayed.
  // Once the keyboard is displayed it schedules the HideIfNecessary() task to
  // detect when the keyboard is or should be hidden.
  void CheckIfKeyboardVisible();

  // Executes as a task and detects if the keyboard was hidden or should be
  // hidden.
  void HideIfNecessary();

  // Notifies observers that the keyboard was displayed.
  // A recurring task HideIfNecessary() is started to detect when the OSK
  // disappears.
  void HandleKeyboardVisible(const gfx::Rect& occluded_rect);

  // Notifies observers that the keyboard was hidden.
  // The observer list is cleared out after this notification.
  void HandleKeyboardHidden();

  // Removes all observers from the list.
  void ClearObservers();

  // The main window which displays the on screen keyboard.
  HWND main_window_ = nullptr;

  // Tracks if the keyboard was displayed.
  bool osk_visible_notification_received_ = false;

  // Set to true if a call to DetectKeyboard() was made.
  bool keyboard_detect_requested_ = false;

  // Contains the number of attempts made to dismiss the keyboard. Please refer
  // to the DismissKeyboard() function for more information.
  int keyboard_dismiss_retry_count_ = 0;

  base::ObserverList<OnScreenKeyboardObserver, false> observers_;

  // Should be the last member in the class. Helps ensure that tasks spawned
  // by this class instance are canceled when it is destroyed.
  base::WeakPtrFactory<OnScreenKeyboardDetector> keyboard_detector_factory_;

  DISALLOW_COPY_AND_ASSIGN(OnScreenKeyboardDetector);
};

// OnScreenKeyboardDetector member definitions.
OnScreenKeyboardDetector::OnScreenKeyboardDetector()
    : keyboard_detector_factory_(this) {}

OnScreenKeyboardDetector::~OnScreenKeyboardDetector() {
  ClearObservers();
}

void OnScreenKeyboardDetector::DetectKeyboard(HWND main_window) {
  main_window_ = main_window;
  keyboard_detect_requested_ = true;
  // The keyboard is displayed by TabTip.exe which is launched via a
  // ShellExecute call in the
  // OnScreenKeyboardDisplayManager::DisplayVirtualKeyboard() function. We use
  // a delayed task to check if the keyboard is visible because of the possible
  // delay between the ShellExecute call and the keyboard becoming visible.
  base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
      FROM_HERE,
      base::BindOnce(&OnScreenKeyboardDetector::CheckIfKeyboardVisible,
                     keyboard_detector_factory_.GetWeakPtr()),
      kCheckOSKDelay);
}

bool OnScreenKeyboardDetector::DismissKeyboard() {
  // We dismiss the virtual keyboard by generating the SC_CLOSE.
  HWND osk = ::FindWindow(kOSKClassName, nullptr);
  if (::IsWindow(osk) && ::IsWindowEnabled(osk)) {
    keyboard_detect_requested_ = false;
    keyboard_dismiss_retry_count_ = 0;
    HandleKeyboardHidden();
    PostMessage(osk, WM_SYSCOMMAND, SC_CLOSE, 0);
    return true;
  }

  if (keyboard_detect_requested_) {
    if (keyboard_dismiss_retry_count_ < kDismissKeyboardMaxRetries) {
      keyboard_dismiss_retry_count_++;
      // Please refer to the comments in the DetectKeyboard() function for more
      // information as to why we need a delayed task here.
      base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
          FROM_HERE,
          base::BindOnce(
              base::IgnoreResult(&OnScreenKeyboardDetector::DismissKeyboard),
              keyboard_detector_factory_.GetWeakPtr()),
          kDismissKeyboardRetryTimeout);
    } else {
      keyboard_dismiss_retry_count_ = 0;
    }
  }
  return false;
}

void OnScreenKeyboardDetector::AddObserver(OnScreenKeyboardObserver* observer) {
  observers_.AddObserver(observer);
}

void OnScreenKeyboardDetector::RemoveObserver(
    OnScreenKeyboardObserver* observer) {
  observers_.RemoveObserver(observer);
}

// static
bool OnScreenKeyboardDetector::IsKeyboardVisible() {
  HWND osk = ::FindWindow(kOSKClassName, nullptr);
  if (!::IsWindow(osk))
    return false;
  return ::IsWindowVisible(osk) && ::IsWindowEnabled(osk);
}

gfx::Rect OnScreenKeyboardDetector::GetOccludedRect() {
  gfx::Rect occluded_rect;
  HWND osk = ::FindWindow(kOSKClassName, nullptr);
  if (!::IsWindow(osk) || !::IsWindowVisible(osk) || !::IsWindowEnabled(osk))
    return occluded_rect;

  RECT osk_rect = {};
  RECT main_window_rect = {};
  if (!::GetWindowRect(osk, &osk_rect) ||
      !::GetWindowRect(main_window_, &main_window_rect)) {
    return occluded_rect;
  }

  gfx::Rect gfx_osk_rect(osk_rect);
  gfx::Rect gfx_main_window_rect(main_window_rect);

  gfx_osk_rect.Intersect(gfx_main_window_rect);

  return display::win::ScreenWin::ScreenToDIPRect(main_window_, gfx_osk_rect);
}

void OnScreenKeyboardDetector::CheckIfKeyboardVisible() {
  gfx::Rect occluded_rect = GetOccludedRect();
  if (!occluded_rect.IsEmpty()) {
    if (!osk_visible_notification_received_)
      HandleKeyboardVisible(occluded_rect);
  } else {
    DVLOG(1) << "OSK did not come up. Something wrong.";
  }
}

void OnScreenKeyboardDetector::HideIfNecessary() {
  HWND osk = ::FindWindow(kOSKClassName, nullptr);
  if (!::IsWindow(osk))
    return;

  // Three cases here.
  // 1. OSK was hidden because the user dismissed it.
  // 2. We are no longer in the foreground.
  // 3. The OSK is still visible.
  // In the first case we just have to notify the observers that the OSK was
  // hidden.
  // In the second case we need to dismiss the OSK which internally will
  // notify the observers about the OSK being hidden.
  if (!::IsWindowEnabled(osk)) {
    if (osk_visible_notification_received_) {
      if (main_window_ == ::GetForegroundWindow()) {
        DVLOG(1) << "OSK window hidden while we are in the foreground.";
        HandleKeyboardHidden();
      }
    }
  } else if (main_window_ != ::GetForegroundWindow()) {
    if (osk_visible_notification_received_) {
      DVLOG(1) << "We are no longer in the foreground. Dismising OSK.";
      DismissKeyboard();
    }
  } else {
    base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
        FROM_HERE,
        base::BindOnce(&OnScreenKeyboardDetector::HideIfNecessary,
                       keyboard_detector_factory_.GetWeakPtr()),
        kCheckOSKDelay);
  }
}

void OnScreenKeyboardDetector::HandleKeyboardVisible(
    const gfx::Rect& occluded_rect) {
  DCHECK(!osk_visible_notification_received_);
  osk_visible_notification_received_ = true;

  for (OnScreenKeyboardObserver& observer : observers_)
    observer.OnKeyboardVisible(occluded_rect);

  // Now that the keyboard is visible, run the task to detect if it was hidden.
  base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
      FROM_HERE,
      base::BindOnce(&OnScreenKeyboardDetector::HideIfNecessary,
                     keyboard_detector_factory_.GetWeakPtr()),
      kCheckOSKDelay);
}

void OnScreenKeyboardDetector::HandleKeyboardHidden() {
  osk_visible_notification_received_ = false;
  for (OnScreenKeyboardObserver& observer : observers_)
    observer.OnKeyboardHidden();
  ClearObservers();
}

void OnScreenKeyboardDetector::ClearObservers() {
  for (auto& observer : observers_)
    RemoveObserver(&observer);
}

// OnScreenKeyboardDisplayManagerTabTip member definitions.
OnScreenKeyboardDisplayManagerTabTip::OnScreenKeyboardDisplayManagerTabTip() {
  DCHECK_GE(base::win::GetVersion(), base::win::VERSION_WIN8);
}

OnScreenKeyboardDisplayManagerTabTip::~OnScreenKeyboardDisplayManagerTabTip() {}

bool OnScreenKeyboardDisplayManagerTabTip::DisplayVirtualKeyboard(
    OnScreenKeyboardObserver* observer) {
  if (base::win::IsKeyboardPresentOnSlate(nullptr, ui::GetHiddenWindow()))
    return false;

  if (osk_path_.empty() && !GetOSKPath(&osk_path_)) {
    DLOG(WARNING) << "Failed to get on screen keyboard path from registry";
    return false;
  }

  HINSTANCE ret = ::ShellExecuteW(nullptr, L"", osk_path_.c_str(), nullptr,
                                  nullptr, SW_SHOW);

  bool success = reinterpret_cast<intptr_t>(ret) > 32;
  if (success) {
    // If multiple calls to DisplayVirtualKeyboard occur one after the other,
    // the last observer would be the one to get notifications.
    keyboard_detector_ = std::make_unique<OnScreenKeyboardDetector>();
    if (observer)
      keyboard_detector_->AddObserver(observer);
    keyboard_detector_->DetectKeyboard(::GetForegroundWindow());
  }
  return success;
}

bool OnScreenKeyboardDisplayManagerTabTip::DismissVirtualKeyboard() {
  return keyboard_detector_ ? keyboard_detector_->DismissKeyboard() : false;
}

void OnScreenKeyboardDisplayManagerTabTip::RemoveObserver(
    OnScreenKeyboardObserver* observer) {
  if (keyboard_detector_)
    keyboard_detector_->RemoveObserver(observer);
}

bool OnScreenKeyboardDisplayManagerTabTip::GetOSKPath(
    base::string16* osk_path) {
  DCHECK(osk_path);

  // We need to launch TabTip.exe from the location specified under the
  // LocalServer32 key for the {{054AAE20-4BEA-4347-8A35-64A533254A9D}}
  // CLSID.
  // TabTip.exe is typically found at
  // c:\program files\common files\microsoft shared\ink on English Windows.
  // We don't want to launch TabTip.exe from
  // c:\program files (x86)\common files\microsoft shared\ink. This path is
  // normally found on 64 bit Windows.
  base::win::RegKey key(HKEY_LOCAL_MACHINE, kWindows8OSKRegPath,
                        KEY_READ | KEY_WOW64_64KEY);
  DWORD osk_path_length = 1024;
  if (key.ReadValue(nullptr, base::WriteInto(osk_path, osk_path_length),
                    &osk_path_length, nullptr) != ERROR_SUCCESS) {
    return false;
  }

  osk_path->resize(base::string16::traits_type::length(osk_path->c_str()));

  *osk_path = base::ToLowerASCII(*osk_path);

  size_t common_program_files_offset = osk_path->find(L"%commonprogramfiles%");
  // Typically the path to TabTip.exe read from the registry will start with
  // %CommonProgramFiles% which needs to be replaced with the corrsponding
  // expanded string.
  // If the path does not begin with %CommonProgramFiles% we use it as is.
  if (common_program_files_offset != base::string16::npos) {
    // Preserve the beginning quote in the path.
    osk_path->erase(common_program_files_offset,
                    wcslen(L"%commonprogramfiles%"));
    // The path read from the registry contains the %CommonProgramFiles%
    // environment variable prefix. On 64 bit Windows the SHGetKnownFolderPath
    // function returns the common program files path with the X86 suffix for
    // the FOLDERID_ProgramFilesCommon value.
    // To get the correct path to TabTip.exe we first read the environment
    // variable CommonProgramW6432 which points to the desired common
    // files path. Failing that we fallback to the SHGetKnownFolderPath API.

    // We then replace the %CommonProgramFiles% value with the actual common
    // files path found in the process.
    base::string16 common_program_files_path;
    DWORD buffer_size =
        GetEnvironmentVariable(L"CommonProgramW6432", nullptr, 0);
    if (buffer_size) {
      GetEnvironmentVariable(
          L"CommonProgramW6432",
          base::WriteInto(&common_program_files_path, buffer_size),
          buffer_size);
      DCHECK(!common_program_files_path.empty());
    } else {
      base::win::ScopedCoMem<wchar_t> common_program_files;
      if (FAILED(SHGetKnownFolderPath(FOLDERID_ProgramFilesCommon, 0, nullptr,
                                      &common_program_files))) {
        return false;
      }
      common_program_files_path = common_program_files;
    }
    osk_path->insert(common_program_files_offset, common_program_files_path);
  }
  return !osk_path->empty();
}

bool OnScreenKeyboardDisplayManagerTabTip::IsKeyboardVisible() const {
  return OnScreenKeyboardDetector::IsKeyboardVisible();
}

}  // namespace ui