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/compiler_specific.h"
11#include "base/strings/string16.h"
12#include "ui/aura/window_tree_host.h"
13#include "ui/base/ime/remote_input_method_delegate_win.h"
14#include "ui/events/event.h"
15#include "ui/events/event_constants.h"
16#include "ui/events/event_source.h"
17#include "ui/gfx/native_widget_types.h"
18#include "ui/metro_viewer/ime_types.h"
19
20struct MetroViewerHostMsg_MouseButtonParams;
21
22namespace base {
23class FilePath;
24}
25
26namespace ui {
27class RemoteInputMethodPrivateWin;
28class ViewProp;
29}
30
31namespace IPC {
32class Message;
33class Sender;
34}
35
36namespace aura {
37
38// WindowTreeHost implementaton that receives events from a different
39// process. In the case of Windows this is the Windows 8 (aka Metro)
40// frontend process, which forwards input events to this class.
41class AURA_EXPORT RemoteWindowTreeHostWin
42    : public WindowTreeHost,
43      public ui::EventSource,
44      public ui::internal::RemoteInputMethodDelegateWin {
45 public:
46  // Returns the current RemoteWindowTreeHostWin. This does *not* create a
47  // RemoteWindowTreeHostWin.
48  static RemoteWindowTreeHostWin* Instance();
49
50  // Returns true if there is a RemoteWindowTreeHostWin and it has a valid
51  // HWND. A return value of false typically indicates we're not in metro mode.
52  static bool IsValid();
53
54  // Sets the handle to the remote window. The |remote_window| is the actual
55  // window owned by the viewer process. Call this before Connected() for some
56  // customers like input method initialization which needs the handle.
57  void SetRemoteWindowHandle(HWND remote_window);
58  HWND remote_window() { return remote_window_; }
59
60  // The |host| can be used when we need to send a message to it.
61  void Connected(IPC::Sender* host);
62  // Called when the remote process has closed its IPC connection.
63  void Disconnected();
64
65  // Called when we have a message from the remote process.
66  bool OnMessageReceived(const IPC::Message& message);
67
68  void HandleOpenURLOnDesktop(const base::FilePath& shortcut,
69                              const base::string16& url);
70
71  void HandleWindowSizeChanged(uint32 width, uint32 height);
72
73  // Returns the active ASH root window.
74  Window* GetAshWindow();
75
76  // Returns true if the remote window is the foreground window according to the
77  // OS.
78  bool IsForegroundWindow();
79
80 protected:
81  RemoteWindowTreeHostWin();
82  virtual ~RemoteWindowTreeHostWin();
83
84 private:
85  // IPC message handing methods:
86  void OnMouseMoved(int32 x, int32 y, int32 flags);
87  void OnMouseButton(const MetroViewerHostMsg_MouseButtonParams& params);
88  void OnKeyDown(uint32 vkey,
89                 uint32 repeat_count,
90                 uint32 scan_code,
91                 uint32 flags);
92  void OnKeyUp(uint32 vkey,
93               uint32 repeat_count,
94               uint32 scan_code,
95               uint32 flags);
96  void OnChar(uint32 key_code,
97              uint32 repeat_count,
98              uint32 scan_code,
99              uint32 flags);
100  void OnWindowActivated(bool repaint);
101  void OnEdgeGesture();
102  void OnTouchDown(int32 x, int32 y, uint64 timestamp, uint32 pointer_id);
103  void OnTouchUp(int32 x, int32 y, uint64 timestamp, uint32 pointer_id);
104  void OnTouchMoved(int32 x, int32 y, uint64 timestamp, uint32 pointer_id);
105  void OnSetCursorPosAck();
106
107  // For Input Method support:
108  ui::RemoteInputMethodPrivateWin* GetRemoteInputMethodPrivate();
109  void OnImeCandidatePopupChanged(bool visible);
110  void OnImeCompositionChanged(
111      const base::string16& text,
112      int32 selection_start,
113      int32 selection_end,
114      const std::vector<metro_viewer::UnderlineInfo>& underlines);
115  void OnImeTextCommitted(const base::string16& text);
116  void OnImeInputSourceChanged(uint16 language_id, bool is_ime);
117
118  // WindowTreeHost overrides:
119  virtual ui::EventSource* GetEventSource() OVERRIDE;
120  virtual gfx::AcceleratedWidget GetAcceleratedWidget() OVERRIDE;
121  virtual void Show() OVERRIDE;
122  virtual void Hide() OVERRIDE;
123  virtual gfx::Rect GetBounds() const OVERRIDE;
124  virtual void SetBounds(const gfx::Rect& bounds) OVERRIDE;
125  virtual gfx::Point GetLocationOnNativeScreen() const OVERRIDE;
126  virtual void SetCapture() OVERRIDE;
127  virtual void ReleaseCapture() OVERRIDE;
128  virtual void PostNativeEvent(const base::NativeEvent& native_event) OVERRIDE;
129  virtual void SetCursorNative(gfx::NativeCursor cursor) OVERRIDE;
130  virtual void MoveCursorToNative(const gfx::Point& location) OVERRIDE;
131  virtual void OnCursorVisibilityChangedNative(bool show) OVERRIDE;
132
133  // ui::EventSource:
134  virtual ui::EventProcessor* GetEventProcessor() OVERRIDE;
135
136  // ui::internal::RemoteInputMethodDelegateWin overrides:
137  virtual void CancelComposition() OVERRIDE;
138  virtual void OnTextInputClientUpdated(
139      const std::vector<int32>& input_scopes,
140      const std::vector<gfx::Rect>& composition_character_bounds) OVERRIDE;
141
142  // Helper function to dispatch a keyboard message to the desired target.
143  // The default target is the WindowEventDispatcher. For nested message loop
144  // invocations we post a synthetic keyboard message directly into the message
145  // loop. The dispatcher for the nested loop would then decide how this
146  // message is routed.
147  void DispatchKeyboardMessage(ui::EventType type,
148                               uint32 vkey,
149                               uint32 repeat_count,
150                               uint32 scan_code,
151                               uint32 flags,
152                               bool is_character);
153
154  // Sets the event flags. |flags| is a bitmask of EventFlags. If there is a
155  // change the system virtual key state is updated as well. This way if chrome
156  // queries for key state it matches that of event being dispatched.
157  void SetEventFlags(uint32 flags);
158
159  uint32 mouse_event_flags() const {
160    return event_flags_ & (ui::EF_LEFT_MOUSE_BUTTON |
161                           ui::EF_MIDDLE_MOUSE_BUTTON |
162                           ui::EF_RIGHT_MOUSE_BUTTON);
163  }
164
165  uint32 key_event_flags() const {
166    return event_flags_ & (ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN |
167                           ui::EF_ALT_DOWN | ui::EF_CAPS_LOCK_DOWN);
168  }
169
170  HWND remote_window_;
171  IPC::Sender* host_;
172  scoped_ptr<ui::ViewProp> prop_;
173
174  // Incremented if we need to ignore mouse messages until the SetCursorPos
175  // operation is acked by the viewer.
176  int ignore_mouse_moves_until_set_cursor_ack_;
177
178  // Tracking last click event for synthetically generated mouse events.
179  scoped_ptr<ui::MouseEvent> last_mouse_click_event_;
180
181  // State of the keyboard/mouse at the time of the last input event. See
182  // description of SetEventFlags().
183  uint32 event_flags_;
184
185  // Current size of this root window.
186  gfx::Size window_size_;
187
188  DISALLOW_COPY_AND_ASSIGN(RemoteWindowTreeHostWin);
189};
190
191}  // namespace aura
192
193#endif  // UI_AURA_REMOTE_WINDOW_TREE_HOST_WIN_H_
194