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/geometry/point.h"
21 #include "ui/gfx/geometry/rect.h"
22 #include "ui/gfx/geometry/size.h"
23 #include "ui/gfx/image/image_skia.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::OnGuestAttached(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 void WebContentsViewGuest::OnGuestDetached(WebContentsView
* old_parent_view
) {
76 old_parent_view
->GetNativeView()->RemoveChild(
77 platform_view_
->GetNativeView());
78 #endif // defined(USE_AURA)
81 void WebContentsViewGuest::GetContainerBounds(gfx::Rect
* out
) const {
82 if (guest_
->embedder_web_contents()) {
83 // We need embedder container's bounds to calculate our bounds.
84 guest_
->embedder_web_contents()->GetView()->GetContainerBounds(out
);
85 gfx::Point guest_coordinates
= guest_
->GetScreenCoordinates(gfx::Point());
86 out
->Offset(guest_coordinates
.x(), guest_coordinates
.y());
88 out
->set_origin(gfx::Point());
94 void WebContentsViewGuest::SizeContents(const gfx::Size
& size
) {
96 RenderWidgetHostView
* rwhv
= web_contents_
->GetRenderWidgetHostView();
101 void WebContentsViewGuest::SetInitialFocus() {
102 platform_view_
->SetInitialFocus();
105 gfx::Rect
WebContentsViewGuest::GetViewBounds() const {
106 return gfx::Rect(size_
);
109 #if defined(OS_MACOSX)
110 void WebContentsViewGuest::SetAllowOtherViews(bool allow
) {
111 platform_view_
->SetAllowOtherViews(allow
);
114 bool WebContentsViewGuest::GetAllowOtherViews() const {
115 return platform_view_
->GetAllowOtherViews();
119 void WebContentsViewGuest::CreateView(const gfx::Size
& initial_size
,
120 gfx::NativeView context
) {
121 platform_view_
->CreateView(initial_size
, context
);
122 size_
= initial_size
;
125 RenderWidgetHostViewBase
* WebContentsViewGuest::CreateViewForWidget(
126 RenderWidgetHost
* render_widget_host
, bool is_guest_view_hack
) {
127 if (render_widget_host
->GetView()) {
128 // During testing, the view will already be set up in most cases to the
129 // test view, so we don't want to clobber it with a real one. To verify that
130 // this actually is happening (and somebody isn't accidentally creating the
131 // view twice), we check for the RVH Factory, which will be set when we're
132 // making special ones (which go along with the special views).
133 DCHECK(RenderViewHostFactory::has_factory());
134 return static_cast<RenderWidgetHostViewBase
*>(
135 render_widget_host
->GetView());
138 RenderWidgetHostViewBase
* platform_widget
=
139 platform_view_
->CreateViewForWidget(render_widget_host
, true);
141 return new RenderWidgetHostViewGuest(render_widget_host
,
143 platform_widget
->GetWeakPtr());
146 RenderWidgetHostViewBase
* WebContentsViewGuest::CreateViewForPopupWidget(
147 RenderWidgetHost
* render_widget_host
) {
148 return platform_view_
->CreateViewForPopupWidget(render_widget_host
);
151 void WebContentsViewGuest::SetPageTitle(const base::string16
& title
) {
154 void WebContentsViewGuest::RenderViewCreated(RenderViewHost
* host
) {
155 platform_view_
->RenderViewCreated(host
);
158 void WebContentsViewGuest::RenderViewSwappedIn(RenderViewHost
* host
) {
159 platform_view_
->RenderViewSwappedIn(host
);
162 void WebContentsViewGuest::SetOverscrollControllerEnabled(bool enabled
) {
163 // This should never override the setting of the embedder view.
166 #if defined(OS_MACOSX)
167 bool WebContentsViewGuest::IsEventTracking() const {
171 void WebContentsViewGuest::CloseTabAfterEventTracking() {
175 WebContents
* WebContentsViewGuest::web_contents() {
176 return web_contents_
;
179 void WebContentsViewGuest::RestoreFocus() {
180 platform_view_
->RestoreFocus();
183 void WebContentsViewGuest::Focus() {
184 platform_view_
->Focus();
187 void WebContentsViewGuest::StoreFocus() {
188 platform_view_
->StoreFocus();
191 DropData
* WebContentsViewGuest::GetDropData() const {
196 void WebContentsViewGuest::UpdateDragCursor(WebDragOperation operation
) {
197 RenderViewHostImpl
* embedder_render_view_host
=
198 static_cast<RenderViewHostImpl
*>(
199 guest_
->embedder_web_contents()->GetRenderViewHost());
200 CHECK(embedder_render_view_host
);
201 RenderViewHostDelegateView
* view
=
202 embedder_render_view_host
->GetDelegate()->GetDelegateView();
204 view
->UpdateDragCursor(operation
);
207 void WebContentsViewGuest::GotFocus() {
210 void WebContentsViewGuest::TakeFocus(bool reverse
) {
213 void WebContentsViewGuest::ShowContextMenu(RenderFrameHost
* render_frame_host
,
214 const ContextMenuParams
& params
) {
215 platform_view_delegate_view_
->ShowContextMenu(render_frame_host
, params
);
218 void WebContentsViewGuest::StartDragging(
219 const DropData
& drop_data
,
220 WebDragOperationsMask ops
,
221 const gfx::ImageSkia
& image
,
222 const gfx::Vector2d
& image_offset
,
223 const DragEventSourceInfo
& event_info
) {
224 WebContentsImpl
* embedder_web_contents
= guest_
->embedder_web_contents();
225 embedder_web_contents
->GetBrowserPluginEmbedder()->StartDrag(guest_
);
226 RenderViewHostImpl
* embedder_render_view_host
=
227 static_cast<RenderViewHostImpl
*>(
228 embedder_web_contents
->GetRenderViewHost());
229 CHECK(embedder_render_view_host
);
230 RenderViewHostDelegateView
* view
=
231 embedder_render_view_host
->GetDelegate()->GetDelegateView();
233 RecordAction(base::UserMetricsAction("BrowserPlugin.Guest.StartDrag"));
234 view
->StartDragging(drop_data
, ops
, image
, image_offset
, event_info
);
236 embedder_web_contents
->SystemDragEnded();
240 } // namespace content