URLFetcherNullWriter for resource requests.
[chromium-blink-merge.git] / ui / aura / remote_window_tree_host_win.cc
blob842598c823ffdafd851c9ebed9b692d8be36e67d
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.
5 #include "ui/aura/remote_window_tree_host_win.h"
7 #include <windows.h>
9 #include <algorithm>
11 #include "base/message_loop/message_loop.h"
12 #include "ipc/ipc_message.h"
13 #include "ipc/ipc_sender.h"
14 #include "ui/aura/client/cursor_client.h"
15 #include "ui/aura/window_event_dispatcher.h"
16 #include "ui/aura/window_property.h"
17 #include "ui/base/cursor/cursor_loader_win.h"
18 #include "ui/base/ime/composition_text.h"
19 #include "ui/base/ime/input_method.h"
20 #include "ui/base/ime/remote_input_method_win.h"
21 #include "ui/base/ime/text_input_client.h"
22 #include "ui/base/view_prop.h"
23 #include "ui/events/event_utils.h"
24 #include "ui/events/keycodes/keyboard_code_conversion_win.h"
25 #include "ui/gfx/geometry/insets.h"
26 #include "ui/gfx/win/dpi.h"
27 #include "ui/metro_viewer/metro_viewer_messages.h"
29 namespace aura {
31 namespace {
33 const char* kWindowTreeHostWinKey = "__AURA_REMOTE_WINDOW_TREE_HOST_WIN__";
35 // Sets the keystate for the virtual key passed in to down or up.
36 void SetKeyState(uint8* key_states, bool key_down, uint32 virtual_key_code) {
37 DCHECK(key_states);
39 if (key_down)
40 key_states[virtual_key_code] |= 0x80;
41 else
42 key_states[virtual_key_code] &= 0x7F;
45 // Sets the keyboard states for the Shift/Control/Alt/Caps lock keys.
46 void SetVirtualKeyStates(uint32 flags) {
47 uint8 keyboard_state[256] = {0};
48 ::GetKeyboardState(keyboard_state);
50 SetKeyState(keyboard_state, !!(flags & ui::EF_SHIFT_DOWN), VK_SHIFT);
51 SetKeyState(keyboard_state, !!(flags & ui::EF_CONTROL_DOWN), VK_CONTROL);
52 SetKeyState(keyboard_state, !!(flags & ui::EF_ALT_DOWN), VK_MENU);
53 SetKeyState(keyboard_state, !!(flags & ui::EF_CAPS_LOCK_DOWN), VK_CAPITAL);
54 SetKeyState(keyboard_state, !!(flags & ui::EF_LEFT_MOUSE_BUTTON), VK_LBUTTON);
55 SetKeyState(keyboard_state, !!(flags & ui::EF_RIGHT_MOUSE_BUTTON),
56 VK_RBUTTON);
57 SetKeyState(keyboard_state, !!(flags & ui::EF_MIDDLE_MOUSE_BUTTON),
58 VK_MBUTTON);
60 ::SetKeyboardState(keyboard_state);
63 void FillCompositionText(
64 const base::string16& text,
65 int32 selection_start,
66 int32 selection_end,
67 const std::vector<metro_viewer::UnderlineInfo>& underlines,
68 ui::CompositionText* composition_text) {
69 composition_text->Clear();
70 composition_text->text = text;
71 composition_text->selection.set_start(selection_start);
72 composition_text->selection.set_end(selection_end);
73 composition_text->underlines.resize(underlines.size());
74 for (size_t i = 0; i < underlines.size(); ++i) {
75 composition_text->underlines[i].start_offset = underlines[i].start_offset;
76 composition_text->underlines[i].end_offset = underlines[i].end_offset;
77 composition_text->underlines[i].color = SK_ColorBLACK;
78 composition_text->underlines[i].thick = underlines[i].thick;
79 composition_text->underlines[i].background_color = SK_ColorTRANSPARENT;
83 } // namespace
85 RemoteWindowTreeHostWin* g_instance = NULL;
87 // static
88 RemoteWindowTreeHostWin* RemoteWindowTreeHostWin::Instance() {
89 return g_instance;
92 RemoteWindowTreeHostWin::RemoteWindowTreeHostWin()
93 : remote_window_(NULL),
94 host_(NULL),
95 ignore_mouse_moves_until_set_cursor_ack_(0),
96 event_flags_(0),
97 window_size_(aura::WindowTreeHost::GetNativeScreenSize()) {
98 CHECK(!g_instance);
99 g_instance = this;
100 prop_.reset(new ui::ViewProp(NULL, kWindowTreeHostWinKey, this));
101 CreateCompositor(GetAcceleratedWidget());
104 RemoteWindowTreeHostWin::~RemoteWindowTreeHostWin() {
105 DestroyCompositor();
106 DestroyDispatcher();
107 DCHECK_EQ(g_instance, this);
108 g_instance = NULL;
111 // static
112 bool RemoteWindowTreeHostWin::IsValid() {
113 return Instance()->remote_window_ != NULL;
116 void RemoteWindowTreeHostWin::SetRemoteWindowHandle(HWND remote_window) {
117 remote_window_ = remote_window;
120 void RemoteWindowTreeHostWin::Connected(IPC::Sender* host) {
121 CHECK(host_ == NULL);
122 DCHECK(remote_window_);
123 host_ = host;
124 // Recreate the compositor for the target surface represented by the
125 // remote_window HWND.
126 CreateCompositor(remote_window_);
127 InitCompositor();
130 void RemoteWindowTreeHostWin::Disconnected() {
131 // Don't CHECK here, Disconnected is called on a channel error which can
132 // happen before we're successfully Connected.
133 if (!host_)
134 return;
135 ui::RemoteInputMethodPrivateWin* remote_input_method_private =
136 GetRemoteInputMethodPrivate();
137 if (remote_input_method_private)
138 remote_input_method_private->SetRemoteDelegate(NULL);
139 host_ = NULL;
140 remote_window_ = NULL;
143 bool RemoteWindowTreeHostWin::OnMessageReceived(const IPC::Message& message) {
144 bool handled = true;
145 IPC_BEGIN_MESSAGE_MAP(RemoteWindowTreeHostWin, message)
146 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_MouseMoved, OnMouseMoved)
147 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_MouseButton, OnMouseButton)
148 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_KeyDown, OnKeyDown)
149 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_KeyUp, OnKeyUp)
150 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_Character, OnChar)
151 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_WindowActivated,
152 OnWindowActivated)
153 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_EdgeGesture, OnEdgeGesture)
154 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_TouchDown,
155 OnTouchDown)
156 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_TouchUp,
157 OnTouchUp)
158 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_TouchMoved,
159 OnTouchMoved)
160 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_SetCursorPosAck,
161 OnSetCursorPosAck)
162 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_ImeCandidatePopupChanged,
163 OnImeCandidatePopupChanged)
164 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_ImeCompositionChanged,
165 OnImeCompositionChanged)
166 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_ImeTextCommitted,
167 OnImeTextCommitted)
168 IPC_MESSAGE_HANDLER(MetroViewerHostMsg_ImeInputSourceChanged,
169 OnImeInputSourceChanged)
170 IPC_MESSAGE_UNHANDLED(handled = false)
171 IPC_END_MESSAGE_MAP()
172 return handled;
175 void RemoteWindowTreeHostWin::HandleOpenURLOnDesktop(
176 const base::FilePath& shortcut,
177 const base::string16& url) {
178 if (!host_)
179 return;
180 host_->Send(new MetroViewerHostMsg_OpenURLOnDesktop(shortcut, url));
183 void RemoteWindowTreeHostWin::HandleWindowSizeChanged(uint32 width,
184 uint32 height) {
185 SetBounds(gfx::Rect(0, 0, width, height));
188 bool RemoteWindowTreeHostWin::IsForegroundWindow() {
189 return ::GetForegroundWindow() == remote_window_;
192 Window* RemoteWindowTreeHostWin::GetAshWindow() {
193 return window();
196 ui::EventSource* RemoteWindowTreeHostWin::GetEventSource() {
197 return this;
200 gfx::AcceleratedWidget RemoteWindowTreeHostWin::GetAcceleratedWidget() {
201 if (remote_window_)
202 return remote_window_;
203 // Getting here should only happen for ash_unittests.exe and related code.
204 return ::GetDesktopWindow();
207 void RemoteWindowTreeHostWin::ShowImpl() {
208 ui::RemoteInputMethodPrivateWin* remote_input_method_private =
209 GetRemoteInputMethodPrivate();
210 if (remote_input_method_private)
211 remote_input_method_private->SetRemoteDelegate(this);
214 void RemoteWindowTreeHostWin::HideImpl() {
215 NOTIMPLEMENTED();
218 gfx::Rect RemoteWindowTreeHostWin::GetBounds() const {
219 return gfx::Rect(window_size_);
222 void RemoteWindowTreeHostWin::SetBounds(const gfx::Rect& bounds) {
223 window_size_ = bounds.size();
224 OnHostResized(bounds.size());
227 gfx::Point RemoteWindowTreeHostWin::GetLocationOnNativeScreen() const {
228 return gfx::Point(0, 0);
231 void RemoteWindowTreeHostWin::SetCapture() {
234 void RemoteWindowTreeHostWin::ReleaseCapture() {
237 void RemoteWindowTreeHostWin::SetCursorNative(gfx::NativeCursor native_cursor) {
238 if (!host_)
239 return;
240 host_->Send(
241 new MetroViewerHostMsg_SetCursor(uint64(native_cursor.platform())));
244 void RemoteWindowTreeHostWin::MoveCursorToNative(const gfx::Point& location) {
245 VLOG(1) << "In MoveCursorTo: " << location.x() << ", " << location.y();
246 if (!host_)
247 return;
249 // This function can be called in cases like when the mouse cursor is
250 // restricted within a viewport (For e.g. LockCursor) which assumes that
251 // subsequent mouse moves would be received starting with the new cursor
252 // coordinates. This is a challenge for Windows ASH for the reasons
253 // outlined below.
254 // Other cases which don't expect this behavior should continue to work
255 // without issues.
257 // The mouse events are received by the viewer process and sent to the
258 // browser. If we invoke the SetCursor API here we continue to receive
259 // mouse messages from the viewer which were posted before the SetCursor
260 // API executes which messes up the state in the browser. To workaround
261 // this we invoke the SetCursor API in the viewer process and ignore
262 // mouse messages until we received an ACK from the viewer indicating that
263 // the SetCursor operation completed.
264 ignore_mouse_moves_until_set_cursor_ack_++;
265 VLOG(1) << "In MoveCursorTo. Sending IPC";
266 host_->Send(new MetroViewerHostMsg_SetCursorPos(location.x(), location.y()));
269 void RemoteWindowTreeHostWin::OnCursorVisibilityChangedNative(bool show) {
270 NOTIMPLEMENTED();
273 void RemoteWindowTreeHostWin::CancelComposition() {
274 if (!host_)
275 return;
276 host_->Send(new MetroViewerHostMsg_ImeCancelComposition);
279 void RemoteWindowTreeHostWin::OnTextInputClientUpdated(
280 const std::vector<int32>& input_scopes,
281 const std::vector<gfx::Rect>& composition_character_bounds) {
282 if (!host_)
283 return;
284 std::vector<metro_viewer::CharacterBounds> character_bounds;
285 for (size_t i = 0; i < composition_character_bounds.size(); ++i) {
286 const gfx::Rect& rect = composition_character_bounds[i];
287 metro_viewer::CharacterBounds bounds;
288 bounds.left = rect.x();
289 bounds.top = rect.y();
290 bounds.right = rect.right();
291 bounds.bottom = rect.bottom();
292 character_bounds.push_back(bounds);
294 host_->Send(new MetroViewerHostMsg_ImeTextInputClientUpdated(
295 input_scopes, character_bounds));
298 gfx::Point PointFromNativeEvent(int32 x, int32 y) {
299 static float scale_factor = gfx::GetDPIScale();
300 gfx::Point result( x * scale_factor, y * scale_factor);
301 return result;
304 void RemoteWindowTreeHostWin::OnMouseMoved(int32 x, int32 y, int32 flags) {
305 if (ignore_mouse_moves_until_set_cursor_ack_)
306 return;
308 gfx::Point location = PointFromNativeEvent(x, y);
309 ui::MouseEvent event(ui::ET_MOUSE_MOVED, location, location,
310 ui::EventTimeForNow(), flags, 0);
311 SendEventToProcessor(&event);
314 void RemoteWindowTreeHostWin::OnMouseButton(
315 const MetroViewerHostMsg_MouseButtonParams& params) {
316 gfx::Point location = PointFromNativeEvent(params.x, params.y);
317 ui::MouseEvent mouse_event(
318 params.event_type, location, location, ui::EventTimeForNow(),
319 static_cast<int>(params.flags), static_cast<int>(params.changed_button));
321 SetEventFlags(params.flags | key_event_flags());
322 if (params.event_type == ui::ET_MOUSEWHEEL) {
323 int x_offset = params.is_horizontal_wheel ? params.extra : 0;
324 int y_offset = !params.is_horizontal_wheel ? params.extra : 0;
325 ui::MouseWheelEvent wheel_event(mouse_event, x_offset, y_offset);
326 SendEventToProcessor(&wheel_event);
327 } else if (params.event_type == ui::ET_MOUSE_PRESSED) {
328 // TODO(shrikant): Ideally modify code in event.cc by adding automatic
329 // tracking of double clicks in synthetic MouseEvent constructor code.
330 // Non-synthetic MouseEvent constructor code does automatically track
331 // this. Need to use some caution while modifying synthetic constructor
332 // as many tests and other code paths depend on it and apparently
333 // specifically depend on non implicit tracking of previous mouse event.
334 if (last_mouse_click_event_ &&
335 ui::MouseEvent::IsRepeatedClickEvent(mouse_event,
336 *last_mouse_click_event_)) {
337 mouse_event.SetClickCount(2);
338 } else {
339 mouse_event.SetClickCount(1);
341 last_mouse_click_event_ .reset(new ui::MouseEvent(mouse_event));
342 SendEventToProcessor(&mouse_event);
343 } else {
344 SendEventToProcessor(&mouse_event);
348 void RemoteWindowTreeHostWin::OnKeyDown(uint32 vkey,
349 uint32 repeat_count,
350 uint32 scan_code,
351 uint32 flags) {
352 DispatchKeyboardMessage(ui::ET_KEY_PRESSED, vkey, repeat_count, scan_code,
353 flags, false);
356 void RemoteWindowTreeHostWin::OnKeyUp(uint32 vkey,
357 uint32 repeat_count,
358 uint32 scan_code,
359 uint32 flags) {
360 DispatchKeyboardMessage(ui::ET_KEY_RELEASED, vkey, repeat_count, scan_code,
361 flags, false);
364 void RemoteWindowTreeHostWin::OnChar(uint32 key_code,
365 uint32 repeat_count,
366 uint32 scan_code,
367 uint32 flags) {
368 DispatchKeyboardMessage(ui::ET_KEY_PRESSED, key_code, repeat_count,
369 scan_code, flags, true);
372 void RemoteWindowTreeHostWin::OnWindowActivated(bool repaint) {
373 OnHostActivated();
374 if (repaint && compositor())
375 compositor()->ScheduleFullRedraw();
378 void RemoteWindowTreeHostWin::OnEdgeGesture() {
379 ui::GestureEvent event(
383 ui::EventTimeForNow(),
384 ui::GestureEventDetails(ui::ET_GESTURE_WIN8_EDGE_SWIPE));
385 SendEventToProcessor(&event);
388 void RemoteWindowTreeHostWin::OnTouchDown(int32 x,
389 int32 y,
390 uint64 timestamp,
391 uint32 pointer_id) {
392 gfx::Point location = PointFromNativeEvent(x, y);
393 ui::TouchEvent event(ui::ET_TOUCH_PRESSED,
394 location,
395 pointer_id,
396 base::TimeDelta::FromMicroseconds(timestamp));
397 SendEventToProcessor(&event);
400 void RemoteWindowTreeHostWin::OnTouchUp(int32 x,
401 int32 y,
402 uint64 timestamp,
403 uint32 pointer_id) {
404 gfx::Point location = PointFromNativeEvent(x, y);
405 ui::TouchEvent event(ui::ET_TOUCH_RELEASED,
406 location,
407 pointer_id,
408 base::TimeDelta::FromMicroseconds(timestamp));
409 SendEventToProcessor(&event);
412 void RemoteWindowTreeHostWin::OnTouchMoved(int32 x,
413 int32 y,
414 uint64 timestamp,
415 uint32 pointer_id) {
416 gfx::Point location = PointFromNativeEvent(x, y);
417 ui::TouchEvent event(ui::ET_TOUCH_MOVED,
418 location,
419 pointer_id,
420 base::TimeDelta::FromMicroseconds(timestamp));
421 SendEventToProcessor(&event);
424 void RemoteWindowTreeHostWin::OnSetCursorPosAck() {
425 DCHECK_GT(ignore_mouse_moves_until_set_cursor_ack_, 0);
426 ignore_mouse_moves_until_set_cursor_ack_--;
429 ui::RemoteInputMethodPrivateWin*
430 RemoteWindowTreeHostWin::GetRemoteInputMethodPrivate() {
431 return ui::RemoteInputMethodPrivateWin::Get(GetInputMethod());
434 void RemoteWindowTreeHostWin::OnImeCandidatePopupChanged(bool visible) {
435 ui::RemoteInputMethodPrivateWin* remote_input_method_private =
436 GetRemoteInputMethodPrivate();
437 if (!remote_input_method_private)
438 return;
439 remote_input_method_private->OnCandidatePopupChanged(visible);
442 void RemoteWindowTreeHostWin::OnImeCompositionChanged(
443 const base::string16& text,
444 int32 selection_start,
445 int32 selection_end,
446 const std::vector<metro_viewer::UnderlineInfo>& underlines) {
447 ui::RemoteInputMethodPrivateWin* remote_input_method_private =
448 GetRemoteInputMethodPrivate();
449 if (!remote_input_method_private)
450 return;
451 ui::CompositionText composition_text;
452 FillCompositionText(
453 text, selection_start, selection_end, underlines, &composition_text);
454 remote_input_method_private->OnCompositionChanged(composition_text);
457 void RemoteWindowTreeHostWin::OnImeTextCommitted(const base::string16& text) {
458 ui::RemoteInputMethodPrivateWin* remote_input_method_private =
459 GetRemoteInputMethodPrivate();
460 if (!remote_input_method_private)
461 return;
462 remote_input_method_private->OnTextCommitted(text);
465 void RemoteWindowTreeHostWin::OnImeInputSourceChanged(uint16 language_id,
466 bool is_ime) {
467 ui::RemoteInputMethodPrivateWin* remote_input_method_private =
468 GetRemoteInputMethodPrivate();
469 if (!remote_input_method_private)
470 return;
471 remote_input_method_private->OnInputSourceChanged(language_id, is_ime);
474 void RemoteWindowTreeHostWin::DispatchKeyboardMessage(ui::EventType type,
475 uint32 vkey,
476 uint32 repeat_count,
477 uint32 scan_code,
478 uint32 flags,
479 bool is_character) {
480 SetEventFlags(flags | mouse_event_flags());
481 if (base::MessageLoop::current()->IsNested()) {
482 int index = (flags & ui::EF_ALT_DOWN) ? 1 : 0;
483 const int char_message[] = {WM_CHAR, WM_SYSCHAR};
484 const int keydown_message[] = {WM_KEYDOWN, WM_SYSKEYDOWN};
485 const int keyup_message[] = {WM_KEYUP, WM_SYSKEYUP};
486 uint32 message = is_character
487 ? char_message[index]
488 : (type == ui::ET_KEY_PRESSED ? keydown_message[index]
489 : keyup_message[index]);
490 ::PostThreadMessage(::GetCurrentThreadId(),
491 message,
492 vkey,
493 repeat_count | scan_code >> 15);
494 } else if (is_character) {
495 ui::KeyEvent event(static_cast<base::char16>(vkey),
496 ui::KeyboardCodeForWindowsKeyCode(vkey),
497 flags);
498 SendEventToProcessor(&event);
499 } else {
500 ui::KeyEvent event(type,
501 ui::KeyboardCodeForWindowsKeyCode(vkey),
502 flags);
503 SendEventToProcessor(&event);
507 void RemoteWindowTreeHostWin::SetEventFlags(uint32 flags) {
508 if (flags == event_flags_)
509 return;
510 event_flags_ = flags;
511 SetVirtualKeyStates(event_flags_);
514 } // namespace aura