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 #ifndef UI_AURA_REMOTE_ROOT_WINDOW_HOST_WIN_H_ 6 #define UI_AURA_REMOTE_ROOT_WINDOW_HOST_WIN_H_ 7 8 #include <vector> 9 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/gfx/native_widget_types.h" 18 #include "ui/metro_viewer/ime_types.h" 19 20 namespace base { 21 class FilePath; 22 } 23 24 namespace ui { 25 class RemoteInputMethodPrivateWin; 26 class ViewProp; 27 } 28 29 namespace IPC { 30 class Message; 31 class Sender; 32 } 33 34 namespace aura { 35 36 typedef base::Callback<void(const base::FilePath&, int, void*)> 37 OpenFileCompletion; 38 39 typedef base::Callback<void(const std::vector<base::FilePath>&, void*)> 40 OpenMultipleFilesCompletion; 41 42 typedef base::Callback<void(const base::FilePath&, int, void*)> 43 SaveFileCompletion; 44 45 typedef base::Callback<void(const base::FilePath&, int, void*)> 46 SelectFolderCompletion; 47 48 typedef base::Callback<void(void*)> FileSelectionCanceled; 49 50 // Handles the open file operation for Metro Chrome Ash. The on_success 51 // callback passed in is invoked when we receive the opened file name from 52 // the metro viewer. The on failure callback is invoked on failure. 53 AURA_EXPORT void HandleOpenFile(const base::string16& title, 54 const base::FilePath& default_path, 55 const base::string16& filter, 56 const OpenFileCompletion& on_success, 57 const FileSelectionCanceled& on_failure); 58 59 // Handles the open multiple file operation for Metro Chrome Ash. The 60 // on_success callback passed in is invoked when we receive the opened file 61 // names from the metro viewer. The on failure callback is invoked on failure. 62 AURA_EXPORT void HandleOpenMultipleFiles( 63 const base::string16& title, 64 const base::FilePath& default_path, 65 const base::string16& filter, 66 const OpenMultipleFilesCompletion& on_success, 67 const FileSelectionCanceled& on_failure); 68 69 // Handles the save file operation for Metro Chrome Ash. The on_success 70 // callback passed in is invoked when we receive the saved file name from 71 // the metro viewer. The on failure callback is invoked on failure. 72 AURA_EXPORT void HandleSaveFile(const base::string16& title, 73 const base::FilePath& default_path, 74 const base::string16& filter, 75 int filter_index, 76 const base::string16& default_extension, 77 const SaveFileCompletion& on_success, 78 const FileSelectionCanceled& on_failure); 79 80 // Handles the select folder for Metro Chrome Ash. The on_success 81 // callback passed in is invoked when we receive the folder name from the 82 // metro viewer. The on failure callback is invoked on failure. 83 AURA_EXPORT void HandleSelectFolder(const base::string16& title, 84 const SelectFolderCompletion& on_success, 85 const FileSelectionCanceled& on_failure); 86 87 // Handles the activate desktop command for Metro Chrome Ash. The |ash_exit| 88 // parameter indicates whether the Ash process would be shutdown after 89 // activating the desktop. 90 AURA_EXPORT void HandleActivateDesktop( 91 const base::FilePath& shortcut, 92 bool ash_exit); 93 94 // Handles the metro exit command. Notifies the metro viewer to shutdown 95 // gracefully. 96 AURA_EXPORT void HandleMetroExit(); 97 98 // RootWindowHost implementaton that receives events from a different 99 // process. In the case of Windows this is the Windows 8 (aka Metro) 100 // frontend process, which forwards input events to this class. 101 class AURA_EXPORT RemoteRootWindowHostWin 102 : public RootWindowHost, 103 public ui::internal::RemoteInputMethodDelegateWin { 104 public: 105 // Returns the only RemoteRootWindowHostWin, if this is the first time 106 // this function is called, it will call Create() wiht empty bounds. 107 static RemoteRootWindowHostWin* Instance(); 108 static RemoteRootWindowHostWin* Create(const gfx::Rect& bounds); 109 110 // Called when the remote process has established its IPC connection. 111 // The |host| can be used when we need to send a message to it and 112 // |remote_window| is the actual window owned by the viewer process. 113 void Connected(IPC::Sender* host, HWND remote_window); 114 // Called when the remote process has closed its IPC connection. 115 void Disconnected(); 116 117 // Called when we have a message from the remote process. 118 bool OnMessageReceived(const IPC::Message& message); 119 120 void HandleOpenURLOnDesktop(const base::FilePath& shortcut, 121 const base::string16& url); 122 123 // The |ash_exit| parameter indicates whether the Ash process would be 124 // shutdown after activating the desktop. 125 void HandleActivateDesktop(const base::FilePath& shortcut, bool ash_exit); 126 127 // Notify the metro viewer that it should shut itself down. 128 void HandleMetroExit(); 129 130 void HandleOpenFile(const base::string16& title, 131 const base::FilePath& default_path, 132 const base::string16& filter, 133 const OpenFileCompletion& on_success, 134 const FileSelectionCanceled& on_failure); 135 136 void HandleOpenMultipleFiles(const base::string16& title, 137 const base::FilePath& default_path, 138 const base::string16& filter, 139 const OpenMultipleFilesCompletion& on_success, 140 const FileSelectionCanceled& on_failure); 141 142 void HandleSaveFile(const base::string16& title, 143 const base::FilePath& default_path, 144 const base::string16& filter, 145 int filter_index, 146 const base::string16& default_extension, 147 const SaveFileCompletion& on_success, 148 const FileSelectionCanceled& on_failure); 149 150 void HandleSelectFolder(const base::string16& title, 151 const SelectFolderCompletion& on_success, 152 const FileSelectionCanceled& on_failure); 153 154 void HandleWindowSizeChanged(uint32 width, uint32 height); 155 156 // Returns the active ASH root window. 157 Window* GetAshWindow(); 158 159 // Returns true if the remote window is the foreground window according to the 160 // OS. 161 bool IsForegroundWindow(); 162 163 private: 164 explicit RemoteRootWindowHostWin(const gfx::Rect& bounds); 165 virtual ~RemoteRootWindowHostWin(); 166 167 // IPC message handing methods: 168 void OnMouseMoved(int32 x, int32 y, int32 flags); 169 void OnMouseButton(int32 x, 170 int32 y, 171 int32 extra, 172 ui::EventType type, 173 ui::EventFlags flags); 174 void OnKeyDown(uint32 vkey, 175 uint32 repeat_count, 176 uint32 scan_code, 177 uint32 flags); 178 void OnKeyUp(uint32 vkey, 179 uint32 repeat_count, 180 uint32 scan_code, 181 uint32 flags); 182 void OnChar(uint32 key_code, 183 uint32 repeat_count, 184 uint32 scan_code, 185 uint32 flags); 186 void OnWindowActivated(); 187 void OnTouchDown(int32 x, int32 y, uint64 timestamp, uint32 pointer_id); 188 void OnTouchUp(int32 x, int32 y, uint64 timestamp, uint32 pointer_id); 189 void OnTouchMoved(int32 x, int32 y, uint64 timestamp, uint32 pointer_id); 190 void OnFileSaveAsDone(bool success, 191 const base::FilePath& filename, 192 int filter_index); 193 void OnFileOpenDone(bool success, const base::FilePath& filename); 194 void OnMultiFileOpenDone(bool success, 195 const std::vector<base::FilePath>& files); 196 void OnSelectFolderDone(bool success, const base::FilePath& folder); 197 void OnSetCursorPosAck(); 198 199 // For Input Method support: 200 ui::RemoteInputMethodPrivateWin* GetRemoteInputMethodPrivate(); 201 void OnImeCandidatePopupChanged(bool visible); 202 void OnImeCompositionChanged( 203 const string16& text, 204 int32 selection_start, 205 int32 selection_end, 206 const std::vector<metro_viewer::UnderlineInfo>& underlines); 207 void OnImeTextCommitted(const string16& text); 208 void OnImeInputSourceChanged(uint16 language_id, bool is_ime); 209 210 // RootWindowHost overrides: 211 virtual RootWindow* GetRootWindow() OVERRIDE; 212 virtual gfx::AcceleratedWidget GetAcceleratedWidget() OVERRIDE; 213 virtual void Show() OVERRIDE; 214 virtual void Hide() OVERRIDE; 215 virtual void ToggleFullScreen() OVERRIDE; 216 virtual gfx::Rect GetBounds() const OVERRIDE; 217 virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE; 218 virtual gfx::Insets GetInsets() const OVERRIDE; 219 virtual void SetInsets(const gfx::Insets& insets) OVERRIDE; 220 virtual gfx::Point GetLocationOnNativeScreen() const OVERRIDE; 221 virtual void SetCapture() OVERRIDE; 222 virtual void ReleaseCapture() OVERRIDE; 223 virtual void SetCursor(gfx::NativeCursor cursor) OVERRIDE; 224 virtual bool QueryMouseLocation(gfx::Point* location_return) OVERRIDE; 225 virtual bool ConfineCursorToRootWindow() OVERRIDE; 226 virtual void UnConfineCursor() OVERRIDE; 227 virtual void OnCursorVisibilityChanged(bool show) OVERRIDE; 228 virtual void MoveCursorTo(const gfx::Point& location) OVERRIDE; 229 virtual void PostNativeEvent(const base::NativeEvent& native_event) OVERRIDE; 230 virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE; 231 virtual void PrepareForShutdown() OVERRIDE; 232 233 // ui::internal::RemoteInputMethodDelegateWin overrides: 234 virtual void CancelComposition() OVERRIDE; 235 virtual void OnTextInputClientUpdated( 236 const std::vector<int32>& input_scopes, 237 const std::vector<gfx::Rect>& composition_character_bounds) OVERRIDE; 238 239 // Helper function to dispatch a keyboard message to the desired target. 240 // The default target is the RootWindowHostDelegate. For nested message loop 241 // invocations we post a synthetic keyboard message directly into the message 242 // loop. The dispatcher for the nested loop would then decide how this 243 // message is routed. 244 void DispatchKeyboardMessage(ui::EventType type, 245 uint32 vkey, 246 uint32 repeat_count, 247 uint32 scan_code, 248 uint32 flags, 249 bool is_character); 250 251 // Sets the event flags. |flags| is a bitmask of EventFlags. If there is a 252 // change the system virtual key state is updated as well. This way if chrome 253 // queries for key state it matches that of event being dispatched. 254 void SetEventFlags(uint32 flags); 255 256 uint32 mouse_event_flags() const { 257 return event_flags_ & (ui::EF_LEFT_MOUSE_BUTTON | 258 ui::EF_MIDDLE_MOUSE_BUTTON | 259 ui::EF_RIGHT_MOUSE_BUTTON); 260 } 261 262 uint32 key_event_flags() const { 263 return event_flags_ & (ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN | 264 ui::EF_ALT_DOWN | ui::EF_CAPS_LOCK_DOWN); 265 } 266 267 HWND remote_window_; 268 IPC::Sender* host_; 269 scoped_ptr<ui::ViewProp> prop_; 270 271 // Saved callbacks which inform the caller about the result of the open file/ 272 // save file/select operations. 273 OpenFileCompletion file_open_completion_callback_; 274 OpenMultipleFilesCompletion multi_file_open_completion_callback_; 275 SaveFileCompletion file_saveas_completion_callback_; 276 SelectFolderCompletion select_folder_completion_callback_; 277 FileSelectionCanceled failure_callback_; 278 279 // Set to true if we need to ignore mouse messages until the SetCursorPos 280 // operation is acked by the viewer. 281 bool ignore_mouse_moves_until_set_cursor_ack_; 282 283 // Tracking last click event for synthetically generated mouse events. 284 scoped_ptr<ui::MouseEvent> last_mouse_click_event_; 285 286 // State of the keyboard/mouse at the time of the last input event. See 287 // description of SetEventFlags(). 288 uint32 event_flags_; 289 290 // Current size of this root window. 291 gfx::Size window_size_; 292 293 DISALLOW_COPY_AND_ASSIGN(RemoteRootWindowHostWin); 294 }; 295 296 } // namespace aura 297 298 #endif // UI_AURA_REMOTE_ROOT_WINDOW_HOST_WIN_H_ 299