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