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/web_contents/web_contents_view_guest.h"
7 #include "build/build_config.h"
8 #include "content/browser/browser_plugin/browser_plugin_embedder.h"
9 #include "content/browser/browser_plugin/browser_plugin_guest.h"
10 #include "content/browser/frame_host/interstitial_page_impl.h"
11 #include "content/browser/frame_host/render_widget_host_view_guest.h"
12 #include "content/browser/renderer_host/render_view_host_factory.h"
13 #include "content/browser/renderer_host/render_view_host_impl.h"
14 #include "content/browser/web_contents/web_contents_impl.h"
15 #include "content/common/drag_messages.h"
16 #include "content/public/browser/user_metrics.h"
17 #include "content/public/browser/web_contents_delegate.h"
18 #include "content/public/common/context_menu_params.h"
19 #include "content/public/common/drop_data.h"
20 #include "ui/gfx/image/image_skia.h"
21 #include "ui/gfx/point.h"
22 #include "ui/gfx/rect.h"
23 #include "ui/gfx/size.h"
26 #include "ui/aura/window.h"
29 using blink::WebDragOperation
;
30 using blink::WebDragOperationsMask
;
34 WebContentsViewGuest::WebContentsViewGuest(
35 WebContentsImpl
* web_contents
,
36 BrowserPluginGuest
* guest
,
37 scoped_ptr
<WebContentsView
> platform_view
,
38 RenderViewHostDelegateView
* platform_view_delegate_view
)
39 : web_contents_(web_contents
),
41 platform_view_(platform_view
.Pass()),
42 platform_view_delegate_view_(platform_view_delegate_view
) {
45 WebContentsViewGuest::~WebContentsViewGuest() {
48 gfx::NativeView
WebContentsViewGuest::GetNativeView() const {
49 return platform_view_
->GetNativeView();
52 gfx::NativeView
WebContentsViewGuest::GetContentNativeView() const {
53 RenderWidgetHostView
* rwhv
= web_contents_
->GetRenderWidgetHostView();
56 return rwhv
->GetNativeView();
59 gfx::NativeWindow
WebContentsViewGuest::GetTopLevelNativeWindow() const {
60 return guest_
->embedder_web_contents()->GetTopLevelNativeWindow();
63 void WebContentsViewGuest::OnGuestInitialized(WebContentsView
* parent_view
) {
65 // In aura, ScreenPositionClient doesn't work properly if we do
66 // not have the native view associated with this WebContentsViewGuest in the
67 // view hierarchy. We add this view as embedder's child here.
68 // This would go in WebContentsViewGuest::CreateView, but that is too early to
69 // access embedder_web_contents(). Therefore, we do it here.
70 parent_view
->GetNativeView()->AddChild(platform_view_
->GetNativeView());
71 #endif // defined(USE_AURA)
74 ContextMenuParams
WebContentsViewGuest::ConvertContextMenuParams(
75 const ContextMenuParams
& params
) const {
77 // Context menu uses ScreenPositionClient::ConvertPointToScreen() in aura
78 // to calculate popup position. Guest's native view
79 // (platform_view_->GetNativeView()) is part of the embedder's view hierarchy,
80 // but is placed at (0, 0) w.r.t. the embedder's position. Therefore, |offset|
81 // is added to |params|.
82 gfx::Rect embedder_bounds
;
83 guest_
->embedder_web_contents()->GetView()->GetContainerBounds(
85 gfx::Rect guest_bounds
;
86 GetContainerBounds(&guest_bounds
);
88 gfx::Vector2d offset
= guest_bounds
.origin() - embedder_bounds
.origin();
89 ContextMenuParams params_in_embedder
= params
;
90 params_in_embedder
.x
+= offset
.x();
91 params_in_embedder
.y
+= offset
.y();
92 return params_in_embedder
;
98 void WebContentsViewGuest::GetContainerBounds(gfx::Rect
* out
) const {
99 // We need embedder container's bounds to calculate our bounds.
100 guest_
->embedder_web_contents()->GetView()->GetContainerBounds(out
);
101 gfx::Point guest_coordinates
= guest_
->GetScreenCoordinates(gfx::Point());
102 out
->Offset(guest_coordinates
.x(), guest_coordinates
.y());
103 out
->set_size(size_
);
106 void WebContentsViewGuest::SizeContents(const gfx::Size
& size
) {
108 RenderWidgetHostView
* rwhv
= web_contents_
->GetRenderWidgetHostView();
113 void WebContentsViewGuest::SetInitialFocus() {
114 platform_view_
->SetInitialFocus();
117 gfx::Rect
WebContentsViewGuest::GetViewBounds() const {
118 return gfx::Rect(size_
);
121 #if defined(OS_MACOSX)
122 void WebContentsViewGuest::SetAllowOverlappingViews(bool overlapping
) {
123 platform_view_
->SetAllowOverlappingViews(overlapping
);
126 bool WebContentsViewGuest::GetAllowOverlappingViews() const {
127 return platform_view_
->GetAllowOverlappingViews();
130 void WebContentsViewGuest::SetOverlayView(
131 WebContentsView
* overlay
, const gfx::Point
& offset
) {
132 platform_view_
->SetOverlayView(overlay
, offset
);
135 void WebContentsViewGuest::RemoveOverlayView() {
136 platform_view_
->RemoveOverlayView();
140 void WebContentsViewGuest::CreateView(const gfx::Size
& initial_size
,
141 gfx::NativeView context
) {
142 platform_view_
->CreateView(initial_size
, context
);
143 size_
= initial_size
;
146 RenderWidgetHostViewBase
* WebContentsViewGuest::CreateViewForWidget(
147 RenderWidgetHost
* render_widget_host
) {
148 if (render_widget_host
->GetView()) {
149 // During testing, the view will already be set up in most cases to the
150 // test view, so we don't want to clobber it with a real one. To verify that
151 // this actually is happening (and somebody isn't accidentally creating the
152 // view twice), we check for the RVH Factory, which will be set when we're
153 // making special ones (which go along with the special views).
154 DCHECK(RenderViewHostFactory::has_factory());
155 return static_cast<RenderWidgetHostViewBase
*>(
156 render_widget_host
->GetView());
159 RenderWidgetHostViewBase
* platform_widget
=
160 platform_view_
->CreateViewForWidget(render_widget_host
);
162 RenderWidgetHostViewBase
* view
= new RenderWidgetHostViewGuest(
170 RenderWidgetHostViewBase
* WebContentsViewGuest::CreateViewForPopupWidget(
171 RenderWidgetHost
* render_widget_host
) {
172 return platform_view_
->CreateViewForPopupWidget(render_widget_host
);
175 void WebContentsViewGuest::SetPageTitle(const base::string16
& title
) {
178 void WebContentsViewGuest::RenderViewCreated(RenderViewHost
* host
) {
179 platform_view_
->RenderViewCreated(host
);
182 void WebContentsViewGuest::RenderViewSwappedIn(RenderViewHost
* host
) {
183 platform_view_
->RenderViewSwappedIn(host
);
186 void WebContentsViewGuest::SetOverscrollControllerEnabled(bool enabled
) {
187 // This should never override the setting of the embedder view.
190 #if defined(OS_MACOSX)
191 bool WebContentsViewGuest::IsEventTracking() const {
195 void WebContentsViewGuest::CloseTabAfterEventTracking() {
199 WebContents
* WebContentsViewGuest::web_contents() {
200 return web_contents_
;
203 void WebContentsViewGuest::RestoreFocus() {
204 platform_view_
->RestoreFocus();
207 void WebContentsViewGuest::Focus() {
208 platform_view_
->Focus();
211 void WebContentsViewGuest::StoreFocus() {
212 platform_view_
->StoreFocus();
215 DropData
* WebContentsViewGuest::GetDropData() const {
220 void WebContentsViewGuest::UpdateDragCursor(WebDragOperation operation
) {
221 RenderViewHostImpl
* embedder_render_view_host
=
222 static_cast<RenderViewHostImpl
*>(
223 guest_
->embedder_web_contents()->GetRenderViewHost());
224 CHECK(embedder_render_view_host
);
225 RenderViewHostDelegateView
* view
=
226 embedder_render_view_host
->GetDelegate()->GetDelegateView();
228 view
->UpdateDragCursor(operation
);
231 void WebContentsViewGuest::GotFocus() {
234 void WebContentsViewGuest::TakeFocus(bool reverse
) {
237 void WebContentsViewGuest::ShowContextMenu(RenderFrameHost
* render_frame_host
,
238 const ContextMenuParams
& params
) {
239 platform_view_delegate_view_
->ShowContextMenu(
240 render_frame_host
, ConvertContextMenuParams(params
));
243 void WebContentsViewGuest::StartDragging(
244 const DropData
& drop_data
,
245 WebDragOperationsMask ops
,
246 const gfx::ImageSkia
& image
,
247 const gfx::Vector2d
& image_offset
,
248 const DragEventSourceInfo
& event_info
) {
249 WebContentsImpl
* embedder_web_contents
= guest_
->embedder_web_contents();
250 embedder_web_contents
->GetBrowserPluginEmbedder()->StartDrag(guest_
);
251 RenderViewHostImpl
* embedder_render_view_host
=
252 static_cast<RenderViewHostImpl
*>(
253 embedder_web_contents
->GetRenderViewHost());
254 CHECK(embedder_render_view_host
);
255 RenderViewHostDelegateView
* view
=
256 embedder_render_view_host
->GetDelegate()->GetDelegateView();
258 RecordAction(base::UserMetricsAction("BrowserPlugin.Guest.StartDrag"));
259 view
->StartDragging(drop_data
, ops
, image
, image_offset
, event_info
);
261 embedder_web_contents
->SystemDragEnded();
265 } // namespace content