Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / ui / aura / remote_window_tree_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_WINDOW_TREE_HOST_WIN_H_
6 #define UI_AURA_REMOTE_WINDOW_TREE_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/events/event_source.h"
18 #include "ui/gfx/native_widget_types.h"
19 #include "ui/metro_viewer/ime_types.h"
20
21 struct MetroViewerHostMsg_MouseButtonParams;
22
23 namespace base {
24 class FilePath;
25 }
26
27 namespace ui {
28 class RemoteInputMethodPrivateWin;
29 class ViewProp;
30 }
31
32 namespace IPC {
33 class Message;
34 class Sender;
35 }
36
37 namespace aura {
38
39 typedef base::Callback<void(const base::FilePath&, int, void*)>
40     OpenFileCompletion;
41
42 typedef base::Callback<void(const std::vector<base::FilePath>&, void*)>
43     OpenMultipleFilesCompletion;
44
45 typedef base::Callback<void(const base::FilePath&, int, void*)>
46     SaveFileCompletion;
47
48 typedef base::Callback<void(const base::FilePath&, int, void*)>
49     SelectFolderCompletion;
50
51 typedef base::Callback<void(void*)> FileSelectionCanceled;
52
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);
61
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);
71
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,
78                                 int filter_index,
79                                 const base::string16& default_extension,
80                                 const SaveFileCompletion& on_success,
81                                 const FileSelectionCanceled& on_failure);
82
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);
89
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,
95     bool ash_exit);
96
97 // Handles the metro exit command.  Notifies the metro viewer to shutdown
98 // gracefully.
99 AURA_EXPORT void HandleMetroExit();
100
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 {
108  public:
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);
113
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();
117
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);
122
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.
126   void Disconnected();
127
128   // Called when we have a message from the remote process.
129   bool OnMessageReceived(const IPC::Message& message);
130
131   void HandleOpenURLOnDesktop(const base::FilePath& shortcut,
132                               const base::string16& url);
133
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);
137
138   // Notify the metro viewer that it should shut itself down.
139   void HandleMetroExit();
140
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);
146
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);
152
153   void HandleSaveFile(const base::string16& title,
154                       const base::FilePath& default_path,
155                       const base::string16& filter,
156                       int filter_index,
157                       const base::string16& default_extension,
158                       const SaveFileCompletion& on_success,
159                       const FileSelectionCanceled& on_failure);
160
161   void HandleSelectFolder(const base::string16& title,
162                           const SelectFolderCompletion& on_success,
163                           const FileSelectionCanceled& on_failure);
164
165   void HandleWindowSizeChanged(uint32 width, uint32 height);
166
167   // Returns the active ASH root window.
168   Window* GetAshWindow();
169
170   // Returns true if the remote window is the foreground window according to the
171   // OS.
172   bool IsForegroundWindow();
173
174  private:
175   explicit RemoteWindowTreeHostWin(const gfx::Rect& bounds);
176   virtual ~RemoteWindowTreeHostWin();
177
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,
182                  uint32 repeat_count,
183                  uint32 scan_code,
184                  uint32 flags);
185   void OnKeyUp(uint32 vkey,
186                uint32 repeat_count,
187                uint32 scan_code,
188                uint32 flags);
189   void OnChar(uint32 key_code,
190               uint32 repeat_count,
191               uint32 scan_code,
192               uint32 flags);
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,
200                         int filter_index);
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();
206
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,
213       int32 selection_end,
214       const std::vector<metro_viewer::UnderlineInfo>& underlines);
215   void OnImeTextCommitted(const base::string16& text);
216   void OnImeInputSourceChanged(uint16 language_id, bool is_ime);
217
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;
240
241   // ui::EventSource:
242   virtual ui::EventProcessor* GetEventProcessor() OVERRIDE;
243
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;
249
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,
256                                uint32 vkey,
257                                uint32 repeat_count,
258                                uint32 scan_code,
259                                uint32 flags,
260                                bool is_character);
261
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);
266
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);
271   }
272
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);
276   }
277
278   HWND remote_window_;
279   IPC::Sender* host_;
280   scoped_ptr<ui::ViewProp> prop_;
281
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_;
289
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_;
293
294   // Tracking last click event for synthetically generated mouse events.
295   scoped_ptr<ui::MouseEvent> last_mouse_click_event_;
296
297   // State of the keyboard/mouse at the time of the last input event. See
298   // description of SetEventFlags().
299   uint32 event_flags_;
300
301   // Current size of this root window.
302   gfx::Size window_size_;
303
304   DISALLOW_COPY_AND_ASSIGN(RemoteWindowTreeHostWin);
305 };
306
307 }  // namespace aura
308
309 #endif  // UI_AURA_REMOTE_WINDOW_TREE_HOST_WIN_H_