Upstream version 7.36.149.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/window_event_dispatcher.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 // static
153 RemoteWindowTreeHostWin* RemoteWindowTreeHostWin::Instance() {
154   if (!g_instance)
155     g_instance = new RemoteWindowTreeHostWin(gfx::Rect());
156   return g_instance;
157 }
158
159 // static
160 void RemoteWindowTreeHostWin::SetInstance(RemoteWindowTreeHostWin* instance) {
161   CHECK(!g_instance);
162   g_instance = instance;
163 }
164
165 RemoteWindowTreeHostWin::RemoteWindowTreeHostWin(const gfx::Rect& bounds)
166     : remote_window_(NULL),
167       host_(NULL),
168       ignore_mouse_moves_until_set_cursor_ack_(false),
169       event_flags_(0),
170       window_size_(aura::WindowTreeHost::GetNativeScreenSize()) {
171   prop_.reset(new ui::ViewProp(NULL, kWindowTreeHostWinKey, this));
172   CreateCompositor(GetAcceleratedWidget());
173 }
174
175 RemoteWindowTreeHostWin::~RemoteWindowTreeHostWin() {
176   DestroyCompositor();
177   DestroyDispatcher();
178   g_instance = NULL;
179 }
180
181 // static
182 bool RemoteWindowTreeHostWin::IsValid() {
183   return Instance()->remote_window_ != NULL;
184 }
185
186 void RemoteWindowTreeHostWin::SetRemoteWindowHandle(HWND remote_window) {
187   remote_window_ = remote_window;
188   // Do not create compositor here, but in Connected() below.
189   // See http://crbug.com/330179 and http://crbug.com/334380.
190 }
191
192 void RemoteWindowTreeHostWin::Connected(IPC::Sender* host) {
193   CHECK(host_ == NULL);
194   DCHECK(remote_window_);
195   host_ = host;
196   // Recreate the compositor for the target surface represented by the
197   // remote_window HWND.
198   CreateCompositor(remote_window_);
199   InitCompositor();
200 }
201
202 void RemoteWindowTreeHostWin::Disconnected() {
203   // Don't CHECK here, Disconnected is called on a channel error which can
204   // happen before we're successfully Connected.
205   if (!host_)
206     return;
207   ui::RemoteInputMethodPrivateWin* remote_input_method_private =
208       GetRemoteInputMethodPrivate();
209   if (remote_input_method_private)
210     remote_input_method_private->SetRemoteDelegate(NULL);
211   host_ = NULL;
212   remote_window_ = NULL;
213 }
214
215 bool RemoteWindowTreeHostWin::OnMessageReceived(const IPC::Message& message) {
216   bool handled = true;
217   IPC_BEGIN_MESSAGE_MAP(RemoteWindowTreeHostWin, message)
218     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_MouseMoved, OnMouseMoved)
219     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_MouseButton, OnMouseButton)
220     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_KeyDown, OnKeyDown)
221     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_KeyUp, OnKeyUp)
222     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_Character, OnChar)
223     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_WindowActivated,
224                         OnWindowActivated)
225     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_EdgeGesture, OnEdgeGesture)
226     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_TouchDown,
227                         OnTouchDown)
228     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_TouchUp,
229                         OnTouchUp)
230     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_TouchMoved,
231                         OnTouchMoved)
232     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_FileSaveAsDone,
233                         OnFileSaveAsDone)
234     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_FileOpenDone,
235                         OnFileOpenDone)
236     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_MultiFileOpenDone,
237                         OnMultiFileOpenDone)
238     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_SelectFolderDone,
239                         OnSelectFolderDone)
240     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_SetCursorPosAck,
241                         OnSetCursorPosAck)
242     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_ImeCandidatePopupChanged,
243                         OnImeCandidatePopupChanged)
244     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_ImeCompositionChanged,
245                         OnImeCompositionChanged)
246     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_ImeTextCommitted,
247                         OnImeTextCommitted)
248     IPC_MESSAGE_HANDLER(MetroViewerHostMsg_ImeInputSourceChanged,
249                         OnImeInputSourceChanged)
250     IPC_MESSAGE_UNHANDLED(handled = false)
251   IPC_END_MESSAGE_MAP()
252   return handled;
253 }
254
255 void RemoteWindowTreeHostWin::HandleOpenURLOnDesktop(
256     const base::FilePath& shortcut,
257     const base::string16& url) {
258   if (!host_)
259     return;
260   host_->Send(new MetroViewerHostMsg_OpenURLOnDesktop(shortcut, url));
261 }
262
263 void RemoteWindowTreeHostWin::HandleActivateDesktop(
264     const base::FilePath& shortcut,
265     bool ash_exit) {
266   if (!host_)
267     return;
268   host_->Send(new MetroViewerHostMsg_ActivateDesktop(shortcut, ash_exit));
269 }
270
271 void RemoteWindowTreeHostWin::HandleMetroExit() {
272   if (!host_)
273     return;
274   host_->Send(new MetroViewerHostMsg_MetroExit());
275 }
276
277 void RemoteWindowTreeHostWin::HandleOpenFile(
278     const base::string16& title,
279     const base::FilePath& default_path,
280     const base::string16& filter,
281     const OpenFileCompletion& on_success,
282     const FileSelectionCanceled& on_failure) {
283   if (!host_)
284     return;
285
286   // Can only have one of these operations in flight.
287   DCHECK(file_open_completion_callback_.is_null());
288   DCHECK(failure_callback_.is_null());
289
290   file_open_completion_callback_ = on_success;
291   failure_callback_ = on_failure;
292
293   host_->Send(new MetroViewerHostMsg_DisplayFileOpen(title,
294                                                      filter,
295                                                      default_path,
296                                                      false));
297 }
298
299 void RemoteWindowTreeHostWin::HandleOpenMultipleFiles(
300     const base::string16& title,
301     const base::FilePath& default_path,
302     const base::string16& filter,
303     const OpenMultipleFilesCompletion& on_success,
304     const FileSelectionCanceled& on_failure) {
305   if (!host_)
306     return;
307
308   // Can only have one of these operations in flight.
309   DCHECK(multi_file_open_completion_callback_.is_null());
310   DCHECK(failure_callback_.is_null());
311   multi_file_open_completion_callback_ = on_success;
312   failure_callback_ = on_failure;
313
314   host_->Send(new MetroViewerHostMsg_DisplayFileOpen(title,
315                                                      filter,
316                                                      default_path,
317                                                      true));
318 }
319
320 void RemoteWindowTreeHostWin::HandleSaveFile(
321     const base::string16& title,
322     const base::FilePath& default_path,
323     const base::string16& filter,
324     int filter_index,
325     const base::string16& default_extension,
326     const SaveFileCompletion& on_success,
327     const FileSelectionCanceled& on_failure) {
328   if (!host_)
329     return;
330
331   MetroViewerHostMsg_SaveAsDialogParams params;
332   params.title = title;
333   params.default_extension = default_extension;
334   params.filter = filter;
335   params.filter_index = filter_index;
336   params.suggested_name = default_path;
337
338   // Can only have one of these operations in flight.
339   DCHECK(file_saveas_completion_callback_.is_null());
340   DCHECK(failure_callback_.is_null());
341   file_saveas_completion_callback_ = on_success;
342   failure_callback_ = on_failure;
343
344   host_->Send(new MetroViewerHostMsg_DisplayFileSaveAs(params));
345 }
346
347 void RemoteWindowTreeHostWin::HandleSelectFolder(
348     const base::string16& title,
349     const SelectFolderCompletion& on_success,
350     const FileSelectionCanceled& on_failure) {
351   if (!host_)
352     return;
353
354   // Can only have one of these operations in flight.
355   DCHECK(select_folder_completion_callback_.is_null());
356   DCHECK(failure_callback_.is_null());
357   select_folder_completion_callback_ = on_success;
358   failure_callback_ = on_failure;
359
360   host_->Send(new MetroViewerHostMsg_DisplaySelectFolder(title));
361 }
362
363 void RemoteWindowTreeHostWin::HandleWindowSizeChanged(uint32 width,
364                                                       uint32 height) {
365   SetBounds(gfx::Rect(0, 0, width, height));
366 }
367
368 bool RemoteWindowTreeHostWin::IsForegroundWindow() {
369   return ::GetForegroundWindow() == remote_window_;
370 }
371
372 Window* RemoteWindowTreeHostWin::GetAshWindow() {
373   return window();
374 }
375
376 ui::EventSource* RemoteWindowTreeHostWin::GetEventSource() {
377   return this;
378 }
379
380 gfx::AcceleratedWidget RemoteWindowTreeHostWin::GetAcceleratedWidget() {
381   if (remote_window_)
382     return remote_window_;
383   // Getting here should only happen for ash_unittests.exe and related code.
384   return ::GetDesktopWindow();
385 }
386
387 void RemoteWindowTreeHostWin::Show() {
388   ui::RemoteInputMethodPrivateWin* remote_input_method_private =
389       GetRemoteInputMethodPrivate();
390   if (remote_input_method_private)
391     remote_input_method_private->SetRemoteDelegate(this);
392 }
393
394 void RemoteWindowTreeHostWin::Hide() {
395   NOTIMPLEMENTED();
396 }
397
398 gfx::Rect RemoteWindowTreeHostWin::GetBounds() const {
399   return gfx::Rect(window_size_);
400 }
401
402 void RemoteWindowTreeHostWin::SetBounds(const gfx::Rect& bounds) {
403   window_size_ = bounds.size();
404   OnHostResized(bounds.size());
405 }
406
407 gfx::Point RemoteWindowTreeHostWin::GetLocationOnNativeScreen() const {
408   return gfx::Point(0, 0);
409 }
410
411 void RemoteWindowTreeHostWin::SetCapture() {
412 }
413
414 void RemoteWindowTreeHostWin::ReleaseCapture() {
415 }
416
417 void RemoteWindowTreeHostWin::SetCursorNative(gfx::NativeCursor native_cursor) {
418   if (!host_)
419     return;
420   host_->Send(
421     new MetroViewerHostMsg_SetCursor(uint64(native_cursor.platform())));
422 }
423
424 void RemoteWindowTreeHostWin::MoveCursorToNative(const gfx::Point& location) {
425   VLOG(1) << "In MoveCursorTo: " << location.x() << ", " << location.y();
426   if (!host_)
427     return;
428
429   // This function can be called in cases like when the mouse cursor is
430   // restricted within a viewport (For e.g. LockCursor) which assumes that
431   // subsequent mouse moves would be received starting with the new cursor
432   // coordinates. This is a challenge for Windows ASH for the reasons
433   // outlined below.
434   // Other cases which don't expect this behavior should continue to work
435   // without issues.
436
437   // The mouse events are received by the viewer process and sent to the
438   // browser. If we invoke the SetCursor API here we continue to receive
439   // mouse messages from the viewer which were posted before the SetCursor
440   // API executes which messes up the state in the browser. To workaround
441   // this we invoke the SetCursor API in the viewer process and ignore
442   // mouse messages until we received an ACK from the viewer indicating that
443   // the SetCursor operation completed.
444   ignore_mouse_moves_until_set_cursor_ack_ = true;
445   VLOG(1) << "In MoveCursorTo. Sending IPC";
446   host_->Send(new MetroViewerHostMsg_SetCursorPos(location.x(), location.y()));
447 }
448
449 void RemoteWindowTreeHostWin::OnCursorVisibilityChangedNative(bool show) {
450   NOTIMPLEMENTED();
451 }
452
453 void RemoteWindowTreeHostWin::PostNativeEvent(
454     const base::NativeEvent& native_event) {
455 }
456
457 void RemoteWindowTreeHostWin::OnDeviceScaleFactorChanged(
458     float device_scale_factor) {
459   NOTIMPLEMENTED();
460 }
461
462 ui::EventProcessor* RemoteWindowTreeHostWin::GetEventProcessor() {
463   return dispatcher();
464 }
465
466 void RemoteWindowTreeHostWin::CancelComposition() {
467   if (!host_)
468     return;
469   host_->Send(new MetroViewerHostMsg_ImeCancelComposition);
470 }
471
472 void RemoteWindowTreeHostWin::OnTextInputClientUpdated(
473     const std::vector<int32>& input_scopes,
474     const std::vector<gfx::Rect>& composition_character_bounds) {
475   if (!host_)
476     return;
477   std::vector<metro_viewer::CharacterBounds> character_bounds;
478   for (size_t i = 0; i < composition_character_bounds.size(); ++i) {
479     const gfx::Rect& rect = composition_character_bounds[i];
480     metro_viewer::CharacterBounds bounds;
481     bounds.left = rect.x();
482     bounds.top = rect.y();
483     bounds.right = rect.right();
484     bounds.bottom = rect.bottom();
485     character_bounds.push_back(bounds);
486   }
487   host_->Send(new MetroViewerHostMsg_ImeTextInputClientUpdated(
488       input_scopes, character_bounds));
489 }
490
491 gfx::Point PointFromNativeEvent(int32 x, int32 y) {
492   static float scale_factor = gfx::GetModernUIScale();
493   gfx::Point result( x * scale_factor, y * scale_factor);
494   return result;
495 }
496
497 void RemoteWindowTreeHostWin::OnMouseMoved(int32 x, int32 y, int32 flags) {
498   if (ignore_mouse_moves_until_set_cursor_ack_)
499     return;
500
501   gfx::Point location = PointFromNativeEvent(x, y);
502   ui::MouseEvent event(ui::ET_MOUSE_MOVED, location, location, flags, 0);
503   SendEventToProcessor(&event);
504 }
505
506 void RemoteWindowTreeHostWin::OnMouseButton(
507     const MetroViewerHostMsg_MouseButtonParams& params) {
508   gfx::Point location = PointFromNativeEvent(params.x, params.y);
509   ui::MouseEvent mouse_event(params.event_type, location, location,
510                              static_cast<int>(params.flags),
511                              static_cast<int>(params.changed_button));
512
513   SetEventFlags(params.flags | key_event_flags());
514   if (params.event_type == ui::ET_MOUSEWHEEL) {
515     int x_offset = params.is_horizontal_wheel ? params.extra : 0;
516     int y_offset = !params.is_horizontal_wheel ? params.extra : 0;
517     ui::MouseWheelEvent wheel_event(mouse_event, x_offset, y_offset);
518     SendEventToProcessor(&wheel_event);
519   } else if (params.event_type == ui::ET_MOUSE_PRESSED) {
520     // TODO(shrikant): Ideally modify code in event.cc by adding automatic
521     // tracking of double clicks in synthetic MouseEvent constructor code.
522     // Non-synthetic MouseEvent constructor code does automatically track
523     // this. Need to use some caution while modifying synthetic constructor
524     // as many tests and other code paths depend on it and apparently
525     // specifically depend on non implicit tracking of previous mouse event.
526     if (last_mouse_click_event_ &&
527         ui::MouseEvent::IsRepeatedClickEvent(mouse_event,
528                                              *last_mouse_click_event_)) {
529       mouse_event.SetClickCount(2);
530     } else {
531       mouse_event.SetClickCount(1);
532     }
533     last_mouse_click_event_ .reset(new ui::MouseEvent(mouse_event));
534     SendEventToProcessor(&mouse_event);
535   } else {
536     SendEventToProcessor(&mouse_event);
537   }
538 }
539
540 void RemoteWindowTreeHostWin::OnKeyDown(uint32 vkey,
541                                         uint32 repeat_count,
542                                         uint32 scan_code,
543                                         uint32 flags) {
544   DispatchKeyboardMessage(ui::ET_KEY_PRESSED, vkey, repeat_count, scan_code,
545                           flags, false);
546 }
547
548 void RemoteWindowTreeHostWin::OnKeyUp(uint32 vkey,
549                                       uint32 repeat_count,
550                                       uint32 scan_code,
551                                       uint32 flags) {
552   DispatchKeyboardMessage(ui::ET_KEY_RELEASED, vkey, repeat_count, scan_code,
553                           flags, false);
554 }
555
556 void RemoteWindowTreeHostWin::OnChar(uint32 key_code,
557                                      uint32 repeat_count,
558                                      uint32 scan_code,
559                                      uint32 flags) {
560   DispatchKeyboardMessage(ui::ET_KEY_PRESSED, key_code, repeat_count,
561                           scan_code, flags, true);
562 }
563
564 void RemoteWindowTreeHostWin::OnWindowActivated() {
565   OnHostActivated();
566 }
567
568 void RemoteWindowTreeHostWin::OnEdgeGesture() {
569   ui::GestureEvent event(
570       ui::ET_GESTURE_WIN8_EDGE_SWIPE,
571       0,
572       0,
573       0,
574       ui::EventTimeForNow(),
575       ui::GestureEventDetails(ui::ET_GESTURE_WIN8_EDGE_SWIPE, 0, 0),
576       0);
577   SendEventToProcessor(&event);
578 }
579
580 void RemoteWindowTreeHostWin::OnTouchDown(int32 x,
581                                           int32 y,
582                                           uint64 timestamp,
583                                           uint32 pointer_id) {
584   gfx::Point location = PointFromNativeEvent(x, y);
585   ui::TouchEvent event(ui::ET_TOUCH_PRESSED,
586                        location,
587                        pointer_id,
588                        base::TimeDelta::FromMicroseconds(timestamp));
589   SendEventToProcessor(&event);
590 }
591
592 void RemoteWindowTreeHostWin::OnTouchUp(int32 x,
593                                         int32 y,
594                                         uint64 timestamp,
595                                         uint32 pointer_id) {
596   gfx::Point location = PointFromNativeEvent(x, y);
597   ui::TouchEvent event(ui::ET_TOUCH_RELEASED,
598                        location,
599                        pointer_id,
600                        base::TimeDelta::FromMicroseconds(timestamp));
601   SendEventToProcessor(&event);
602 }
603
604 void RemoteWindowTreeHostWin::OnTouchMoved(int32 x,
605                                            int32 y,
606                                            uint64 timestamp,
607                                            uint32 pointer_id) {
608   gfx::Point location = PointFromNativeEvent(x, y);
609   ui::TouchEvent event(ui::ET_TOUCH_MOVED,
610                        location,
611                        pointer_id,
612                        base::TimeDelta::FromMicroseconds(timestamp));
613   SendEventToProcessor(&event);
614 }
615
616 void RemoteWindowTreeHostWin::OnFileSaveAsDone(bool success,
617                                                const base::FilePath& filename,
618                                                int filter_index) {
619   if (success)
620     file_saveas_completion_callback_.Run(filename, filter_index, NULL);
621   else
622     failure_callback_.Run(NULL);
623   file_saveas_completion_callback_.Reset();
624   failure_callback_.Reset();
625 }
626
627
628 void RemoteWindowTreeHostWin::OnFileOpenDone(bool success,
629                                              const base::FilePath& filename) {
630   if (success)
631     file_open_completion_callback_.Run(base::FilePath(filename), 0, NULL);
632   else
633     failure_callback_.Run(NULL);
634   file_open_completion_callback_.Reset();
635   failure_callback_.Reset();
636 }
637
638 void RemoteWindowTreeHostWin::OnMultiFileOpenDone(
639     bool success,
640     const std::vector<base::FilePath>& files) {
641   if (success)
642     multi_file_open_completion_callback_.Run(files, NULL);
643   else
644     failure_callback_.Run(NULL);
645   multi_file_open_completion_callback_.Reset();
646   failure_callback_.Reset();
647 }
648
649 void RemoteWindowTreeHostWin::OnSelectFolderDone(
650     bool success,
651     const base::FilePath& folder) {
652   if (success)
653     select_folder_completion_callback_.Run(base::FilePath(folder), 0, NULL);
654   else
655     failure_callback_.Run(NULL);
656   select_folder_completion_callback_.Reset();
657   failure_callback_.Reset();
658 }
659
660 void RemoteWindowTreeHostWin::OnSetCursorPosAck() {
661   DCHECK(ignore_mouse_moves_until_set_cursor_ack_);
662   ignore_mouse_moves_until_set_cursor_ack_ = false;
663 }
664
665 ui::RemoteInputMethodPrivateWin*
666 RemoteWindowTreeHostWin::GetRemoteInputMethodPrivate() {
667   ui::InputMethod* input_method = GetAshWindow()->GetProperty(
668       aura::client::kRootWindowInputMethodKey);
669   return ui::RemoteInputMethodPrivateWin::Get(input_method);
670 }
671
672 void RemoteWindowTreeHostWin::OnImeCandidatePopupChanged(bool visible) {
673   ui::RemoteInputMethodPrivateWin* remote_input_method_private =
674       GetRemoteInputMethodPrivate();
675   if (!remote_input_method_private)
676     return;
677   remote_input_method_private->OnCandidatePopupChanged(visible);
678 }
679
680 void RemoteWindowTreeHostWin::OnImeCompositionChanged(
681     const base::string16& text,
682     int32 selection_start,
683     int32 selection_end,
684     const std::vector<metro_viewer::UnderlineInfo>& underlines) {
685   ui::RemoteInputMethodPrivateWin* remote_input_method_private =
686       GetRemoteInputMethodPrivate();
687   if (!remote_input_method_private)
688     return;
689   ui::CompositionText composition_text;
690   FillCompositionText(
691       text, selection_start, selection_end, underlines, &composition_text);
692   remote_input_method_private->OnCompositionChanged(composition_text);
693 }
694
695 void RemoteWindowTreeHostWin::OnImeTextCommitted(const base::string16& text) {
696   ui::RemoteInputMethodPrivateWin* remote_input_method_private =
697       GetRemoteInputMethodPrivate();
698   if (!remote_input_method_private)
699     return;
700   remote_input_method_private->OnTextCommitted(text);
701 }
702
703 void RemoteWindowTreeHostWin::OnImeInputSourceChanged(uint16 language_id,
704                                                       bool is_ime) {
705   ui::RemoteInputMethodPrivateWin* remote_input_method_private =
706       GetRemoteInputMethodPrivate();
707   if (!remote_input_method_private)
708     return;
709   remote_input_method_private->OnInputSourceChanged(language_id, is_ime);
710 }
711
712 void RemoteWindowTreeHostWin::DispatchKeyboardMessage(ui::EventType type,
713                                                       uint32 vkey,
714                                                       uint32 repeat_count,
715                                                       uint32 scan_code,
716                                                       uint32 flags,
717                                                       bool is_character) {
718   SetEventFlags(flags | mouse_event_flags());
719   if (base::MessageLoop::current()->IsNested()) {
720     int index = (flags & ui::EF_ALT_DOWN) ? 1 : 0;
721     const int char_message[] = {WM_CHAR, WM_SYSCHAR};
722     const int keydown_message[] = {WM_KEYDOWN, WM_SYSKEYDOWN};
723     const int keyup_message[] = {WM_KEYUP, WM_SYSKEYUP};
724     uint32 message = is_character
725                          ? char_message[index]
726                          : (type == ui::ET_KEY_PRESSED ? keydown_message[index]
727                                                        : keyup_message[index]);
728     ::PostThreadMessage(::GetCurrentThreadId(),
729                         message,
730                         vkey,
731                         repeat_count | scan_code >> 15);
732   } else {
733     ui::KeyEvent event(type,
734                        ui::KeyboardCodeForWindowsKeyCode(vkey),
735                        flags,
736                        is_character);
737     SendEventToProcessor(&event);
738   }
739 }
740
741 void RemoteWindowTreeHostWin::SetEventFlags(uint32 flags) {
742   if (flags == event_flags_)
743     return;
744   event_flags_ = flags;
745   SetVirtualKeyStates(event_flags_);
746 }
747
748 }  // namespace aura