cc: Make picture pile base thread safe.
[chromium-blink-merge.git] / content / browser / renderer_host / render_widget_host_view_aura.cc
bloba735aff095f707f38d36f69c7fe8b78be89dc7c5
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 "content/browser/renderer_host/render_widget_host_view_aura.h"
7 #include "base/auto_reset.h"
8 #include "base/basictypes.h"
9 #include "base/bind.h"
10 #include "base/callback_helpers.h"
11 #include "base/command_line.h"
12 #include "base/debug/trace_event.h"
13 #include "base/logging.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "cc/layers/layer.h"
17 #include "cc/output/copy_output_request.h"
18 #include "cc/output/copy_output_result.h"
19 #include "cc/resources/texture_mailbox.h"
20 #include "cc/trees/layer_tree_settings.h"
21 #include "content/browser/accessibility/browser_accessibility_manager.h"
22 #include "content/browser/accessibility/browser_accessibility_state_impl.h"
23 #include "content/browser/frame_host/frame_tree.h"
24 #include "content/browser/frame_host/frame_tree_node.h"
25 #include "content/browser/frame_host/render_frame_host_impl.h"
26 #include "content/browser/gpu/compositor_util.h"
27 #include "content/browser/renderer_host/compositor_resize_lock_aura.h"
28 #include "content/browser/renderer_host/dip_util.h"
29 #include "content/browser/renderer_host/input/synthetic_gesture_target_aura.h"
30 #include "content/browser/renderer_host/overscroll_controller.h"
31 #include "content/browser/renderer_host/render_view_host_delegate.h"
32 #include "content/browser/renderer_host/render_view_host_delegate_view.h"
33 #include "content/browser/renderer_host/render_view_host_impl.h"
34 #include "content/browser/renderer_host/render_widget_host_impl.h"
35 #include "content/browser/renderer_host/ui_events_helper.h"
36 #include "content/browser/renderer_host/web_input_event_aura.h"
37 #include "content/common/gpu/client/gl_helper.h"
38 #include "content/common/gpu/gpu_messages.h"
39 #include "content/common/view_messages.h"
40 #include "content/public/browser/content_browser_client.h"
41 #include "content/public/browser/overscroll_configuration.h"
42 #include "content/public/browser/render_view_host.h"
43 #include "content/public/browser/render_widget_host_view_frame_subscriber.h"
44 #include "content/public/browser/user_metrics.h"
45 #include "content/public/common/content_switches.h"
46 #include "third_party/WebKit/public/platform/WebScreenInfo.h"
47 #include "third_party/WebKit/public/web/WebCompositionUnderline.h"
48 #include "third_party/WebKit/public/web/WebInputEvent.h"
49 #include "ui/aura/client/aura_constants.h"
50 #include "ui/aura/client/cursor_client.h"
51 #include "ui/aura/client/cursor_client_observer.h"
52 #include "ui/aura/client/focus_client.h"
53 #include "ui/aura/client/screen_position_client.h"
54 #include "ui/aura/client/window_tree_client.h"
55 #include "ui/aura/env.h"
56 #include "ui/aura/window.h"
57 #include "ui/aura/window_event_dispatcher.h"
58 #include "ui/aura/window_observer.h"
59 #include "ui/aura/window_tracker.h"
60 #include "ui/aura/window_tree_host.h"
61 #include "ui/base/clipboard/scoped_clipboard_writer.h"
62 #include "ui/base/hit_test.h"
63 #include "ui/base/ime/input_method.h"
64 #include "ui/base/ui_base_types.h"
65 #include "ui/compositor/compositor_vsync_manager.h"
66 #include "ui/compositor/dip_util.h"
67 #include "ui/events/event.h"
68 #include "ui/events/event_utils.h"
69 #include "ui/events/gestures/gesture_recognizer.h"
70 #include "ui/gfx/canvas.h"
71 #include "ui/gfx/display.h"
72 #include "ui/gfx/rect_conversions.h"
73 #include "ui/gfx/screen.h"
74 #include "ui/gfx/size_conversions.h"
75 #include "ui/gfx/skia_util.h"
76 #include "ui/wm/public/activation_client.h"
77 #include "ui/wm/public/scoped_tooltip_disabler.h"
78 #include "ui/wm/public/tooltip_client.h"
79 #include "ui/wm/public/transient_window_client.h"
80 #include "ui/wm/public/window_types.h"
82 #if defined(OS_WIN)
83 #include "content/browser/accessibility/browser_accessibility_manager_win.h"
84 #include "content/browser/accessibility/browser_accessibility_win.h"
85 #include "content/browser/renderer_host/legacy_render_widget_host_win.h"
86 #include "content/common/plugin_constants_win.h"
87 #include "ui/base/win/hidden_window.h"
88 #include "ui/gfx/gdi_util.h"
89 #include "ui/gfx/win/dpi.h"
90 #endif
92 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
93 #include "content/common/input_messages.h"
94 #include "ui/events/linux/text_edit_command_auralinux.h"
95 #include "ui/events/linux/text_edit_key_bindings_delegate_auralinux.h"
96 #endif
98 using gfx::RectToSkIRect;
99 using gfx::SkIRectToRect;
101 using blink::WebScreenInfo;
102 using blink::WebInputEvent;
103 using blink::WebGestureEvent;
104 using blink::WebTouchEvent;
106 namespace content {
108 namespace {
110 // In mouse lock mode, we need to prevent the (invisible) cursor from hitting
111 // the border of the view, in order to get valid movement information. However,
112 // forcing the cursor back to the center of the view after each mouse move
113 // doesn't work well. It reduces the frequency of useful mouse move messages
114 // significantly. Therefore, we move the cursor to the center of the view only
115 // if it approaches the border. |kMouseLockBorderPercentage| specifies the width
116 // of the border area, in percentage of the corresponding dimension.
117 const int kMouseLockBorderPercentage = 15;
119 // When accelerated compositing is enabled and a widget resize is pending,
120 // we delay further resizes of the UI. The following constant is the maximum
121 // length of time that we should delay further UI resizes while waiting for a
122 // resized frame from a renderer.
123 const int kResizeLockTimeoutMs = 67;
125 #if defined(OS_WIN)
126 // Used to associate a plugin HWND with its RenderWidgetHostViewAura instance.
127 const wchar_t kWidgetOwnerProperty[] = L"RenderWidgetHostViewAuraOwner";
129 BOOL CALLBACK WindowDestroyingCallback(HWND window, LPARAM param) {
130 RenderWidgetHostViewAura* widget =
131 reinterpret_cast<RenderWidgetHostViewAura*>(param);
132 if (GetProp(window, kWidgetOwnerProperty) == widget) {
133 // Properties set on HWNDs must be removed to avoid leaks.
134 RemoveProp(window, kWidgetOwnerProperty);
135 RenderWidgetHostViewBase::DetachPluginWindowsCallback(window);
137 return TRUE;
140 BOOL CALLBACK HideWindowsCallback(HWND window, LPARAM param) {
141 RenderWidgetHostViewAura* widget =
142 reinterpret_cast<RenderWidgetHostViewAura*>(param);
143 if (GetProp(window, kWidgetOwnerProperty) == widget)
144 SetParent(window, ui::GetHiddenWindow());
145 return TRUE;
148 BOOL CALLBACK ShowWindowsCallback(HWND window, LPARAM param) {
149 RenderWidgetHostViewAura* widget =
150 reinterpret_cast<RenderWidgetHostViewAura*>(param);
152 if (GetProp(window, kWidgetOwnerProperty) == widget &&
153 widget->GetNativeView()->GetHost()) {
154 HWND parent = widget->GetNativeView()->GetHost()->GetAcceleratedWidget();
155 SetParent(window, parent);
157 return TRUE;
160 struct CutoutRectsParams {
161 RenderWidgetHostViewAura* widget;
162 std::vector<gfx::Rect> cutout_rects;
163 std::map<HWND, WebPluginGeometry>* geometry;
166 // Used to update the region for the windowed plugin to draw in. We start with
167 // the clip rect from the renderer, then remove the cutout rects from the
168 // renderer, and then remove the transient windows from the root window and the
169 // constrained windows from the parent window.
170 BOOL CALLBACK SetCutoutRectsCallback(HWND window, LPARAM param) {
171 CutoutRectsParams* params = reinterpret_cast<CutoutRectsParams*>(param);
173 if (GetProp(window, kWidgetOwnerProperty) == params->widget) {
174 // First calculate the offset of this plugin from the root window, since
175 // the cutouts are relative to the root window.
176 HWND parent =
177 params->widget->GetNativeView()->GetHost()->GetAcceleratedWidget();
178 POINT offset;
179 offset.x = offset.y = 0;
180 MapWindowPoints(window, parent, &offset, 1);
182 // Now get the cached clip rect and cutouts for this plugin window that came
183 // from the renderer.
184 std::map<HWND, WebPluginGeometry>::iterator i = params->geometry->begin();
185 while (i != params->geometry->end() &&
186 i->second.window != window &&
187 GetParent(i->second.window) != window) {
188 ++i;
191 if (i == params->geometry->end()) {
192 NOTREACHED();
193 return TRUE;
196 HRGN hrgn = CreateRectRgn(i->second.clip_rect.x(),
197 i->second.clip_rect.y(),
198 i->second.clip_rect.right(),
199 i->second.clip_rect.bottom());
200 // We start with the cutout rects that came from the renderer, then add the
201 // ones that came from transient and constrained windows.
202 std::vector<gfx::Rect> cutout_rects = i->second.cutout_rects;
203 for (size_t i = 0; i < params->cutout_rects.size(); ++i) {
204 gfx::Rect offset_cutout = params->cutout_rects[i];
205 offset_cutout.Offset(-offset.x, -offset.y);
206 cutout_rects.push_back(offset_cutout);
208 gfx::SubtractRectanglesFromRegion(hrgn, cutout_rects);
209 // If we don't have any cutout rects then no point in messing with the
210 // window region.
211 if (cutout_rects.size())
212 SetWindowRgn(window, hrgn, TRUE);
214 return TRUE;
217 // A callback function for EnumThreadWindows to enumerate and dismiss
218 // any owned popup windows.
219 BOOL CALLBACK DismissOwnedPopups(HWND window, LPARAM arg) {
220 const HWND toplevel_hwnd = reinterpret_cast<HWND>(arg);
222 if (::IsWindowVisible(window)) {
223 const HWND owner = ::GetWindow(window, GW_OWNER);
224 if (toplevel_hwnd == owner) {
225 ::PostMessage(window, WM_CANCELMODE, 0, 0);
229 return TRUE;
231 #endif
233 void UpdateWebTouchEventAfterDispatch(blink::WebTouchEvent* event,
234 blink::WebTouchPoint* point) {
235 if (point->state != blink::WebTouchPoint::StateReleased &&
236 point->state != blink::WebTouchPoint::StateCancelled)
237 return;
239 const unsigned new_length = event->touchesLength - 1;
240 // Work around a gcc 4.9 bug. crbug.com/392872
241 if (new_length >= event->touchesLengthCap)
242 return;
244 for (unsigned i = point - event->touches; i < new_length; ++i)
245 event->touches[i] = event->touches[i + 1];
246 event->touchesLength = new_length;
249 bool CanRendererHandleEvent(const ui::MouseEvent* event) {
250 if (event->type() == ui::ET_MOUSE_CAPTURE_CHANGED)
251 return false;
253 #if defined(OS_WIN)
254 // Renderer cannot handle WM_XBUTTON or NC events.
255 switch (event->native_event().message) {
256 case WM_XBUTTONDOWN:
257 case WM_XBUTTONUP:
258 case WM_XBUTTONDBLCLK:
259 case WM_NCMOUSELEAVE:
260 case WM_NCMOUSEMOVE:
261 case WM_NCLBUTTONDOWN:
262 case WM_NCLBUTTONUP:
263 case WM_NCLBUTTONDBLCLK:
264 case WM_NCRBUTTONDOWN:
265 case WM_NCRBUTTONUP:
266 case WM_NCRBUTTONDBLCLK:
267 case WM_NCMBUTTONDOWN:
268 case WM_NCMBUTTONUP:
269 case WM_NCMBUTTONDBLCLK:
270 case WM_NCXBUTTONDOWN:
271 case WM_NCXBUTTONUP:
272 case WM_NCXBUTTONDBLCLK:
273 return false;
274 default:
275 break;
277 #elif defined(USE_X11)
278 // Renderer only supports standard mouse buttons, so ignore programmable
279 // buttons.
280 switch (event->type()) {
281 case ui::ET_MOUSE_PRESSED:
282 case ui::ET_MOUSE_RELEASED:
283 return event->IsAnyButton();
284 default:
285 break;
287 #endif
288 return true;
291 // We don't mark these as handled so that they're sent back to the
292 // DefWindowProc so it can generate WM_APPCOMMAND as necessary.
293 bool IsXButtonUpEvent(const ui::MouseEvent* event) {
294 #if defined(OS_WIN)
295 switch (event->native_event().message) {
296 case WM_XBUTTONUP:
297 case WM_NCXBUTTONUP:
298 return true;
300 #endif
301 return false;
304 void GetScreenInfoForWindow(WebScreenInfo* results, aura::Window* window) {
305 const gfx::Display display = window ?
306 gfx::Screen::GetScreenFor(window)->GetDisplayNearestWindow(window) :
307 gfx::Screen::GetScreenFor(window)->GetPrimaryDisplay();
308 results->rect = display.bounds();
309 results->availableRect = display.work_area();
310 // TODO(derat|oshima): Don't hardcode this. Get this from display object.
311 results->depth = 24;
312 results->depthPerComponent = 8;
313 results->deviceScaleFactor = display.device_scale_factor();
315 // The Display rotation and the WebScreenInfo orientation are not the same
316 // angle. The former is the physical display rotation while the later is the
317 // rotation required by the content to be shown properly on the screen, in
318 // other words, relative to the physical display.
319 results->orientationAngle = display.RotationAsDegree();
320 if (results->orientationAngle == 90)
321 results->orientationAngle = 270;
322 else if (results->orientationAngle == 270)
323 results->orientationAngle = 90;
325 results->orientationType =
326 RenderWidgetHostViewBase::GetOrientationTypeForDesktop(display);
329 bool PointerEventActivates(const ui::Event& event) {
330 if (event.type() == ui::ET_MOUSE_PRESSED)
331 return true;
333 if (event.type() == ui::ET_GESTURE_BEGIN) {
334 const ui::GestureEvent& gesture =
335 static_cast<const ui::GestureEvent&>(event);
336 return gesture.details().touch_points() == 1;
339 return false;
342 } // namespace
344 // We need to watch for mouse events outside a Web Popup or its parent
345 // and dismiss the popup for certain events.
346 class RenderWidgetHostViewAura::EventFilterForPopupExit
347 : public ui::EventHandler {
348 public:
349 explicit EventFilterForPopupExit(RenderWidgetHostViewAura* rwhva)
350 : rwhva_(rwhva) {
351 DCHECK(rwhva_);
352 aura::Env::GetInstance()->AddPreTargetHandler(this);
355 virtual ~EventFilterForPopupExit() {
356 aura::Env::GetInstance()->RemovePreTargetHandler(this);
359 // Overridden from ui::EventHandler
360 virtual void OnMouseEvent(ui::MouseEvent* event) override {
361 rwhva_->ApplyEventFilterForPopupExit(event);
364 virtual void OnTouchEvent(ui::TouchEvent* event) override {
365 rwhva_->ApplyEventFilterForPopupExit(event);
368 private:
369 RenderWidgetHostViewAura* rwhva_;
371 DISALLOW_COPY_AND_ASSIGN(EventFilterForPopupExit);
374 void RenderWidgetHostViewAura::ApplyEventFilterForPopupExit(
375 ui::LocatedEvent* event) {
376 if (in_shutdown_ || is_fullscreen_ || !event->target())
377 return;
379 if (event->type() != ui::ET_MOUSE_PRESSED &&
380 event->type() != ui::ET_TOUCH_PRESSED) {
381 return;
384 aura::Window* target = static_cast<aura::Window*>(event->target());
385 if (target != window_ &&
386 (!popup_parent_host_view_ ||
387 target != popup_parent_host_view_->window_)) {
388 // Note: popup_parent_host_view_ may be NULL when there are multiple
389 // popup children per view. See: RenderWidgetHostViewAura::InitAsPopup().
390 in_shutdown_ = true;
391 host_->Shutdown();
395 // We have to implement the WindowObserver interface on a separate object
396 // because clang doesn't like implementing multiple interfaces that have
397 // methods with the same name. This object is owned by the
398 // RenderWidgetHostViewAura.
399 class RenderWidgetHostViewAura::WindowObserver : public aura::WindowObserver {
400 public:
401 explicit WindowObserver(RenderWidgetHostViewAura* view)
402 : view_(view) {
403 view_->window_->AddObserver(this);
406 virtual ~WindowObserver() {
407 view_->window_->RemoveObserver(this);
410 // Overridden from aura::WindowObserver:
411 virtual void OnWindowAddedToRootWindow(aura::Window* window) override {
412 if (window == view_->window_)
413 view_->AddedToRootWindow();
416 virtual void OnWindowRemovingFromRootWindow(aura::Window* window,
417 aura::Window* new_root) override {
418 if (window == view_->window_)
419 view_->RemovingFromRootWindow();
422 private:
423 RenderWidgetHostViewAura* view_;
425 DISALLOW_COPY_AND_ASSIGN(WindowObserver);
428 ////////////////////////////////////////////////////////////////////////////////
429 // RenderWidgetHostViewAura, public:
431 RenderWidgetHostViewAura::RenderWidgetHostViewAura(RenderWidgetHost* host,
432 bool is_guest_view_hack)
433 : host_(RenderWidgetHostImpl::From(host)),
434 window_(new aura::Window(this)),
435 delegated_frame_host_(new DelegatedFrameHost(this)),
436 in_shutdown_(false),
437 in_bounds_changed_(false),
438 is_fullscreen_(false),
439 popup_parent_host_view_(NULL),
440 popup_child_host_view_(NULL),
441 is_loading_(false),
442 text_input_type_(ui::TEXT_INPUT_TYPE_NONE),
443 text_input_mode_(ui::TEXT_INPUT_MODE_DEFAULT),
444 text_input_flags_(0),
445 can_compose_inline_(true),
446 has_composition_text_(false),
447 accept_return_character_(false),
448 last_swapped_software_frame_scale_factor_(1.f),
449 paint_canvas_(NULL),
450 synthetic_move_sent_(false),
451 cursor_visibility_state_in_renderer_(UNKNOWN),
452 #if defined(OS_WIN)
453 legacy_render_widget_host_HWND_(NULL),
454 legacy_window_destroyed_(false),
455 #endif
456 has_snapped_to_boundary_(false),
457 touch_editing_client_(NULL),
458 is_guest_view_hack_(is_guest_view_hack),
459 weak_ptr_factory_(this) {
460 if (!is_guest_view_hack_)
461 host_->SetView(this);
463 window_observer_.reset(new WindowObserver(this));
464 aura::client::SetTooltipText(window_, &tooltip_);
465 aura::client::SetActivationDelegate(window_, this);
466 aura::client::SetActivationChangeObserver(window_, this);
467 aura::client::SetFocusChangeObserver(window_, this);
468 window_->set_layer_owner_delegate(delegated_frame_host_.get());
469 gfx::Screen::GetScreenFor(window_)->AddObserver(this);
471 bool overscroll_enabled = base::CommandLine::ForCurrentProcess()->
472 GetSwitchValueASCII(switches::kOverscrollHistoryNavigation) != "0";
473 SetOverscrollControllerEnabled(overscroll_enabled);
476 ////////////////////////////////////////////////////////////////////////////////
477 // RenderWidgetHostViewAura, RenderWidgetHostView implementation:
479 bool RenderWidgetHostViewAura::OnMessageReceived(
480 const IPC::Message& message) {
481 bool handled = true;
482 IPC_BEGIN_MESSAGE_MAP(RenderWidgetHostViewAura, message)
483 // TODO(kevers): Move to RenderWidgetHostViewImpl and consolidate IPC
484 // messages for TextInput<State|Type>Changed. Corresponding code in
485 // RenderWidgetHostViewAndroid should also be moved at the same time.
486 IPC_MESSAGE_HANDLER(ViewHostMsg_TextInputStateChanged,
487 OnTextInputStateChanged)
488 IPC_MESSAGE_UNHANDLED(handled = false)
489 IPC_END_MESSAGE_MAP()
490 return handled;
493 void RenderWidgetHostViewAura::InitAsChild(
494 gfx::NativeView parent_view) {
495 window_->SetType(ui::wm::WINDOW_TYPE_CONTROL);
496 window_->Init(aura::WINDOW_LAYER_SOLID_COLOR);
497 window_->SetName("RenderWidgetHostViewAura");
498 window_->layer()->SetColor(background_color_);
501 void RenderWidgetHostViewAura::InitAsPopup(
502 RenderWidgetHostView* parent_host_view,
503 const gfx::Rect& bounds_in_screen) {
504 popup_parent_host_view_ =
505 static_cast<RenderWidgetHostViewAura*>(parent_host_view);
507 // TransientWindowClient may be NULL during tests.
508 aura::client::TransientWindowClient* transient_window_client =
509 aura::client::GetTransientWindowClient();
510 RenderWidgetHostViewAura* old_child =
511 popup_parent_host_view_->popup_child_host_view_;
512 if (old_child) {
513 // TODO(jhorwich): Allow multiple popup_child_host_view_ per view, or
514 // similar mechanism to ensure a second popup doesn't cause the first one
515 // to never get a chance to filter events. See crbug.com/160589.
516 DCHECK(old_child->popup_parent_host_view_ == popup_parent_host_view_);
517 if (transient_window_client) {
518 transient_window_client->RemoveTransientChild(
519 popup_parent_host_view_->window_, old_child->window_);
521 old_child->popup_parent_host_view_ = NULL;
523 popup_parent_host_view_->popup_child_host_view_ = this;
524 window_->SetType(ui::wm::WINDOW_TYPE_MENU);
525 window_->Init(aura::WINDOW_LAYER_SOLID_COLOR);
526 window_->SetName("RenderWidgetHostViewAura");
527 window_->layer()->SetColor(background_color_);
529 // Setting the transient child allows for the popup to get mouse events when
530 // in a system modal dialog. Do this before calling ParentWindowWithContext
531 // below so that the transient parent is visible to WindowTreeClient.
532 // This fixes crbug.com/328593.
533 if (transient_window_client) {
534 transient_window_client->AddTransientChild(
535 popup_parent_host_view_->window_, window_);
538 aura::Window* root = popup_parent_host_view_->window_->GetRootWindow();
539 aura::client::ParentWindowWithContext(window_, root, bounds_in_screen);
541 SetBounds(bounds_in_screen);
542 Show();
543 if (NeedsMouseCapture())
544 window_->SetCapture();
546 event_filter_for_popup_exit_.reset(new EventFilterForPopupExit(this));
549 void RenderWidgetHostViewAura::InitAsFullscreen(
550 RenderWidgetHostView* reference_host_view) {
551 is_fullscreen_ = true;
552 window_->SetType(ui::wm::WINDOW_TYPE_NORMAL);
553 window_->Init(aura::WINDOW_LAYER_SOLID_COLOR);
554 window_->SetName("RenderWidgetHostViewAura");
555 window_->SetProperty(aura::client::kShowStateKey, ui::SHOW_STATE_FULLSCREEN);
556 window_->layer()->SetColor(background_color_);
558 aura::Window* parent = NULL;
559 gfx::Rect bounds;
560 if (reference_host_view) {
561 aura::Window* reference_window =
562 static_cast<RenderWidgetHostViewAura*>(reference_host_view)->window_;
563 if (reference_window) {
564 host_tracker_.reset(new aura::WindowTracker);
565 host_tracker_->Add(reference_window);
567 gfx::Display display = gfx::Screen::GetScreenFor(window_)->
568 GetDisplayNearestWindow(reference_window);
569 parent = reference_window->GetRootWindow();
570 bounds = display.bounds();
572 aura::client::ParentWindowWithContext(window_, parent, bounds);
573 Show();
574 Focus();
577 RenderWidgetHost* RenderWidgetHostViewAura::GetRenderWidgetHost() const {
578 return host_;
581 void RenderWidgetHostViewAura::WasShown() {
582 DCHECK(host_);
583 if (!host_->is_hidden())
584 return;
586 bool has_saved_frame = delegated_frame_host_->HasSavedFrame();
587 ui::LatencyInfo renderer_latency_info, browser_latency_info;
588 if (has_saved_frame) {
589 browser_latency_info.AddLatencyNumber(
590 ui::TAB_SHOW_COMPONENT, host_->GetLatencyComponentId(), 0);
591 } else {
592 renderer_latency_info.AddLatencyNumber(
593 ui::TAB_SHOW_COMPONENT, host_->GetLatencyComponentId(), 0);
595 host_->WasShown(renderer_latency_info);
597 aura::Window* root = window_->GetRootWindow();
598 if (root) {
599 aura::client::CursorClient* cursor_client =
600 aura::client::GetCursorClient(root);
601 if (cursor_client)
602 NotifyRendererOfCursorVisibilityState(cursor_client->IsCursorVisible());
605 delegated_frame_host_->WasShown(browser_latency_info);
607 #if defined(OS_WIN)
608 if (legacy_render_widget_host_HWND_) {
609 // Reparent the legacy Chrome_RenderWidgetHostHWND window to the parent
610 // window before reparenting any plugins. This ensures that the plugin
611 // windows stay on top of the child Zorder in the parent and receive
612 // mouse events, etc.
613 legacy_render_widget_host_HWND_->UpdateParent(
614 GetNativeView()->GetHost()->GetAcceleratedWidget());
615 legacy_render_widget_host_HWND_->SetBounds(
616 window_->GetBoundsInRootWindow());
618 LPARAM lparam = reinterpret_cast<LPARAM>(this);
619 EnumChildWindows(ui::GetHiddenWindow(), ShowWindowsCallback, lparam);
620 #endif
623 void RenderWidgetHostViewAura::WasHidden() {
624 if (!host_ || host_->is_hidden())
625 return;
626 host_->WasHidden();
627 delegated_frame_host_->WasHidden();
629 #if defined(OS_WIN)
630 constrained_rects_.clear();
631 aura::WindowTreeHost* host = window_->GetHost();
632 if (host) {
633 HWND parent = host->GetAcceleratedWidget();
634 LPARAM lparam = reinterpret_cast<LPARAM>(this);
635 EnumChildWindows(parent, HideWindowsCallback, lparam);
636 // We reparent the legacy Chrome_RenderWidgetHostHWND window to the global
637 // hidden window on the same lines as Windowed plugin windows.
638 if (legacy_render_widget_host_HWND_)
639 legacy_render_widget_host_HWND_->UpdateParent(ui::GetHiddenWindow());
641 #endif
644 void RenderWidgetHostViewAura::SetSize(const gfx::Size& size) {
645 // For a SetSize operation, we don't care what coordinate system the origin
646 // of the window is in, it's only important to make sure that the origin
647 // remains constant after the operation.
648 InternalSetBounds(gfx::Rect(window_->bounds().origin(), size));
651 void RenderWidgetHostViewAura::SetBounds(const gfx::Rect& rect) {
652 gfx::Point relative_origin(rect.origin());
654 // RenderWidgetHostViewAura::SetBounds() takes screen coordinates, but
655 // Window::SetBounds() takes parent coordinates, so do the conversion here.
656 aura::Window* root = window_->GetRootWindow();
657 if (root) {
658 aura::client::ScreenPositionClient* screen_position_client =
659 aura::client::GetScreenPositionClient(root);
660 if (screen_position_client) {
661 screen_position_client->ConvertPointFromScreen(
662 window_->parent(), &relative_origin);
666 InternalSetBounds(gfx::Rect(relative_origin, rect.size()));
669 gfx::Vector2dF RenderWidgetHostViewAura::GetLastScrollOffset() const {
670 return last_scroll_offset_;
673 gfx::NativeView RenderWidgetHostViewAura::GetNativeView() const {
674 return window_;
677 gfx::NativeViewId RenderWidgetHostViewAura::GetNativeViewId() const {
678 #if defined(OS_WIN)
679 aura::WindowTreeHost* host = window_->GetHost();
680 if (host)
681 return reinterpret_cast<gfx::NativeViewId>(host->GetAcceleratedWidget());
682 #endif
683 return static_cast<gfx::NativeViewId>(NULL);
686 gfx::NativeViewAccessible RenderWidgetHostViewAura::GetNativeViewAccessible() {
687 #if defined(OS_WIN)
688 aura::WindowTreeHost* host = window_->GetHost();
689 if (!host)
690 return static_cast<gfx::NativeViewAccessible>(NULL);
691 HWND hwnd = host->GetAcceleratedWidget();
692 BrowserAccessibilityManager* manager =
693 host_->GetOrCreateRootBrowserAccessibilityManager();
694 if (manager)
695 return manager->GetRoot()->ToBrowserAccessibilityWin();
696 #endif
698 NOTIMPLEMENTED();
699 return static_cast<gfx::NativeViewAccessible>(NULL);
702 ui::TextInputClient* RenderWidgetHostViewAura::GetTextInputClient() {
703 return this;
706 void RenderWidgetHostViewAura::SetKeyboardFocus() {
707 #if defined(OS_WIN)
708 if (CanFocus()) {
709 aura::WindowTreeHost* host = window_->GetHost();
710 if (host)
711 ::SetFocus(host->GetAcceleratedWidget());
713 #endif
716 RenderFrameHostImpl* RenderWidgetHostViewAura::GetFocusedFrame() {
717 if (!host_->IsRenderView())
718 return NULL;
719 RenderViewHost* rvh = RenderViewHost::From(host_);
720 FrameTreeNode* focused_frame =
721 rvh->GetDelegate()->GetFrameTree()->GetFocusedFrame();
722 if (!focused_frame)
723 return NULL;
725 return focused_frame->current_frame_host();
728 void RenderWidgetHostViewAura::MovePluginWindows(
729 const std::vector<WebPluginGeometry>& plugin_window_moves) {
730 #if defined(OS_WIN)
731 // We need to clip the rectangle to the tab's viewport, otherwise we will draw
732 // over the browser UI.
733 if (!window_->GetRootWindow()) {
734 DCHECK(plugin_window_moves.empty());
735 return;
737 HWND parent = window_->GetHost()->GetAcceleratedWidget();
738 gfx::Rect view_bounds = window_->GetBoundsInRootWindow();
739 std::vector<WebPluginGeometry> moves = plugin_window_moves;
741 gfx::Rect view_port(view_bounds.size());
743 for (size_t i = 0; i < moves.size(); ++i) {
744 gfx::Rect clip(moves[i].clip_rect);
745 gfx::Vector2d view_port_offset(
746 moves[i].window_rect.OffsetFromOrigin());
747 clip.Offset(view_port_offset);
748 clip.Intersect(view_port);
749 clip.Offset(-view_port_offset);
750 moves[i].clip_rect = clip;
752 moves[i].window_rect.Offset(view_bounds.OffsetFromOrigin());
754 plugin_window_moves_[moves[i].window] = moves[i];
756 // constrained_rects_ are relative to the root window. We want to convert
757 // them to be relative to the plugin window.
758 for (size_t j = 0; j < constrained_rects_.size(); ++j) {
759 gfx::Rect offset_cutout = constrained_rects_[j];
760 offset_cutout -= moves[i].window_rect.OffsetFromOrigin();
761 moves[i].cutout_rects.push_back(offset_cutout);
765 MovePluginWindowsHelper(parent, moves);
767 // Make sure each plugin window (or its wrapper if it exists) has a pointer to
768 // |this|.
769 for (size_t i = 0; i < moves.size(); ++i) {
770 HWND window = moves[i].window;
771 if (GetParent(window) != parent) {
772 window = GetParent(window);
774 if (!GetProp(window, kWidgetOwnerProperty))
775 SetProp(window, kWidgetOwnerProperty, this);
777 #endif // defined(OS_WIN)
780 void RenderWidgetHostViewAura::Focus() {
781 // Make sure we have a FocusClient before attempting to Focus(). In some
782 // situations we may not yet be in a valid Window hierarchy (such as reloading
783 // after out of memory discarded the tab).
784 aura::client::FocusClient* client = aura::client::GetFocusClient(window_);
785 if (client)
786 window_->Focus();
789 void RenderWidgetHostViewAura::Blur() {
790 window_->Blur();
793 bool RenderWidgetHostViewAura::HasFocus() const {
794 return window_->HasFocus();
797 bool RenderWidgetHostViewAura::IsSurfaceAvailableForCopy() const {
798 return delegated_frame_host_->CanCopyToBitmap();
801 void RenderWidgetHostViewAura::Show() {
802 window_->Show();
803 WasShown();
804 #if defined(OS_WIN)
805 if (legacy_render_widget_host_HWND_)
806 legacy_render_widget_host_HWND_->Show();
807 #endif
810 void RenderWidgetHostViewAura::Hide() {
811 window_->Hide();
812 WasHidden();
813 #if defined(OS_WIN)
814 if (legacy_render_widget_host_HWND_)
815 legacy_render_widget_host_HWND_->Hide();
816 #endif
819 bool RenderWidgetHostViewAura::IsShowing() {
820 return window_->IsVisible();
823 gfx::Rect RenderWidgetHostViewAura::GetViewBounds() const {
824 return window_->GetBoundsInScreen();
827 void RenderWidgetHostViewAura::SetBackgroundColor(SkColor color) {
828 RenderWidgetHostViewBase::SetBackgroundColor(color);
829 bool opaque = GetBackgroundOpaque();
830 host_->SetBackgroundOpaque(opaque);
831 window_->layer()->SetFillsBoundsOpaquely(opaque);
832 window_->layer()->SetColor(color);
835 gfx::Size RenderWidgetHostViewAura::GetVisibleViewportSize() const {
836 gfx::Rect requested_rect(GetRequestedRendererSize());
837 requested_rect.Inset(insets_);
838 return requested_rect.size();
841 void RenderWidgetHostViewAura::SetInsets(const gfx::Insets& insets) {
842 if (insets != insets_) {
843 insets_ = insets;
844 host_->WasResized();
848 void RenderWidgetHostViewAura::UpdateCursor(const WebCursor& cursor) {
849 current_cursor_ = cursor;
850 const gfx::Display display = gfx::Screen::GetScreenFor(window_)->
851 GetDisplayNearestWindow(window_);
852 current_cursor_.SetDisplayInfo(display);
853 UpdateCursorIfOverSelf();
856 void RenderWidgetHostViewAura::SetIsLoading(bool is_loading) {
857 is_loading_ = is_loading;
858 UpdateCursorIfOverSelf();
861 void RenderWidgetHostViewAura::TextInputTypeChanged(
862 ui::TextInputType type,
863 ui::TextInputMode input_mode,
864 bool can_compose_inline,
865 int flags) {
866 if (text_input_type_ != type ||
867 text_input_mode_ != input_mode ||
868 can_compose_inline_ != can_compose_inline ||
869 text_input_flags_ != flags) {
870 text_input_type_ = type;
871 text_input_mode_ = input_mode;
872 can_compose_inline_ = can_compose_inline;
873 text_input_flags_ = flags;
874 if (GetInputMethod())
875 GetInputMethod()->OnTextInputTypeChanged(this);
876 if (touch_editing_client_)
877 touch_editing_client_->OnTextInputTypeChanged(text_input_type_);
881 void RenderWidgetHostViewAura::OnTextInputStateChanged(
882 const ViewHostMsg_TextInputState_Params& params) {
883 text_input_flags_ = params.flags;
884 if (params.show_ime_if_needed && params.type != ui::TEXT_INPUT_TYPE_NONE) {
885 if (GetInputMethod())
886 GetInputMethod()->ShowImeIfNeeded();
890 void RenderWidgetHostViewAura::ImeCancelComposition() {
891 if (GetInputMethod())
892 GetInputMethod()->CancelComposition(this);
893 has_composition_text_ = false;
896 void RenderWidgetHostViewAura::ImeCompositionRangeChanged(
897 const gfx::Range& range,
898 const std::vector<gfx::Rect>& character_bounds) {
899 composition_character_bounds_ = character_bounds;
902 void RenderWidgetHostViewAura::RenderProcessGone(base::TerminationStatus status,
903 int error_code) {
904 UpdateCursorIfOverSelf();
905 Destroy();
908 void RenderWidgetHostViewAura::Destroy() {
909 // Beware, this function is not called on all destruction paths. It will
910 // implicitly end up calling ~RenderWidgetHostViewAura though, so all
911 // destruction/cleanup code should happen there, not here.
912 in_shutdown_ = true;
913 delete window_;
916 void RenderWidgetHostViewAura::SetTooltipText(
917 const base::string16& tooltip_text) {
918 tooltip_ = tooltip_text;
919 aura::Window* root_window = window_->GetRootWindow();
920 aura::client::TooltipClient* tooltip_client =
921 aura::client::GetTooltipClient(root_window);
922 if (tooltip_client) {
923 tooltip_client->UpdateTooltip(window_);
924 // Content tooltips should be visible indefinitely.
925 tooltip_client->SetTooltipShownTimeout(window_, 0);
929 void RenderWidgetHostViewAura::SelectionChanged(const base::string16& text,
930 size_t offset,
931 const gfx::Range& range) {
932 RenderWidgetHostViewBase::SelectionChanged(text, offset, range);
934 #if defined(USE_X11) && !defined(OS_CHROMEOS)
935 if (text.empty() || range.is_empty())
936 return;
937 size_t pos = range.GetMin() - offset;
938 size_t n = range.length();
940 DCHECK(pos + n <= text.length()) << "The text can not fully cover range.";
941 if (pos >= text.length()) {
942 NOTREACHED() << "The text can not cover range.";
943 return;
946 // Set the CLIPBOARD_TYPE_SELECTION to the ui::Clipboard.
947 ui::ScopedClipboardWriter clipboard_writer(ui::CLIPBOARD_TYPE_SELECTION);
948 clipboard_writer.WriteText(text.substr(pos, n));
949 #endif // defined(USE_X11) && !defined(OS_CHROMEOS)
952 gfx::Size RenderWidgetHostViewAura::GetRequestedRendererSize() const {
953 return delegated_frame_host_->GetRequestedRendererSize();
956 void RenderWidgetHostViewAura::SelectionBoundsChanged(
957 const ViewHostMsg_SelectionBounds_Params& params) {
958 if (selection_anchor_rect_ == params.anchor_rect &&
959 selection_focus_rect_ == params.focus_rect)
960 return;
962 selection_anchor_rect_ = params.anchor_rect;
963 selection_focus_rect_ = params.focus_rect;
965 if (GetInputMethod())
966 GetInputMethod()->OnCaretBoundsChanged(this);
968 if (touch_editing_client_) {
969 touch_editing_client_->OnSelectionOrCursorChanged(selection_anchor_rect_,
970 selection_focus_rect_);
974 void RenderWidgetHostViewAura::CopyFromCompositingSurface(
975 const gfx::Rect& src_subrect,
976 const gfx::Size& dst_size,
977 CopyFromCompositingSurfaceCallback& callback,
978 const SkColorType color_type) {
979 delegated_frame_host_->CopyFromCompositingSurface(
980 src_subrect, dst_size, callback, color_type);
983 void RenderWidgetHostViewAura::CopyFromCompositingSurfaceToVideoFrame(
984 const gfx::Rect& src_subrect,
985 const scoped_refptr<media::VideoFrame>& target,
986 const base::Callback<void(bool)>& callback) {
987 delegated_frame_host_->CopyFromCompositingSurfaceToVideoFrame(
988 src_subrect, target, callback);
991 bool RenderWidgetHostViewAura::CanCopyToVideoFrame() const {
992 return delegated_frame_host_->CanCopyToVideoFrame();
995 bool RenderWidgetHostViewAura::CanSubscribeFrame() const {
996 return delegated_frame_host_->CanSubscribeFrame();
999 void RenderWidgetHostViewAura::BeginFrameSubscription(
1000 scoped_ptr<RenderWidgetHostViewFrameSubscriber> subscriber) {
1001 delegated_frame_host_->BeginFrameSubscription(subscriber.Pass());
1004 void RenderWidgetHostViewAura::EndFrameSubscription() {
1005 delegated_frame_host_->EndFrameSubscription();
1008 #if defined(OS_WIN)
1009 bool RenderWidgetHostViewAura::UsesNativeWindowFrame() const {
1010 return (legacy_render_widget_host_HWND_ != NULL);
1013 void RenderWidgetHostViewAura::UpdateConstrainedWindowRects(
1014 const std::vector<gfx::Rect>& rects) {
1015 // Check this before setting constrained_rects_, so that next time they're set
1016 // and we have a root window we don't early return.
1017 if (!window_->GetHost())
1018 return;
1020 if (rects == constrained_rects_)
1021 return;
1023 constrained_rects_ = rects;
1025 HWND parent = window_->GetHost()->GetAcceleratedWidget();
1026 CutoutRectsParams params;
1027 params.widget = this;
1028 params.cutout_rects = constrained_rects_;
1029 params.geometry = &plugin_window_moves_;
1030 LPARAM lparam = reinterpret_cast<LPARAM>(&params);
1031 EnumChildWindows(parent, SetCutoutRectsCallback, lparam);
1034 void RenderWidgetHostViewAura::UpdateMouseLockRegion() {
1035 // Clip the cursor if chrome is running on regular desktop.
1036 if (gfx::Screen::GetScreenFor(window_) == gfx::Screen::GetNativeScreen()) {
1037 RECT window_rect = window_->GetBoundsInScreen().ToRECT();
1038 ::ClipCursor(&window_rect);
1042 void RenderWidgetHostViewAura::OnLegacyWindowDestroyed() {
1043 legacy_render_widget_host_HWND_ = NULL;
1044 legacy_window_destroyed_ = true;
1046 #endif
1048 void RenderWidgetHostViewAura::OnSwapCompositorFrame(
1049 uint32 output_surface_id,
1050 scoped_ptr<cc::CompositorFrame> frame) {
1051 TRACE_EVENT0("content", "RenderWidgetHostViewAura::OnSwapCompositorFrame");
1053 last_scroll_offset_ = frame->metadata.root_scroll_offset;
1054 if (frame->delegated_frame_data) {
1055 delegated_frame_host_->SwapDelegatedFrame(
1056 output_surface_id,
1057 frame->delegated_frame_data.Pass(),
1058 frame->metadata.device_scale_factor,
1059 frame->metadata.latency_info);
1060 return;
1063 if (frame->software_frame_data) {
1064 DLOG(ERROR) << "Unable to use software frame in aura";
1065 RecordAction(
1066 base::UserMetricsAction("BadMessageTerminate_SharedMemoryAura"));
1067 host_->GetProcess()->ReceivedBadMessage();
1068 return;
1072 void RenderWidgetHostViewAura::DidStopFlinging() {
1073 if (touch_editing_client_)
1074 touch_editing_client_->DidStopFlinging();
1077 #if defined(OS_WIN)
1078 void RenderWidgetHostViewAura::SetParentNativeViewAccessible(
1079 gfx::NativeViewAccessible accessible_parent) {
1082 gfx::NativeViewId RenderWidgetHostViewAura::GetParentForWindowlessPlugin()
1083 const {
1084 if (legacy_render_widget_host_HWND_) {
1085 return reinterpret_cast<gfx::NativeViewId>(
1086 legacy_render_widget_host_HWND_->hwnd());
1088 return NULL;
1090 #endif
1092 bool RenderWidgetHostViewAura::HasAcceleratedSurface(
1093 const gfx::Size& desired_size) {
1094 // Aura doesn't use GetBackingStore for accelerated pages, so it doesn't
1095 // matter what is returned here as GetBackingStore is the only caller of this
1096 // method. TODO(jbates) implement this if other Aura code needs it.
1097 return false;
1100 void RenderWidgetHostViewAura::GetScreenInfo(WebScreenInfo* results) {
1101 GetScreenInfoForWindow(results, window_->GetRootWindow() ? window_ : NULL);
1104 gfx::Rect RenderWidgetHostViewAura::GetBoundsInRootWindow() {
1105 aura::Window* top_level = window_->GetToplevelWindow();
1106 gfx::Rect bounds(top_level->GetBoundsInScreen());
1108 #if defined(OS_WIN)
1109 // TODO(zturner,iyengar): This will break when we remove support for NPAPI and
1110 // remove the legacy hwnd, so a better fix will need to be decided when that
1111 // happens.
1112 if (UsesNativeWindowFrame()) {
1113 // aura::Window doesn't take into account non-client area of native windows
1114 // (e.g. HWNDs), so for that case ask Windows directly what the bounds are.
1115 aura::WindowTreeHost* host = top_level->GetHost();
1116 if (!host)
1117 return top_level->GetBoundsInScreen();
1118 RECT window_rect = {0};
1119 HWND hwnd = host->GetAcceleratedWidget();
1120 ::GetWindowRect(hwnd, &window_rect);
1121 bounds = gfx::Rect(window_rect);
1123 // Maximized windows are outdented from the work area by the frame thickness
1124 // even though this "frame" is not painted. This confuses code (and people)
1125 // that think of a maximized window as corresponding exactly to the work
1126 // area. Correct for this by subtracting the frame thickness back off.
1127 if (::IsZoomed(hwnd)) {
1128 bounds.Inset(GetSystemMetrics(SM_CXSIZEFRAME),
1129 GetSystemMetrics(SM_CYSIZEFRAME));
1131 bounds.Inset(GetSystemMetrics(SM_CXPADDEDBORDER),
1132 GetSystemMetrics(SM_CXPADDEDBORDER));
1136 bounds = gfx::win::ScreenToDIPRect(bounds);
1137 #endif
1139 return bounds;
1142 void RenderWidgetHostViewAura::WheelEventAck(
1143 const blink::WebMouseWheelEvent& event,
1144 InputEventAckState ack_result) {
1145 if (overscroll_controller_) {
1146 overscroll_controller_->ReceivedEventACK(
1147 event, (INPUT_EVENT_ACK_STATE_CONSUMED == ack_result));
1151 void RenderWidgetHostViewAura::GestureEventAck(
1152 const blink::WebGestureEvent& event,
1153 InputEventAckState ack_result) {
1154 if (touch_editing_client_)
1155 touch_editing_client_->GestureEventAck(event.type);
1157 if (overscroll_controller_) {
1158 overscroll_controller_->ReceivedEventACK(
1159 event, (INPUT_EVENT_ACK_STATE_CONSUMED == ack_result));
1163 void RenderWidgetHostViewAura::ProcessAckedTouchEvent(
1164 const TouchEventWithLatencyInfo& touch, InputEventAckState ack_result) {
1165 ScopedVector<ui::TouchEvent> events;
1166 if (!MakeUITouchEventsFromWebTouchEvents(touch, &events,
1167 SCREEN_COORDINATES))
1168 return;
1170 aura::WindowTreeHost* host = window_->GetHost();
1171 // |host| is NULL during tests.
1172 if (!host)
1173 return;
1175 ui::EventResult result = (ack_result ==
1176 INPUT_EVENT_ACK_STATE_CONSUMED) ? ui::ER_HANDLED : ui::ER_UNHANDLED;
1177 for (ScopedVector<ui::TouchEvent>::iterator iter = events.begin(),
1178 end = events.end(); iter != end; ++iter) {
1179 host->dispatcher()->ProcessedTouchEvent((*iter), window_, result);
1183 scoped_ptr<SyntheticGestureTarget>
1184 RenderWidgetHostViewAura::CreateSyntheticGestureTarget() {
1185 return scoped_ptr<SyntheticGestureTarget>(
1186 new SyntheticGestureTargetAura(host_));
1189 InputEventAckState RenderWidgetHostViewAura::FilterInputEvent(
1190 const blink::WebInputEvent& input_event) {
1191 bool consumed = false;
1192 if (input_event.type == WebInputEvent::GestureFlingStart) {
1193 const WebGestureEvent& gesture_event =
1194 static_cast<const WebGestureEvent&>(input_event);
1195 // Zero-velocity touchpad flings are an Aura-specific signal that the
1196 // touchpad scroll has ended, and should not be forwarded to the renderer.
1197 if (gesture_event.sourceDevice == blink::WebGestureDeviceTouchpad &&
1198 !gesture_event.data.flingStart.velocityX &&
1199 !gesture_event.data.flingStart.velocityY) {
1200 consumed = true;
1204 if (overscroll_controller_)
1205 consumed |= overscroll_controller_->WillHandleEvent(input_event);
1207 return consumed && !WebTouchEvent::isTouchEventType(input_event.type)
1208 ? INPUT_EVENT_ACK_STATE_CONSUMED
1209 : INPUT_EVENT_ACK_STATE_NOT_CONSUMED;
1212 BrowserAccessibilityManager*
1213 RenderWidgetHostViewAura::CreateBrowserAccessibilityManager(
1214 BrowserAccessibilityDelegate* delegate) {
1215 BrowserAccessibilityManager* manager = NULL;
1216 #if defined(OS_WIN)
1217 manager = new BrowserAccessibilityManagerWin(
1218 BrowserAccessibilityManagerWin::GetEmptyDocument(), delegate);
1219 #else
1220 manager = BrowserAccessibilityManager::Create(
1221 BrowserAccessibilityManager::GetEmptyDocument(), delegate);
1222 #endif
1223 return manager;
1226 gfx::AcceleratedWidget
1227 RenderWidgetHostViewAura::AccessibilityGetAcceleratedWidget() {
1228 #if defined(OS_WIN)
1229 if (legacy_render_widget_host_HWND_)
1230 return legacy_render_widget_host_HWND_->hwnd();
1231 #endif
1232 return gfx::kNullAcceleratedWidget;
1235 gfx::NativeViewAccessible
1236 RenderWidgetHostViewAura::AccessibilityGetNativeViewAccessible() {
1237 #if defined(OS_WIN)
1238 if (legacy_render_widget_host_HWND_)
1239 return legacy_render_widget_host_HWND_->window_accessible();
1240 #endif
1241 return NULL;
1245 gfx::GLSurfaceHandle RenderWidgetHostViewAura::GetCompositingSurface() {
1246 return ImageTransportFactory::GetInstance()->GetSharedSurfaceHandle();
1249 void RenderWidgetHostViewAura::ShowDisambiguationPopup(
1250 const gfx::Rect& rect_pixels,
1251 const SkBitmap& zoomed_bitmap) {
1252 // |target_rect| is provided in pixels, not DIPs. So we convert it to DIPs
1253 // by scaling it by the inverse of the device scale factor.
1254 gfx::RectF screen_target_rect_f(rect_pixels);
1255 screen_target_rect_f.Scale(1.0f / current_device_scale_factor_);
1256 disambiguation_target_rect_ = gfx::ToEnclosingRect(screen_target_rect_f);
1258 float scale = static_cast<float>(zoomed_bitmap.width()) /
1259 static_cast<float>(rect_pixels.width());
1260 gfx::Size zoomed_size(gfx::ToCeiledSize(
1261 gfx::ScaleSize(disambiguation_target_rect_.size(), scale)));
1263 // Save of a copy of the |last_scroll_offset_| for comparison when the copy
1264 // callback fires, to ensure that we haven't scrolled.
1265 disambiguation_scroll_offset_ = last_scroll_offset_;
1267 CopyFromCompositingSurface(
1268 disambiguation_target_rect_,
1269 zoomed_size,
1270 base::Bind(&RenderWidgetHostViewAura::DisambiguationPopupRendered,
1271 base::internal::SupportsWeakPtrBase::StaticAsWeakPtr
1272 <RenderWidgetHostViewAura>(this)),
1273 kN32_SkColorType);
1276 void RenderWidgetHostViewAura::DisambiguationPopupRendered(
1277 bool success,
1278 const SkBitmap& result) {
1279 if (!success || disambiguation_scroll_offset_ != last_scroll_offset_)
1280 return;
1282 // Use RenderViewHostDelegate to get to the WebContentsViewAura, which will
1283 // actually show the delegate.
1284 RenderViewHostDelegate* delegate = NULL;
1285 if (host_->IsRenderView())
1286 delegate = RenderViewHost::From(host_)->GetDelegate();
1287 RenderViewHostDelegateView* delegate_view = NULL;
1288 if (delegate)
1289 delegate_view = delegate->GetDelegateView();
1290 if (delegate_view) {
1291 delegate_view->ShowDisambiguationPopup(
1292 disambiguation_target_rect_,
1293 result,
1294 base::Bind(&RenderWidgetHostViewAura::ProcessDisambiguationGesture,
1295 base::internal::SupportsWeakPtrBase::StaticAsWeakPtr
1296 <RenderWidgetHostViewAura>(this)),
1297 base::Bind(&RenderWidgetHostViewAura::ProcessDisambiguationMouse,
1298 base::internal::SupportsWeakPtrBase::StaticAsWeakPtr
1299 <RenderWidgetHostViewAura>(this)));
1303 void RenderWidgetHostViewAura::HideDisambiguationPopup() {
1304 RenderViewHostDelegate* delegate = NULL;
1305 if (host_->IsRenderView())
1306 delegate = RenderViewHost::From(host_)->GetDelegate();
1307 RenderViewHostDelegateView* delegate_view = NULL;
1308 if (delegate)
1309 delegate_view = delegate->GetDelegateView();
1310 if (delegate_view)
1311 delegate_view->HideDisambiguationPopup();
1314 void RenderWidgetHostViewAura::ProcessDisambiguationGesture(
1315 ui::GestureEvent* event) {
1316 blink::WebGestureEvent web_gesture = content::MakeWebGestureEvent(event);
1317 // If we fail to make a WebGestureEvent that is a Tap from the provided event,
1318 // don't forward it to Blink.
1319 if (web_gesture.type < blink::WebInputEvent::Type::GestureTap ||
1320 web_gesture.type > blink::WebInputEvent::Type::GestureTapCancel)
1321 return;
1323 host_->ForwardGestureEvent(web_gesture);
1326 void RenderWidgetHostViewAura::ProcessDisambiguationMouse(
1327 ui::MouseEvent* event) {
1328 blink::WebMouseEvent web_mouse = content::MakeWebMouseEvent(event);
1329 host_->ForwardMouseEvent(web_mouse);
1332 bool RenderWidgetHostViewAura::LockMouse() {
1333 aura::Window* root_window = window_->GetRootWindow();
1334 if (!root_window)
1335 return false;
1337 if (mouse_locked_)
1338 return true;
1340 mouse_locked_ = true;
1341 #if !defined(OS_WIN)
1342 window_->SetCapture();
1343 #else
1344 UpdateMouseLockRegion();
1345 #endif
1346 aura::client::CursorClient* cursor_client =
1347 aura::client::GetCursorClient(root_window);
1348 if (cursor_client) {
1349 cursor_client->HideCursor();
1350 cursor_client->LockCursor();
1353 if (ShouldMoveToCenter()) {
1354 synthetic_move_sent_ = true;
1355 window_->MoveCursorTo(gfx::Rect(window_->bounds().size()).CenterPoint());
1357 tooltip_disabler_.reset(new aura::client::ScopedTooltipDisabler(root_window));
1358 return true;
1361 void RenderWidgetHostViewAura::UnlockMouse() {
1362 tooltip_disabler_.reset();
1364 aura::Window* root_window = window_->GetRootWindow();
1365 if (!mouse_locked_ || !root_window)
1366 return;
1368 mouse_locked_ = false;
1370 #if !defined(OS_WIN)
1371 window_->ReleaseCapture();
1372 #else
1373 ::ClipCursor(NULL);
1374 #endif
1375 window_->MoveCursorTo(unlocked_mouse_position_);
1376 aura::client::CursorClient* cursor_client =
1377 aura::client::GetCursorClient(root_window);
1378 if (cursor_client) {
1379 cursor_client->UnlockCursor();
1380 cursor_client->ShowCursor();
1383 host_->LostMouseLock();
1386 ////////////////////////////////////////////////////////////////////////////////
1387 // RenderWidgetHostViewAura, ui::TextInputClient implementation:
1388 void RenderWidgetHostViewAura::SetCompositionText(
1389 const ui::CompositionText& composition) {
1390 if (!host_)
1391 return;
1393 // TODO(suzhe): convert both renderer_host and renderer to use
1394 // ui::CompositionText.
1395 std::vector<blink::WebCompositionUnderline> underlines;
1396 underlines.reserve(composition.underlines.size());
1397 for (std::vector<ui::CompositionUnderline>::const_iterator it =
1398 composition.underlines.begin();
1399 it != composition.underlines.end(); ++it) {
1400 underlines.push_back(
1401 blink::WebCompositionUnderline(static_cast<unsigned>(it->start_offset),
1402 static_cast<unsigned>(it->end_offset),
1403 it->color,
1404 it->thick,
1405 it->background_color));
1408 // TODO(suzhe): due to a bug of webkit, we can't use selection range with
1409 // composition string. See: https://bugs.webkit.org/show_bug.cgi?id=37788
1410 host_->ImeSetComposition(composition.text, underlines,
1411 composition.selection.end(),
1412 composition.selection.end());
1414 has_composition_text_ = !composition.text.empty();
1417 void RenderWidgetHostViewAura::ConfirmCompositionText() {
1418 if (host_ && has_composition_text_) {
1419 host_->ImeConfirmComposition(base::string16(), gfx::Range::InvalidRange(),
1420 false);
1422 has_composition_text_ = false;
1425 void RenderWidgetHostViewAura::ClearCompositionText() {
1426 if (host_ && has_composition_text_)
1427 host_->ImeCancelComposition();
1428 has_composition_text_ = false;
1431 void RenderWidgetHostViewAura::InsertText(const base::string16& text) {
1432 DCHECK(text_input_type_ != ui::TEXT_INPUT_TYPE_NONE);
1433 if (host_)
1434 host_->ImeConfirmComposition(text, gfx::Range::InvalidRange(), false);
1435 has_composition_text_ = false;
1438 void RenderWidgetHostViewAura::InsertChar(base::char16 ch, int flags) {
1439 if (popup_child_host_view_ && popup_child_host_view_->NeedsInputGrab()) {
1440 popup_child_host_view_->InsertChar(ch, flags);
1441 return;
1444 // Ignore character messages for VKEY_RETURN sent on CTRL+M. crbug.com/315547
1445 if (host_ && (accept_return_character_ || ch != ui::VKEY_RETURN)) {
1446 double now = ui::EventTimeForNow().InSecondsF();
1447 // Send a blink::WebInputEvent::Char event to |host_|.
1448 NativeWebKeyboardEvent webkit_event(ui::ET_KEY_PRESSED,
1449 true /* is_char */,
1451 flags,
1452 now);
1453 ForwardKeyboardEvent(webkit_event);
1457 gfx::NativeWindow RenderWidgetHostViewAura::GetAttachedWindow() const {
1458 return window_;
1461 ui::TextInputType RenderWidgetHostViewAura::GetTextInputType() const {
1462 return text_input_type_;
1465 ui::TextInputMode RenderWidgetHostViewAura::GetTextInputMode() const {
1466 return text_input_mode_;
1469 int RenderWidgetHostViewAura::GetTextInputFlags() const {
1470 return text_input_flags_;
1473 bool RenderWidgetHostViewAura::CanComposeInline() const {
1474 return can_compose_inline_;
1477 gfx::Rect RenderWidgetHostViewAura::ConvertRectToScreen(
1478 const gfx::Rect& rect) const {
1479 gfx::Point origin = rect.origin();
1480 gfx::Point end = gfx::Point(rect.right(), rect.bottom());
1482 aura::Window* root_window = window_->GetRootWindow();
1483 if (!root_window)
1484 return rect;
1485 aura::client::ScreenPositionClient* screen_position_client =
1486 aura::client::GetScreenPositionClient(root_window);
1487 if (!screen_position_client)
1488 return rect;
1489 screen_position_client->ConvertPointToScreen(window_, &origin);
1490 screen_position_client->ConvertPointToScreen(window_, &end);
1491 return gfx::Rect(origin.x(),
1492 origin.y(),
1493 end.x() - origin.x(),
1494 end.y() - origin.y());
1497 gfx::Rect RenderWidgetHostViewAura::ConvertRectFromScreen(
1498 const gfx::Rect& rect) const {
1499 gfx::Point origin = rect.origin();
1500 gfx::Point end = gfx::Point(rect.right(), rect.bottom());
1502 aura::Window* root_window = window_->GetRootWindow();
1503 if (root_window) {
1504 aura::client::ScreenPositionClient* screen_position_client =
1505 aura::client::GetScreenPositionClient(root_window);
1506 screen_position_client->ConvertPointFromScreen(window_, &origin);
1507 screen_position_client->ConvertPointFromScreen(window_, &end);
1508 return gfx::Rect(origin.x(),
1509 origin.y(),
1510 end.x() - origin.x(),
1511 end.y() - origin.y());
1514 return rect;
1517 gfx::Rect RenderWidgetHostViewAura::GetCaretBounds() const {
1518 const gfx::Rect rect =
1519 gfx::UnionRects(selection_anchor_rect_, selection_focus_rect_);
1520 return ConvertRectToScreen(rect);
1523 bool RenderWidgetHostViewAura::GetCompositionCharacterBounds(
1524 uint32 index,
1525 gfx::Rect* rect) const {
1526 DCHECK(rect);
1527 if (index >= composition_character_bounds_.size())
1528 return false;
1529 *rect = ConvertRectToScreen(composition_character_bounds_[index]);
1530 return true;
1533 bool RenderWidgetHostViewAura::HasCompositionText() const {
1534 return has_composition_text_;
1537 bool RenderWidgetHostViewAura::GetTextRange(gfx::Range* range) const {
1538 range->set_start(selection_text_offset_);
1539 range->set_end(selection_text_offset_ + selection_text_.length());
1540 return true;
1543 bool RenderWidgetHostViewAura::GetCompositionTextRange(
1544 gfx::Range* range) const {
1545 // TODO(suzhe): implement this method when fixing http://crbug.com/55130.
1546 NOTIMPLEMENTED();
1547 return false;
1550 bool RenderWidgetHostViewAura::GetSelectionRange(gfx::Range* range) const {
1551 range->set_start(selection_range_.start());
1552 range->set_end(selection_range_.end());
1553 return true;
1556 bool RenderWidgetHostViewAura::SetSelectionRange(const gfx::Range& range) {
1557 // TODO(suzhe): implement this method when fixing http://crbug.com/55130.
1558 NOTIMPLEMENTED();
1559 return false;
1562 bool RenderWidgetHostViewAura::DeleteRange(const gfx::Range& range) {
1563 // TODO(suzhe): implement this method when fixing http://crbug.com/55130.
1564 NOTIMPLEMENTED();
1565 return false;
1568 bool RenderWidgetHostViewAura::GetTextFromRange(
1569 const gfx::Range& range,
1570 base::string16* text) const {
1571 gfx::Range selection_text_range(selection_text_offset_,
1572 selection_text_offset_ + selection_text_.length());
1574 if (!selection_text_range.Contains(range)) {
1575 text->clear();
1576 return false;
1578 if (selection_text_range.EqualsIgnoringDirection(range)) {
1579 // Avoid calling substr whose performance is low.
1580 *text = selection_text_;
1581 } else {
1582 *text = selection_text_.substr(
1583 range.GetMin() - selection_text_offset_,
1584 range.length());
1586 return true;
1589 void RenderWidgetHostViewAura::OnInputMethodChanged() {
1590 if (!host_)
1591 return;
1593 if (GetInputMethod())
1594 host_->SetInputMethodActive(GetInputMethod()->IsActive());
1596 // TODO(suzhe): implement the newly added “locale” property of HTML DOM
1597 // TextEvent.
1600 bool RenderWidgetHostViewAura::ChangeTextDirectionAndLayoutAlignment(
1601 base::i18n::TextDirection direction) {
1602 if (!host_)
1603 return false;
1604 host_->UpdateTextDirection(
1605 direction == base::i18n::RIGHT_TO_LEFT ?
1606 blink::WebTextDirectionRightToLeft :
1607 blink::WebTextDirectionLeftToRight);
1608 host_->NotifyTextDirection();
1609 return true;
1612 void RenderWidgetHostViewAura::ExtendSelectionAndDelete(
1613 size_t before, size_t after) {
1614 RenderFrameHostImpl* rfh = GetFocusedFrame();
1615 if (rfh)
1616 rfh->ExtendSelectionAndDelete(before, after);
1619 void RenderWidgetHostViewAura::EnsureCaretInRect(const gfx::Rect& rect) {
1620 gfx::Rect intersected_rect(
1621 gfx::IntersectRects(rect, window_->GetBoundsInScreen()));
1623 if (intersected_rect.IsEmpty())
1624 return;
1626 host_->ScrollFocusedEditableNodeIntoRect(
1627 ConvertRectFromScreen(intersected_rect));
1630 void RenderWidgetHostViewAura::OnCandidateWindowShown() {
1631 host_->CandidateWindowShown();
1634 void RenderWidgetHostViewAura::OnCandidateWindowUpdated() {
1635 host_->CandidateWindowUpdated();
1638 void RenderWidgetHostViewAura::OnCandidateWindowHidden() {
1639 host_->CandidateWindowHidden();
1642 bool RenderWidgetHostViewAura::IsEditingCommandEnabled(int command_id) {
1643 return false;
1646 void RenderWidgetHostViewAura::ExecuteEditingCommand(int command_id) {
1649 ////////////////////////////////////////////////////////////////////////////////
1650 // RenderWidgetHostViewAura, gfx::DisplayObserver implementation:
1652 void RenderWidgetHostViewAura::OnDisplayAdded(
1653 const gfx::Display& new_display) {
1656 void RenderWidgetHostViewAura::OnDisplayRemoved(
1657 const gfx::Display& old_display) {
1660 void RenderWidgetHostViewAura::OnDisplayMetricsChanged(
1661 const gfx::Display& display, uint32_t metrics) {
1662 // The screen info should be updated regardless of the metric change.
1663 gfx::Screen* screen = gfx::Screen::GetScreenFor(window_);
1664 if (display.id() == screen->GetDisplayNearestWindow(window_).id()) {
1665 UpdateScreenInfo(window_);
1666 current_cursor_.SetDisplayInfo(display);
1667 UpdateCursorIfOverSelf();
1671 ////////////////////////////////////////////////////////////////////////////////
1672 // RenderWidgetHostViewAura, aura::WindowDelegate implementation:
1674 gfx::Size RenderWidgetHostViewAura::GetMinimumSize() const {
1675 return gfx::Size();
1678 gfx::Size RenderWidgetHostViewAura::GetMaximumSize() const {
1679 return gfx::Size();
1682 void RenderWidgetHostViewAura::OnBoundsChanged(const gfx::Rect& old_bounds,
1683 const gfx::Rect& new_bounds) {
1684 base::AutoReset<bool> in_bounds_changed(&in_bounds_changed_, true);
1685 // We care about this whenever RenderWidgetHostViewAura is not owned by a
1686 // WebContentsViewAura since changes to the Window's bounds need to be
1687 // messaged to the renderer. WebContentsViewAura invokes SetSize() or
1688 // SetBounds() itself. No matter how we got here, any redundant calls are
1689 // harmless.
1690 SetSize(new_bounds.size());
1692 if (GetInputMethod())
1693 GetInputMethod()->OnCaretBoundsChanged(this);
1696 gfx::NativeCursor RenderWidgetHostViewAura::GetCursor(const gfx::Point& point) {
1697 if (mouse_locked_)
1698 return ui::kCursorNone;
1699 return current_cursor_.GetNativeCursor();
1702 int RenderWidgetHostViewAura::GetNonClientComponent(
1703 const gfx::Point& point) const {
1704 return HTCLIENT;
1707 bool RenderWidgetHostViewAura::ShouldDescendIntoChildForEventHandling(
1708 aura::Window* child,
1709 const gfx::Point& location) {
1710 return true;
1713 bool RenderWidgetHostViewAura::CanFocus() {
1714 return popup_type_ == blink::WebPopupTypeNone;
1717 void RenderWidgetHostViewAura::OnCaptureLost() {
1718 host_->LostCapture();
1719 if (touch_editing_client_)
1720 touch_editing_client_->EndTouchEditing(false);
1723 void RenderWidgetHostViewAura::OnPaint(gfx::Canvas* canvas) {
1724 NOTREACHED();
1727 void RenderWidgetHostViewAura::OnDeviceScaleFactorChanged(
1728 float device_scale_factor) {
1729 if (!host_ || !window_->GetRootWindow())
1730 return;
1732 UpdateScreenInfo(window_);
1734 const gfx::Display display = gfx::Screen::GetScreenFor(window_)->
1735 GetDisplayNearestWindow(window_);
1736 DCHECK_EQ(device_scale_factor, display.device_scale_factor());
1737 current_cursor_.SetDisplayInfo(display);
1738 SnapToPhysicalPixelBoundary();
1741 void RenderWidgetHostViewAura::OnWindowDestroying(aura::Window* window) {
1742 #if defined(OS_WIN)
1743 HWND parent = NULL;
1744 // If the tab was hidden and it's closed, host_->is_hidden would have been
1745 // reset to false in RenderWidgetHostImpl::RendererExited.
1746 if (!window_->GetRootWindow() || host_->is_hidden()) {
1747 parent = ui::GetHiddenWindow();
1748 } else {
1749 parent = window_->GetHost()->GetAcceleratedWidget();
1751 LPARAM lparam = reinterpret_cast<LPARAM>(this);
1752 EnumChildWindows(parent, WindowDestroyingCallback, lparam);
1754 // The LegacyRenderWidgetHostHWND instance is destroyed when its window is
1755 // destroyed. Normally we control when that happens via the Destroy call
1756 // in the dtor. However there may be cases where the window is destroyed
1757 // by Windows, i.e. the parent window is destroyed before the
1758 // RenderWidgetHostViewAura instance goes away etc. To avoid that we
1759 // destroy the LegacyRenderWidgetHostHWND instance here.
1760 if (legacy_render_widget_host_HWND_) {
1761 legacy_render_widget_host_HWND_->set_host(NULL);
1762 legacy_render_widget_host_HWND_->Destroy();
1763 // The Destroy call above will delete the LegacyRenderWidgetHostHWND
1764 // instance.
1765 legacy_render_widget_host_HWND_ = NULL;
1767 #endif
1769 // Make sure that the input method no longer references to this object before
1770 // this object is removed from the root window (i.e. this object loses access
1771 // to the input method).
1772 ui::InputMethod* input_method = GetInputMethod();
1773 if (input_method)
1774 input_method->DetachTextInputClient(this);
1776 if (overscroll_controller_)
1777 overscroll_controller_->Reset();
1780 void RenderWidgetHostViewAura::OnWindowDestroyed(aura::Window* window) {
1781 // Ask the RWH to drop reference to us.
1782 if (!is_guest_view_hack_)
1783 host_->ViewDestroyed();
1785 delete this;
1788 void RenderWidgetHostViewAura::OnWindowTargetVisibilityChanged(bool visible) {
1791 bool RenderWidgetHostViewAura::HasHitTestMask() const {
1792 return false;
1795 void RenderWidgetHostViewAura::GetHitTestMask(gfx::Path* mask) const {
1798 ////////////////////////////////////////////////////////////////////////////////
1799 // RenderWidgetHostViewAura, ui::EventHandler implementation:
1801 void RenderWidgetHostViewAura::OnKeyEvent(ui::KeyEvent* event) {
1802 TRACE_EVENT0("input", "RenderWidgetHostViewAura::OnKeyEvent");
1803 if (touch_editing_client_ && touch_editing_client_->HandleInputEvent(event))
1804 return;
1806 if (popup_child_host_view_ && popup_child_host_view_->NeedsInputGrab()) {
1807 popup_child_host_view_->OnKeyEvent(event);
1808 if (event->handled())
1809 return;
1812 // We need to handle the Escape key for Pepper Flash.
1813 if (is_fullscreen_ && event->key_code() == ui::VKEY_ESCAPE) {
1814 // Focus the window we were created from.
1815 if (host_tracker_.get() && !host_tracker_->windows().empty()) {
1816 aura::Window* host = *(host_tracker_->windows().begin());
1817 aura::client::FocusClient* client = aura::client::GetFocusClient(host);
1818 if (client) {
1819 // Calling host->Focus() may delete |this|. We create a local observer
1820 // for that. In that case we exit without further access to any members.
1821 aura::WindowTracker tracker;
1822 aura::Window* window = window_;
1823 tracker.Add(window);
1824 host->Focus();
1825 if (!tracker.Contains(window)) {
1826 event->SetHandled();
1827 return;
1831 if (!in_shutdown_) {
1832 in_shutdown_ = true;
1833 host_->Shutdown();
1835 } else {
1836 if (event->key_code() == ui::VKEY_RETURN) {
1837 // Do not forward return key release events if no press event was handled.
1838 if (event->type() == ui::ET_KEY_RELEASED && !accept_return_character_)
1839 return;
1840 // Accept return key character events between press and release events.
1841 accept_return_character_ = event->type() == ui::ET_KEY_PRESSED;
1844 // We don't have to communicate with an input method here.
1845 if (!event->HasNativeEvent()) {
1846 NativeWebKeyboardEvent webkit_event(
1847 event->type(),
1848 event->is_char(),
1849 event->is_char() ? event->GetCharacter() : event->key_code(),
1850 event->flags(),
1851 ui::EventTimeForNow().InSecondsF());
1852 ForwardKeyboardEvent(webkit_event);
1853 } else {
1854 NativeWebKeyboardEvent webkit_event(event);
1855 ForwardKeyboardEvent(webkit_event);
1858 event->SetHandled();
1861 void RenderWidgetHostViewAura::OnMouseEvent(ui::MouseEvent* event) {
1862 TRACE_EVENT0("input", "RenderWidgetHostViewAura::OnMouseEvent");
1864 if (touch_editing_client_ && touch_editing_client_->HandleInputEvent(event))
1865 return;
1867 if (mouse_locked_) {
1868 aura::client::CursorClient* cursor_client =
1869 aura::client::GetCursorClient(window_->GetRootWindow());
1870 DCHECK(!cursor_client || !cursor_client->IsCursorVisible());
1872 if (event->type() == ui::ET_MOUSEWHEEL) {
1873 blink::WebMouseWheelEvent mouse_wheel_event =
1874 MakeWebMouseWheelEvent(static_cast<ui::MouseWheelEvent*>(event));
1875 if (mouse_wheel_event.deltaX != 0 || mouse_wheel_event.deltaY != 0)
1876 host_->ForwardWheelEvent(mouse_wheel_event);
1877 return;
1880 gfx::Point center(gfx::Rect(window_->bounds().size()).CenterPoint());
1882 // If we receive non client mouse messages while we are in the locked state
1883 // it probably means that the mouse left the borders of our window and
1884 // needs to be moved back to the center.
1885 if (event->flags() & ui::EF_IS_NON_CLIENT) {
1886 synthetic_move_sent_ = true;
1887 window_->MoveCursorTo(center);
1888 return;
1891 blink::WebMouseEvent mouse_event = MakeWebMouseEvent(event);
1893 bool is_move_to_center_event = (event->type() == ui::ET_MOUSE_MOVED ||
1894 event->type() == ui::ET_MOUSE_DRAGGED) &&
1895 mouse_event.x == center.x() && mouse_event.y == center.y();
1897 ModifyEventMovementAndCoords(&mouse_event);
1899 bool should_not_forward = is_move_to_center_event && synthetic_move_sent_;
1900 if (should_not_forward) {
1901 synthetic_move_sent_ = false;
1902 } else {
1903 // Check if the mouse has reached the border and needs to be centered.
1904 if (ShouldMoveToCenter()) {
1905 synthetic_move_sent_ = true;
1906 window_->MoveCursorTo(center);
1908 // Forward event to renderer.
1909 if (CanRendererHandleEvent(event) &&
1910 !(event->flags() & ui::EF_FROM_TOUCH)) {
1911 host_->ForwardMouseEvent(mouse_event);
1912 // Ensure that we get keyboard focus on mouse down as a plugin window
1913 // may have grabbed keyboard focus.
1914 if (event->type() == ui::ET_MOUSE_PRESSED)
1915 SetKeyboardFocus();
1918 return;
1921 // As the overscroll is handled during scroll events from the trackpad, the
1922 // RWHVA window is transformed by the overscroll controller. This transform
1923 // triggers a synthetic mouse-move event to be generated (by the aura
1924 // RootWindow). But this event interferes with the overscroll gesture. So,
1925 // ignore such synthetic mouse-move events if an overscroll gesture is in
1926 // progress.
1927 if (overscroll_controller_ &&
1928 overscroll_controller_->overscroll_mode() != OVERSCROLL_NONE &&
1929 event->flags() & ui::EF_IS_SYNTHESIZED &&
1930 (event->type() == ui::ET_MOUSE_ENTERED ||
1931 event->type() == ui::ET_MOUSE_EXITED ||
1932 event->type() == ui::ET_MOUSE_MOVED)) {
1933 event->StopPropagation();
1934 return;
1937 if (event->type() == ui::ET_MOUSEWHEEL) {
1938 #if defined(OS_WIN)
1939 // We get mouse wheel/scroll messages even if we are not in the foreground.
1940 // So here we check if we have any owned popup windows in the foreground and
1941 // dismiss them.
1942 aura::WindowTreeHost* host = window_->GetHost();
1943 if (host) {
1944 HWND parent = host->GetAcceleratedWidget();
1945 HWND toplevel_hwnd = ::GetAncestor(parent, GA_ROOT);
1946 EnumThreadWindows(GetCurrentThreadId(),
1947 DismissOwnedPopups,
1948 reinterpret_cast<LPARAM>(toplevel_hwnd));
1950 #endif
1951 // The Disambiguation popup does not parent itself from this window, so we
1952 // manually dismiss it.
1953 HideDisambiguationPopup();
1955 blink::WebMouseWheelEvent mouse_wheel_event =
1956 MakeWebMouseWheelEvent(static_cast<ui::MouseWheelEvent*>(event));
1957 if (mouse_wheel_event.deltaX != 0 || mouse_wheel_event.deltaY != 0)
1958 host_->ForwardWheelEvent(mouse_wheel_event);
1959 } else if (CanRendererHandleEvent(event) &&
1960 !(event->flags() & ui::EF_FROM_TOUCH)) {
1961 // Confirm existing composition text on mouse press, to make sure
1962 // the input caret won't be moved with an ongoing composition text.
1963 if (event->type() == ui::ET_MOUSE_PRESSED)
1964 FinishImeCompositionSession();
1966 blink::WebMouseEvent mouse_event = MakeWebMouseEvent(event);
1967 ModifyEventMovementAndCoords(&mouse_event);
1968 host_->ForwardMouseEvent(mouse_event);
1969 // Ensure that we get keyboard focus on mouse down as a plugin window may
1970 // have grabbed keyboard focus.
1971 if (event->type() == ui::ET_MOUSE_PRESSED)
1972 SetKeyboardFocus();
1975 switch (event->type()) {
1976 case ui::ET_MOUSE_PRESSED:
1977 window_->SetCapture();
1978 break;
1979 case ui::ET_MOUSE_RELEASED:
1980 if (!NeedsMouseCapture())
1981 window_->ReleaseCapture();
1982 break;
1983 default:
1984 break;
1987 // Needed to propagate mouse event to |window_->parent()->delegate()|, but
1988 // note that it might be something other than a WebContentsViewAura instance.
1989 // TODO(pkotwicz): Find a better way of doing this.
1990 // In fullscreen mode which is typically used by flash, don't forward
1991 // the mouse events to the parent. The renderer and the plugin process
1992 // handle these events.
1993 if (!is_fullscreen_ && window_->parent() && window_->parent()->delegate() &&
1994 !(event->flags() & ui::EF_FROM_TOUCH)) {
1995 event->ConvertLocationToTarget(window_, window_->parent());
1996 window_->parent()->delegate()->OnMouseEvent(event);
1999 if (!IsXButtonUpEvent(event))
2000 event->SetHandled();
2003 void RenderWidgetHostViewAura::OnScrollEvent(ui::ScrollEvent* event) {
2004 TRACE_EVENT0("input", "RenderWidgetHostViewAura::OnScrollEvent");
2005 if (touch_editing_client_ && touch_editing_client_->HandleInputEvent(event))
2006 return;
2008 if (event->type() == ui::ET_SCROLL) {
2009 #if !defined(OS_WIN)
2010 // TODO(ananta)
2011 // Investigate if this is true for Windows 8 Metro ASH as well.
2012 if (event->finger_count() != 2)
2013 return;
2014 #endif
2015 blink::WebGestureEvent gesture_event =
2016 MakeWebGestureEventFlingCancel();
2017 host_->ForwardGestureEvent(gesture_event);
2018 blink::WebMouseWheelEvent mouse_wheel_event =
2019 MakeWebMouseWheelEvent(event);
2020 host_->ForwardWheelEvent(mouse_wheel_event);
2021 RecordAction(base::UserMetricsAction("TrackpadScroll"));
2022 } else if (event->type() == ui::ET_SCROLL_FLING_START ||
2023 event->type() == ui::ET_SCROLL_FLING_CANCEL) {
2024 blink::WebGestureEvent gesture_event =
2025 MakeWebGestureEvent(event);
2026 host_->ForwardGestureEvent(gesture_event);
2027 if (event->type() == ui::ET_SCROLL_FLING_START)
2028 RecordAction(base::UserMetricsAction("TrackpadScrollFling"));
2031 event->SetHandled();
2034 void RenderWidgetHostViewAura::OnTouchEvent(ui::TouchEvent* event) {
2035 TRACE_EVENT0("input", "RenderWidgetHostViewAura::OnTouchEvent");
2036 if (touch_editing_client_ && touch_editing_client_->HandleInputEvent(event))
2037 return;
2039 // Update the touch event first.
2040 blink::WebTouchPoint* point = UpdateWebTouchEventFromUIEvent(*event,
2041 &touch_event_);
2043 // Forward the touch event only if a touch point was updated, and there's a
2044 // touch-event handler in the page, and no other touch-event is in the queue.
2045 // It is important to always consume the event if there is a touch-event
2046 // handler in the page, or some touch-event is already in the queue, even if
2047 // no point has been updated, to make sure that this event does not get
2048 // processed by the gesture recognizer before the events in the queue.
2049 if (host_->ShouldForwardTouchEvent())
2050 event->StopPropagation();
2052 if (point) {
2053 if (host_->ShouldForwardTouchEvent())
2054 host_->ForwardTouchEventWithLatencyInfo(touch_event_, *event->latency());
2055 UpdateWebTouchEventAfterDispatch(&touch_event_, point);
2059 void RenderWidgetHostViewAura::OnGestureEvent(ui::GestureEvent* event) {
2060 TRACE_EVENT0("input", "RenderWidgetHostViewAura::OnGestureEvent");
2061 if ((event->type() == ui::ET_GESTURE_PINCH_BEGIN ||
2062 event->type() == ui::ET_GESTURE_PINCH_UPDATE ||
2063 event->type() == ui::ET_GESTURE_PINCH_END) && !pinch_zoom_enabled_) {
2064 event->SetHandled();
2065 return;
2068 if (touch_editing_client_ && touch_editing_client_->HandleInputEvent(event))
2069 return;
2071 // Confirm existing composition text on TAP gesture, to make sure the input
2072 // caret won't be moved with an ongoing composition text.
2073 if (event->type() == ui::ET_GESTURE_TAP)
2074 FinishImeCompositionSession();
2076 RenderViewHostDelegate* delegate = NULL;
2077 if (host_->IsRenderView())
2078 delegate = RenderViewHost::From(host_)->GetDelegate();
2080 if (delegate && event->type() == ui::ET_GESTURE_BEGIN &&
2081 event->details().touch_points() == 1) {
2082 delegate->HandleGestureBegin();
2085 blink::WebGestureEvent gesture = MakeWebGestureEvent(event);
2086 if (event->type() == ui::ET_GESTURE_TAP_DOWN) {
2087 // Webkit does not stop a fling-scroll on tap-down. So explicitly send an
2088 // event to stop any in-progress flings.
2089 blink::WebGestureEvent fling_cancel = gesture;
2090 fling_cancel.type = blink::WebInputEvent::GestureFlingCancel;
2091 fling_cancel.sourceDevice = blink::WebGestureDeviceTouchscreen;
2092 host_->ForwardGestureEvent(fling_cancel);
2095 if (gesture.type != blink::WebInputEvent::Undefined) {
2096 host_->ForwardGestureEventWithLatencyInfo(gesture, *event->latency());
2098 if (event->type() == ui::ET_GESTURE_SCROLL_BEGIN ||
2099 event->type() == ui::ET_GESTURE_SCROLL_UPDATE ||
2100 event->type() == ui::ET_GESTURE_SCROLL_END) {
2101 RecordAction(base::UserMetricsAction("TouchscreenScroll"));
2102 } else if (event->type() == ui::ET_SCROLL_FLING_START) {
2103 RecordAction(base::UserMetricsAction("TouchscreenScrollFling"));
2107 if (delegate && event->type() == ui::ET_GESTURE_END &&
2108 event->details().touch_points() == 1) {
2109 delegate->HandleGestureEnd();
2112 // If a gesture is not processed by the webpage, then WebKit processes it
2113 // (e.g. generates synthetic mouse events).
2114 event->SetHandled();
2117 ////////////////////////////////////////////////////////////////////////////////
2118 // RenderWidgetHostViewAura, aura::client::ActivationDelegate implementation:
2120 bool RenderWidgetHostViewAura::ShouldActivate() const {
2121 aura::WindowTreeHost* host = window_->GetHost();
2122 if (!host)
2123 return true;
2124 const ui::Event* event = host->dispatcher()->current_event();
2125 if (!event)
2126 return true;
2127 return is_fullscreen_;
2130 ////////////////////////////////////////////////////////////////////////////////
2131 // RenderWidgetHostViewAura,
2132 // aura::client::ActivationChangeObserver implementation:
2134 void RenderWidgetHostViewAura::OnWindowActivated(aura::Window* gained_active,
2135 aura::Window* lost_active) {
2136 DCHECK(window_ == gained_active || window_ == lost_active);
2137 if (window_ == gained_active) {
2138 const ui::Event* event = window_->GetHost()->dispatcher()->current_event();
2139 if (event && PointerEventActivates(*event))
2140 host_->OnPointerEventActivate();
2144 ////////////////////////////////////////////////////////////////////////////////
2145 // RenderWidgetHostViewAura, aura::client::CursorClientObserver implementation:
2147 void RenderWidgetHostViewAura::OnCursorVisibilityChanged(bool is_visible) {
2148 NotifyRendererOfCursorVisibilityState(is_visible);
2151 ////////////////////////////////////////////////////////////////////////////////
2152 // RenderWidgetHostViewAura, aura::client::FocusChangeObserver implementation:
2154 void RenderWidgetHostViewAura::OnWindowFocused(aura::Window* gained_focus,
2155 aura::Window* lost_focus) {
2156 DCHECK(window_ == gained_focus || window_ == lost_focus);
2157 if (window_ == gained_focus) {
2158 // We need to honor input bypass if the associated tab is does not want
2159 // input. This gives the current focused window a chance to be the text
2160 // input client and handle events.
2161 if (host_->ignore_input_events())
2162 return;
2164 host_->GotFocus();
2165 host_->SetActive(true);
2167 ui::InputMethod* input_method = GetInputMethod();
2168 if (input_method) {
2169 // Ask the system-wide IME to send all TextInputClient messages to |this|
2170 // object.
2171 input_method->SetFocusedTextInputClient(this);
2172 host_->SetInputMethodActive(input_method->IsActive());
2174 // Often the application can set focus to the view in response to a key
2175 // down. However the following char event shouldn't be sent to the web
2176 // page.
2177 host_->SuppressNextCharEvents();
2178 } else {
2179 host_->SetInputMethodActive(false);
2182 BrowserAccessibilityManager* manager =
2183 host_->GetRootBrowserAccessibilityManager();
2184 if (manager)
2185 manager->OnWindowFocused();
2186 } else if (window_ == lost_focus) {
2187 host_->SetActive(false);
2188 host_->Blur();
2190 DetachFromInputMethod();
2191 host_->SetInputMethodActive(false);
2193 if (touch_editing_client_)
2194 touch_editing_client_->EndTouchEditing(false);
2196 if (overscroll_controller_)
2197 overscroll_controller_->Cancel();
2199 BrowserAccessibilityManager* manager =
2200 host_->GetRootBrowserAccessibilityManager();
2201 if (manager)
2202 manager->OnWindowBlurred();
2204 // If we lose the focus while fullscreen, close the window; Pepper Flash
2205 // won't do it for us (unlike NPAPI Flash). However, we do not close the
2206 // window if we lose the focus to a window on another display.
2207 gfx::Screen* screen = gfx::Screen::GetScreenFor(window_);
2208 bool focusing_other_display =
2209 gained_focus && screen->GetNumDisplays() > 1 &&
2210 (screen->GetDisplayNearestWindow(window_).id() !=
2211 screen->GetDisplayNearestWindow(gained_focus).id());
2212 if (is_fullscreen_ && !in_shutdown_ && !focusing_other_display) {
2213 #if defined(OS_WIN)
2214 // On Windows, if we are switching to a non Aura Window on a different
2215 // screen we should not close the fullscreen window.
2216 if (!gained_focus) {
2217 POINT point = {0};
2218 ::GetCursorPos(&point);
2219 if (screen->GetDisplayNearestWindow(window_).id() !=
2220 screen->GetDisplayNearestPoint(gfx::Point(point)).id())
2221 return;
2223 #endif
2224 in_shutdown_ = true;
2225 host_->Shutdown();
2230 ////////////////////////////////////////////////////////////////////////////////
2231 // RenderWidgetHostViewAura, aura::WindowTreeHostObserver implementation:
2233 void RenderWidgetHostViewAura::OnHostMoved(const aura::WindowTreeHost* host,
2234 const gfx::Point& new_origin) {
2235 TRACE_EVENT1("ui", "RenderWidgetHostViewAura::OnHostMoved",
2236 "new_origin", new_origin.ToString());
2238 UpdateScreenInfo(window_);
2241 ////////////////////////////////////////////////////////////////////////////////
2242 // RenderWidgetHostViewAura, private:
2244 RenderWidgetHostViewAura::~RenderWidgetHostViewAura() {
2245 if (touch_editing_client_)
2246 touch_editing_client_->OnViewDestroyed();
2248 delegated_frame_host_.reset();
2249 window_observer_.reset();
2250 if (window_->GetHost())
2251 window_->GetHost()->RemoveObserver(this);
2252 UnlockMouse();
2253 if (popup_parent_host_view_) {
2254 DCHECK(popup_parent_host_view_->popup_child_host_view_ == NULL ||
2255 popup_parent_host_view_->popup_child_host_view_ == this);
2256 popup_parent_host_view_->popup_child_host_view_ = NULL;
2258 if (popup_child_host_view_) {
2259 DCHECK(popup_child_host_view_->popup_parent_host_view_ == NULL ||
2260 popup_child_host_view_->popup_parent_host_view_ == this);
2261 popup_child_host_view_->popup_parent_host_view_ = NULL;
2263 event_filter_for_popup_exit_.reset();
2264 aura::client::SetTooltipText(window_, NULL);
2265 gfx::Screen::GetScreenFor(window_)->RemoveObserver(this);
2267 // This call is usually no-op since |this| object is already removed from the
2268 // Aura root window and we don't have a way to get an input method object
2269 // associated with the window, but just in case.
2270 DetachFromInputMethod();
2272 #if defined(OS_WIN)
2273 // The LegacyRenderWidgetHostHWND window should have been destroyed in
2274 // RenderWidgetHostViewAura::OnWindowDestroying and the pointer should
2275 // be set to NULL.
2276 DCHECK(!legacy_render_widget_host_HWND_);
2277 #endif
2280 void RenderWidgetHostViewAura::UpdateCursorIfOverSelf() {
2281 const gfx::Point screen_point =
2282 gfx::Screen::GetScreenFor(GetNativeView())->GetCursorScreenPoint();
2283 aura::Window* root_window = window_->GetRootWindow();
2284 if (!root_window)
2285 return;
2287 gfx::Point root_window_point = screen_point;
2288 aura::client::ScreenPositionClient* screen_position_client =
2289 aura::client::GetScreenPositionClient(root_window);
2290 if (screen_position_client) {
2291 screen_position_client->ConvertPointFromScreen(
2292 root_window, &root_window_point);
2295 if (root_window->GetEventHandlerForPoint(root_window_point) != window_)
2296 return;
2298 gfx::NativeCursor cursor = current_cursor_.GetNativeCursor();
2299 // Do not show loading cursor when the cursor is currently hidden.
2300 if (is_loading_ && cursor != ui::kCursorNone)
2301 cursor = ui::kCursorPointer;
2303 aura::client::CursorClient* cursor_client =
2304 aura::client::GetCursorClient(root_window);
2305 if (cursor_client) {
2306 cursor_client->SetCursor(cursor);
2310 ui::InputMethod* RenderWidgetHostViewAura::GetInputMethod() const {
2311 aura::Window* root_window = window_->GetRootWindow();
2312 if (!root_window)
2313 return NULL;
2314 return root_window->GetProperty(aura::client::kRootWindowInputMethodKey);
2317 bool RenderWidgetHostViewAura::NeedsInputGrab() {
2318 return popup_type_ == blink::WebPopupTypeSelect;
2321 bool RenderWidgetHostViewAura::NeedsMouseCapture() {
2322 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
2323 return NeedsInputGrab();
2324 #endif
2325 return false;
2328 void RenderWidgetHostViewAura::FinishImeCompositionSession() {
2329 if (!has_composition_text_)
2330 return;
2331 if (host_) {
2332 host_->ImeConfirmComposition(base::string16(), gfx::Range::InvalidRange(),
2333 false);
2335 ImeCancelComposition();
2338 void RenderWidgetHostViewAura::ModifyEventMovementAndCoords(
2339 blink::WebMouseEvent* event) {
2340 // If the mouse has just entered, we must report zero movementX/Y. Hence we
2341 // reset any global_mouse_position set previously.
2342 if (event->type == blink::WebInputEvent::MouseEnter ||
2343 event->type == blink::WebInputEvent::MouseLeave)
2344 global_mouse_position_.SetPoint(event->globalX, event->globalY);
2346 // Movement is computed by taking the difference of the new cursor position
2347 // and the previous. Under mouse lock the cursor will be warped back to the
2348 // center so that we are not limited by clipping boundaries.
2349 // We do not measure movement as the delta from cursor to center because
2350 // we may receive more mouse movement events before our warp has taken
2351 // effect.
2352 event->movementX = event->globalX - global_mouse_position_.x();
2353 event->movementY = event->globalY - global_mouse_position_.y();
2355 global_mouse_position_.SetPoint(event->globalX, event->globalY);
2357 // Under mouse lock, coordinates of mouse are locked to what they were when
2358 // mouse lock was entered.
2359 if (mouse_locked_) {
2360 event->x = unlocked_mouse_position_.x();
2361 event->y = unlocked_mouse_position_.y();
2362 event->windowX = unlocked_mouse_position_.x();
2363 event->windowY = unlocked_mouse_position_.y();
2364 event->globalX = unlocked_global_mouse_position_.x();
2365 event->globalY = unlocked_global_mouse_position_.y();
2366 } else {
2367 unlocked_mouse_position_.SetPoint(event->windowX, event->windowY);
2368 unlocked_global_mouse_position_.SetPoint(event->globalX, event->globalY);
2372 void RenderWidgetHostViewAura::NotifyRendererOfCursorVisibilityState(
2373 bool is_visible) {
2374 if (host_->is_hidden() ||
2375 (cursor_visibility_state_in_renderer_ == VISIBLE && is_visible) ||
2376 (cursor_visibility_state_in_renderer_ == NOT_VISIBLE && !is_visible))
2377 return;
2379 cursor_visibility_state_in_renderer_ = is_visible ? VISIBLE : NOT_VISIBLE;
2380 host_->SendCursorVisibilityState(is_visible);
2383 void RenderWidgetHostViewAura::SetOverscrollControllerEnabled(bool enabled) {
2384 if (!enabled)
2385 overscroll_controller_.reset();
2386 else if (!overscroll_controller_)
2387 overscroll_controller_.reset(new OverscrollController());
2390 void RenderWidgetHostViewAura::SnapToPhysicalPixelBoundary() {
2391 // The top left corner of our view in window coordinates might not land on a
2392 // device pixel boundary if we have a non-integer device scale. In that case,
2393 // to avoid the web contents area looking blurry we translate the web contents
2394 // in the +x, +y direction to land on the nearest pixel boundary. This may
2395 // cause the bottom and right edges to be clipped slightly, but that's ok.
2396 aura::Window* snapped = NULL;
2397 // On desktop, use the root window. On alternative environment (ash),
2398 // use the toplevel window which must be already snapped.
2399 if (gfx::Screen::GetScreenFor(window_) !=
2400 gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE)) {
2401 snapped = window_->GetRootWindow();
2402 } else {
2403 snapped = window_->GetToplevelWindow();
2405 if (snapped && snapped != window_)
2406 ui::SnapLayerToPhysicalPixelBoundary(snapped->layer(), window_->layer());
2408 has_snapped_to_boundary_ = true;
2411 void RenderWidgetHostViewAura::InternalSetBounds(const gfx::Rect& rect) {
2412 if (HasDisplayPropertyChanged(window_))
2413 host_->InvalidateScreenInfo();
2415 SnapToPhysicalPixelBoundary();
2416 // Don't recursively call SetBounds if this bounds update is the result of
2417 // a Window::SetBoundsInternal call.
2418 if (!in_bounds_changed_)
2419 window_->SetBounds(rect);
2420 host_->WasResized();
2421 delegated_frame_host_->WasResized();
2422 if (touch_editing_client_) {
2423 touch_editing_client_->OnSelectionOrCursorChanged(selection_anchor_rect_,
2424 selection_focus_rect_);
2426 #if defined(OS_WIN)
2427 // Create the legacy dummy window which corresponds to the bounds of the
2428 // webcontents. This will be passed as the container window for windowless
2429 // plugins.
2430 // Plugins like Flash assume the container window which is returned via the
2431 // NPNVnetscapeWindow property corresponds to the bounds of the webpage.
2432 // This is not true in Aura where we have only HWND which is the main Aura
2433 // window. If we return this window to plugins like Flash then it causes the
2434 // coordinate translations done by these plugins to break.
2435 // Additonally the legacy dummy window is needed for accessibility and for
2436 // scrolling to work in legacy drivers for trackpoints/trackpads, etc.
2437 if (!legacy_window_destroyed_ && GetNativeViewId()) {
2438 if (!legacy_render_widget_host_HWND_) {
2439 legacy_render_widget_host_HWND_ = LegacyRenderWidgetHostHWND::Create(
2440 reinterpret_cast<HWND>(GetNativeViewId()));
2442 if (legacy_render_widget_host_HWND_) {
2443 legacy_render_widget_host_HWND_->set_host(this);
2444 legacy_render_widget_host_HWND_->SetBounds(
2445 window_->GetBoundsInRootWindow());
2446 // There are cases where the parent window is created, made visible and
2447 // the associated RenderWidget is also visible before the
2448 // LegacyRenderWidgetHostHWND instace is created. Ensure that it is shown
2449 // here.
2450 if (!host_->is_hidden())
2451 legacy_render_widget_host_HWND_->Show();
2455 if (mouse_locked_)
2456 UpdateMouseLockRegion();
2457 #endif
2460 void RenderWidgetHostViewAura::SchedulePaintIfNotInClip(
2461 const gfx::Rect& rect,
2462 const gfx::Rect& clip) {
2463 if (!clip.IsEmpty()) {
2464 gfx::Rect to_paint = gfx::SubtractRects(rect, clip);
2465 if (!to_paint.IsEmpty())
2466 window_->SchedulePaintInRect(to_paint);
2467 } else {
2468 window_->SchedulePaintInRect(rect);
2472 bool RenderWidgetHostViewAura::ShouldMoveToCenter() {
2473 gfx::Rect rect = window_->bounds();
2474 rect = ConvertRectToScreen(rect);
2475 int border_x = rect.width() * kMouseLockBorderPercentage / 100;
2476 int border_y = rect.height() * kMouseLockBorderPercentage / 100;
2478 return global_mouse_position_.x() < rect.x() + border_x ||
2479 global_mouse_position_.x() > rect.right() - border_x ||
2480 global_mouse_position_.y() < rect.y() + border_y ||
2481 global_mouse_position_.y() > rect.bottom() - border_y;
2484 void RenderWidgetHostViewAura::AddedToRootWindow() {
2485 window_->GetHost()->AddObserver(this);
2486 UpdateScreenInfo(window_);
2488 aura::client::CursorClient* cursor_client =
2489 aura::client::GetCursorClient(window_->GetRootWindow());
2490 if (cursor_client) {
2491 cursor_client->AddObserver(this);
2492 NotifyRendererOfCursorVisibilityState(cursor_client->IsCursorVisible());
2494 if (HasFocus()) {
2495 ui::InputMethod* input_method = GetInputMethod();
2496 if (input_method)
2497 input_method->SetFocusedTextInputClient(this);
2500 #if defined(OS_WIN)
2501 // The parent may have changed here. Ensure that the legacy window is
2502 // reparented accordingly.
2503 if (legacy_render_widget_host_HWND_)
2504 legacy_render_widget_host_HWND_->UpdateParent(
2505 reinterpret_cast<HWND>(GetNativeViewId()));
2506 #endif
2508 delegated_frame_host_->AddedToWindow();
2511 void RenderWidgetHostViewAura::RemovingFromRootWindow() {
2512 aura::client::CursorClient* cursor_client =
2513 aura::client::GetCursorClient(window_->GetRootWindow());
2514 if (cursor_client)
2515 cursor_client->RemoveObserver(this);
2517 DetachFromInputMethod();
2519 window_->GetHost()->RemoveObserver(this);
2520 delegated_frame_host_->RemovingFromWindow();
2522 #if defined(OS_WIN)
2523 // Update the legacy window's parent temporarily to the desktop window. It
2524 // will eventually get reparented to the right root.
2525 if (legacy_render_widget_host_HWND_)
2526 legacy_render_widget_host_HWND_->UpdateParent(::GetDesktopWindow());
2527 #endif
2530 void RenderWidgetHostViewAura::DetachFromInputMethod() {
2531 ui::InputMethod* input_method = GetInputMethod();
2532 if (input_method && input_method->GetTextInputClient() == this)
2533 input_method->SetFocusedTextInputClient(NULL);
2536 void RenderWidgetHostViewAura::ForwardKeyboardEvent(
2537 const NativeWebKeyboardEvent& event) {
2538 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
2539 ui::TextEditKeyBindingsDelegateAuraLinux* keybinding_delegate =
2540 ui::GetTextEditKeyBindingsDelegate();
2541 std::vector<ui::TextEditCommandAuraLinux> commands;
2542 if (!event.skip_in_browser &&
2543 keybinding_delegate &&
2544 event.os_event &&
2545 keybinding_delegate->MatchEvent(*event.os_event, &commands)) {
2546 // Transform from ui/ types to content/ types.
2547 EditCommands edit_commands;
2548 for (std::vector<ui::TextEditCommandAuraLinux>::const_iterator it =
2549 commands.begin(); it != commands.end(); ++it) {
2550 edit_commands.push_back(EditCommand(it->GetCommandString(),
2551 it->argument()));
2553 host_->Send(new InputMsg_SetEditCommandsForNextKeyEvent(
2554 host_->GetRoutingID(), edit_commands));
2555 NativeWebKeyboardEvent copy_event(event);
2556 copy_event.match_edit_command = true;
2557 host_->ForwardKeyboardEvent(copy_event);
2558 return;
2560 #endif
2562 host_->ForwardKeyboardEvent(event);
2565 SkColorType RenderWidgetHostViewAura::PreferredReadbackFormat() {
2566 return kN32_SkColorType;
2569 ////////////////////////////////////////////////////////////////////////////////
2570 // DelegatedFrameHost, public:
2572 ui::Compositor* RenderWidgetHostViewAura::GetCompositor() const {
2573 aura::WindowTreeHost* host = window_->GetHost();
2574 return host ? host->compositor() : NULL;
2577 ui::Layer* RenderWidgetHostViewAura::GetLayer() {
2578 return window_->layer();
2581 RenderWidgetHostImpl* RenderWidgetHostViewAura::GetHost() {
2582 return host_;
2585 bool RenderWidgetHostViewAura::IsVisible() {
2586 return IsShowing();
2589 gfx::Size RenderWidgetHostViewAura::DesiredFrameSize() {
2590 return window_->bounds().size();
2593 float RenderWidgetHostViewAura::CurrentDeviceScaleFactor() {
2594 return current_device_scale_factor_;
2597 gfx::Size RenderWidgetHostViewAura::ConvertViewSizeToPixel(
2598 const gfx::Size& size) {
2599 return content::ConvertViewSizeToPixel(this, size);
2602 scoped_ptr<ResizeLock> RenderWidgetHostViewAura::CreateResizeLock(
2603 bool defer_compositor_lock) {
2604 gfx::Size desired_size = window_->bounds().size();
2605 return scoped_ptr<ResizeLock>(new CompositorResizeLock(
2606 window_->GetHost(),
2607 desired_size,
2608 defer_compositor_lock,
2609 base::TimeDelta::FromMilliseconds(kResizeLockTimeoutMs)));
2612 DelegatedFrameHost* RenderWidgetHostViewAura::GetDelegatedFrameHost() const {
2613 return delegated_frame_host_.get();
2616 ////////////////////////////////////////////////////////////////////////////////
2617 // RenderWidgetHostViewBase, public:
2619 // static
2620 void RenderWidgetHostViewBase::GetDefaultScreenInfo(WebScreenInfo* results) {
2621 GetScreenInfoForWindow(results, NULL);
2624 } // namespace content