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"
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_impl.h"
33 #include "content/browser/renderer_host/render_widget_host_impl.h"
34 #include "content/browser/renderer_host/ui_events_helper.h"
35 #include "content/browser/renderer_host/web_input_event_aura.h"
36 #include "content/common/gpu/client/gl_helper.h"
37 #include "content/common/gpu/gpu_messages.h"
38 #include "content/common/view_messages.h"
39 #include "content/public/browser/content_browser_client.h"
40 #include "content/public/browser/overscroll_configuration.h"
41 #include "content/public/browser/render_view_host.h"
42 #include "content/public/browser/render_widget_host_view_frame_subscriber.h"
43 #include "content/public/browser/user_metrics.h"
44 #include "content/public/common/content_switches.h"
45 #include "third_party/WebKit/public/platform/WebScreenInfo.h"
46 #include "third_party/WebKit/public/web/WebCompositionUnderline.h"
47 #include "third_party/WebKit/public/web/WebInputEvent.h"
48 #include "ui/aura/client/aura_constants.h"
49 #include "ui/aura/client/cursor_client.h"
50 #include "ui/aura/client/cursor_client_observer.h"
51 #include "ui/aura/client/focus_client.h"
52 #include "ui/aura/client/screen_position_client.h"
53 #include "ui/aura/client/window_tree_client.h"
54 #include "ui/aura/env.h"
55 #include "ui/aura/window.h"
56 #include "ui/aura/window_event_dispatcher.h"
57 #include "ui/aura/window_observer.h"
58 #include "ui/aura/window_tracker.h"
59 #include "ui/aura/window_tree_host.h"
60 #include "ui/base/clipboard/scoped_clipboard_writer.h"
61 #include "ui/base/hit_test.h"
62 #include "ui/base/ime/input_method.h"
63 #include "ui/base/ui_base_types.h"
64 #include "ui/compositor/compositor_vsync_manager.h"
65 #include "ui/compositor/dip_util.h"
66 #include "ui/events/event.h"
67 #include "ui/events/event_utils.h"
68 #include "ui/events/gestures/gesture_recognizer.h"
69 #include "ui/gfx/canvas.h"
70 #include "ui/gfx/display.h"
71 #include "ui/gfx/rect_conversions.h"
72 #include "ui/gfx/screen.h"
73 #include "ui/gfx/size_conversions.h"
74 #include "ui/gfx/skia_util.h"
75 #include "ui/wm/public/activation_client.h"
76 #include "ui/wm/public/scoped_tooltip_disabler.h"
77 #include "ui/wm/public/tooltip_client.h"
78 #include "ui/wm/public/transient_window_client.h"
79 #include "ui/wm/public/window_types.h"
82 #include "content/browser/accessibility/browser_accessibility_manager_win.h"
83 #include "content/browser/accessibility/browser_accessibility_win.h"
84 #include "content/browser/renderer_host/legacy_render_widget_host_win.h"
85 #include "content/common/plugin_constants_win.h"
86 #include "ui/base/win/hidden_window.h"
87 #include "ui/gfx/gdi_util.h"
88 #include "ui/gfx/win/dpi.h"
91 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
92 #include "content/common/input_messages.h"
93 #include "ui/events/linux/text_edit_command_auralinux.h"
94 #include "ui/events/linux/text_edit_key_bindings_delegate_auralinux.h"
97 using gfx::RectToSkIRect
;
98 using gfx::SkIRectToRect
;
100 using blink::WebScreenInfo
;
101 using blink::WebInputEvent
;
102 using blink::WebGestureEvent
;
103 using blink::WebTouchEvent
;
109 // In mouse lock mode, we need to prevent the (invisible) cursor from hitting
110 // the border of the view, in order to get valid movement information. However,
111 // forcing the cursor back to the center of the view after each mouse move
112 // doesn't work well. It reduces the frequency of useful mouse move messages
113 // significantly. Therefore, we move the cursor to the center of the view only
114 // if it approaches the border. |kMouseLockBorderPercentage| specifies the width
115 // of the border area, in percentage of the corresponding dimension.
116 const int kMouseLockBorderPercentage
= 15;
118 // When accelerated compositing is enabled and a widget resize is pending,
119 // we delay further resizes of the UI. The following constant is the maximum
120 // length of time that we should delay further UI resizes while waiting for a
121 // resized frame from a renderer.
122 const int kResizeLockTimeoutMs
= 67;
125 // Used to associate a plugin HWND with its RenderWidgetHostViewAura instance.
126 const wchar_t kWidgetOwnerProperty
[] = L
"RenderWidgetHostViewAuraOwner";
128 BOOL CALLBACK
WindowDestroyingCallback(HWND window
, LPARAM param
) {
129 RenderWidgetHostViewAura
* widget
=
130 reinterpret_cast<RenderWidgetHostViewAura
*>(param
);
131 if (GetProp(window
, kWidgetOwnerProperty
) == widget
) {
132 // Properties set on HWNDs must be removed to avoid leaks.
133 RemoveProp(window
, kWidgetOwnerProperty
);
134 RenderWidgetHostViewBase::DetachPluginWindowsCallback(window
);
139 BOOL CALLBACK
HideWindowsCallback(HWND window
, LPARAM param
) {
140 RenderWidgetHostViewAura
* widget
=
141 reinterpret_cast<RenderWidgetHostViewAura
*>(param
);
142 if (GetProp(window
, kWidgetOwnerProperty
) == widget
)
143 SetParent(window
, ui::GetHiddenWindow());
147 BOOL CALLBACK
ShowWindowsCallback(HWND window
, LPARAM param
) {
148 RenderWidgetHostViewAura
* widget
=
149 reinterpret_cast<RenderWidgetHostViewAura
*>(param
);
151 if (GetProp(window
, kWidgetOwnerProperty
) == widget
&&
152 widget
->GetNativeView()->GetHost()) {
153 HWND parent
= widget
->GetNativeView()->GetHost()->GetAcceleratedWidget();
154 SetParent(window
, parent
);
159 struct CutoutRectsParams
{
160 RenderWidgetHostViewAura
* widget
;
161 std::vector
<gfx::Rect
> cutout_rects
;
162 std::map
<HWND
, WebPluginGeometry
>* geometry
;
165 // Used to update the region for the windowed plugin to draw in. We start with
166 // the clip rect from the renderer, then remove the cutout rects from the
167 // renderer, and then remove the transient windows from the root window and the
168 // constrained windows from the parent window.
169 BOOL CALLBACK
SetCutoutRectsCallback(HWND window
, LPARAM param
) {
170 CutoutRectsParams
* params
= reinterpret_cast<CutoutRectsParams
*>(param
);
172 if (GetProp(window
, kWidgetOwnerProperty
) == params
->widget
) {
173 // First calculate the offset of this plugin from the root window, since
174 // the cutouts are relative to the root window.
176 params
->widget
->GetNativeView()->GetHost()->GetAcceleratedWidget();
178 offset
.x
= offset
.y
= 0;
179 MapWindowPoints(window
, parent
, &offset
, 1);
181 // Now get the cached clip rect and cutouts for this plugin window that came
182 // from the renderer.
183 std::map
<HWND
, WebPluginGeometry
>::iterator i
= params
->geometry
->begin();
184 while (i
!= params
->geometry
->end() &&
185 i
->second
.window
!= window
&&
186 GetParent(i
->second
.window
) != window
) {
190 if (i
== params
->geometry
->end()) {
195 HRGN hrgn
= CreateRectRgn(i
->second
.clip_rect
.x(),
196 i
->second
.clip_rect
.y(),
197 i
->second
.clip_rect
.right(),
198 i
->second
.clip_rect
.bottom());
199 // We start with the cutout rects that came from the renderer, then add the
200 // ones that came from transient and constrained windows.
201 std::vector
<gfx::Rect
> cutout_rects
= i
->second
.cutout_rects
;
202 for (size_t i
= 0; i
< params
->cutout_rects
.size(); ++i
) {
203 gfx::Rect offset_cutout
= params
->cutout_rects
[i
];
204 offset_cutout
.Offset(-offset
.x
, -offset
.y
);
205 cutout_rects
.push_back(offset_cutout
);
207 gfx::SubtractRectanglesFromRegion(hrgn
, cutout_rects
);
208 // If we don't have any cutout rects then no point in messing with the
210 if (cutout_rects
.size())
211 SetWindowRgn(window
, hrgn
, TRUE
);
216 // A callback function for EnumThreadWindows to enumerate and dismiss
217 // any owned popup windows.
218 BOOL CALLBACK
DismissOwnedPopups(HWND window
, LPARAM arg
) {
219 const HWND toplevel_hwnd
= reinterpret_cast<HWND
>(arg
);
221 if (::IsWindowVisible(window
)) {
222 const HWND owner
= ::GetWindow(window
, GW_OWNER
);
223 if (toplevel_hwnd
== owner
) {
224 ::PostMessage(window
, WM_CANCELMODE
, 0, 0);
232 void UpdateWebTouchEventAfterDispatch(blink::WebTouchEvent
* event
,
233 blink::WebTouchPoint
* point
) {
234 if (point
->state
!= blink::WebTouchPoint::StateReleased
&&
235 point
->state
!= blink::WebTouchPoint::StateCancelled
)
238 const unsigned new_length
= event
->touchesLength
- 1;
239 // Work around a gcc 4.9 bug. crbug.com/392872
240 if (new_length
>= event
->touchesLengthCap
)
243 for (unsigned i
= point
- event
->touches
; i
< new_length
; ++i
)
244 event
->touches
[i
] = event
->touches
[i
+ 1];
245 event
->touchesLength
= new_length
;
248 bool CanRendererHandleEvent(const ui::MouseEvent
* event
) {
249 if (event
->type() == ui::ET_MOUSE_CAPTURE_CHANGED
)
253 // Renderer cannot handle WM_XBUTTON or NC events.
254 switch (event
->native_event().message
) {
257 case WM_XBUTTONDBLCLK
:
258 case WM_NCMOUSELEAVE
:
260 case WM_NCLBUTTONDOWN
:
262 case WM_NCLBUTTONDBLCLK
:
263 case WM_NCRBUTTONDOWN
:
265 case WM_NCRBUTTONDBLCLK
:
266 case WM_NCMBUTTONDOWN
:
268 case WM_NCMBUTTONDBLCLK
:
269 case WM_NCXBUTTONDOWN
:
271 case WM_NCXBUTTONDBLCLK
:
276 #elif defined(USE_X11)
277 // Renderer only supports standard mouse buttons, so ignore programmable
279 switch (event
->type()) {
280 case ui::ET_MOUSE_PRESSED
:
281 case ui::ET_MOUSE_RELEASED
:
282 return event
->IsAnyButton();
290 // We don't mark these as handled so that they're sent back to the
291 // DefWindowProc so it can generate WM_APPCOMMAND as necessary.
292 bool IsXButtonUpEvent(const ui::MouseEvent
* event
) {
294 switch (event
->native_event().message
) {
303 void GetScreenInfoForWindow(WebScreenInfo
* results
, aura::Window
* window
) {
304 const gfx::Display display
= window
?
305 gfx::Screen::GetScreenFor(window
)->GetDisplayNearestWindow(window
) :
306 gfx::Screen::GetScreenFor(window
)->GetPrimaryDisplay();
307 results
->rect
= display
.bounds();
308 results
->availableRect
= display
.work_area();
309 // TODO(derat|oshima): Don't hardcode this. Get this from display object.
311 results
->depthPerComponent
= 8;
312 results
->deviceScaleFactor
= display
.device_scale_factor();
314 // The Display rotation and the WebScreenInfo orientation are not the same
315 // angle. The former is the physical display rotation while the later is the
316 // rotation required by the content to be shown properly on the screen, in
317 // other words, relative to the physical display.
318 results
->orientationAngle
= display
.RotationAsDegree();
319 if (results
->orientationAngle
== 90)
320 results
->orientationAngle
= 270;
321 else if (results
->orientationAngle
== 270)
322 results
->orientationAngle
= 90;
324 results
->orientationType
=
325 RenderWidgetHostViewBase::GetOrientationTypeForDesktop(display
);
328 bool PointerEventActivates(const ui::Event
& event
) {
329 if (event
.type() == ui::ET_MOUSE_PRESSED
)
332 if (event
.type() == ui::ET_GESTURE_BEGIN
) {
333 const ui::GestureEvent
& gesture
=
334 static_cast<const ui::GestureEvent
&>(event
);
335 return gesture
.details().touch_points() == 1;
343 // We need to watch for mouse events outside a Web Popup or its parent
344 // and dismiss the popup for certain events.
345 class RenderWidgetHostViewAura::EventFilterForPopupExit
346 : public ui::EventHandler
{
348 explicit EventFilterForPopupExit(RenderWidgetHostViewAura
* rwhva
)
351 aura::Env::GetInstance()->AddPreTargetHandler(this);
354 virtual ~EventFilterForPopupExit() {
355 aura::Env::GetInstance()->RemovePreTargetHandler(this);
358 // Overridden from ui::EventHandler
359 virtual void OnMouseEvent(ui::MouseEvent
* event
) OVERRIDE
{
360 rwhva_
->ApplyEventFilterForPopupExit(event
);
363 virtual void OnTouchEvent(ui::TouchEvent
* event
) OVERRIDE
{
364 rwhva_
->ApplyEventFilterForPopupExit(event
);
368 RenderWidgetHostViewAura
* rwhva_
;
370 DISALLOW_COPY_AND_ASSIGN(EventFilterForPopupExit
);
373 void RenderWidgetHostViewAura::ApplyEventFilterForPopupExit(
374 ui::LocatedEvent
* event
) {
375 if (in_shutdown_
|| is_fullscreen_
|| !event
->target())
378 if (event
->type() != ui::ET_MOUSE_PRESSED
&&
379 event
->type() != ui::ET_TOUCH_PRESSED
) {
383 aura::Window
* target
= static_cast<aura::Window
*>(event
->target());
384 if (target
!= window_
&&
385 (!popup_parent_host_view_
||
386 target
!= popup_parent_host_view_
->window_
)) {
387 // Note: popup_parent_host_view_ may be NULL when there are multiple
388 // popup children per view. See: RenderWidgetHostViewAura::InitAsPopup().
394 // We have to implement the WindowObserver interface on a separate object
395 // because clang doesn't like implementing multiple interfaces that have
396 // methods with the same name. This object is owned by the
397 // RenderWidgetHostViewAura.
398 class RenderWidgetHostViewAura::WindowObserver
: public aura::WindowObserver
{
400 explicit WindowObserver(RenderWidgetHostViewAura
* view
)
402 view_
->window_
->AddObserver(this);
405 virtual ~WindowObserver() {
406 view_
->window_
->RemoveObserver(this);
409 // Overridden from aura::WindowObserver:
410 virtual void OnWindowAddedToRootWindow(aura::Window
* window
) OVERRIDE
{
411 if (window
== view_
->window_
)
412 view_
->AddedToRootWindow();
415 virtual void OnWindowRemovingFromRootWindow(aura::Window
* window
,
416 aura::Window
* new_root
) OVERRIDE
{
417 if (window
== view_
->window_
)
418 view_
->RemovingFromRootWindow();
422 RenderWidgetHostViewAura
* view_
;
424 DISALLOW_COPY_AND_ASSIGN(WindowObserver
);
427 ////////////////////////////////////////////////////////////////////////////////
428 // RenderWidgetHostViewAura, public:
430 RenderWidgetHostViewAura::RenderWidgetHostViewAura(RenderWidgetHost
* host
)
431 : host_(RenderWidgetHostImpl::From(host
)),
432 window_(new aura::Window(this)),
433 delegated_frame_host_(new DelegatedFrameHost(this)),
435 in_bounds_changed_(false),
436 is_fullscreen_(false),
437 popup_parent_host_view_(NULL
),
438 popup_child_host_view_(NULL
),
440 text_input_type_(ui::TEXT_INPUT_TYPE_NONE
),
441 text_input_mode_(ui::TEXT_INPUT_MODE_DEFAULT
),
442 can_compose_inline_(true),
443 has_composition_text_(false),
444 accept_return_character_(false),
445 last_swapped_software_frame_scale_factor_(1.f
),
447 synthetic_move_sent_(false),
448 cursor_visibility_state_in_renderer_(UNKNOWN
),
450 legacy_render_widget_host_HWND_(NULL
),
451 legacy_window_destroyed_(false),
453 has_snapped_to_boundary_(false),
454 touch_editing_client_(NULL
),
455 weak_ptr_factory_(this) {
456 host_
->SetView(this);
457 window_observer_
.reset(new WindowObserver(this));
458 aura::client::SetTooltipText(window_
, &tooltip_
);
459 aura::client::SetActivationDelegate(window_
, this);
460 aura::client::SetActivationChangeObserver(window_
, this);
461 aura::client::SetFocusChangeObserver(window_
, this);
462 window_
->set_layer_owner_delegate(delegated_frame_host_
.get());
463 gfx::Screen::GetScreenFor(window_
)->AddObserver(this);
465 bool overscroll_enabled
= base::CommandLine::ForCurrentProcess()->
466 GetSwitchValueASCII(switches::kOverscrollHistoryNavigation
) != "0";
467 SetOverscrollControllerEnabled(overscroll_enabled
);
470 ////////////////////////////////////////////////////////////////////////////////
471 // RenderWidgetHostViewAura, RenderWidgetHostView implementation:
473 void RenderWidgetHostViewAura::InitAsChild(
474 gfx::NativeView parent_view
) {
475 window_
->SetType(ui::wm::WINDOW_TYPE_CONTROL
);
476 window_
->Init(aura::WINDOW_LAYER_TEXTURED
);
477 window_
->SetName("RenderWidgetHostViewAura");
480 void RenderWidgetHostViewAura::InitAsPopup(
481 RenderWidgetHostView
* parent_host_view
,
482 const gfx::Rect
& bounds_in_screen
) {
483 popup_parent_host_view_
=
484 static_cast<RenderWidgetHostViewAura
*>(parent_host_view
);
486 // TransientWindowClient may be NULL during tests.
487 aura::client::TransientWindowClient
* transient_window_client
=
488 aura::client::GetTransientWindowClient();
489 RenderWidgetHostViewAura
* old_child
=
490 popup_parent_host_view_
->popup_child_host_view_
;
492 // TODO(jhorwich): Allow multiple popup_child_host_view_ per view, or
493 // similar mechanism to ensure a second popup doesn't cause the first one
494 // to never get a chance to filter events. See crbug.com/160589.
495 DCHECK(old_child
->popup_parent_host_view_
== popup_parent_host_view_
);
496 if (transient_window_client
) {
497 transient_window_client
->RemoveTransientChild(
498 popup_parent_host_view_
->window_
, old_child
->window_
);
500 old_child
->popup_parent_host_view_
= NULL
;
502 popup_parent_host_view_
->popup_child_host_view_
= this;
503 window_
->SetType(ui::wm::WINDOW_TYPE_MENU
);
504 window_
->Init(aura::WINDOW_LAYER_TEXTURED
);
505 window_
->SetName("RenderWidgetHostViewAura");
507 aura::Window
* root
= popup_parent_host_view_
->window_
->GetRootWindow();
508 aura::client::ParentWindowWithContext(window_
, root
, bounds_in_screen
);
509 // Setting the transient child allows for the popup to get mouse events when
510 // in a system modal dialog.
511 // This fixes crbug.com/328593.
512 if (transient_window_client
) {
513 transient_window_client
->AddTransientChild(
514 popup_parent_host_view_
->window_
, window_
);
517 SetBounds(bounds_in_screen
);
519 #if !defined(OS_WIN) && !defined(OS_CHROMEOS)
520 if (NeedsInputGrab())
521 window_
->SetCapture();
524 event_filter_for_popup_exit_
.reset(new EventFilterForPopupExit(this));
527 void RenderWidgetHostViewAura::InitAsFullscreen(
528 RenderWidgetHostView
* reference_host_view
) {
529 is_fullscreen_
= true;
530 window_
->SetType(ui::wm::WINDOW_TYPE_NORMAL
);
531 window_
->Init(aura::WINDOW_LAYER_TEXTURED
);
532 window_
->SetName("RenderWidgetHostViewAura");
533 window_
->SetProperty(aura::client::kShowStateKey
, ui::SHOW_STATE_FULLSCREEN
);
535 aura::Window
* parent
= NULL
;
537 if (reference_host_view
) {
538 aura::Window
* reference_window
=
539 static_cast<RenderWidgetHostViewAura
*>(reference_host_view
)->window_
;
540 if (reference_window
) {
541 host_tracker_
.reset(new aura::WindowTracker
);
542 host_tracker_
->Add(reference_window
);
544 gfx::Display display
= gfx::Screen::GetScreenFor(window_
)->
545 GetDisplayNearestWindow(reference_window
);
546 parent
= reference_window
->GetRootWindow();
547 bounds
= display
.bounds();
549 aura::client::ParentWindowWithContext(window_
, parent
, bounds
);
554 RenderWidgetHost
* RenderWidgetHostViewAura::GetRenderWidgetHost() const {
558 void RenderWidgetHostViewAura::WasShown() {
560 if (!host_
->is_hidden())
563 bool has_saved_frame
= delegated_frame_host_
->HasSavedFrame();
564 ui::LatencyInfo renderer_latency_info
, browser_latency_info
;
565 if (has_saved_frame
) {
566 browser_latency_info
.AddLatencyNumber(
567 ui::TAB_SHOW_COMPONENT
, host_
->GetLatencyComponentId(), 0);
569 renderer_latency_info
.AddLatencyNumber(
570 ui::TAB_SHOW_COMPONENT
, host_
->GetLatencyComponentId(), 0);
572 host_
->WasShown(renderer_latency_info
);
574 aura::Window
* root
= window_
->GetRootWindow();
576 aura::client::CursorClient
* cursor_client
=
577 aura::client::GetCursorClient(root
);
579 NotifyRendererOfCursorVisibilityState(cursor_client
->IsCursorVisible());
582 delegated_frame_host_
->WasShown(browser_latency_info
);
585 if (legacy_render_widget_host_HWND_
) {
586 // Reparent the legacy Chrome_RenderWidgetHostHWND window to the parent
587 // window before reparenting any plugins. This ensures that the plugin
588 // windows stay on top of the child Zorder in the parent and receive
589 // mouse events, etc.
590 legacy_render_widget_host_HWND_
->UpdateParent(
591 GetNativeView()->GetHost()->GetAcceleratedWidget());
592 legacy_render_widget_host_HWND_
->SetBounds(
593 window_
->GetBoundsInRootWindow());
595 LPARAM lparam
= reinterpret_cast<LPARAM
>(this);
596 EnumChildWindows(ui::GetHiddenWindow(), ShowWindowsCallback
, lparam
);
600 void RenderWidgetHostViewAura::WasHidden() {
601 if (!host_
|| host_
->is_hidden())
604 delegated_frame_host_
->WasHidden();
607 constrained_rects_
.clear();
608 aura::WindowTreeHost
* host
= window_
->GetHost();
610 HWND parent
= host
->GetAcceleratedWidget();
611 LPARAM lparam
= reinterpret_cast<LPARAM
>(this);
612 EnumChildWindows(parent
, HideWindowsCallback
, lparam
);
613 // We reparent the legacy Chrome_RenderWidgetHostHWND window to the global
614 // hidden window on the same lines as Windowed plugin windows.
615 if (legacy_render_widget_host_HWND_
)
616 legacy_render_widget_host_HWND_
->UpdateParent(ui::GetHiddenWindow());
621 void RenderWidgetHostViewAura::SetSize(const gfx::Size
& size
) {
622 // For a SetSize operation, we don't care what coordinate system the origin
623 // of the window is in, it's only important to make sure that the origin
624 // remains constant after the operation.
625 InternalSetBounds(gfx::Rect(window_
->bounds().origin(), size
));
628 void RenderWidgetHostViewAura::SetBounds(const gfx::Rect
& rect
) {
629 gfx::Point
relative_origin(rect
.origin());
631 // RenderWidgetHostViewAura::SetBounds() takes screen coordinates, but
632 // Window::SetBounds() takes parent coordinates, so do the conversion here.
633 aura::Window
* root
= window_
->GetRootWindow();
635 aura::client::ScreenPositionClient
* screen_position_client
=
636 aura::client::GetScreenPositionClient(root
);
637 if (screen_position_client
) {
638 screen_position_client
->ConvertPointFromScreen(
639 window_
->parent(), &relative_origin
);
643 InternalSetBounds(gfx::Rect(relative_origin
, rect
.size()));
646 gfx::NativeView
RenderWidgetHostViewAura::GetNativeView() const {
650 gfx::NativeViewId
RenderWidgetHostViewAura::GetNativeViewId() const {
652 aura::WindowTreeHost
* host
= window_
->GetHost();
654 return reinterpret_cast<gfx::NativeViewId
>(host
->GetAcceleratedWidget());
656 return static_cast<gfx::NativeViewId
>(NULL
);
659 gfx::NativeViewAccessible
RenderWidgetHostViewAura::GetNativeViewAccessible() {
661 aura::WindowTreeHost
* host
= window_
->GetHost();
663 return static_cast<gfx::NativeViewAccessible
>(NULL
);
664 HWND hwnd
= host
->GetAcceleratedWidget();
665 BrowserAccessibilityManager
* manager
=
666 host_
->GetOrCreateRootBrowserAccessibilityManager();
668 return manager
->GetRoot()->ToBrowserAccessibilityWin();
672 return static_cast<gfx::NativeViewAccessible
>(NULL
);
675 ui::TextInputClient
* RenderWidgetHostViewAura::GetTextInputClient() {
679 void RenderWidgetHostViewAura::SetKeyboardFocus() {
682 aura::WindowTreeHost
* host
= window_
->GetHost();
684 ::SetFocus(host
->GetAcceleratedWidget());
689 RenderFrameHostImpl
* RenderWidgetHostViewAura::GetFocusedFrame() {
690 if (!host_
->IsRenderView())
692 RenderViewHost
* rvh
= RenderViewHost::From(host_
);
693 FrameTreeNode
* focused_frame
=
694 rvh
->GetDelegate()->GetFrameTree()->GetFocusedFrame();
698 return focused_frame
->current_frame_host();
701 void RenderWidgetHostViewAura::MovePluginWindows(
702 const std::vector
<WebPluginGeometry
>& plugin_window_moves
) {
704 // We need to clip the rectangle to the tab's viewport, otherwise we will draw
705 // over the browser UI.
706 if (!window_
->GetRootWindow()) {
707 DCHECK(plugin_window_moves
.empty());
710 HWND parent
= window_
->GetHost()->GetAcceleratedWidget();
711 gfx::Rect view_bounds
= window_
->GetBoundsInRootWindow();
712 std::vector
<WebPluginGeometry
> moves
= plugin_window_moves
;
714 gfx::Rect
view_port(view_bounds
.size());
716 for (size_t i
= 0; i
< moves
.size(); ++i
) {
717 gfx::Rect
clip(moves
[i
].clip_rect
);
718 gfx::Vector2d
view_port_offset(
719 moves
[i
].window_rect
.OffsetFromOrigin());
720 clip
.Offset(view_port_offset
);
721 clip
.Intersect(view_port
);
722 clip
.Offset(-view_port_offset
);
723 moves
[i
].clip_rect
= clip
;
725 moves
[i
].window_rect
.Offset(view_bounds
.OffsetFromOrigin());
727 plugin_window_moves_
[moves
[i
].window
] = moves
[i
];
729 // constrained_rects_ are relative to the root window. We want to convert
730 // them to be relative to the plugin window.
731 for (size_t j
= 0; j
< constrained_rects_
.size(); ++j
) {
732 gfx::Rect offset_cutout
= constrained_rects_
[j
];
733 offset_cutout
-= moves
[i
].window_rect
.OffsetFromOrigin();
734 moves
[i
].cutout_rects
.push_back(offset_cutout
);
738 MovePluginWindowsHelper(parent
, moves
);
740 // Make sure each plugin window (or its wrapper if it exists) has a pointer to
742 for (size_t i
= 0; i
< moves
.size(); ++i
) {
743 HWND window
= moves
[i
].window
;
744 if (GetParent(window
) != parent
) {
745 window
= GetParent(window
);
747 if (!GetProp(window
, kWidgetOwnerProperty
))
748 SetProp(window
, kWidgetOwnerProperty
, this);
750 #endif // defined(OS_WIN)
753 void RenderWidgetHostViewAura::Focus() {
754 // Make sure we have a FocusClient before attempting to Focus(). In some
755 // situations we may not yet be in a valid Window hierarchy (such as reloading
756 // after out of memory discarded the tab).
757 aura::client::FocusClient
* client
= aura::client::GetFocusClient(window_
);
762 void RenderWidgetHostViewAura::Blur() {
766 bool RenderWidgetHostViewAura::HasFocus() const {
767 return window_
->HasFocus();
770 bool RenderWidgetHostViewAura::IsSurfaceAvailableForCopy() const {
771 return delegated_frame_host_
->CanCopyToBitmap();
774 void RenderWidgetHostViewAura::Show() {
778 if (legacy_render_widget_host_HWND_
)
779 legacy_render_widget_host_HWND_
->Show();
783 void RenderWidgetHostViewAura::Hide() {
787 if (legacy_render_widget_host_HWND_
)
788 legacy_render_widget_host_HWND_
->Hide();
792 bool RenderWidgetHostViewAura::IsShowing() {
793 return window_
->IsVisible();
796 gfx::Rect
RenderWidgetHostViewAura::GetViewBounds() const {
797 return window_
->GetBoundsInScreen();
800 void RenderWidgetHostViewAura::SetBackgroundOpaque(bool opaque
) {
801 RenderWidgetHostViewBase::SetBackgroundOpaque(opaque
);
802 host_
->SetBackgroundOpaque(opaque
);
803 window_
->layer()->SetFillsBoundsOpaquely(opaque
);
806 gfx::Size
RenderWidgetHostViewAura::GetVisibleViewportSize() const {
807 gfx::Rect
requested_rect(GetRequestedRendererSize());
808 requested_rect
.Inset(insets_
);
809 return requested_rect
.size();
812 void RenderWidgetHostViewAura::SetInsets(const gfx::Insets
& insets
) {
813 if (insets
!= insets_
) {
819 void RenderWidgetHostViewAura::UpdateCursor(const WebCursor
& cursor
) {
820 current_cursor_
= cursor
;
821 const gfx::Display display
= gfx::Screen::GetScreenFor(window_
)->
822 GetDisplayNearestWindow(window_
);
823 current_cursor_
.SetDisplayInfo(display
);
824 UpdateCursorIfOverSelf();
827 void RenderWidgetHostViewAura::SetIsLoading(bool is_loading
) {
828 is_loading_
= is_loading
;
829 UpdateCursorIfOverSelf();
832 void RenderWidgetHostViewAura::TextInputStateChanged(
833 const ViewHostMsg_TextInputState_Params
& params
) {
834 if (text_input_type_
!= params
.type
||
835 text_input_mode_
!= params
.mode
||
836 can_compose_inline_
!= params
.can_compose_inline
) {
837 text_input_type_
= params
.type
;
838 text_input_mode_
= params
.mode
;
839 can_compose_inline_
= params
.can_compose_inline
;
840 if (GetInputMethod())
841 GetInputMethod()->OnTextInputTypeChanged(this);
842 if (touch_editing_client_
)
843 touch_editing_client_
->OnTextInputTypeChanged(text_input_type_
);
845 if (params
.show_ime_if_needed
&& params
.type
!= ui::TEXT_INPUT_TYPE_NONE
) {
846 if (GetInputMethod())
847 GetInputMethod()->ShowImeIfNeeded();
851 void RenderWidgetHostViewAura::ImeCancelComposition() {
852 if (GetInputMethod())
853 GetInputMethod()->CancelComposition(this);
854 has_composition_text_
= false;
857 void RenderWidgetHostViewAura::ImeCompositionRangeChanged(
858 const gfx::Range
& range
,
859 const std::vector
<gfx::Rect
>& character_bounds
) {
860 composition_character_bounds_
= character_bounds
;
863 void RenderWidgetHostViewAura::RenderProcessGone(base::TerminationStatus status
,
865 UpdateCursorIfOverSelf();
869 void RenderWidgetHostViewAura::Destroy() {
870 // Beware, this function is not called on all destruction paths. It will
871 // implicitly end up calling ~RenderWidgetHostViewAura though, so all
872 // destruction/cleanup code should happen there, not here.
877 void RenderWidgetHostViewAura::SetTooltipText(
878 const base::string16
& tooltip_text
) {
879 tooltip_
= tooltip_text
;
880 aura::Window
* root_window
= window_
->GetRootWindow();
881 aura::client::TooltipClient
* tooltip_client
=
882 aura::client::GetTooltipClient(root_window
);
883 if (tooltip_client
) {
884 tooltip_client
->UpdateTooltip(window_
);
885 // Content tooltips should be visible indefinitely.
886 tooltip_client
->SetTooltipShownTimeout(window_
, 0);
890 void RenderWidgetHostViewAura::SelectionChanged(const base::string16
& text
,
892 const gfx::Range
& range
) {
893 RenderWidgetHostViewBase::SelectionChanged(text
, offset
, range
);
895 #if defined(USE_X11) && !defined(OS_CHROMEOS)
896 if (text
.empty() || range
.is_empty())
898 size_t pos
= range
.GetMin() - offset
;
899 size_t n
= range
.length();
901 DCHECK(pos
+ n
<= text
.length()) << "The text can not fully cover range.";
902 if (pos
>= text
.length()) {
903 NOTREACHED() << "The text can not cover range.";
907 // Set the CLIPBOARD_TYPE_SELECTION to the ui::Clipboard.
908 ui::ScopedClipboardWriter
clipboard_writer(
909 ui::Clipboard::GetForCurrentThread(),
910 ui::CLIPBOARD_TYPE_SELECTION
);
911 clipboard_writer
.WriteText(text
.substr(pos
, n
));
912 #endif // defined(USE_X11) && !defined(OS_CHROMEOS)
915 gfx::Size
RenderWidgetHostViewAura::GetRequestedRendererSize() const {
916 return delegated_frame_host_
->GetRequestedRendererSize();
919 void RenderWidgetHostViewAura::SelectionBoundsChanged(
920 const ViewHostMsg_SelectionBounds_Params
& params
) {
921 if (selection_anchor_rect_
== params
.anchor_rect
&&
922 selection_focus_rect_
== params
.focus_rect
)
925 selection_anchor_rect_
= params
.anchor_rect
;
926 selection_focus_rect_
= params
.focus_rect
;
928 if (GetInputMethod())
929 GetInputMethod()->OnCaretBoundsChanged(this);
931 if (touch_editing_client_
) {
932 touch_editing_client_
->OnSelectionOrCursorChanged(selection_anchor_rect_
,
933 selection_focus_rect_
);
937 void RenderWidgetHostViewAura::CopyFromCompositingSurface(
938 const gfx::Rect
& src_subrect
,
939 const gfx::Size
& dst_size
,
940 const base::Callback
<void(bool, const SkBitmap
&)>& callback
,
941 const SkColorType color_type
) {
942 delegated_frame_host_
->CopyFromCompositingSurface(
943 src_subrect
, dst_size
, callback
, color_type
);
946 void RenderWidgetHostViewAura::CopyFromCompositingSurfaceToVideoFrame(
947 const gfx::Rect
& src_subrect
,
948 const scoped_refptr
<media::VideoFrame
>& target
,
949 const base::Callback
<void(bool)>& callback
) {
950 delegated_frame_host_
->CopyFromCompositingSurfaceToVideoFrame(
951 src_subrect
, target
, callback
);
954 bool RenderWidgetHostViewAura::CanCopyToVideoFrame() const {
955 return delegated_frame_host_
->CanCopyToVideoFrame();
958 bool RenderWidgetHostViewAura::CanSubscribeFrame() const {
959 return delegated_frame_host_
->CanSubscribeFrame();
962 void RenderWidgetHostViewAura::BeginFrameSubscription(
963 scoped_ptr
<RenderWidgetHostViewFrameSubscriber
> subscriber
) {
964 delegated_frame_host_
->BeginFrameSubscription(subscriber
.Pass());
967 void RenderWidgetHostViewAura::EndFrameSubscription() {
968 delegated_frame_host_
->EndFrameSubscription();
971 void RenderWidgetHostViewAura::AcceleratedSurfaceInitialized(int host_id
,
976 bool RenderWidgetHostViewAura::UsesNativeWindowFrame() const {
977 return (legacy_render_widget_host_HWND_
!= NULL
);
980 void RenderWidgetHostViewAura::UpdateConstrainedWindowRects(
981 const std::vector
<gfx::Rect
>& rects
) {
982 // Check this before setting constrained_rects_, so that next time they're set
983 // and we have a root window we don't early return.
984 if (!window_
->GetHost())
987 if (rects
== constrained_rects_
)
990 constrained_rects_
= rects
;
992 HWND parent
= window_
->GetHost()->GetAcceleratedWidget();
993 CutoutRectsParams params
;
994 params
.widget
= this;
995 params
.cutout_rects
= constrained_rects_
;
996 params
.geometry
= &plugin_window_moves_
;
997 LPARAM lparam
= reinterpret_cast<LPARAM
>(¶ms
);
998 EnumChildWindows(parent
, SetCutoutRectsCallback
, lparam
);
1001 void RenderWidgetHostViewAura::UpdateMouseLockRegion() {
1002 // Clip the cursor if chrome is running on regular desktop.
1003 if (gfx::Screen::GetScreenFor(window_
) == gfx::Screen::GetNativeScreen()) {
1004 RECT window_rect
= window_
->GetBoundsInScreen().ToRECT();
1005 ::ClipCursor(&window_rect
);
1009 void RenderWidgetHostViewAura::OnLegacyWindowDestroyed() {
1010 legacy_render_widget_host_HWND_
= NULL
;
1011 legacy_window_destroyed_
= true;
1015 void RenderWidgetHostViewAura::AcceleratedSurfaceBuffersSwapped(
1016 const GpuHostMsg_AcceleratedSurfaceBuffersSwapped_Params
& params_in_pixel
,
1018 // Oldschool composited mode is no longer supported.
1021 void RenderWidgetHostViewAura::OnSwapCompositorFrame(
1022 uint32 output_surface_id
,
1023 scoped_ptr
<cc::CompositorFrame
> frame
) {
1024 TRACE_EVENT0("content", "RenderWidgetHostViewAura::OnSwapCompositorFrame");
1025 if (frame
->delegated_frame_data
) {
1026 delegated_frame_host_
->SwapDelegatedFrame(
1028 frame
->delegated_frame_data
.Pass(),
1029 frame
->metadata
.device_scale_factor
,
1030 frame
->metadata
.latency_info
);
1034 if (frame
->software_frame_data
) {
1035 DLOG(ERROR
) << "Unable to use software frame in aura";
1037 base::UserMetricsAction("BadMessageTerminate_SharedMemoryAura"));
1038 host_
->GetProcess()->ReceivedBadMessage();
1044 void RenderWidgetHostViewAura::SetParentNativeViewAccessible(
1045 gfx::NativeViewAccessible accessible_parent
) {
1048 gfx::NativeViewId
RenderWidgetHostViewAura::GetParentForWindowlessPlugin()
1050 if (legacy_render_widget_host_HWND_
) {
1051 return reinterpret_cast<gfx::NativeViewId
>(
1052 legacy_render_widget_host_HWND_
->hwnd());
1058 void RenderWidgetHostViewAura::AcceleratedSurfacePostSubBuffer(
1059 const GpuHostMsg_AcceleratedSurfacePostSubBuffer_Params
& params_in_pixel
,
1061 // Oldschool composited mode is no longer supported.
1064 void RenderWidgetHostViewAura::AcceleratedSurfaceSuspend() {
1067 void RenderWidgetHostViewAura::AcceleratedSurfaceRelease() {
1070 bool RenderWidgetHostViewAura::HasAcceleratedSurface(
1071 const gfx::Size
& desired_size
) {
1072 // Aura doesn't use GetBackingStore for accelerated pages, so it doesn't
1073 // matter what is returned here as GetBackingStore is the only caller of this
1074 // method. TODO(jbates) implement this if other Aura code needs it.
1078 void RenderWidgetHostViewAura::GetScreenInfo(WebScreenInfo
* results
) {
1079 GetScreenInfoForWindow(results
, window_
->GetRootWindow() ? window_
: NULL
);
1082 gfx::Rect
RenderWidgetHostViewAura::GetBoundsInRootWindow() {
1083 aura::Window
* top_level
= window_
->GetToplevelWindow();
1084 gfx::Rect
bounds(top_level
->GetBoundsInScreen());
1087 // TODO(zturner,iyengar): This will break when we remove support for NPAPI and
1088 // remove the legacy hwnd, so a better fix will need to be decided when that
1090 if (UsesNativeWindowFrame()) {
1091 // aura::Window doesn't take into account non-client area of native windows
1092 // (e.g. HWNDs), so for that case ask Windows directly what the bounds are.
1093 aura::WindowTreeHost
* host
= top_level
->GetHost();
1095 return top_level
->GetBoundsInScreen();
1096 RECT window_rect
= {0};
1097 HWND hwnd
= host
->GetAcceleratedWidget();
1098 ::GetWindowRect(hwnd
, &window_rect
);
1099 bounds
= gfx::Rect(window_rect
);
1101 // Maximized windows are outdented from the work area by the frame thickness
1102 // even though this "frame" is not painted. This confuses code (and people)
1103 // that think of a maximized window as corresponding exactly to the work
1104 // area. Correct for this by subtracting the frame thickness back off.
1105 if (::IsZoomed(hwnd
)) {
1106 bounds
.Inset(GetSystemMetrics(SM_CXSIZEFRAME
),
1107 GetSystemMetrics(SM_CYSIZEFRAME
));
1109 bounds
.Inset(GetSystemMetrics(SM_CXPADDEDBORDER
),
1110 GetSystemMetrics(SM_CXPADDEDBORDER
));
1114 bounds
= gfx::win::ScreenToDIPRect(bounds
);
1120 void RenderWidgetHostViewAura::WheelEventAck(
1121 const blink::WebMouseWheelEvent
& event
,
1122 InputEventAckState ack_result
) {
1123 if (overscroll_controller_
) {
1124 overscroll_controller_
->ReceivedEventACK(
1125 event
, (INPUT_EVENT_ACK_STATE_CONSUMED
== ack_result
));
1129 void RenderWidgetHostViewAura::GestureEventAck(
1130 const blink::WebGestureEvent
& event
,
1131 InputEventAckState ack_result
) {
1132 if (touch_editing_client_
)
1133 touch_editing_client_
->GestureEventAck(event
.type
);
1135 if (overscroll_controller_
) {
1136 overscroll_controller_
->ReceivedEventACK(
1137 event
, (INPUT_EVENT_ACK_STATE_CONSUMED
== ack_result
));
1141 void RenderWidgetHostViewAura::ProcessAckedTouchEvent(
1142 const TouchEventWithLatencyInfo
& touch
, InputEventAckState ack_result
) {
1143 ScopedVector
<ui::TouchEvent
> events
;
1144 if (!MakeUITouchEventsFromWebTouchEvents(touch
, &events
,
1145 SCREEN_COORDINATES
))
1148 aura::WindowTreeHost
* host
= window_
->GetHost();
1149 // |host| is NULL during tests.
1153 ui::EventResult result
= (ack_result
==
1154 INPUT_EVENT_ACK_STATE_CONSUMED
) ? ui::ER_HANDLED
: ui::ER_UNHANDLED
;
1155 for (ScopedVector
<ui::TouchEvent
>::iterator iter
= events
.begin(),
1156 end
= events
.end(); iter
!= end
; ++iter
) {
1157 host
->dispatcher()->ProcessedTouchEvent((*iter
), window_
, result
);
1161 scoped_ptr
<SyntheticGestureTarget
>
1162 RenderWidgetHostViewAura::CreateSyntheticGestureTarget() {
1163 return scoped_ptr
<SyntheticGestureTarget
>(
1164 new SyntheticGestureTargetAura(host_
));
1167 InputEventAckState
RenderWidgetHostViewAura::FilterInputEvent(
1168 const blink::WebInputEvent
& input_event
) {
1169 bool consumed
= false;
1170 if (input_event
.type
== WebInputEvent::GestureFlingStart
) {
1171 const WebGestureEvent
& gesture_event
=
1172 static_cast<const WebGestureEvent
&>(input_event
);
1173 // Zero-velocity touchpad flings are an Aura-specific signal that the
1174 // touchpad scroll has ended, and should not be forwarded to the renderer.
1175 if (gesture_event
.sourceDevice
== blink::WebGestureDeviceTouchpad
&&
1176 !gesture_event
.data
.flingStart
.velocityX
&&
1177 !gesture_event
.data
.flingStart
.velocityY
) {
1182 if (overscroll_controller_
)
1183 consumed
|= overscroll_controller_
->WillHandleEvent(input_event
);
1185 return consumed
&& !WebTouchEvent::isTouchEventType(input_event
.type
)
1186 ? INPUT_EVENT_ACK_STATE_CONSUMED
1187 : INPUT_EVENT_ACK_STATE_NOT_CONSUMED
;
1190 BrowserAccessibilityManager
*
1191 RenderWidgetHostViewAura::CreateBrowserAccessibilityManager(
1192 BrowserAccessibilityDelegate
* delegate
) {
1193 BrowserAccessibilityManager
* manager
= NULL
;
1195 manager
= new BrowserAccessibilityManagerWin(
1196 BrowserAccessibilityManagerWin::GetEmptyDocument(), delegate
);
1198 manager
= BrowserAccessibilityManager::Create(
1199 BrowserAccessibilityManager::GetEmptyDocument(), delegate
);
1204 gfx::AcceleratedWidget
1205 RenderWidgetHostViewAura::AccessibilityGetAcceleratedWidget() {
1207 if (legacy_render_widget_host_HWND_
)
1208 return legacy_render_widget_host_HWND_
->hwnd();
1210 return gfx::kNullAcceleratedWidget
;
1213 gfx::NativeViewAccessible
1214 RenderWidgetHostViewAura::AccessibilityGetNativeViewAccessible() {
1216 if (legacy_render_widget_host_HWND_
)
1217 return legacy_render_widget_host_HWND_
->window_accessible();
1223 gfx::GLSurfaceHandle
RenderWidgetHostViewAura::GetCompositingSurface() {
1224 return ImageTransportFactory::GetInstance()->GetSharedSurfaceHandle();
1227 bool RenderWidgetHostViewAura::LockMouse() {
1228 aura::Window
* root_window
= window_
->GetRootWindow();
1235 mouse_locked_
= true;
1236 #if !defined(OS_WIN)
1237 window_
->SetCapture();
1239 UpdateMouseLockRegion();
1241 aura::client::CursorClient
* cursor_client
=
1242 aura::client::GetCursorClient(root_window
);
1243 if (cursor_client
) {
1244 cursor_client
->HideCursor();
1245 cursor_client
->LockCursor();
1248 if (ShouldMoveToCenter()) {
1249 synthetic_move_sent_
= true;
1250 window_
->MoveCursorTo(gfx::Rect(window_
->bounds().size()).CenterPoint());
1252 tooltip_disabler_
.reset(new aura::client::ScopedTooltipDisabler(root_window
));
1256 void RenderWidgetHostViewAura::UnlockMouse() {
1257 tooltip_disabler_
.reset();
1259 aura::Window
* root_window
= window_
->GetRootWindow();
1260 if (!mouse_locked_
|| !root_window
)
1263 mouse_locked_
= false;
1265 #if !defined(OS_WIN)
1266 window_
->ReleaseCapture();
1270 window_
->MoveCursorTo(unlocked_mouse_position_
);
1271 aura::client::CursorClient
* cursor_client
=
1272 aura::client::GetCursorClient(root_window
);
1273 if (cursor_client
) {
1274 cursor_client
->UnlockCursor();
1275 cursor_client
->ShowCursor();
1278 host_
->LostMouseLock();
1281 ////////////////////////////////////////////////////////////////////////////////
1282 // RenderWidgetHostViewAura, ui::TextInputClient implementation:
1283 void RenderWidgetHostViewAura::SetCompositionText(
1284 const ui::CompositionText
& composition
) {
1288 // TODO(suzhe): convert both renderer_host and renderer to use
1289 // ui::CompositionText.
1290 std::vector
<blink::WebCompositionUnderline
> underlines
;
1291 underlines
.reserve(composition
.underlines
.size());
1292 for (std::vector
<ui::CompositionUnderline
>::const_iterator it
=
1293 composition
.underlines
.begin();
1294 it
!= composition
.underlines
.end(); ++it
) {
1295 underlines
.push_back(
1296 blink::WebCompositionUnderline(static_cast<unsigned>(it
->start_offset
),
1297 static_cast<unsigned>(it
->end_offset
),
1300 it
->background_color
));
1303 // TODO(suzhe): due to a bug of webkit, we can't use selection range with
1304 // composition string. See: https://bugs.webkit.org/show_bug.cgi?id=37788
1305 host_
->ImeSetComposition(composition
.text
, underlines
,
1306 composition
.selection
.end(),
1307 composition
.selection
.end());
1309 has_composition_text_
= !composition
.text
.empty();
1312 void RenderWidgetHostViewAura::ConfirmCompositionText() {
1313 if (host_
&& has_composition_text_
) {
1314 host_
->ImeConfirmComposition(base::string16(), gfx::Range::InvalidRange(),
1317 has_composition_text_
= false;
1320 void RenderWidgetHostViewAura::ClearCompositionText() {
1321 if (host_
&& has_composition_text_
)
1322 host_
->ImeCancelComposition();
1323 has_composition_text_
= false;
1326 void RenderWidgetHostViewAura::InsertText(const base::string16
& text
) {
1327 DCHECK(text_input_type_
!= ui::TEXT_INPUT_TYPE_NONE
);
1329 host_
->ImeConfirmComposition(text
, gfx::Range::InvalidRange(), false);
1330 has_composition_text_
= false;
1333 void RenderWidgetHostViewAura::InsertChar(base::char16 ch
, int flags
) {
1334 if (popup_child_host_view_
&& popup_child_host_view_
->NeedsInputGrab()) {
1335 popup_child_host_view_
->InsertChar(ch
, flags
);
1339 // Ignore character messages for VKEY_RETURN sent on CTRL+M. crbug.com/315547
1340 if (host_
&& (accept_return_character_
|| ch
!= ui::VKEY_RETURN
)) {
1341 double now
= ui::EventTimeForNow().InSecondsF();
1342 // Send a blink::WebInputEvent::Char event to |host_|.
1343 NativeWebKeyboardEvent
webkit_event(ui::ET_KEY_PRESSED
,
1348 ForwardKeyboardEvent(webkit_event
);
1352 gfx::NativeWindow
RenderWidgetHostViewAura::GetAttachedWindow() const {
1356 ui::TextInputType
RenderWidgetHostViewAura::GetTextInputType() const {
1357 return text_input_type_
;
1360 ui::TextInputMode
RenderWidgetHostViewAura::GetTextInputMode() const {
1361 return text_input_mode_
;
1364 bool RenderWidgetHostViewAura::CanComposeInline() const {
1365 return can_compose_inline_
;
1368 gfx::Rect
RenderWidgetHostViewAura::ConvertRectToScreen(
1369 const gfx::Rect
& rect
) const {
1370 gfx::Point origin
= rect
.origin();
1371 gfx::Point end
= gfx::Point(rect
.right(), rect
.bottom());
1373 aura::Window
* root_window
= window_
->GetRootWindow();
1376 aura::client::ScreenPositionClient
* screen_position_client
=
1377 aura::client::GetScreenPositionClient(root_window
);
1378 if (!screen_position_client
)
1380 screen_position_client
->ConvertPointToScreen(window_
, &origin
);
1381 screen_position_client
->ConvertPointToScreen(window_
, &end
);
1382 return gfx::Rect(origin
.x(),
1384 end
.x() - origin
.x(),
1385 end
.y() - origin
.y());
1388 gfx::Rect
RenderWidgetHostViewAura::ConvertRectFromScreen(
1389 const gfx::Rect
& rect
) const {
1390 gfx::Point origin
= rect
.origin();
1391 gfx::Point end
= gfx::Point(rect
.right(), rect
.bottom());
1393 aura::Window
* root_window
= window_
->GetRootWindow();
1395 aura::client::ScreenPositionClient
* screen_position_client
=
1396 aura::client::GetScreenPositionClient(root_window
);
1397 screen_position_client
->ConvertPointFromScreen(window_
, &origin
);
1398 screen_position_client
->ConvertPointFromScreen(window_
, &end
);
1399 return gfx::Rect(origin
.x(),
1401 end
.x() - origin
.x(),
1402 end
.y() - origin
.y());
1408 gfx::Rect
RenderWidgetHostViewAura::GetCaretBounds() const {
1409 const gfx::Rect rect
=
1410 gfx::UnionRects(selection_anchor_rect_
, selection_focus_rect_
);
1411 return ConvertRectToScreen(rect
);
1414 bool RenderWidgetHostViewAura::GetCompositionCharacterBounds(
1416 gfx::Rect
* rect
) const {
1418 if (index
>= composition_character_bounds_
.size())
1420 *rect
= ConvertRectToScreen(composition_character_bounds_
[index
]);
1424 bool RenderWidgetHostViewAura::HasCompositionText() const {
1425 return has_composition_text_
;
1428 bool RenderWidgetHostViewAura::GetTextRange(gfx::Range
* range
) const {
1429 range
->set_start(selection_text_offset_
);
1430 range
->set_end(selection_text_offset_
+ selection_text_
.length());
1434 bool RenderWidgetHostViewAura::GetCompositionTextRange(
1435 gfx::Range
* range
) const {
1436 // TODO(suzhe): implement this method when fixing http://crbug.com/55130.
1441 bool RenderWidgetHostViewAura::GetSelectionRange(gfx::Range
* range
) const {
1442 range
->set_start(selection_range_
.start());
1443 range
->set_end(selection_range_
.end());
1447 bool RenderWidgetHostViewAura::SetSelectionRange(const gfx::Range
& range
) {
1448 // TODO(suzhe): implement this method when fixing http://crbug.com/55130.
1453 bool RenderWidgetHostViewAura::DeleteRange(const gfx::Range
& range
) {
1454 // TODO(suzhe): implement this method when fixing http://crbug.com/55130.
1459 bool RenderWidgetHostViewAura::GetTextFromRange(
1460 const gfx::Range
& range
,
1461 base::string16
* text
) const {
1462 gfx::Range
selection_text_range(selection_text_offset_
,
1463 selection_text_offset_
+ selection_text_
.length());
1465 if (!selection_text_range
.Contains(range
)) {
1469 if (selection_text_range
.EqualsIgnoringDirection(range
)) {
1470 // Avoid calling substr whose performance is low.
1471 *text
= selection_text_
;
1473 *text
= selection_text_
.substr(
1474 range
.GetMin() - selection_text_offset_
,
1480 void RenderWidgetHostViewAura::OnInputMethodChanged() {
1484 if (GetInputMethod())
1485 host_
->SetInputMethodActive(GetInputMethod()->IsActive());
1487 // TODO(suzhe): implement the newly added “locale” property of HTML DOM
1491 bool RenderWidgetHostViewAura::ChangeTextDirectionAndLayoutAlignment(
1492 base::i18n::TextDirection direction
) {
1495 host_
->UpdateTextDirection(
1496 direction
== base::i18n::RIGHT_TO_LEFT
?
1497 blink::WebTextDirectionRightToLeft
:
1498 blink::WebTextDirectionLeftToRight
);
1499 host_
->NotifyTextDirection();
1503 void RenderWidgetHostViewAura::ExtendSelectionAndDelete(
1504 size_t before
, size_t after
) {
1505 RenderFrameHostImpl
* rfh
= GetFocusedFrame();
1507 rfh
->ExtendSelectionAndDelete(before
, after
);
1510 void RenderWidgetHostViewAura::EnsureCaretInRect(const gfx::Rect
& rect
) {
1511 gfx::Rect
intersected_rect(
1512 gfx::IntersectRects(rect
, window_
->GetBoundsInScreen()));
1514 if (intersected_rect
.IsEmpty())
1517 host_
->ScrollFocusedEditableNodeIntoRect(
1518 ConvertRectFromScreen(intersected_rect
));
1521 void RenderWidgetHostViewAura::OnCandidateWindowShown() {
1522 host_
->CandidateWindowShown();
1525 void RenderWidgetHostViewAura::OnCandidateWindowUpdated() {
1526 host_
->CandidateWindowUpdated();
1529 void RenderWidgetHostViewAura::OnCandidateWindowHidden() {
1530 host_
->CandidateWindowHidden();
1533 bool RenderWidgetHostViewAura::IsEditingCommandEnabled(int command_id
) {
1537 void RenderWidgetHostViewAura::ExecuteEditingCommand(int command_id
) {
1540 ////////////////////////////////////////////////////////////////////////////////
1541 // RenderWidgetHostViewAura, gfx::DisplayObserver implementation:
1543 void RenderWidgetHostViewAura::OnDisplayAdded(
1544 const gfx::Display
& new_display
) {
1547 void RenderWidgetHostViewAura::OnDisplayRemoved(
1548 const gfx::Display
& old_display
) {
1551 void RenderWidgetHostViewAura::OnDisplayMetricsChanged(
1552 const gfx::Display
& display
, uint32_t metrics
) {
1553 // The screen info should be updated regardless of the metric change.
1554 gfx::Screen
* screen
= gfx::Screen::GetScreenFor(window_
);
1555 if (display
.id() == screen
->GetDisplayNearestWindow(window_
).id()) {
1556 UpdateScreenInfo(window_
);
1557 current_cursor_
.SetDisplayInfo(display
);
1558 UpdateCursorIfOverSelf();
1562 ////////////////////////////////////////////////////////////////////////////////
1563 // RenderWidgetHostViewAura, aura::WindowDelegate implementation:
1565 gfx::Size
RenderWidgetHostViewAura::GetMinimumSize() const {
1569 gfx::Size
RenderWidgetHostViewAura::GetMaximumSize() const {
1573 void RenderWidgetHostViewAura::OnBoundsChanged(const gfx::Rect
& old_bounds
,
1574 const gfx::Rect
& new_bounds
) {
1575 base::AutoReset
<bool> in_bounds_changed(&in_bounds_changed_
, true);
1576 // We care about this whenever RenderWidgetHostViewAura is not owned by a
1577 // WebContentsViewAura since changes to the Window's bounds need to be
1578 // messaged to the renderer. WebContentsViewAura invokes SetSize() or
1579 // SetBounds() itself. No matter how we got here, any redundant calls are
1581 SetSize(new_bounds
.size());
1583 if (GetInputMethod())
1584 GetInputMethod()->OnCaretBoundsChanged(this);
1587 gfx::NativeCursor
RenderWidgetHostViewAura::GetCursor(const gfx::Point
& point
) {
1589 return ui::kCursorNone
;
1590 return current_cursor_
.GetNativeCursor();
1593 int RenderWidgetHostViewAura::GetNonClientComponent(
1594 const gfx::Point
& point
) const {
1598 bool RenderWidgetHostViewAura::ShouldDescendIntoChildForEventHandling(
1599 aura::Window
* child
,
1600 const gfx::Point
& location
) {
1604 bool RenderWidgetHostViewAura::CanFocus() {
1605 return popup_type_
== blink::WebPopupTypeNone
;
1608 void RenderWidgetHostViewAura::OnCaptureLost() {
1609 host_
->LostCapture();
1610 if (touch_editing_client_
)
1611 touch_editing_client_
->EndTouchEditing(false);
1614 void RenderWidgetHostViewAura::OnPaint(gfx::Canvas
* canvas
) {
1615 // For non-opaque windows, we don't draw anything, since we depend on the
1616 // canvas coming from the compositor to already be initialized as
1618 if (window_
->layer()->fills_bounds_opaquely())
1619 canvas
->DrawColor(SK_ColorWHITE
);
1622 void RenderWidgetHostViewAura::OnDeviceScaleFactorChanged(
1623 float device_scale_factor
) {
1624 if (!host_
|| !window_
->GetRootWindow())
1627 UpdateScreenInfo(window_
);
1629 const gfx::Display display
= gfx::Screen::GetScreenFor(window_
)->
1630 GetDisplayNearestWindow(window_
);
1631 DCHECK_EQ(device_scale_factor
, display
.device_scale_factor());
1632 current_cursor_
.SetDisplayInfo(display
);
1633 SnapToPhysicalPixelBoundary();
1636 void RenderWidgetHostViewAura::OnWindowDestroying(aura::Window
* window
) {
1639 // If the tab was hidden and it's closed, host_->is_hidden would have been
1640 // reset to false in RenderWidgetHostImpl::RendererExited.
1641 if (!window_
->GetRootWindow() || host_
->is_hidden()) {
1642 parent
= ui::GetHiddenWindow();
1644 parent
= window_
->GetHost()->GetAcceleratedWidget();
1646 LPARAM lparam
= reinterpret_cast<LPARAM
>(this);
1647 EnumChildWindows(parent
, WindowDestroyingCallback
, lparam
);
1649 // The LegacyRenderWidgetHostHWND instance is destroyed when its window is
1650 // destroyed. Normally we control when that happens via the Destroy call
1651 // in the dtor. However there may be cases where the window is destroyed
1652 // by Windows, i.e. the parent window is destroyed before the
1653 // RenderWidgetHostViewAura instance goes away etc. To avoid that we
1654 // destroy the LegacyRenderWidgetHostHWND instance here.
1655 if (legacy_render_widget_host_HWND_
) {
1656 legacy_render_widget_host_HWND_
->set_host(NULL
);
1657 legacy_render_widget_host_HWND_
->Destroy();
1658 // The Destroy call above will delete the LegacyRenderWidgetHostHWND
1660 legacy_render_widget_host_HWND_
= NULL
;
1664 // Make sure that the input method no longer references to this object before
1665 // this object is removed from the root window (i.e. this object loses access
1666 // to the input method).
1667 ui::InputMethod
* input_method
= GetInputMethod();
1669 input_method
->DetachTextInputClient(this);
1671 if (overscroll_controller_
)
1672 overscroll_controller_
->Reset();
1675 void RenderWidgetHostViewAura::OnWindowDestroyed(aura::Window
* window
) {
1676 host_
->ViewDestroyed();
1680 void RenderWidgetHostViewAura::OnWindowTargetVisibilityChanged(bool visible
) {
1683 bool RenderWidgetHostViewAura::HasHitTestMask() const {
1687 void RenderWidgetHostViewAura::GetHitTestMask(gfx::Path
* mask
) const {
1690 ////////////////////////////////////////////////////////////////////////////////
1691 // RenderWidgetHostViewAura, ui::EventHandler implementation:
1693 void RenderWidgetHostViewAura::OnKeyEvent(ui::KeyEvent
* event
) {
1694 TRACE_EVENT0("input", "RenderWidgetHostViewAura::OnKeyEvent");
1695 if (touch_editing_client_
&& touch_editing_client_
->HandleInputEvent(event
))
1698 if (popup_child_host_view_
&& popup_child_host_view_
->NeedsInputGrab()) {
1699 popup_child_host_view_
->OnKeyEvent(event
);
1700 if (event
->handled())
1704 // We need to handle the Escape key for Pepper Flash.
1705 if (is_fullscreen_
&& event
->key_code() == ui::VKEY_ESCAPE
) {
1706 // Focus the window we were created from.
1707 if (host_tracker_
.get() && !host_tracker_
->windows().empty()) {
1708 aura::Window
* host
= *(host_tracker_
->windows().begin());
1709 aura::client::FocusClient
* client
= aura::client::GetFocusClient(host
);
1711 // Calling host->Focus() may delete |this|. We create a local observer
1712 // for that. In that case we exit without further access to any members.
1713 aura::WindowTracker tracker
;
1714 aura::Window
* window
= window_
;
1715 tracker
.Add(window
);
1717 if (!tracker
.Contains(window
)) {
1718 event
->SetHandled();
1723 if (!in_shutdown_
) {
1724 in_shutdown_
= true;
1728 if (event
->key_code() == ui::VKEY_RETURN
) {
1729 // Do not forward return key release events if no press event was handled.
1730 if (event
->type() == ui::ET_KEY_RELEASED
&& !accept_return_character_
)
1732 // Accept return key character events between press and release events.
1733 accept_return_character_
= event
->type() == ui::ET_KEY_PRESSED
;
1736 // We don't have to communicate with an input method here.
1737 if (!event
->HasNativeEvent()) {
1738 NativeWebKeyboardEvent
webkit_event(
1741 event
->is_char() ? event
->GetCharacter() : event
->key_code(),
1743 ui::EventTimeForNow().InSecondsF());
1744 ForwardKeyboardEvent(webkit_event
);
1746 NativeWebKeyboardEvent
webkit_event(event
);
1747 ForwardKeyboardEvent(webkit_event
);
1750 event
->SetHandled();
1753 void RenderWidgetHostViewAura::OnMouseEvent(ui::MouseEvent
* event
) {
1754 TRACE_EVENT0("input", "RenderWidgetHostViewAura::OnMouseEvent");
1756 if (touch_editing_client_
&& touch_editing_client_
->HandleInputEvent(event
))
1759 if (mouse_locked_
) {
1760 aura::client::CursorClient
* cursor_client
=
1761 aura::client::GetCursorClient(window_
->GetRootWindow());
1762 DCHECK(!cursor_client
|| !cursor_client
->IsCursorVisible());
1764 if (event
->type() == ui::ET_MOUSEWHEEL
) {
1765 blink::WebMouseWheelEvent mouse_wheel_event
=
1766 MakeWebMouseWheelEvent(static_cast<ui::MouseWheelEvent
*>(event
));
1767 if (mouse_wheel_event
.deltaX
!= 0 || mouse_wheel_event
.deltaY
!= 0)
1768 host_
->ForwardWheelEvent(mouse_wheel_event
);
1772 gfx::Point
center(gfx::Rect(window_
->bounds().size()).CenterPoint());
1774 // If we receive non client mouse messages while we are in the locked state
1775 // it probably means that the mouse left the borders of our window and
1776 // needs to be moved back to the center.
1777 if (event
->flags() & ui::EF_IS_NON_CLIENT
) {
1778 synthetic_move_sent_
= true;
1779 window_
->MoveCursorTo(center
);
1783 blink::WebMouseEvent mouse_event
= MakeWebMouseEvent(event
);
1785 bool is_move_to_center_event
= (event
->type() == ui::ET_MOUSE_MOVED
||
1786 event
->type() == ui::ET_MOUSE_DRAGGED
) &&
1787 mouse_event
.x
== center
.x() && mouse_event
.y
== center
.y();
1789 ModifyEventMovementAndCoords(&mouse_event
);
1791 bool should_not_forward
= is_move_to_center_event
&& synthetic_move_sent_
;
1792 if (should_not_forward
) {
1793 synthetic_move_sent_
= false;
1795 // Check if the mouse has reached the border and needs to be centered.
1796 if (ShouldMoveToCenter()) {
1797 synthetic_move_sent_
= true;
1798 window_
->MoveCursorTo(center
);
1800 // Forward event to renderer.
1801 if (CanRendererHandleEvent(event
) &&
1802 !(event
->flags() & ui::EF_FROM_TOUCH
)) {
1803 host_
->ForwardMouseEvent(mouse_event
);
1804 // Ensure that we get keyboard focus on mouse down as a plugin window
1805 // may have grabbed keyboard focus.
1806 if (event
->type() == ui::ET_MOUSE_PRESSED
)
1813 // As the overscroll is handled during scroll events from the trackpad, the
1814 // RWHVA window is transformed by the overscroll controller. This transform
1815 // triggers a synthetic mouse-move event to be generated (by the aura
1816 // RootWindow). But this event interferes with the overscroll gesture. So,
1817 // ignore such synthetic mouse-move events if an overscroll gesture is in
1819 if (overscroll_controller_
&&
1820 overscroll_controller_
->overscroll_mode() != OVERSCROLL_NONE
&&
1821 event
->flags() & ui::EF_IS_SYNTHESIZED
&&
1822 (event
->type() == ui::ET_MOUSE_ENTERED
||
1823 event
->type() == ui::ET_MOUSE_EXITED
||
1824 event
->type() == ui::ET_MOUSE_MOVED
)) {
1825 event
->StopPropagation();
1829 if (event
->type() == ui::ET_MOUSEWHEEL
) {
1831 // We get mouse wheel/scroll messages even if we are not in the foreground.
1832 // So here we check if we have any owned popup windows in the foreground and
1834 aura::WindowTreeHost
* host
= window_
->GetHost();
1836 HWND parent
= host
->GetAcceleratedWidget();
1837 HWND toplevel_hwnd
= ::GetAncestor(parent
, GA_ROOT
);
1838 EnumThreadWindows(GetCurrentThreadId(),
1840 reinterpret_cast<LPARAM
>(toplevel_hwnd
));
1843 blink::WebMouseWheelEvent mouse_wheel_event
=
1844 MakeWebMouseWheelEvent(static_cast<ui::MouseWheelEvent
*>(event
));
1845 if (mouse_wheel_event
.deltaX
!= 0 || mouse_wheel_event
.deltaY
!= 0)
1846 host_
->ForwardWheelEvent(mouse_wheel_event
);
1847 } else if (CanRendererHandleEvent(event
) &&
1848 !(event
->flags() & ui::EF_FROM_TOUCH
)) {
1849 blink::WebMouseEvent mouse_event
= MakeWebMouseEvent(event
);
1850 ModifyEventMovementAndCoords(&mouse_event
);
1851 host_
->ForwardMouseEvent(mouse_event
);
1852 // Ensure that we get keyboard focus on mouse down as a plugin window may
1853 // have grabbed keyboard focus.
1854 if (event
->type() == ui::ET_MOUSE_PRESSED
)
1858 switch (event
->type()) {
1859 case ui::ET_MOUSE_PRESSED
:
1860 window_
->SetCapture();
1861 // Confirm existing composition text on mouse click events, to make sure
1862 // the input caret won't be moved with an ongoing composition text.
1863 FinishImeCompositionSession();
1865 case ui::ET_MOUSE_RELEASED
:
1866 window_
->ReleaseCapture();
1872 // Needed to propagate mouse event to |window_->parent()->delegate()|, but
1873 // note that it might be something other than a WebContentsViewAura instance.
1874 // TODO(pkotwicz): Find a better way of doing this.
1875 // In fullscreen mode which is typically used by flash, don't forward
1876 // the mouse events to the parent. The renderer and the plugin process
1877 // handle these events.
1878 if (!is_fullscreen_
&& window_
->parent()->delegate() &&
1879 !(event
->flags() & ui::EF_FROM_TOUCH
)) {
1880 event
->ConvertLocationToTarget(window_
, window_
->parent());
1881 window_
->parent()->delegate()->OnMouseEvent(event
);
1884 if (!IsXButtonUpEvent(event
))
1885 event
->SetHandled();
1888 void RenderWidgetHostViewAura::OnScrollEvent(ui::ScrollEvent
* event
) {
1889 TRACE_EVENT0("input", "RenderWidgetHostViewAura::OnScrollEvent");
1890 if (touch_editing_client_
&& touch_editing_client_
->HandleInputEvent(event
))
1893 if (event
->type() == ui::ET_SCROLL
) {
1894 #if !defined(OS_WIN)
1896 // Investigate if this is true for Windows 8 Metro ASH as well.
1897 if (event
->finger_count() != 2)
1900 blink::WebGestureEvent gesture_event
=
1901 MakeWebGestureEventFlingCancel();
1902 host_
->ForwardGestureEvent(gesture_event
);
1903 blink::WebMouseWheelEvent mouse_wheel_event
=
1904 MakeWebMouseWheelEvent(event
);
1905 host_
->ForwardWheelEvent(mouse_wheel_event
);
1906 RecordAction(base::UserMetricsAction("TrackpadScroll"));
1907 } else if (event
->type() == ui::ET_SCROLL_FLING_START
||
1908 event
->type() == ui::ET_SCROLL_FLING_CANCEL
) {
1909 blink::WebGestureEvent gesture_event
=
1910 MakeWebGestureEvent(event
);
1911 host_
->ForwardGestureEvent(gesture_event
);
1912 if (event
->type() == ui::ET_SCROLL_FLING_START
)
1913 RecordAction(base::UserMetricsAction("TrackpadScrollFling"));
1916 event
->SetHandled();
1919 void RenderWidgetHostViewAura::OnTouchEvent(ui::TouchEvent
* event
) {
1920 TRACE_EVENT0("input", "RenderWidgetHostViewAura::OnTouchEvent");
1921 if (touch_editing_client_
&& touch_editing_client_
->HandleInputEvent(event
))
1924 // Update the touch event first.
1925 blink::WebTouchPoint
* point
= UpdateWebTouchEventFromUIEvent(*event
,
1928 // Forward the touch event only if a touch point was updated, and there's a
1929 // touch-event handler in the page, and no other touch-event is in the queue.
1930 // It is important to always consume the event if there is a touch-event
1931 // handler in the page, or some touch-event is already in the queue, even if
1932 // no point has been updated, to make sure that this event does not get
1933 // processed by the gesture recognizer before the events in the queue.
1934 if (host_
->ShouldForwardTouchEvent())
1935 event
->StopPropagation();
1938 if (host_
->ShouldForwardTouchEvent())
1939 host_
->ForwardTouchEventWithLatencyInfo(touch_event_
, *event
->latency());
1940 UpdateWebTouchEventAfterDispatch(&touch_event_
, point
);
1944 void RenderWidgetHostViewAura::OnGestureEvent(ui::GestureEvent
* event
) {
1945 TRACE_EVENT0("input", "RenderWidgetHostViewAura::OnGestureEvent");
1946 if ((event
->type() == ui::ET_GESTURE_PINCH_BEGIN
||
1947 event
->type() == ui::ET_GESTURE_PINCH_UPDATE
||
1948 event
->type() == ui::ET_GESTURE_PINCH_END
) && !pinch_zoom_enabled_
) {
1949 event
->SetHandled();
1953 if (touch_editing_client_
&& touch_editing_client_
->HandleInputEvent(event
))
1956 RenderViewHostDelegate
* delegate
= NULL
;
1957 if (host_
->IsRenderView())
1958 delegate
= RenderViewHost::From(host_
)->GetDelegate();
1960 if (delegate
&& event
->type() == ui::ET_GESTURE_BEGIN
&&
1961 event
->details().touch_points() == 1) {
1962 delegate
->HandleGestureBegin();
1965 blink::WebGestureEvent gesture
= MakeWebGestureEvent(event
);
1966 if (event
->type() == ui::ET_GESTURE_TAP_DOWN
) {
1967 // Webkit does not stop a fling-scroll on tap-down. So explicitly send an
1968 // event to stop any in-progress flings.
1969 blink::WebGestureEvent fling_cancel
= gesture
;
1970 fling_cancel
.type
= blink::WebInputEvent::GestureFlingCancel
;
1971 fling_cancel
.sourceDevice
= blink::WebGestureDeviceTouchscreen
;
1972 host_
->ForwardGestureEvent(fling_cancel
);
1975 if (gesture
.type
!= blink::WebInputEvent::Undefined
) {
1976 host_
->ForwardGestureEventWithLatencyInfo(gesture
, *event
->latency());
1978 if (event
->type() == ui::ET_GESTURE_SCROLL_BEGIN
||
1979 event
->type() == ui::ET_GESTURE_SCROLL_UPDATE
||
1980 event
->type() == ui::ET_GESTURE_SCROLL_END
) {
1981 RecordAction(base::UserMetricsAction("TouchscreenScroll"));
1982 } else if (event
->type() == ui::ET_SCROLL_FLING_START
) {
1983 RecordAction(base::UserMetricsAction("TouchscreenScrollFling"));
1987 if (delegate
&& event
->type() == ui::ET_GESTURE_END
&&
1988 event
->details().touch_points() == 1) {
1989 delegate
->HandleGestureEnd();
1992 // If a gesture is not processed by the webpage, then WebKit processes it
1993 // (e.g. generates synthetic mouse events).
1994 event
->SetHandled();
1997 ////////////////////////////////////////////////////////////////////////////////
1998 // RenderWidgetHostViewAura, aura::client::ActivationDelegate implementation:
2000 bool RenderWidgetHostViewAura::ShouldActivate() const {
2001 aura::WindowTreeHost
* host
= window_
->GetHost();
2004 const ui::Event
* event
= host
->dispatcher()->current_event();
2007 return is_fullscreen_
;
2010 ////////////////////////////////////////////////////////////////////////////////
2011 // RenderWidgetHostViewAura,
2012 // aura::client::ActivationChangeObserver implementation:
2014 void RenderWidgetHostViewAura::OnWindowActivated(aura::Window
* gained_active
,
2015 aura::Window
* lost_active
) {
2016 DCHECK(window_
== gained_active
|| window_
== lost_active
);
2017 if (window_
== gained_active
) {
2018 const ui::Event
* event
= window_
->GetHost()->dispatcher()->current_event();
2019 if (event
&& PointerEventActivates(*event
))
2020 host_
->OnPointerEventActivate();
2024 ////////////////////////////////////////////////////////////////////////////////
2025 // RenderWidgetHostViewAura, aura::client::CursorClientObserver implementation:
2027 void RenderWidgetHostViewAura::OnCursorVisibilityChanged(bool is_visible
) {
2028 NotifyRendererOfCursorVisibilityState(is_visible
);
2031 ////////////////////////////////////////////////////////////////////////////////
2032 // RenderWidgetHostViewAura, aura::client::FocusChangeObserver implementation:
2034 void RenderWidgetHostViewAura::OnWindowFocused(aura::Window
* gained_focus
,
2035 aura::Window
* lost_focus
) {
2036 DCHECK(window_
== gained_focus
|| window_
== lost_focus
);
2037 if (window_
== gained_focus
) {
2038 // We need to honor input bypass if the associated tab is does not want
2039 // input. This gives the current focused window a chance to be the text
2040 // input client and handle events.
2041 if (host_
->ignore_input_events())
2045 host_
->SetActive(true);
2047 ui::InputMethod
* input_method
= GetInputMethod();
2049 // Ask the system-wide IME to send all TextInputClient messages to |this|
2051 input_method
->SetFocusedTextInputClient(this);
2052 host_
->SetInputMethodActive(input_method
->IsActive());
2054 // Often the application can set focus to the view in response to a key
2055 // down. However the following char event shouldn't be sent to the web
2057 host_
->SuppressNextCharEvents();
2059 host_
->SetInputMethodActive(false);
2062 BrowserAccessibilityManager
* manager
=
2063 host_
->GetRootBrowserAccessibilityManager();
2065 manager
->OnWindowFocused();
2066 } else if (window_
== lost_focus
) {
2067 host_
->SetActive(false);
2070 DetachFromInputMethod();
2071 host_
->SetInputMethodActive(false);
2073 if (touch_editing_client_
)
2074 touch_editing_client_
->EndTouchEditing(false);
2076 if (overscroll_controller_
)
2077 overscroll_controller_
->Cancel();
2079 BrowserAccessibilityManager
* manager
=
2080 host_
->GetRootBrowserAccessibilityManager();
2082 manager
->OnWindowBlurred();
2084 // If we lose the focus while fullscreen, close the window; Pepper Flash
2085 // won't do it for us (unlike NPAPI Flash). However, we do not close the
2086 // window if we lose the focus to a window on another display.
2087 gfx::Screen
* screen
= gfx::Screen::GetScreenFor(window_
);
2088 bool focusing_other_display
=
2089 gained_focus
&& screen
->GetNumDisplays() > 1 &&
2090 (screen
->GetDisplayNearestWindow(window_
).id() !=
2091 screen
->GetDisplayNearestWindow(gained_focus
).id());
2092 if (is_fullscreen_
&& !in_shutdown_
&& !focusing_other_display
) {
2094 // On Windows, if we are switching to a non Aura Window on a different
2095 // screen we should not close the fullscreen window.
2096 if (!gained_focus
) {
2098 ::GetCursorPos(&point
);
2099 if (screen
->GetDisplayNearestWindow(window_
).id() !=
2100 screen
->GetDisplayNearestPoint(gfx::Point(point
)).id())
2104 in_shutdown_
= true;
2110 ////////////////////////////////////////////////////////////////////////////////
2111 // RenderWidgetHostViewAura, aura::WindowTreeHostObserver implementation:
2113 void RenderWidgetHostViewAura::OnHostMoved(const aura::WindowTreeHost
* host
,
2114 const gfx::Point
& new_origin
) {
2115 TRACE_EVENT1("ui", "RenderWidgetHostViewAura::OnHostMoved",
2116 "new_origin", new_origin
.ToString());
2118 UpdateScreenInfo(window_
);
2121 ////////////////////////////////////////////////////////////////////////////////
2122 // RenderWidgetHostViewAura, private:
2124 RenderWidgetHostViewAura::~RenderWidgetHostViewAura() {
2125 if (touch_editing_client_
)
2126 touch_editing_client_
->OnViewDestroyed();
2128 delegated_frame_host_
.reset();
2129 window_observer_
.reset();
2130 if (window_
->GetHost())
2131 window_
->GetHost()->RemoveObserver(this);
2133 if (popup_parent_host_view_
) {
2134 DCHECK(popup_parent_host_view_
->popup_child_host_view_
== NULL
||
2135 popup_parent_host_view_
->popup_child_host_view_
== this);
2136 popup_parent_host_view_
->popup_child_host_view_
= NULL
;
2138 if (popup_child_host_view_
) {
2139 DCHECK(popup_child_host_view_
->popup_parent_host_view_
== NULL
||
2140 popup_child_host_view_
->popup_parent_host_view_
== this);
2141 popup_child_host_view_
->popup_parent_host_view_
= NULL
;
2143 event_filter_for_popup_exit_
.reset();
2144 aura::client::SetTooltipText(window_
, NULL
);
2145 gfx::Screen::GetScreenFor(window_
)->RemoveObserver(this);
2147 // This call is usually no-op since |this| object is already removed from the
2148 // Aura root window and we don't have a way to get an input method object
2149 // associated with the window, but just in case.
2150 DetachFromInputMethod();
2153 // The LegacyRenderWidgetHostHWND window should have been destroyed in
2154 // RenderWidgetHostViewAura::OnWindowDestroying and the pointer should
2156 DCHECK(!legacy_render_widget_host_HWND_
);
2160 void RenderWidgetHostViewAura::UpdateCursorIfOverSelf() {
2161 const gfx::Point screen_point
=
2162 gfx::Screen::GetScreenFor(GetNativeView())->GetCursorScreenPoint();
2163 aura::Window
* root_window
= window_
->GetRootWindow();
2167 gfx::Point root_window_point
= screen_point
;
2168 aura::client::ScreenPositionClient
* screen_position_client
=
2169 aura::client::GetScreenPositionClient(root_window
);
2170 if (screen_position_client
) {
2171 screen_position_client
->ConvertPointFromScreen(
2172 root_window
, &root_window_point
);
2175 if (root_window
->GetEventHandlerForPoint(root_window_point
) != window_
)
2178 gfx::NativeCursor cursor
= current_cursor_
.GetNativeCursor();
2179 // Do not show loading cursor when the cursor is currently hidden.
2180 if (is_loading_
&& cursor
!= ui::kCursorNone
)
2181 cursor
= ui::kCursorPointer
;
2183 aura::client::CursorClient
* cursor_client
=
2184 aura::client::GetCursorClient(root_window
);
2185 if (cursor_client
) {
2186 cursor_client
->SetCursor(cursor
);
2190 ui::InputMethod
* RenderWidgetHostViewAura::GetInputMethod() const {
2191 aura::Window
* root_window
= window_
->GetRootWindow();
2194 return root_window
->GetProperty(aura::client::kRootWindowInputMethodKey
);
2197 bool RenderWidgetHostViewAura::NeedsInputGrab() {
2198 return popup_type_
== blink::WebPopupTypeSelect
;
2201 void RenderWidgetHostViewAura::FinishImeCompositionSession() {
2202 if (!has_composition_text_
)
2205 host_
->ImeConfirmComposition(base::string16(), gfx::Range::InvalidRange(),
2208 ImeCancelComposition();
2211 void RenderWidgetHostViewAura::ModifyEventMovementAndCoords(
2212 blink::WebMouseEvent
* event
) {
2213 // If the mouse has just entered, we must report zero movementX/Y. Hence we
2214 // reset any global_mouse_position set previously.
2215 if (event
->type
== blink::WebInputEvent::MouseEnter
||
2216 event
->type
== blink::WebInputEvent::MouseLeave
)
2217 global_mouse_position_
.SetPoint(event
->globalX
, event
->globalY
);
2219 // Movement is computed by taking the difference of the new cursor position
2220 // and the previous. Under mouse lock the cursor will be warped back to the
2221 // center so that we are not limited by clipping boundaries.
2222 // We do not measure movement as the delta from cursor to center because
2223 // we may receive more mouse movement events before our warp has taken
2225 event
->movementX
= event
->globalX
- global_mouse_position_
.x();
2226 event
->movementY
= event
->globalY
- global_mouse_position_
.y();
2228 global_mouse_position_
.SetPoint(event
->globalX
, event
->globalY
);
2230 // Under mouse lock, coordinates of mouse are locked to what they were when
2231 // mouse lock was entered.
2232 if (mouse_locked_
) {
2233 event
->x
= unlocked_mouse_position_
.x();
2234 event
->y
= unlocked_mouse_position_
.y();
2235 event
->windowX
= unlocked_mouse_position_
.x();
2236 event
->windowY
= unlocked_mouse_position_
.y();
2237 event
->globalX
= unlocked_global_mouse_position_
.x();
2238 event
->globalY
= unlocked_global_mouse_position_
.y();
2240 unlocked_mouse_position_
.SetPoint(event
->windowX
, event
->windowY
);
2241 unlocked_global_mouse_position_
.SetPoint(event
->globalX
, event
->globalY
);
2245 void RenderWidgetHostViewAura::NotifyRendererOfCursorVisibilityState(
2247 if (host_
->is_hidden() ||
2248 (cursor_visibility_state_in_renderer_
== VISIBLE
&& is_visible
) ||
2249 (cursor_visibility_state_in_renderer_
== NOT_VISIBLE
&& !is_visible
))
2252 cursor_visibility_state_in_renderer_
= is_visible
? VISIBLE
: NOT_VISIBLE
;
2253 host_
->SendCursorVisibilityState(is_visible
);
2256 void RenderWidgetHostViewAura::SetOverscrollControllerEnabled(bool enabled
) {
2258 overscroll_controller_
.reset();
2259 else if (!overscroll_controller_
)
2260 overscroll_controller_
.reset(new OverscrollController());
2263 void RenderWidgetHostViewAura::SnapToPhysicalPixelBoundary() {
2264 // The top left corner of our view in window coordinates might not land on a
2265 // device pixel boundary if we have a non-integer device scale. In that case,
2266 // to avoid the web contents area looking blurry we translate the web contents
2267 // in the +x, +y direction to land on the nearest pixel boundary. This may
2268 // cause the bottom and right edges to be clipped slightly, but that's ok.
2269 aura::Window
* snapped
= NULL
;
2270 // On desktop, use the root window. On alternative environment (ash),
2271 // use the toplevel window which must be already snapped.
2272 if (gfx::Screen::GetScreenFor(window_
) !=
2273 gfx::Screen::GetScreenByType(gfx::SCREEN_TYPE_ALTERNATE
)) {
2274 snapped
= window_
->GetRootWindow();
2276 snapped
= window_
->GetToplevelWindow();
2278 if (snapped
&& snapped
!= window_
)
2279 ui::SnapLayerToPhysicalPixelBoundary(snapped
->layer(), window_
->layer());
2281 has_snapped_to_boundary_
= true;
2284 void RenderWidgetHostViewAura::InternalSetBounds(const gfx::Rect
& rect
) {
2285 if (HasDisplayPropertyChanged(window_
))
2286 host_
->InvalidateScreenInfo();
2288 SnapToPhysicalPixelBoundary();
2289 // Don't recursively call SetBounds if this bounds update is the result of
2290 // a Window::SetBoundsInternal call.
2291 if (!in_bounds_changed_
)
2292 window_
->SetBounds(rect
);
2293 host_
->WasResized();
2294 delegated_frame_host_
->WasResized();
2295 if (touch_editing_client_
) {
2296 touch_editing_client_
->OnSelectionOrCursorChanged(selection_anchor_rect_
,
2297 selection_focus_rect_
);
2300 // Create the legacy dummy window which corresponds to the bounds of the
2301 // webcontents. This will be passed as the container window for windowless
2303 // Plugins like Flash assume the container window which is returned via the
2304 // NPNVnetscapeWindow property corresponds to the bounds of the webpage.
2305 // This is not true in Aura where we have only HWND which is the main Aura
2306 // window. If we return this window to plugins like Flash then it causes the
2307 // coordinate translations done by these plugins to break.
2308 // Additonally the legacy dummy window is needed for accessibility and for
2309 // scrolling to work in legacy drivers for trackpoints/trackpads, etc.
2310 if (!legacy_window_destroyed_
&& GetNativeViewId()) {
2311 if (!legacy_render_widget_host_HWND_
) {
2312 legacy_render_widget_host_HWND_
= LegacyRenderWidgetHostHWND::Create(
2313 reinterpret_cast<HWND
>(GetNativeViewId()));
2315 if (legacy_render_widget_host_HWND_
) {
2316 legacy_render_widget_host_HWND_
->set_host(this);
2317 legacy_render_widget_host_HWND_
->SetBounds(
2318 window_
->GetBoundsInRootWindow());
2319 // There are cases where the parent window is created, made visible and
2320 // the associated RenderWidget is also visible before the
2321 // LegacyRenderWidgetHostHWND instace is created. Ensure that it is shown
2323 if (!host_
->is_hidden())
2324 legacy_render_widget_host_HWND_
->Show();
2329 UpdateMouseLockRegion();
2333 void RenderWidgetHostViewAura::SchedulePaintIfNotInClip(
2334 const gfx::Rect
& rect
,
2335 const gfx::Rect
& clip
) {
2336 if (!clip
.IsEmpty()) {
2337 gfx::Rect to_paint
= gfx::SubtractRects(rect
, clip
);
2338 if (!to_paint
.IsEmpty())
2339 window_
->SchedulePaintInRect(to_paint
);
2341 window_
->SchedulePaintInRect(rect
);
2345 bool RenderWidgetHostViewAura::ShouldMoveToCenter() {
2346 gfx::Rect rect
= window_
->bounds();
2347 rect
= ConvertRectToScreen(rect
);
2348 int border_x
= rect
.width() * kMouseLockBorderPercentage
/ 100;
2349 int border_y
= rect
.height() * kMouseLockBorderPercentage
/ 100;
2351 return global_mouse_position_
.x() < rect
.x() + border_x
||
2352 global_mouse_position_
.x() > rect
.right() - border_x
||
2353 global_mouse_position_
.y() < rect
.y() + border_y
||
2354 global_mouse_position_
.y() > rect
.bottom() - border_y
;
2357 void RenderWidgetHostViewAura::AddedToRootWindow() {
2358 window_
->GetHost()->AddObserver(this);
2359 UpdateScreenInfo(window_
);
2361 aura::client::CursorClient
* cursor_client
=
2362 aura::client::GetCursorClient(window_
->GetRootWindow());
2363 if (cursor_client
) {
2364 cursor_client
->AddObserver(this);
2365 NotifyRendererOfCursorVisibilityState(cursor_client
->IsCursorVisible());
2368 ui::InputMethod
* input_method
= GetInputMethod();
2370 input_method
->SetFocusedTextInputClient(this);
2374 // The parent may have changed here. Ensure that the legacy window is
2375 // reparented accordingly.
2376 if (legacy_render_widget_host_HWND_
)
2377 legacy_render_widget_host_HWND_
->UpdateParent(
2378 reinterpret_cast<HWND
>(GetNativeViewId()));
2381 delegated_frame_host_
->AddedToWindow();
2384 void RenderWidgetHostViewAura::RemovingFromRootWindow() {
2385 aura::client::CursorClient
* cursor_client
=
2386 aura::client::GetCursorClient(window_
->GetRootWindow());
2388 cursor_client
->RemoveObserver(this);
2390 DetachFromInputMethod();
2392 window_
->GetHost()->RemoveObserver(this);
2393 delegated_frame_host_
->RemovingFromWindow();
2396 // Update the legacy window's parent temporarily to the desktop window. It
2397 // will eventually get reparented to the right root.
2398 if (legacy_render_widget_host_HWND_
)
2399 legacy_render_widget_host_HWND_
->UpdateParent(::GetDesktopWindow());
2403 void RenderWidgetHostViewAura::DetachFromInputMethod() {
2404 ui::InputMethod
* input_method
= GetInputMethod();
2405 if (input_method
&& input_method
->GetTextInputClient() == this)
2406 input_method
->SetFocusedTextInputClient(NULL
);
2409 void RenderWidgetHostViewAura::ForwardKeyboardEvent(
2410 const NativeWebKeyboardEvent
& event
) {
2411 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
2412 ui::TextEditKeyBindingsDelegateAuraLinux
* keybinding_delegate
=
2413 ui::GetTextEditKeyBindingsDelegate();
2414 std::vector
<ui::TextEditCommandAuraLinux
> commands
;
2415 if (!event
.skip_in_browser
&&
2416 keybinding_delegate
&&
2418 keybinding_delegate
->MatchEvent(*event
.os_event
, &commands
)) {
2419 // Transform from ui/ types to content/ types.
2420 EditCommands edit_commands
;
2421 for (std::vector
<ui::TextEditCommandAuraLinux
>::const_iterator it
=
2422 commands
.begin(); it
!= commands
.end(); ++it
) {
2423 edit_commands
.push_back(EditCommand(it
->GetCommandString(),
2426 host_
->Send(new InputMsg_SetEditCommandsForNextKeyEvent(
2427 host_
->GetRoutingID(), edit_commands
));
2428 NativeWebKeyboardEvent
copy_event(event
);
2429 copy_event
.match_edit_command
= true;
2430 host_
->ForwardKeyboardEvent(copy_event
);
2435 host_
->ForwardKeyboardEvent(event
);
2438 SkColorType
RenderWidgetHostViewAura::PreferredReadbackFormat() {
2439 return kN32_SkColorType
;
2442 ////////////////////////////////////////////////////////////////////////////////
2443 // DelegatedFrameHost, public:
2445 ui::Compositor
* RenderWidgetHostViewAura::GetCompositor() const {
2446 aura::WindowTreeHost
* host
= window_
->GetHost();
2447 return host
? host
->compositor() : NULL
;
2450 ui::Layer
* RenderWidgetHostViewAura::GetLayer() {
2451 return window_
->layer();
2454 RenderWidgetHostImpl
* RenderWidgetHostViewAura::GetHost() {
2458 bool RenderWidgetHostViewAura::IsVisible() {
2462 gfx::Size
RenderWidgetHostViewAura::DesiredFrameSize() {
2463 return window_
->bounds().size();
2466 float RenderWidgetHostViewAura::CurrentDeviceScaleFactor() {
2467 return current_device_scale_factor_
;
2470 gfx::Size
RenderWidgetHostViewAura::ConvertViewSizeToPixel(
2471 const gfx::Size
& size
) {
2472 return content::ConvertViewSizeToPixel(this, size
);
2475 scoped_ptr
<ResizeLock
> RenderWidgetHostViewAura::CreateResizeLock(
2476 bool defer_compositor_lock
) {
2477 gfx::Size desired_size
= window_
->bounds().size();
2478 return scoped_ptr
<ResizeLock
>(new CompositorResizeLock(
2481 defer_compositor_lock
,
2482 base::TimeDelta::FromMilliseconds(kResizeLockTimeoutMs
)));
2485 DelegatedFrameHost
* RenderWidgetHostViewAura::GetDelegatedFrameHost() const {
2486 return delegated_frame_host_
.get();
2489 ////////////////////////////////////////////////////////////////////////////////
2490 // RenderWidgetHostViewBase, public:
2493 void RenderWidgetHostViewBase::GetDefaultScreenInfo(WebScreenInfo
* results
) {
2494 GetScreenInfoForWindow(results
, NULL
);
2497 } // namespace content