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 #ifndef UI_AURA_REMOTE_WINDOW_TREE_HOST_WIN_H_
6 #define UI_AURA_REMOTE_WINDOW_TREE_HOST_WIN_H_
10 #include "base/callback.h"
11 #include "base/compiler_specific.h"
12 #include "base/strings/string16.h"
13 #include "ui/aura/window_tree_host.h"
14 #include "ui/base/ime/remote_input_method_delegate_win.h"
15 #include "ui/events/event.h"
16 #include "ui/events/event_constants.h"
17 #include "ui/events/event_source.h"
18 #include "ui/gfx/native_widget_types.h"
19 #include "ui/metro_viewer/ime_types.h"
21 struct MetroViewerHostMsg_MouseButtonParams;
28 class RemoteInputMethodPrivateWin;
39 typedef base::Callback<void(const base::FilePath&, int, void*)>
42 typedef base::Callback<void(const std::vector<base::FilePath>&, void*)>
43 OpenMultipleFilesCompletion;
45 typedef base::Callback<void(const base::FilePath&, int, void*)>
48 typedef base::Callback<void(const base::FilePath&, int, void*)>
49 SelectFolderCompletion;
51 typedef base::Callback<void(void*)> FileSelectionCanceled;
53 // Handles the open file operation for Metro Chrome Ash. The on_success
54 // callback passed in is invoked when we receive the opened file name from
55 // the metro viewer. The on failure callback is invoked on failure.
56 AURA_EXPORT void HandleOpenFile(const base::string16& title,
57 const base::FilePath& default_path,
58 const base::string16& filter,
59 const OpenFileCompletion& on_success,
60 const FileSelectionCanceled& on_failure);
62 // Handles the open multiple file operation for Metro Chrome Ash. The
63 // on_success callback passed in is invoked when we receive the opened file
64 // names from the metro viewer. The on failure callback is invoked on failure.
65 AURA_EXPORT void HandleOpenMultipleFiles(
66 const base::string16& title,
67 const base::FilePath& default_path,
68 const base::string16& filter,
69 const OpenMultipleFilesCompletion& on_success,
70 const FileSelectionCanceled& on_failure);
72 // Handles the save file operation for Metro Chrome Ash. The on_success
73 // callback passed in is invoked when we receive the saved file name from
74 // the metro viewer. The on failure callback is invoked on failure.
75 AURA_EXPORT void HandleSaveFile(const base::string16& title,
76 const base::FilePath& default_path,
77 const base::string16& filter,
79 const base::string16& default_extension,
80 const SaveFileCompletion& on_success,
81 const FileSelectionCanceled& on_failure);
83 // Handles the select folder for Metro Chrome Ash. The on_success
84 // callback passed in is invoked when we receive the folder name from the
85 // metro viewer. The on failure callback is invoked on failure.
86 AURA_EXPORT void HandleSelectFolder(const base::string16& title,
87 const SelectFolderCompletion& on_success,
88 const FileSelectionCanceled& on_failure);
90 // Handles the activate desktop command for Metro Chrome Ash. The |ash_exit|
91 // parameter indicates whether the Ash process would be shutdown after
92 // activating the desktop.
93 AURA_EXPORT void HandleActivateDesktop(
94 const base::FilePath& shortcut,
97 // Handles the metro exit command. Notifies the metro viewer to shutdown
99 AURA_EXPORT void HandleMetroExit();
101 // WindowTreeHost implementaton that receives events from a different
102 // process. In the case of Windows this is the Windows 8 (aka Metro)
103 // frontend process, which forwards input events to this class.
104 class AURA_EXPORT RemoteWindowTreeHostWin
105 : public WindowTreeHost,
106 public ui::EventSource,
107 public ui::internal::RemoteInputMethodDelegateWin {
109 // Returns the only RemoteWindowTreeHostWin, if this is the first time
110 // this function is called, it will call Create() wiht empty bounds.
111 static RemoteWindowTreeHostWin* Instance();
112 static RemoteWindowTreeHostWin* Create(const gfx::Rect& bounds);
114 // Returns true if there is a RemoteWindowTreeHostWin and it has a valid
115 // HWND. A return value of false typically indicates we're not in metro mode.
116 static bool IsValid();
118 // Sets the handle to the remote window. The |remote_window| is the actual
119 // window owned by the viewer process. Call this before Connected() for some
120 // customers like input method initialization which needs the handle.
121 void SetRemoteWindowHandle(HWND remote_window);
123 // The |host| can be used when we need to send a message to it.
124 void Connected(IPC::Sender* host);
125 // Called when the remote process has closed its IPC connection.
128 // Called when we have a message from the remote process.
129 bool OnMessageReceived(const IPC::Message& message);
131 void HandleOpenURLOnDesktop(const base::FilePath& shortcut,
132 const base::string16& url);
134 // The |ash_exit| parameter indicates whether the Ash process would be
135 // shutdown after activating the desktop.
136 void HandleActivateDesktop(const base::FilePath& shortcut, bool ash_exit);
138 // Notify the metro viewer that it should shut itself down.
139 void HandleMetroExit();
141 void HandleOpenFile(const base::string16& title,
142 const base::FilePath& default_path,
143 const base::string16& filter,
144 const OpenFileCompletion& on_success,
145 const FileSelectionCanceled& on_failure);
147 void HandleOpenMultipleFiles(const base::string16& title,
148 const base::FilePath& default_path,
149 const base::string16& filter,
150 const OpenMultipleFilesCompletion& on_success,
151 const FileSelectionCanceled& on_failure);
153 void HandleSaveFile(const base::string16& title,
154 const base::FilePath& default_path,
155 const base::string16& filter,
157 const base::string16& default_extension,
158 const SaveFileCompletion& on_success,
159 const FileSelectionCanceled& on_failure);
161 void HandleSelectFolder(const base::string16& title,
162 const SelectFolderCompletion& on_success,
163 const FileSelectionCanceled& on_failure);
165 void HandleWindowSizeChanged(uint32 width, uint32 height);
167 // Returns the active ASH root window.
168 Window* GetAshWindow();
170 // Returns true if the remote window is the foreground window according to the
172 bool IsForegroundWindow();
175 explicit RemoteWindowTreeHostWin(const gfx::Rect& bounds);
176 virtual ~RemoteWindowTreeHostWin();
178 // IPC message handing methods:
179 void OnMouseMoved(int32 x, int32 y, int32 flags);
180 void OnMouseButton(const MetroViewerHostMsg_MouseButtonParams& params);
181 void OnKeyDown(uint32 vkey,
185 void OnKeyUp(uint32 vkey,
189 void OnChar(uint32 key_code,
193 void OnWindowActivated();
194 void OnEdgeGesture();
195 void OnTouchDown(int32 x, int32 y, uint64 timestamp, uint32 pointer_id);
196 void OnTouchUp(int32 x, int32 y, uint64 timestamp, uint32 pointer_id);
197 void OnTouchMoved(int32 x, int32 y, uint64 timestamp, uint32 pointer_id);
198 void OnFileSaveAsDone(bool success,
199 const base::FilePath& filename,
201 void OnFileOpenDone(bool success, const base::FilePath& filename);
202 void OnMultiFileOpenDone(bool success,
203 const std::vector<base::FilePath>& files);
204 void OnSelectFolderDone(bool success, const base::FilePath& folder);
205 void OnSetCursorPosAck();
207 // For Input Method support:
208 ui::RemoteInputMethodPrivateWin* GetRemoteInputMethodPrivate();
209 void OnImeCandidatePopupChanged(bool visible);
210 void OnImeCompositionChanged(
211 const base::string16& text,
212 int32 selection_start,
214 const std::vector<metro_viewer::UnderlineInfo>& underlines);
215 void OnImeTextCommitted(const base::string16& text);
216 void OnImeInputSourceChanged(uint16 language_id, bool is_ime);
218 // WindowTreeHost overrides:
219 virtual RootWindow* GetRootWindow() OVERRIDE;
220 virtual gfx::AcceleratedWidget GetAcceleratedWidget() OVERRIDE;
221 virtual void Show() OVERRIDE;
222 virtual void Hide() OVERRIDE;
223 virtual void ToggleFullScreen() OVERRIDE;
224 virtual gfx::Rect GetBounds() const OVERRIDE;
225 virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE;
226 virtual gfx::Insets GetInsets() const OVERRIDE;
227 virtual void SetInsets(const gfx::Insets& insets) OVERRIDE;
228 virtual gfx::Point GetLocationOnNativeScreen() const OVERRIDE;
229 virtual void SetCapture() OVERRIDE;
230 virtual void ReleaseCapture() OVERRIDE;
231 virtual bool QueryMouseLocation(gfx::Point* location_return) OVERRIDE;
232 virtual bool ConfineCursorToRootWindow() OVERRIDE;
233 virtual void UnConfineCursor() OVERRIDE;
234 virtual void PostNativeEvent(const base::NativeEvent& native_event) OVERRIDE;
235 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE;
236 virtual void PrepareForShutdown() OVERRIDE;
237 virtual void SetCursorNative(gfx::NativeCursor cursor) OVERRIDE;
238 virtual void MoveCursorToNative(const gfx::Point& location) OVERRIDE;
239 virtual void OnCursorVisibilityChangedNative(bool show) OVERRIDE;
242 virtual ui::EventProcessor* GetEventProcessor() OVERRIDE;
244 // ui::internal::RemoteInputMethodDelegateWin overrides:
245 virtual void CancelComposition() OVERRIDE;
246 virtual void OnTextInputClientUpdated(
247 const std::vector<int32>& input_scopes,
248 const std::vector<gfx::Rect>& composition_character_bounds) OVERRIDE;
250 // Helper function to dispatch a keyboard message to the desired target.
251 // The default target is the WindowTreeHostDelegate. For nested message loop
252 // invocations we post a synthetic keyboard message directly into the message
253 // loop. The dispatcher for the nested loop would then decide how this
254 // message is routed.
255 void DispatchKeyboardMessage(ui::EventType type,
262 // Sets the event flags. |flags| is a bitmask of EventFlags. If there is a
263 // change the system virtual key state is updated as well. This way if chrome
264 // queries for key state it matches that of event being dispatched.
265 void SetEventFlags(uint32 flags);
267 uint32 mouse_event_flags() const {
268 return event_flags_ & (ui::EF_LEFT_MOUSE_BUTTON |
269 ui::EF_MIDDLE_MOUSE_BUTTON |
270 ui::EF_RIGHT_MOUSE_BUTTON);
273 uint32 key_event_flags() const {
274 return event_flags_ & (ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN |
275 ui::EF_ALT_DOWN | ui::EF_CAPS_LOCK_DOWN);
280 scoped_ptr<ui::ViewProp> prop_;
282 // Saved callbacks which inform the caller about the result of the open file/
283 // save file/select operations.
284 OpenFileCompletion file_open_completion_callback_;
285 OpenMultipleFilesCompletion multi_file_open_completion_callback_;
286 SaveFileCompletion file_saveas_completion_callback_;
287 SelectFolderCompletion select_folder_completion_callback_;
288 FileSelectionCanceled failure_callback_;
290 // Set to true if we need to ignore mouse messages until the SetCursorPos
291 // operation is acked by the viewer.
292 bool ignore_mouse_moves_until_set_cursor_ack_;
294 // Tracking last click event for synthetically generated mouse events.
295 scoped_ptr<ui::MouseEvent> last_mouse_click_event_;
297 // State of the keyboard/mouse at the time of the last input event. See
298 // description of SetEventFlags().
301 // Current size of this root window.
302 gfx::Size window_size_;
304 DISALLOW_COPY_AND_ASSIGN(RemoteWindowTreeHostWin);
309 #endif // UI_AURA_REMOTE_WINDOW_TREE_HOST_WIN_H_