Upstream version 7.36.149.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 and the instance have never set by SetInstance,
111   // it will call Create() wiht empty bounds.
112   static RemoteWindowTreeHostWin* Instance();
113
114   // Manually sets the instance to be used as a return value of |Instance()|
115   // method above. This should not be called if the instance has already
116   // been set or created, and doing so will result in CHECK failure.
117   static void SetInstance(RemoteWindowTreeHostWin* instance);
118
119   // Returns true if there is a RemoteWindowTreeHostWin and it has a valid
120   // HWND. A return value of false typically indicates we're not in metro mode.
121   static bool IsValid();
122
123   // Sets the handle to the remote window. The |remote_window| is the actual
124   // window owned by the viewer process. Call this before Connected() for some
125   // customers like input method initialization which needs the handle.
126   void SetRemoteWindowHandle(HWND remote_window);
127
128   // The |host| can be used when we need to send a message to it.
129   void Connected(IPC::Sender* host);
130   // Called when the remote process has closed its IPC connection.
131   void Disconnected();
132
133   // Called when we have a message from the remote process.
134   bool OnMessageReceived(const IPC::Message& message);
135
136   void HandleOpenURLOnDesktop(const base::FilePath& shortcut,
137                               const base::string16& url);
138
139   // The |ash_exit| parameter indicates whether the Ash process would be
140   // shutdown after activating the desktop.
141   void HandleActivateDesktop(const base::FilePath& shortcut, bool ash_exit);
142
143   // Notify the metro viewer that it should shut itself down.
144   void HandleMetroExit();
145
146   void HandleOpenFile(const base::string16& title,
147                       const base::FilePath& default_path,
148                       const base::string16& filter,
149                       const OpenFileCompletion& on_success,
150                       const FileSelectionCanceled& on_failure);
151
152   void HandleOpenMultipleFiles(const base::string16& title,
153                                const base::FilePath& default_path,
154                                const base::string16& filter,
155                                const OpenMultipleFilesCompletion& on_success,
156                                const FileSelectionCanceled& on_failure);
157
158   void HandleSaveFile(const base::string16& title,
159                       const base::FilePath& default_path,
160                       const base::string16& filter,
161                       int filter_index,
162                       const base::string16& default_extension,
163                       const SaveFileCompletion& on_success,
164                       const FileSelectionCanceled& on_failure);
165
166   void HandleSelectFolder(const base::string16& title,
167                           const SelectFolderCompletion& on_success,
168                           const FileSelectionCanceled& on_failure);
169
170   void HandleWindowSizeChanged(uint32 width, uint32 height);
171
172   // Returns the active ASH root window.
173   Window* GetAshWindow();
174
175   // Returns true if the remote window is the foreground window according to the
176   // OS.
177   bool IsForegroundWindow();
178
179  protected:
180   explicit RemoteWindowTreeHostWin(const gfx::Rect& bounds);
181   virtual ~RemoteWindowTreeHostWin();
182
183  private:
184   // IPC message handing methods:
185   void OnMouseMoved(int32 x, int32 y, int32 flags);
186   void OnMouseButton(const MetroViewerHostMsg_MouseButtonParams& params);
187   void OnKeyDown(uint32 vkey,
188                  uint32 repeat_count,
189                  uint32 scan_code,
190                  uint32 flags);
191   void OnKeyUp(uint32 vkey,
192                uint32 repeat_count,
193                uint32 scan_code,
194                uint32 flags);
195   void OnChar(uint32 key_code,
196               uint32 repeat_count,
197               uint32 scan_code,
198               uint32 flags);
199   void OnWindowActivated();
200   void OnEdgeGesture();
201   void OnTouchDown(int32 x, int32 y, uint64 timestamp, uint32 pointer_id);
202   void OnTouchUp(int32 x, int32 y, uint64 timestamp, uint32 pointer_id);
203   void OnTouchMoved(int32 x, int32 y, uint64 timestamp, uint32 pointer_id);
204   void OnFileSaveAsDone(bool success,
205                         const base::FilePath& filename,
206                         int filter_index);
207   void OnFileOpenDone(bool success, const base::FilePath& filename);
208   void OnMultiFileOpenDone(bool success,
209                            const std::vector<base::FilePath>& files);
210   void OnSelectFolderDone(bool success, const base::FilePath& folder);
211   void OnSetCursorPosAck();
212
213   // For Input Method support:
214   ui::RemoteInputMethodPrivateWin* GetRemoteInputMethodPrivate();
215   void OnImeCandidatePopupChanged(bool visible);
216   void OnImeCompositionChanged(
217       const base::string16& text,
218       int32 selection_start,
219       int32 selection_end,
220       const std::vector<metro_viewer::UnderlineInfo>& underlines);
221   void OnImeTextCommitted(const base::string16& text);
222   void OnImeInputSourceChanged(uint16 language_id, bool is_ime);
223
224   // WindowTreeHost overrides:
225   virtual ui::EventSource* GetEventSource() OVERRIDE;
226   virtual gfx::AcceleratedWidget GetAcceleratedWidget() OVERRIDE;
227   virtual void Show() OVERRIDE;
228   virtual void Hide() OVERRIDE;
229   virtual gfx::Rect GetBounds() const OVERRIDE;
230   virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE;
231   virtual gfx::Point GetLocationOnNativeScreen() const OVERRIDE;
232   virtual void SetCapture() OVERRIDE;
233   virtual void ReleaseCapture() OVERRIDE;
234   virtual void PostNativeEvent(const base::NativeEvent& native_event) OVERRIDE;
235   virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE;
236   virtual void SetCursorNative(gfx::NativeCursor cursor) OVERRIDE;
237   virtual void MoveCursorToNative(const gfx::Point& location) OVERRIDE;
238   virtual void OnCursorVisibilityChangedNative(bool show) OVERRIDE;
239
240   // ui::EventSource:
241   virtual ui::EventProcessor* GetEventProcessor() OVERRIDE;
242
243   // ui::internal::RemoteInputMethodDelegateWin overrides:
244   virtual void CancelComposition() OVERRIDE;
245   virtual void OnTextInputClientUpdated(
246       const std::vector<int32>& input_scopes,
247       const std::vector<gfx::Rect>& composition_character_bounds) OVERRIDE;
248
249   // Helper function to dispatch a keyboard message to the desired target.
250   // The default target is the WindowEventDispatcher. For nested message loop
251   // invocations we post a synthetic keyboard message directly into the message
252   // loop. The dispatcher for the nested loop would then decide how this
253   // message is routed.
254   void DispatchKeyboardMessage(ui::EventType type,
255                                uint32 vkey,
256                                uint32 repeat_count,
257                                uint32 scan_code,
258                                uint32 flags,
259                                bool is_character);
260
261   // Sets the event flags. |flags| is a bitmask of EventFlags. If there is a
262   // change the system virtual key state is updated as well. This way if chrome
263   // queries for key state it matches that of event being dispatched.
264   void SetEventFlags(uint32 flags);
265
266   uint32 mouse_event_flags() const {
267     return event_flags_ & (ui::EF_LEFT_MOUSE_BUTTON |
268                            ui::EF_MIDDLE_MOUSE_BUTTON |
269                            ui::EF_RIGHT_MOUSE_BUTTON);
270   }
271
272   uint32 key_event_flags() const {
273     return event_flags_ & (ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN |
274                            ui::EF_ALT_DOWN | ui::EF_CAPS_LOCK_DOWN);
275   }
276
277   HWND remote_window_;
278   IPC::Sender* host_;
279   scoped_ptr<ui::ViewProp> prop_;
280
281   // Saved callbacks which inform the caller about the result of the open file/
282   // save file/select operations.
283   OpenFileCompletion file_open_completion_callback_;
284   OpenMultipleFilesCompletion multi_file_open_completion_callback_;
285   SaveFileCompletion file_saveas_completion_callback_;
286   SelectFolderCompletion select_folder_completion_callback_;
287   FileSelectionCanceled failure_callback_;
288
289   // Set to true if we need to ignore mouse messages until the SetCursorPos
290   // operation is acked by the viewer.
291   bool ignore_mouse_moves_until_set_cursor_ack_;
292
293   // Tracking last click event for synthetically generated mouse events.
294   scoped_ptr<ui::MouseEvent> last_mouse_click_event_;
295
296   // State of the keyboard/mouse at the time of the last input event. See
297   // description of SetEventFlags().
298   uint32 event_flags_;
299
300   // Current size of this root window.
301   gfx::Size window_size_;
302
303   DISALLOW_COPY_AND_ASSIGN(RemoteWindowTreeHostWin);
304 };
305
306 }  // namespace aura
307
308 #endif  // UI_AURA_REMOTE_WINDOW_TREE_HOST_WIN_H_