Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ui / aura / remote_window_tree_host_win.cc
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.
4
5 #include "ui/aura/remote_window_tree_host_win.h"
6
7 #include <windows.h>
8
9 #include <algorithm>
10
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/aura_constants.h"
15 #include "ui/aura/client/cursor_client.h"
16 #include "ui/aura/root_window.h"
17 #include "ui/aura/window_property.h"
18 #include "ui/base/cursor/cursor_loader_win.h"
19 #include "ui/base/ime/composition_text.h"
20 #include "ui/base/ime/input_method.h"
21 #include "ui/base/ime/remote_input_method_win.h"
22 #include "ui/base/ime/text_input_client.h"
23 #include "ui/base/view_prop.h"
24 #include "ui/events/event_utils.h"
25 #include "ui/events/keycodes/keyboard_code_conversion_win.h"
26 #include "ui/gfx/insets.h"
27 #include "ui/gfx/win/dpi.h"
28 #include "ui/metro_viewer/metro_viewer_messages.h"
29
30 namespace aura {
31
32 namespace {
33
34 const char* kWindowTreeHostWinKey = "__AURA_REMOTE_WINDOW_TREE_HOST_WIN__";
35
36 // Sets the keystate for the virtual key passed in to down or up.
37 void SetKeyState(uint8* key_states, bool key_down, uint32 virtual_key_code) {
38   DCHECK(key_states);
39
40   if (key_down)
41     key_states[virtual_key_code] |= 0x80;
42   else
43     key_states[virtual_key_code] &= 0x7F;
44 }
45
46 // Sets the keyboard states for the Shift/Control/Alt/Caps lock keys.
47 void SetVirtualKeyStates(uint32 flags) {
48   uint8 keyboard_state[256] = {0};
49   ::GetKeyboardState(keyboard_state);
50
51   SetKeyState(keyboard_state, !!(flags & ui::EF_SHIFT_DOWN), VK_SHIFT);
52   SetKeyState(keyboard_state, !!(flags & ui::EF_CONTROL_DOWN), VK_CONTROL);
53   SetKeyState(keyboard_state, !!(flags & ui::EF_ALT_DOWN), VK_MENU);
54   SetKeyState(keyboard_state, !!(flags & ui::EF_CAPS_LOCK_DOWN), VK_CAPITAL);
55   SetKeyState(keyboard_state, !!(flags & ui::EF_LEFT_MOUSE_BUTTON), VK_LBUTTON);
56   SetKeyState(keyboard_state, !!(flags & ui::EF_RIGHT_MOUSE_BUTTON),
57               VK_RBUTTON);
58   SetKeyState(keyboard_state, !!(flags & ui::EF_MIDDLE_MOUSE_BUTTON),
59               VK_MBUTTON);
60
61   ::SetKeyboardState(keyboard_state);
62 }
63
64 void FillCompositionText(
65     const base::string16& text,
66     int32 selection_start,
67     int32 selection_end,
68     const std::vector<metro_viewer::UnderlineInfo>& underlines,
69     ui::CompositionText* composition_text) {
70   composition_text->Clear();
71   composition_text->text = text;
72   composition_text->selection.set_start(selection_start);
73   composition_text->selection.set_end(selection_end);
74   composition_text->underlines.resize(underlines.size());
75   for (size_t i = 0; i < underlines.size(); ++i) {
76     composition_text->underlines[i].start_offset = underlines[i].start_offset;
77     composition_text->underlines[i].end_offset = underlines[i].end_offset;
78     composition_text->underlines[i].color = SK_ColorBLACK;
79     composition_text->underlines[i].thick = underlines[i].thick;
80   }
81 }
82
83 }  // namespace
84
85 void HandleOpenFile(const base::string16& title,
86                     const base::FilePath& default_path,
87                     const base::string16& filter,
88                     const OpenFileCompletion& on_success,
89                     const FileSelectionCanceled& on_failure) {
90   DCHECK(aura::RemoteWindowTreeHostWin::Instance());
91   aura::RemoteWindowTreeHostWin::Instance()->HandleOpenFile(title,
92                                                             default_path,
93                                                             filter,
94                                                             on_success,
95                                                             on_failure);
96 }
97
98 void HandleOpenMultipleFiles(const base::string16& title,
99                              const base::FilePath& default_path,
100                              const base::string16& filter,
101                              const OpenMultipleFilesCompletion& on_success,
102                              const FileSelectionCanceled& on_failure) {
103   DCHECK(aura::RemoteWindowTreeHostWin::Instance());
104   aura::RemoteWindowTreeHostWin::Instance()->HandleOpenMultipleFiles(
105       title,
106       default_path,
107       filter,
108       on_success,
109       on_failure);
110 }
111
112 void HandleSaveFile(const base::string16& title,
113                     const base::FilePath& default_path,
114                     const base::string16& filter,
115                     int filter_index,
116                     const base::string16& default_extension,
117                     const SaveFileCompletion& on_success,
118                     const FileSelectionCanceled& on_failure) {
119   DCHECK(aura::RemoteWindowTreeHostWin::Instance());
120   aura::RemoteWindowTreeHostWin::Instance()->HandleSaveFile(title,
121                                                             default_path,
122                                                             filter,
123                                                             filter_index,
124                                                             default_extension,
125                                                             on_success,
126                                                             on_failure);
127 }
128
129 void HandleSelectFolder(const base::string16& title,
130                         const SelectFolderCompletion& on_success,
131                         const FileSelectionCanceled& on_failure) {
132   DCHECK(aura::RemoteWindowTreeHostWin::Instance());
133   aura::RemoteWindowTreeHostWin::Instance()->HandleSelectFolder(title,
134                                                                 on_success,
135                                                                 on_failure);
136 }
137
138 void HandleActivateDesktop(const base::FilePath& shortcut,
139                            bool ash_exit) {
140   DCHECK(aura::RemoteWindowTreeHostWin::Instance());
141   aura::RemoteWindowTreeHostWin::Instance()->HandleActivateDesktop(shortcut,
142                                                                    ash_exit);
143 }
144
145 void HandleMetroExit() {
146   DCHECK(aura::RemoteWindowTreeHostWin::Instance());
147   aura::RemoteWindowTreeHostWin::Instance()->HandleMetroExit();
148 }
149
150 RemoteWindowTreeHostWin* g_instance = NULL;
151
152 RemoteWindowTreeHostWin* RemoteWindowTreeHostWin::Instance() {
153   if (g_instance)
154     return g_instance;
155   return Create(gfx::Rect());
156 }
157
158 RemoteWindowTreeHostWin* RemoteWindowTreeHostWin::Create(
159     const gfx::Rect& bounds) {
160   g_instance = g_instance ? g_instance : new RemoteWindowTreeHostWin(bounds);
161   return g_instance;
162 }
163
164 RemoteWindowTreeHostWin::RemoteWindowTreeHostWin(const gfx::Rect& bounds)
165     : remote_window_(NULL),
166       host_(NULL),
167       ignore_mouse_moves_until_set_cursor_ack_(false),
168       event_flags_(0),
169       window_size_(aura::WindowTreeHost::GetNativeScreenSize()) {
170   prop_.reset(new ui::ViewProp(NULL, kWindowTreeHostWinKey, this));
171   CreateCompositor(GetAcceleratedWidget());
172 }
173
174 RemoteWindowTreeHostWin::~RemoteWindowTreeHostWin() {
175   DestroyCompositor();
176   g_instance = NULL;
177 }
178
179 // static
180 bool RemoteWindowTreeHostWin::IsValid() {
181   return Instance()->remote_window_ != NULL;
182 }
183
184 void RemoteWindowTreeHostWin::SetRemoteWindowHandle(HWND remote_window) {
185   remote_window_ = remote_window;
186   // Do not create compositor here, but in Connected() below.
187   // See http://crbug.com/330179 and http://crbug.com/334380.
188 }
189
190 void RemoteWindowTreeHostWin::Connected(IPC::Sender* host) {
191   CHECK(host_ == NULL);
192   DCHECK(remote_window_);
193   host_ = host;
194   // Recreate the compositor for the target surface represented by the
195   // remote_window HWND.
196   CreateCompositor(remote_window_);
197   InitCompositor();
198 }
199
200 void RemoteWindowTreeHostWin::Disconnected() {
201   // Don't CHECK here, Disconnected is called on a channel error which can
202   // happen before we're successfully Connected.
203   if (!host_)
204     return;
205   ui::RemoteInputMethodPrivateWin* remote_input_method_private =
206       GetRemoteInputMethodPrivate();
207   if (remote_input_method_private)
208     remote_input_method_private->SetRemoteDelegate(NULL);
209   host_ = NULL;
210   remote_window_ = NULL;
211 }
212
213 bool RemoteWindowTreeHostWin::OnMessageReceived(const IPC::Message& message) {
214   bool handled = true;
215   IPC_BEGIN_MESSAGE_MAP(RemoteWindowTreeHostWin, message)
216     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_MouseMoved, OnMouseMoved)
217     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_MouseButton, OnMouseButton)
218     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_KeyDown, OnKeyDown)
219     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_KeyUp, OnKeyUp)
220     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_Character, OnChar)
221     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_WindowActivated,
222                         OnWindowActivated)
223     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_EdgeGesture, OnEdgeGesture)
224     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_TouchDown,
225                         OnTouchDown)
226     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_TouchUp,
227                         OnTouchUp)
228     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_TouchMoved,
229                         OnTouchMoved)
230     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_FileSaveAsDone,
231                         OnFileSaveAsDone)
232     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_FileOpenDone,
233                         OnFileOpenDone)
234     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_MultiFileOpenDone,
235                         OnMultiFileOpenDone)
236     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_SelectFolderDone,
237                         OnSelectFolderDone)
238     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_SetCursorPosAck,
239                         OnSetCursorPosAck)
240     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_ImeCandidatePopupChanged,
241                         OnImeCandidatePopupChanged)
242     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_ImeCompositionChanged,
243                         OnImeCompositionChanged)
244     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_ImeTextCommitted,
245                         OnImeTextCommitted)
246     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_ImeInputSourceChanged,
247                         OnImeInputSourceChanged)
248     IPC_MESSAGE_UNHANDLED(handled = false)
249   IPC_END_MESSAGE_MAP()
250   return handled;
251 }
252
253 void RemoteWindowTreeHostWin::HandleOpenURLOnDesktop(
254     const base::FilePath& shortcut,
255     const base::string16& url) {
256   if (!host_)
257     return;
258   host_->Send(new MetroViewerHostMsg_OpenURLOnDesktop(shortcut, url));
259 }
260
261 void RemoteWindowTreeHostWin::HandleActivateDesktop(
262     const base::FilePath& shortcut,
263     bool ash_exit) {
264   if (!host_)
265     return;
266   host_->Send(new MetroViewerHostMsg_ActivateDesktop(shortcut, ash_exit));
267 }
268
269 void RemoteWindowTreeHostWin::HandleMetroExit() {
270   if (!host_)
271     return;
272   host_->Send(new MetroViewerHostMsg_MetroExit());
273 }
274
275 void RemoteWindowTreeHostWin::HandleOpenFile(
276     const base::string16& title,
277     const base::FilePath& default_path,
278     const base::string16& filter,
279     const OpenFileCompletion& on_success,
280     const FileSelectionCanceled& on_failure) {
281   if (!host_)
282     return;
283
284   // Can only have one of these operations in flight.
285   DCHECK(file_open_completion_callback_.is_null());
286   DCHECK(failure_callback_.is_null());
287
288   file_open_completion_callback_ = on_success;
289   failure_callback_ = on_failure;
290
291   host_->Send(new MetroViewerHostMsg_DisplayFileOpen(title,
292                                                      filter,
293                                                      default_path,
294                                                      false));
295 }
296
297 void RemoteWindowTreeHostWin::HandleOpenMultipleFiles(
298     const base::string16& title,
299     const base::FilePath& default_path,
300     const base::string16& filter,
301     const OpenMultipleFilesCompletion& on_success,
302     const FileSelectionCanceled& on_failure) {
303   if (!host_)
304     return;
305
306   // Can only have one of these operations in flight.
307   DCHECK(multi_file_open_completion_callback_.is_null());
308   DCHECK(failure_callback_.is_null());
309   multi_file_open_completion_callback_ = on_success;
310   failure_callback_ = on_failure;
311
312   host_->Send(new MetroViewerHostMsg_DisplayFileOpen(title,
313                                                      filter,
314                                                      default_path,
315                                                      true));
316 }
317
318 void RemoteWindowTreeHostWin::HandleSaveFile(
319     const base::string16& title,
320     const base::FilePath& default_path,
321     const base::string16& filter,
322     int filter_index,
323     const base::string16& default_extension,
324     const SaveFileCompletion& on_success,
325     const FileSelectionCanceled& on_failure) {
326   if (!host_)
327     return;
328
329   MetroViewerHostMsg_SaveAsDialogParams params;
330   params.title = title;
331   params.default_extension = default_extension;
332   params.filter = filter;
333   params.filter_index = filter_index;
334   params.suggested_name = default_path;
335
336   // Can only have one of these operations in flight.
337   DCHECK(file_saveas_completion_callback_.is_null());
338   DCHECK(failure_callback_.is_null());
339   file_saveas_completion_callback_ = on_success;
340   failure_callback_ = on_failure;
341
342   host_->Send(new MetroViewerHostMsg_DisplayFileSaveAs(params));
343 }
344
345 void RemoteWindowTreeHostWin::HandleSelectFolder(
346     const base::string16& title,
347     const SelectFolderCompletion& on_success,
348     const FileSelectionCanceled& on_failure) {
349   if (!host_)
350     return;
351
352   // Can only have one of these operations in flight.
353   DCHECK(select_folder_completion_callback_.is_null());
354   DCHECK(failure_callback_.is_null());
355   select_folder_completion_callback_ = on_success;
356   failure_callback_ = on_failure;
357
358   host_->Send(new MetroViewerHostMsg_DisplaySelectFolder(title));
359 }
360
361 void RemoteWindowTreeHostWin::HandleWindowSizeChanged(uint32 width,
362                                                       uint32 height) {
363   SetBounds(gfx::Rect(0, 0, width, height));
364 }
365
366 bool RemoteWindowTreeHostWin::IsForegroundWindow() {
367   return ::GetForegroundWindow() == remote_window_;
368 }
369
370 Window* RemoteWindowTreeHostWin::GetAshWindow() {
371   return GetRootWindow()->window();
372 }
373
374 RootWindow* RemoteWindowTreeHostWin::GetRootWindow() {
375   return delegate_->AsRootWindow();
376 }
377
378 gfx::AcceleratedWidget RemoteWindowTreeHostWin::GetAcceleratedWidget() {
379   if (remote_window_)
380     return remote_window_;
381   // Getting here should only happen for ash_unittests.exe and related code.
382   return ::GetDesktopWindow();
383 }
384
385 void RemoteWindowTreeHostWin::Show() {
386   ui::RemoteInputMethodPrivateWin* remote_input_method_private =
387       GetRemoteInputMethodPrivate();
388   if (remote_input_method_private)
389     remote_input_method_private->SetRemoteDelegate(this);
390 }
391
392 void RemoteWindowTreeHostWin::Hide() {
393   NOTIMPLEMENTED();
394 }
395
396 void RemoteWindowTreeHostWin::ToggleFullScreen() {
397 }
398
399 gfx::Rect RemoteWindowTreeHostWin::GetBounds() const {
400   return gfx::Rect(window_size_);
401 }
402
403 void RemoteWindowTreeHostWin::SetBounds(const gfx::Rect& bounds) {
404   window_size_ = bounds.size();
405   NotifyHostResized(bounds.size());
406 }
407
408 gfx::Insets RemoteWindowTreeHostWin::GetInsets() const {
409   return gfx::Insets();
410 }
411
412 void RemoteWindowTreeHostWin::SetInsets(const gfx::Insets& insets) {
413 }
414
415 gfx::Point RemoteWindowTreeHostWin::GetLocationOnNativeScreen() const {
416   return gfx::Point(0, 0);
417 }
418
419 void RemoteWindowTreeHostWin::SetCapture() {
420 }
421
422 void RemoteWindowTreeHostWin::ReleaseCapture() {
423 }
424
425 bool RemoteWindowTreeHostWin::QueryMouseLocation(gfx::Point* location_return) {
426   aura::client::CursorClient* cursor_client =
427       aura::client::GetCursorClient(GetRootWindow()->window());
428   if (cursor_client && !cursor_client->IsMouseEventsEnabled()) {
429     *location_return = gfx::Point(0, 0);
430     return false;
431   }
432   POINT pt;
433   GetCursorPos(&pt);
434   *location_return =
435       gfx::Point(static_cast<int>(pt.x), static_cast<int>(pt.y));
436   return true;
437 }
438
439 bool RemoteWindowTreeHostWin::ConfineCursorToRootWindow() {
440   return true;
441 }
442
443 void RemoteWindowTreeHostWin::UnConfineCursor() {
444 }
445
446 void RemoteWindowTreeHostWin::SetCursorNative(gfx::NativeCursor native_cursor) {
447   if (!host_)
448     return;
449   host_->Send(
450     new MetroViewerHostMsg_SetCursor(uint64(native_cursor.platform())));
451 }
452
453 void RemoteWindowTreeHostWin::MoveCursorToNative(const gfx::Point& location) {
454   VLOG(1) << "In MoveCursorTo: " << location.x() << ", " << location.y();
455   if (!host_)
456     return;
457
458   // This function can be called in cases like when the mouse cursor is
459   // restricted within a viewport (For e.g. LockCursor) which assumes that
460   // subsequent mouse moves would be received starting with the new cursor
461   // coordinates. This is a challenge for Windows ASH for the reasons
462   // outlined below.
463   // Other cases which don't expect this behavior should continue to work
464   // without issues.
465
466   // The mouse events are received by the viewer process and sent to the
467   // browser. If we invoke the SetCursor API here we continue to receive
468   // mouse messages from the viewer which were posted before the SetCursor
469   // API executes which messes up the state in the browser. To workaround
470   // this we invoke the SetCursor API in the viewer process and ignore
471   // mouse messages until we received an ACK from the viewer indicating that
472   // the SetCursor operation completed.
473   ignore_mouse_moves_until_set_cursor_ack_ = true;
474   VLOG(1) << "In MoveCursorTo. Sending IPC";
475   host_->Send(new MetroViewerHostMsg_SetCursorPos(location.x(), location.y()));
476 }
477
478 void RemoteWindowTreeHostWin::OnCursorVisibilityChangedNative(bool show) {
479   NOTIMPLEMENTED();
480 }
481
482 void RemoteWindowTreeHostWin::PostNativeEvent(
483     const base::NativeEvent& native_event) {
484 }
485
486 void RemoteWindowTreeHostWin::OnDeviceScaleFactorChanged(
487     float device_scale_factor) {
488   NOTIMPLEMENTED();
489 }
490
491 void RemoteWindowTreeHostWin::PrepareForShutdown() {
492 }
493
494 ui::EventProcessor* RemoteWindowTreeHostWin::GetEventProcessor() {
495   return delegate_->GetEventProcessor();
496 }
497
498 void RemoteWindowTreeHostWin::CancelComposition() {
499   if (!host_)
500     return;
501   host_->Send(new MetroViewerHostMsg_ImeCancelComposition);
502 }
503
504 void RemoteWindowTreeHostWin::OnTextInputClientUpdated(
505     const std::vector<int32>& input_scopes,
506     const std::vector<gfx::Rect>& composition_character_bounds) {
507   if (!host_)
508     return;
509   std::vector<metro_viewer::CharacterBounds> character_bounds;
510   for (size_t i = 0; i < composition_character_bounds.size(); ++i) {
511     const gfx::Rect& rect = composition_character_bounds[i];
512     metro_viewer::CharacterBounds bounds;
513     bounds.left = rect.x();
514     bounds.top = rect.y();
515     bounds.right = rect.right();
516     bounds.bottom = rect.bottom();
517     character_bounds.push_back(bounds);
518   }
519   host_->Send(new MetroViewerHostMsg_ImeTextInputClientUpdated(
520       input_scopes, character_bounds));
521 }
522
523 gfx::Point PointFromNativeEvent(int32 x, int32 y) {
524   static float scale_factor = gfx::GetModernUIScale();
525   gfx::Point result( x * scale_factor, y * scale_factor);
526   return result;
527 }
528
529 void RemoteWindowTreeHostWin::OnMouseMoved(int32 x, int32 y, int32 flags) {
530   if (ignore_mouse_moves_until_set_cursor_ack_)
531     return;
532
533   gfx::Point location = PointFromNativeEvent(x, y);
534   ui::MouseEvent event(ui::ET_MOUSE_MOVED, location, location, flags, 0);
535   SendEventToProcessor(&event);
536 }
537
538 void RemoteWindowTreeHostWin::OnMouseButton(
539     const MetroViewerHostMsg_MouseButtonParams& params) {
540   gfx::Point location = PointFromNativeEvent(params.x, params.y);
541   ui::MouseEvent mouse_event(params.event_type, location, location,
542                              static_cast<int>(params.flags),
543                              static_cast<int>(params.changed_button));
544
545   SetEventFlags(params.flags | key_event_flags());
546   if (params.event_type == ui::ET_MOUSEWHEEL) {
547     int x_offset = params.is_horizontal_wheel ? params.extra : 0;
548     int y_offset = !params.is_horizontal_wheel ? params.extra : 0;
549     ui::MouseWheelEvent wheel_event(mouse_event, x_offset, y_offset);
550     SendEventToProcessor(&wheel_event);
551   } else if (params.event_type == ui::ET_MOUSE_PRESSED) {
552     // TODO(shrikant): Ideally modify code in event.cc by adding automatic
553     // tracking of double clicks in synthetic MouseEvent constructor code.
554     // Non-synthetic MouseEvent constructor code does automatically track
555     // this. Need to use some caution while modifying synthetic constructor
556     // as many tests and other code paths depend on it and apparently
557     // specifically depend on non implicit tracking of previous mouse event.
558     if (last_mouse_click_event_ &&
559         ui::MouseEvent::IsRepeatedClickEvent(mouse_event,
560                                              *last_mouse_click_event_)) {
561       mouse_event.SetClickCount(2);
562     } else {
563       mouse_event.SetClickCount(1);
564     }
565     last_mouse_click_event_ .reset(new ui::MouseEvent(mouse_event));
566     SendEventToProcessor(&mouse_event);
567   } else {
568     SendEventToProcessor(&mouse_event);
569   }
570 }
571
572 void RemoteWindowTreeHostWin::OnKeyDown(uint32 vkey,
573                                         uint32 repeat_count,
574                                         uint32 scan_code,
575                                         uint32 flags) {
576   DispatchKeyboardMessage(ui::ET_KEY_PRESSED, vkey, repeat_count, scan_code,
577                           flags, false);
578 }
579
580 void RemoteWindowTreeHostWin::OnKeyUp(uint32 vkey,
581                                       uint32 repeat_count,
582                                       uint32 scan_code,
583                                       uint32 flags) {
584   DispatchKeyboardMessage(ui::ET_KEY_RELEASED, vkey, repeat_count, scan_code,
585                           flags, false);
586 }
587
588 void RemoteWindowTreeHostWin::OnChar(uint32 key_code,
589                                      uint32 repeat_count,
590                                      uint32 scan_code,
591                                      uint32 flags) {
592   DispatchKeyboardMessage(ui::ET_KEY_PRESSED, key_code, repeat_count,
593                           scan_code, flags, true);
594 }
595
596 void RemoteWindowTreeHostWin::OnWindowActivated() {
597   delegate_->OnHostActivated();
598 }
599
600 void RemoteWindowTreeHostWin::OnEdgeGesture() {
601   ui::GestureEvent event(
602       ui::ET_GESTURE_WIN8_EDGE_SWIPE,
603       0,
604       0,
605       0,
606       ui::EventTimeForNow(),
607       ui::GestureEventDetails(ui::ET_GESTURE_WIN8_EDGE_SWIPE, 0, 0),
608       0);
609   SendEventToProcessor(&event);
610 }
611
612 void RemoteWindowTreeHostWin::OnTouchDown(int32 x,
613                                           int32 y,
614                                           uint64 timestamp,
615                                           uint32 pointer_id) {
616   gfx::Point location = PointFromNativeEvent(x, y);
617   ui::TouchEvent event(ui::ET_TOUCH_PRESSED,
618                        location,
619                        pointer_id,
620                        base::TimeDelta::FromMicroseconds(timestamp));
621   SendEventToProcessor(&event);
622 }
623
624 void RemoteWindowTreeHostWin::OnTouchUp(int32 x,
625                                         int32 y,
626                                         uint64 timestamp,
627                                         uint32 pointer_id) {
628   gfx::Point location = PointFromNativeEvent(x, y);
629   ui::TouchEvent event(ui::ET_TOUCH_RELEASED,
630                        location,
631                        pointer_id,
632                        base::TimeDelta::FromMicroseconds(timestamp));
633   SendEventToProcessor(&event);
634 }
635
636 void RemoteWindowTreeHostWin::OnTouchMoved(int32 x,
637                                            int32 y,
638                                            uint64 timestamp,
639                                            uint32 pointer_id) {
640   gfx::Point location = PointFromNativeEvent(x, y);
641   ui::TouchEvent event(ui::ET_TOUCH_MOVED,
642                        location,
643                        pointer_id,
644                        base::TimeDelta::FromMicroseconds(timestamp));
645   SendEventToProcessor(&event);
646 }
647
648 void RemoteWindowTreeHostWin::OnFileSaveAsDone(bool success,
649                                                const base::FilePath& filename,
650                                                int filter_index) {
651   if (success)
652     file_saveas_completion_callback_.Run(filename, filter_index, NULL);
653   else
654     failure_callback_.Run(NULL);
655   file_saveas_completion_callback_.Reset();
656   failure_callback_.Reset();
657 }
658
659
660 void RemoteWindowTreeHostWin::OnFileOpenDone(bool success,
661                                              const base::FilePath& filename) {
662   if (success)
663     file_open_completion_callback_.Run(base::FilePath(filename), 0, NULL);
664   else
665     failure_callback_.Run(NULL);
666   file_open_completion_callback_.Reset();
667   failure_callback_.Reset();
668 }
669
670 void RemoteWindowTreeHostWin::OnMultiFileOpenDone(
671     bool success,
672     const std::vector<base::FilePath>& files) {
673   if (success)
674     multi_file_open_completion_callback_.Run(files, NULL);
675   else
676     failure_callback_.Run(NULL);
677   multi_file_open_completion_callback_.Reset();
678   failure_callback_.Reset();
679 }
680
681 void RemoteWindowTreeHostWin::OnSelectFolderDone(
682     bool success,
683     const base::FilePath& folder) {
684   if (success)
685     select_folder_completion_callback_.Run(base::FilePath(folder), 0, NULL);
686   else
687     failure_callback_.Run(NULL);
688   select_folder_completion_callback_.Reset();
689   failure_callback_.Reset();
690 }
691
692 void RemoteWindowTreeHostWin::OnSetCursorPosAck() {
693   DCHECK(ignore_mouse_moves_until_set_cursor_ack_);
694   ignore_mouse_moves_until_set_cursor_ack_ = false;
695 }
696
697 ui::RemoteInputMethodPrivateWin*
698 RemoteWindowTreeHostWin::GetRemoteInputMethodPrivate() {
699   ui::InputMethod* input_method = GetAshWindow()->GetProperty(
700       aura::client::kRootWindowInputMethodKey);
701   return ui::RemoteInputMethodPrivateWin::Get(input_method);
702 }
703
704 void RemoteWindowTreeHostWin::OnImeCandidatePopupChanged(bool visible) {
705   ui::RemoteInputMethodPrivateWin* remote_input_method_private =
706       GetRemoteInputMethodPrivate();
707   if (!remote_input_method_private)
708     return;
709   remote_input_method_private->OnCandidatePopupChanged(visible);
710 }
711
712 void RemoteWindowTreeHostWin::OnImeCompositionChanged(
713     const base::string16& text,
714     int32 selection_start,
715     int32 selection_end,
716     const std::vector<metro_viewer::UnderlineInfo>& underlines) {
717   ui::RemoteInputMethodPrivateWin* remote_input_method_private =
718       GetRemoteInputMethodPrivate();
719   if (!remote_input_method_private)
720     return;
721   ui::CompositionText composition_text;
722   FillCompositionText(
723       text, selection_start, selection_end, underlines, &composition_text);
724   remote_input_method_private->OnCompositionChanged(composition_text);
725 }
726
727 void RemoteWindowTreeHostWin::OnImeTextCommitted(const base::string16& text) {
728   ui::RemoteInputMethodPrivateWin* remote_input_method_private =
729       GetRemoteInputMethodPrivate();
730   if (!remote_input_method_private)
731     return;
732   remote_input_method_private->OnTextCommitted(text);
733 }
734
735 void RemoteWindowTreeHostWin::OnImeInputSourceChanged(uint16 language_id,
736                                                       bool is_ime) {
737   ui::RemoteInputMethodPrivateWin* remote_input_method_private =
738       GetRemoteInputMethodPrivate();
739   if (!remote_input_method_private)
740     return;
741   remote_input_method_private->OnInputSourceChanged(language_id, is_ime);
742 }
743
744 void RemoteWindowTreeHostWin::DispatchKeyboardMessage(ui::EventType type,
745                                                       uint32 vkey,
746                                                       uint32 repeat_count,
747                                                       uint32 scan_code,
748                                                       uint32 flags,
749                                                       bool is_character) {
750   SetEventFlags(flags | mouse_event_flags());
751   if (base::MessageLoop::current()->IsNested()) {
752     int index = (flags & ui::EF_ALT_DOWN) ? 1 : 0;
753     const int char_message[] = {WM_CHAR, WM_SYSCHAR};
754     const int keydown_message[] = {WM_KEYDOWN, WM_SYSKEYDOWN};
755     const int keyup_message[] = {WM_KEYUP, WM_SYSKEYUP};
756     uint32 message = is_character
757                          ? char_message[index]
758                          : (type == ui::ET_KEY_PRESSED ? keydown_message[index]
759                                                        : keyup_message[index]);
760     ::PostThreadMessage(::GetCurrentThreadId(),
761                         message,
762                         vkey,
763                         repeat_count | scan_code >> 15);
764   } else {
765     ui::KeyEvent event(type,
766                        ui::KeyboardCodeForWindowsKeyCode(vkey),
767                        flags,
768                        is_character);
769     SendEventToProcessor(&event);
770   }
771 }
772
773 void RemoteWindowTreeHostWin::SetEventFlags(uint32 flags) {
774   if (flags == event_flags_)
775     return;
776   event_flags_ = flags;
777   SetVirtualKeyStates(event_flags_);
778 }
779
780 }  // namespace aura