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
3 // found in the LICENSE file.
5 #include "ui/aura/remote_root_window_host_win.h"
11 #include "base/message_loop/message_loop.h"
12 #include "ipc/ipc_message.h"
13 #include "ipc/ipc_sender.h"
14 #include "ui/aura/client/cursor_client.h"
15 #include "ui/aura/root_window.h"
16 #include "ui/base/cursor/cursor_loader_win.h"
17 #include "ui/events/event_utils.h"
18 #include "ui/events/keycodes/keyboard_code_conversion_win.h"
19 #include "ui/base/view_prop.h"
20 #include "ui/gfx/insets.h"
21 #include "ui/metro_viewer/metro_viewer_messages.h"
27 const char* kRootWindowHostWinKey = "__AURA_REMOTE_ROOT_WINDOW_HOST_WIN__";
29 // Sets the keystate for the virtual key passed in to down or up.
30 void SetKeyState(uint8* key_states, bool key_down, uint32 virtual_key_code) {
34 key_states[virtual_key_code] |= 0x80;
36 key_states[virtual_key_code] &= 0x7F;
39 // Sets the keyboard states for the Shift/Control/Alt/Caps lock keys.
40 void SetVirtualKeyStates(uint32 flags) {
41 uint8 keyboard_state[256] = {0};
42 ::GetKeyboardState(keyboard_state);
44 SetKeyState(keyboard_state, !!(flags & ui::EF_SHIFT_DOWN), VK_SHIFT);
45 SetKeyState(keyboard_state, !!(flags & ui::EF_CONTROL_DOWN), VK_CONTROL);
46 SetKeyState(keyboard_state, !!(flags & ui::EF_ALT_DOWN), VK_MENU);
47 SetKeyState(keyboard_state, !!(flags & ui::EF_CAPS_LOCK_DOWN), VK_CAPITAL);
48 SetKeyState(keyboard_state, !!(flags & ui::EF_LEFT_MOUSE_BUTTON), VK_LBUTTON);
49 SetKeyState(keyboard_state, !!(flags & ui::EF_RIGHT_MOUSE_BUTTON),
51 SetKeyState(keyboard_state, !!(flags & ui::EF_MIDDLE_MOUSE_BUTTON),
54 ::SetKeyboardState(keyboard_state);
59 void HandleOpenFile(const base::string16& title,
60 const base::FilePath& default_path,
61 const base::string16& filter,
62 const OpenFileCompletion& on_success,
63 const FileSelectionCanceled& on_failure) {
64 DCHECK(aura::RemoteRootWindowHostWin::Instance());
65 aura::RemoteRootWindowHostWin::Instance()->HandleOpenFile(title,
72 void HandleOpenMultipleFiles(const base::string16& title,
73 const base::FilePath& default_path,
74 const base::string16& filter,
75 const OpenMultipleFilesCompletion& on_success,
76 const FileSelectionCanceled& on_failure) {
77 DCHECK(aura::RemoteRootWindowHostWin::Instance());
78 aura::RemoteRootWindowHostWin::Instance()->HandleOpenMultipleFiles(
86 void HandleSaveFile(const base::string16& title,
87 const base::FilePath& default_path,
88 const base::string16& filter,
90 const base::string16& default_extension,
91 const SaveFileCompletion& on_success,
92 const FileSelectionCanceled& on_failure) {
93 DCHECK(aura::RemoteRootWindowHostWin::Instance());
94 aura::RemoteRootWindowHostWin::Instance()->HandleSaveFile(title,
103 void HandleSelectFolder(const base::string16& title,
104 const SelectFolderCompletion& on_success,
105 const FileSelectionCanceled& on_failure) {
106 DCHECK(aura::RemoteRootWindowHostWin::Instance());
107 aura::RemoteRootWindowHostWin::Instance()->HandleSelectFolder(title,
112 void HandleActivateDesktop(const base::FilePath& shortcut,
113 const ActivateDesktopCompleted& on_success) {
114 DCHECK(aura::RemoteRootWindowHostWin::Instance());
115 aura::RemoteRootWindowHostWin::Instance()->HandleActivateDesktop(shortcut,
119 RemoteRootWindowHostWin* g_instance = NULL;
121 RemoteRootWindowHostWin* RemoteRootWindowHostWin::Instance() {
124 return Create(gfx::Rect());
127 RemoteRootWindowHostWin* RemoteRootWindowHostWin::Create(
128 const gfx::Rect& bounds) {
129 g_instance = g_instance ? g_instance : new RemoteRootWindowHostWin(bounds);
133 RemoteRootWindowHostWin::RemoteRootWindowHostWin(const gfx::Rect& bounds)
134 : remote_window_(NULL),
137 ignore_mouse_moves_until_set_cursor_ack_(false),
139 prop_.reset(new ui::ViewProp(NULL, kRootWindowHostWinKey, this));
142 RemoteRootWindowHostWin::~RemoteRootWindowHostWin() {
146 void RemoteRootWindowHostWin::Connected(IPC::Sender* host, HWND remote_window) {
147 CHECK(host_ == NULL);
149 remote_window_ = remote_window;
152 void RemoteRootWindowHostWin::Disconnected() {
153 // Don't CHECK here, Disconnected is called on a channel error which can
154 // happen before we're successfully Connected.
156 remote_window_ = NULL;
159 bool RemoteRootWindowHostWin::OnMessageReceived(const IPC::Message& message) {
161 IPC_BEGIN_MESSAGE_MAP(RemoteRootWindowHostWin, message)
162 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_MouseMoved, OnMouseMoved)
163 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_MouseButton, OnMouseButton)
164 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_KeyDown, OnKeyDown)
165 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_KeyUp, OnKeyUp)
166 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_Character, OnChar)
167 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_WindowActivated,
169 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_TouchDown,
171 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_TouchUp,
173 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_TouchMoved,
175 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_FileSaveAsDone,
177 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_FileOpenDone,
179 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_MultiFileOpenDone,
181 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_SelectFolderDone,
183 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_SetCursorPosAck,
185 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_WindowSizeChanged,
187 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_ActivateDesktopDone,
189 IPC_MESSAGE_UNHANDLED(handled = false)
190 IPC_END_MESSAGE_MAP()
194 void RemoteRootWindowHostWin::HandleOpenURLOnDesktop(
195 const base::FilePath& shortcut,
196 const base::string16& url) {
199 host_->Send(new MetroViewerHostMsg_OpenURLOnDesktop(shortcut, url));
202 void RemoteRootWindowHostWin::HandleActivateDesktop(
203 const base::FilePath& shortcut,
204 const ActivateDesktopCompleted& on_success) {
207 DCHECK(activate_completed_callback_.is_null());
208 activate_completed_callback_ = on_success;
209 host_->Send(new MetroViewerHostMsg_ActivateDesktop(shortcut));
212 void RemoteRootWindowHostWin::HandleOpenFile(
213 const base::string16& title,
214 const base::FilePath& default_path,
215 const base::string16& filter,
216 const OpenFileCompletion& on_success,
217 const FileSelectionCanceled& on_failure) {
221 // Can only have one of these operations in flight.
222 DCHECK(file_open_completion_callback_.is_null());
223 DCHECK(failure_callback_.is_null());
225 file_open_completion_callback_ = on_success;
226 failure_callback_ = on_failure;
228 host_->Send(new MetroViewerHostMsg_DisplayFileOpen(title,
234 void RemoteRootWindowHostWin::HandleOpenMultipleFiles(
235 const base::string16& title,
236 const base::FilePath& default_path,
237 const base::string16& filter,
238 const OpenMultipleFilesCompletion& on_success,
239 const FileSelectionCanceled& on_failure) {
243 // Can only have one of these operations in flight.
244 DCHECK(multi_file_open_completion_callback_.is_null());
245 DCHECK(failure_callback_.is_null());
246 multi_file_open_completion_callback_ = on_success;
247 failure_callback_ = on_failure;
249 host_->Send(new MetroViewerHostMsg_DisplayFileOpen(title,
255 void RemoteRootWindowHostWin::HandleSaveFile(
256 const base::string16& title,
257 const base::FilePath& default_path,
258 const base::string16& filter,
260 const base::string16& default_extension,
261 const SaveFileCompletion& on_success,
262 const FileSelectionCanceled& on_failure) {
266 MetroViewerHostMsg_SaveAsDialogParams params;
267 params.title = title;
268 params.default_extension = default_extension;
269 params.filter = filter;
270 params.filter_index = filter_index;
271 params.suggested_name = default_path;
273 // Can only have one of these operations in flight.
274 DCHECK(file_saveas_completion_callback_.is_null());
275 DCHECK(failure_callback_.is_null());
276 file_saveas_completion_callback_ = on_success;
277 failure_callback_ = on_failure;
279 host_->Send(new MetroViewerHostMsg_DisplayFileSaveAs(params));
282 void RemoteRootWindowHostWin::HandleSelectFolder(
283 const base::string16& title,
284 const SelectFolderCompletion& on_success,
285 const FileSelectionCanceled& on_failure) {
289 // Can only have one of these operations in flight.
290 DCHECK(select_folder_completion_callback_.is_null());
291 DCHECK(failure_callback_.is_null());
292 select_folder_completion_callback_ = on_success;
293 failure_callback_ = on_failure;
295 host_->Send(new MetroViewerHostMsg_DisplaySelectFolder(title));
298 Window* RemoteRootWindowHostWin::GetAshWindow() {
299 return GetRootWindow();
302 void RemoteRootWindowHostWin::SetDelegate(RootWindowHostDelegate* delegate) {
303 delegate_ = delegate;
306 RootWindow* RemoteRootWindowHostWin::GetRootWindow() {
307 return delegate_->AsRootWindow();
310 gfx::AcceleratedWidget RemoteRootWindowHostWin::GetAcceleratedWidget() {
312 return remote_window_;
313 // Getting here should only happen for ash_unittests.exe and related code.
314 return ::GetDesktopWindow();
317 void RemoteRootWindowHostWin::Show() {
320 void RemoteRootWindowHostWin::Hide() {
324 void RemoteRootWindowHostWin::ToggleFullScreen() {
327 gfx::Rect RemoteRootWindowHostWin::GetBounds() const {
328 gfx::Rect r(gfx::Point(0, 0), aura::RootWindowHost::GetNativeScreenSize());
332 void RemoteRootWindowHostWin::SetBounds(const gfx::Rect& bounds) {
333 delegate_->OnHostResized(bounds.size());
336 gfx::Insets RemoteRootWindowHostWin::GetInsets() const {
337 return gfx::Insets();
340 void RemoteRootWindowHostWin::SetInsets(const gfx::Insets& insets) {
343 gfx::Point RemoteRootWindowHostWin::GetLocationOnNativeScreen() const {
344 return gfx::Point(0, 0);
347 void RemoteRootWindowHostWin::SetCursor(gfx::NativeCursor native_cursor) {
351 new MetroViewerHostMsg_SetCursor(uint64(native_cursor.platform())));
354 void RemoteRootWindowHostWin::SetCapture() {
357 void RemoteRootWindowHostWin::ReleaseCapture() {
360 bool RemoteRootWindowHostWin::QueryMouseLocation(gfx::Point* location_return) {
361 aura::client::CursorClient* cursor_client =
362 aura::client::GetCursorClient(GetRootWindow());
363 if (cursor_client && !cursor_client->IsMouseEventsEnabled()) {
364 *location_return = gfx::Point(0, 0);
370 gfx::Point(static_cast<int>(pt.x), static_cast<int>(pt.y));
374 bool RemoteRootWindowHostWin::ConfineCursorToRootWindow() {
378 void RemoteRootWindowHostWin::UnConfineCursor() {
381 void RemoteRootWindowHostWin::OnCursorVisibilityChanged(bool show) {
385 void RemoteRootWindowHostWin::MoveCursorTo(const gfx::Point& location) {
386 VLOG(1) << "In MoveCursorTo: " << location.x() << ", " << location.y();
390 // This function can be called in cases like when the mouse cursor is
391 // restricted within a viewport (For e.g. LockCursor) which assumes that
392 // subsequent mouse moves would be received starting with the new cursor
393 // coordinates. This is a challenge for Windows ASH for the reasons
395 // Other cases which don't expect this behavior should continue to work
398 // The mouse events are received by the viewer process and sent to the
399 // browser. If we invoke the SetCursor API here we continue to receive
400 // mouse messages from the viewer which were posted before the SetCursor
401 // API executes which messes up the state in the browser. To workaround
402 // this we invoke the SetCursor API in the viewer process and ignore
403 // mouse messages until we received an ACK from the viewer indicating that
404 // the SetCursor operation completed.
405 ignore_mouse_moves_until_set_cursor_ack_ = true;
406 VLOG(1) << "In MoveCursorTo. Sending IPC";
407 host_->Send(new MetroViewerHostMsg_SetCursorPos(location.x(), location.y()));
410 void RemoteRootWindowHostWin::SetFocusWhenShown(bool focus_when_shown) {
414 void RemoteRootWindowHostWin::PostNativeEvent(
415 const base::NativeEvent& native_event) {
418 void RemoteRootWindowHostWin::OnDeviceScaleFactorChanged(
419 float device_scale_factor) {
423 void RemoteRootWindowHostWin::PrepareForShutdown() {
426 void RemoteRootWindowHostWin::OnMouseMoved(int32 x, int32 y, int32 flags) {
427 if (ignore_mouse_moves_until_set_cursor_ack_)
430 gfx::Point location(x, y);
431 ui::MouseEvent event(ui::ET_MOUSE_MOVED, location, location, flags);
432 delegate_->OnHostMouseEvent(&event);
435 void RemoteRootWindowHostWin::OnMouseButton(
440 ui::EventFlags flags) {
441 gfx::Point location(x, y);
442 ui::MouseEvent mouse_event(type, location, location, flags);
444 SetEventFlags(flags | key_event_flags());
445 if (type == ui::ET_MOUSEWHEEL) {
446 ui::MouseWheelEvent wheel_event(mouse_event, 0, extra);
447 delegate_->OnHostMouseEvent(&wheel_event);
448 } else if (type == ui::ET_MOUSE_PRESSED) {
449 // TODO(shrikant): Ideally modify code in event.cc by adding automatic
450 // tracking of double clicks in synthetic MouseEvent constructor code.
451 // Non-synthetic MouseEvent constructor code does automatically track
452 // this. Need to use some caution while modifying synthetic constructor
453 // as many tests and other code paths depend on it and apparently
454 // specifically depend on non implicit tracking of previous mouse event.
455 if (last_mouse_click_event_ &&
456 ui::MouseEvent::IsRepeatedClickEvent(mouse_event,
457 *last_mouse_click_event_)) {
458 mouse_event.SetClickCount(2);
460 mouse_event.SetClickCount(1);
462 last_mouse_click_event_ .reset(new ui::MouseEvent(mouse_event));
463 delegate_->OnHostMouseEvent(&mouse_event);
465 delegate_->OnHostMouseEvent(&mouse_event);
469 void RemoteRootWindowHostWin::OnKeyDown(uint32 vkey,
473 DispatchKeyboardMessage(ui::ET_KEY_PRESSED, vkey, repeat_count, scan_code,
477 void RemoteRootWindowHostWin::OnKeyUp(uint32 vkey,
481 DispatchKeyboardMessage(ui::ET_KEY_RELEASED, vkey, repeat_count, scan_code,
485 void RemoteRootWindowHostWin::OnChar(uint32 key_code,
489 DispatchKeyboardMessage(ui::ET_KEY_PRESSED, key_code, repeat_count,
490 scan_code, flags, true);
493 void RemoteRootWindowHostWin::OnWindowActivated() {
494 delegate_->OnHostActivated();
497 void RemoteRootWindowHostWin::OnTouchDown(int32 x,
501 ui::TouchEvent event(ui::ET_TOUCH_PRESSED,
504 base::TimeDelta::FromMicroseconds(timestamp));
505 delegate_->OnHostTouchEvent(&event);
508 void RemoteRootWindowHostWin::OnTouchUp(int32 x,
512 ui::TouchEvent event(ui::ET_TOUCH_RELEASED,
515 base::TimeDelta::FromMicroseconds(timestamp));
516 delegate_->OnHostTouchEvent(&event);
519 void RemoteRootWindowHostWin::OnTouchMoved(int32 x,
523 ui::TouchEvent event(ui::ET_TOUCH_MOVED,
526 base::TimeDelta::FromMicroseconds(timestamp));
527 delegate_->OnHostTouchEvent(&event);
530 void RemoteRootWindowHostWin::OnFileSaveAsDone(bool success,
531 const base::FilePath& filename,
534 file_saveas_completion_callback_.Run(filename, filter_index, NULL);
536 failure_callback_.Run(NULL);
537 file_saveas_completion_callback_.Reset();
538 failure_callback_.Reset();
542 void RemoteRootWindowHostWin::OnFileOpenDone(bool success,
543 const base::FilePath& filename) {
545 file_open_completion_callback_.Run(base::FilePath(filename), 0, NULL);
547 failure_callback_.Run(NULL);
548 file_open_completion_callback_.Reset();
549 failure_callback_.Reset();
552 void RemoteRootWindowHostWin::OnMultiFileOpenDone(
554 const std::vector<base::FilePath>& files) {
556 multi_file_open_completion_callback_.Run(files, NULL);
558 failure_callback_.Run(NULL);
559 multi_file_open_completion_callback_.Reset();
560 failure_callback_.Reset();
563 void RemoteRootWindowHostWin::OnSelectFolderDone(
565 const base::FilePath& folder) {
567 select_folder_completion_callback_.Run(base::FilePath(folder), 0, NULL);
569 failure_callback_.Run(NULL);
570 select_folder_completion_callback_.Reset();
571 failure_callback_.Reset();
574 void RemoteRootWindowHostWin::OnSetCursorPosAck() {
575 DCHECK(ignore_mouse_moves_until_set_cursor_ack_);
576 ignore_mouse_moves_until_set_cursor_ack_ = false;
579 void RemoteRootWindowHostWin::OnWindowSizeChanged(uint32 width, uint32 height) {
580 SetBounds(gfx::Rect(0, 0, width, height));
583 void RemoteRootWindowHostWin::OnDesktopActivated() {
584 ActivateDesktopCompleted temp = activate_completed_callback_;
585 activate_completed_callback_.Reset();
589 void RemoteRootWindowHostWin::DispatchKeyboardMessage(ui::EventType type,
595 SetEventFlags(flags | mouse_event_flags());
596 if (base::MessageLoop::current()->IsNested()) {
597 uint32 message = is_character ? WM_CHAR :
598 (type == ui::ET_KEY_PRESSED ? WM_KEYDOWN : WM_KEYUP);
599 ::PostThreadMessage(::GetCurrentThreadId(),
602 repeat_count | scan_code >> 15);
604 ui::KeyEvent event(type,
605 ui::KeyboardCodeForWindowsKeyCode(vkey),
608 delegate_->OnHostKeyEvent(&event);
612 void RemoteRootWindowHostWin::SetEventFlags(uint32 flags) {
613 if (flags == event_flags_)
615 event_flags_ = flags;
616 SetVirtualKeyStates(event_flags_);