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 {
76 // We need to add |offset| of the guest from the embedder to position the
78 gfx::Rect embedder_bounds
;
79 guest_
->embedder_web_contents()->GetView()->GetContainerBounds(
81 gfx::Rect guest_bounds
;
82 GetContainerBounds(&guest_bounds
);
84 gfx::Vector2d offset
= guest_bounds
.origin() - embedder_bounds
.origin();
85 ContextMenuParams params_in_embedder
= params
;
86 params_in_embedder
.x
+= offset
.x();
87 params_in_embedder
.y
+= offset
.y();
88 return params_in_embedder
;
91 void WebContentsViewGuest::GetContainerBounds(gfx::Rect
* out
) const {
92 // We need embedder container's bounds to calculate our bounds.
93 guest_
->embedder_web_contents()->GetView()->GetContainerBounds(out
);
94 gfx::Point guest_coordinates
= guest_
->GetScreenCoordinates(gfx::Point());
95 out
->Offset(guest_coordinates
.x(), guest_coordinates
.y());
99 void WebContentsViewGuest::SizeContents(const gfx::Size
& size
) {
101 RenderWidgetHostView
* rwhv
= web_contents_
->GetRenderWidgetHostView();
106 void WebContentsViewGuest::SetInitialFocus() {
107 platform_view_
->SetInitialFocus();
110 gfx::Rect
WebContentsViewGuest::GetViewBounds() const {
111 return gfx::Rect(size_
);
114 #if defined(OS_MACOSX)
115 void WebContentsViewGuest::SetAllowOtherViews(bool allow
) {
116 platform_view_
->SetAllowOtherViews(allow
);
119 bool WebContentsViewGuest::GetAllowOtherViews() const {
120 return platform_view_
->GetAllowOtherViews();
124 void WebContentsViewGuest::CreateView(const gfx::Size
& initial_size
,
125 gfx::NativeView context
) {
126 platform_view_
->CreateView(initial_size
, context
);
127 size_
= initial_size
;
130 RenderWidgetHostViewBase
* WebContentsViewGuest::CreateViewForWidget(
131 RenderWidgetHost
* render_widget_host
) {
132 if (render_widget_host
->GetView()) {
133 // During testing, the view will already be set up in most cases to the
134 // test view, so we don't want to clobber it with a real one. To verify that
135 // this actually is happening (and somebody isn't accidentally creating the
136 // view twice), we check for the RVH Factory, which will be set when we're
137 // making special ones (which go along with the special views).
138 DCHECK(RenderViewHostFactory::has_factory());
139 return static_cast<RenderWidgetHostViewBase
*>(
140 render_widget_host
->GetView());
143 RenderWidgetHostViewBase
* platform_widget
=
144 platform_view_
->CreateViewForWidget(render_widget_host
);
146 RenderWidgetHostViewBase
* view
= new RenderWidgetHostViewGuest(
154 RenderWidgetHostViewBase
* WebContentsViewGuest::CreateViewForPopupWidget(
155 RenderWidgetHost
* render_widget_host
) {
156 return platform_view_
->CreateViewForPopupWidget(render_widget_host
);
159 void WebContentsViewGuest::SetPageTitle(const base::string16
& title
) {
162 void WebContentsViewGuest::RenderViewCreated(RenderViewHost
* host
) {
163 platform_view_
->RenderViewCreated(host
);
166 void WebContentsViewGuest::RenderViewSwappedIn(RenderViewHost
* host
) {
167 platform_view_
->RenderViewSwappedIn(host
);
170 void WebContentsViewGuest::SetOverscrollControllerEnabled(bool enabled
) {
171 // This should never override the setting of the embedder view.
174 #if defined(OS_MACOSX)
175 bool WebContentsViewGuest::IsEventTracking() const {
179 void WebContentsViewGuest::CloseTabAfterEventTracking() {
183 WebContents
* WebContentsViewGuest::web_contents() {
184 return web_contents_
;
187 void WebContentsViewGuest::RestoreFocus() {
188 platform_view_
->RestoreFocus();
191 void WebContentsViewGuest::Focus() {
192 platform_view_
->Focus();
195 void WebContentsViewGuest::StoreFocus() {
196 platform_view_
->StoreFocus();
199 DropData
* WebContentsViewGuest::GetDropData() const {
204 void WebContentsViewGuest::UpdateDragCursor(WebDragOperation operation
) {
205 RenderViewHostImpl
* embedder_render_view_host
=
206 static_cast<RenderViewHostImpl
*>(
207 guest_
->embedder_web_contents()->GetRenderViewHost());
208 CHECK(embedder_render_view_host
);
209 RenderViewHostDelegateView
* view
=
210 embedder_render_view_host
->GetDelegate()->GetDelegateView();
212 view
->UpdateDragCursor(operation
);
215 void WebContentsViewGuest::GotFocus() {
218 void WebContentsViewGuest::TakeFocus(bool reverse
) {
221 void WebContentsViewGuest::ShowContextMenu(RenderFrameHost
* render_frame_host
,
222 const ContextMenuParams
& params
) {
223 platform_view_delegate_view_
->ShowContextMenu(
224 render_frame_host
, ConvertContextMenuParams(params
));
227 void WebContentsViewGuest::StartDragging(
228 const DropData
& drop_data
,
229 WebDragOperationsMask ops
,
230 const gfx::ImageSkia
& image
,
231 const gfx::Vector2d
& image_offset
,
232 const DragEventSourceInfo
& event_info
) {
233 WebContentsImpl
* embedder_web_contents
= guest_
->embedder_web_contents();
234 embedder_web_contents
->GetBrowserPluginEmbedder()->StartDrag(guest_
);
235 RenderViewHostImpl
* embedder_render_view_host
=
236 static_cast<RenderViewHostImpl
*>(
237 embedder_web_contents
->GetRenderViewHost());
238 CHECK(embedder_render_view_host
);
239 RenderViewHostDelegateView
* view
=
240 embedder_render_view_host
->GetDelegate()->GetDelegateView();
242 RecordAction(base::UserMetricsAction("BrowserPlugin.Guest.StartDrag"));
243 view
->StartDragging(drop_data
, ops
, image
, image_offset
, event_info
);
245 embedder_web_contents
->SystemDragEnded();
249 } // namespace content