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 ContextMenuParams
WebContentsViewGuest::ConvertContextMenuParams(
82 const ContextMenuParams
& params
) const {
83 // We need to add |offset| of the guest from the embedder to position the
85 gfx::Rect embedder_bounds
;
86 guest_
->embedder_web_contents()->GetView()->GetContainerBounds(
88 gfx::Rect guest_bounds
;
89 GetContainerBounds(&guest_bounds
);
91 gfx::Vector2d offset
= guest_bounds
.origin() - embedder_bounds
.origin();
92 ContextMenuParams params_in_embedder
= params
;
93 params_in_embedder
.x
+= offset
.x();
94 params_in_embedder
.y
+= offset
.y();
95 return params_in_embedder
;
98 void WebContentsViewGuest::GetContainerBounds(gfx::Rect
* out
) const {
99 if (guest_
->embedder_web_contents()) {
100 // We need embedder container's bounds to calculate our bounds.
101 guest_
->embedder_web_contents()->GetView()->GetContainerBounds(out
);
102 gfx::Point guest_coordinates
= guest_
->GetScreenCoordinates(gfx::Point());
103 out
->Offset(guest_coordinates
.x(), guest_coordinates
.y());
105 out
->set_origin(gfx::Point());
108 out
->set_size(size_
);
111 void WebContentsViewGuest::SizeContents(const gfx::Size
& size
) {
113 RenderWidgetHostView
* rwhv
= web_contents_
->GetRenderWidgetHostView();
118 void WebContentsViewGuest::SetInitialFocus() {
119 platform_view_
->SetInitialFocus();
122 gfx::Rect
WebContentsViewGuest::GetViewBounds() const {
123 return gfx::Rect(size_
);
126 #if defined(OS_MACOSX)
127 void WebContentsViewGuest::SetAllowOtherViews(bool allow
) {
128 platform_view_
->SetAllowOtherViews(allow
);
131 bool WebContentsViewGuest::GetAllowOtherViews() const {
132 return platform_view_
->GetAllowOtherViews();
136 void WebContentsViewGuest::CreateView(const gfx::Size
& initial_size
,
137 gfx::NativeView context
) {
138 platform_view_
->CreateView(initial_size
, context
);
139 size_
= initial_size
;
142 RenderWidgetHostViewBase
* WebContentsViewGuest::CreateViewForWidget(
143 RenderWidgetHost
* render_widget_host
, bool is_guest_view_hack
) {
144 if (render_widget_host
->GetView()) {
145 // During testing, the view will already be set up in most cases to the
146 // test view, so we don't want to clobber it with a real one. To verify that
147 // this actually is happening (and somebody isn't accidentally creating the
148 // view twice), we check for the RVH Factory, which will be set when we're
149 // making special ones (which go along with the special views).
150 DCHECK(RenderViewHostFactory::has_factory());
151 return static_cast<RenderWidgetHostViewBase
*>(
152 render_widget_host
->GetView());
155 RenderWidgetHostViewBase
* platform_widget
=
156 platform_view_
->CreateViewForWidget(render_widget_host
, true);
158 return new RenderWidgetHostViewGuest(render_widget_host
,
160 platform_widget
->GetWeakPtr());
163 RenderWidgetHostViewBase
* WebContentsViewGuest::CreateViewForPopupWidget(
164 RenderWidgetHost
* render_widget_host
) {
165 return platform_view_
->CreateViewForPopupWidget(render_widget_host
);
168 void WebContentsViewGuest::SetPageTitle(const base::string16
& title
) {
171 void WebContentsViewGuest::RenderViewCreated(RenderViewHost
* host
) {
172 platform_view_
->RenderViewCreated(host
);
175 void WebContentsViewGuest::RenderViewSwappedIn(RenderViewHost
* host
) {
176 platform_view_
->RenderViewSwappedIn(host
);
179 void WebContentsViewGuest::SetOverscrollControllerEnabled(bool enabled
) {
180 // This should never override the setting of the embedder view.
183 #if defined(OS_MACOSX)
184 bool WebContentsViewGuest::IsEventTracking() const {
188 void WebContentsViewGuest::CloseTabAfterEventTracking() {
192 WebContents
* WebContentsViewGuest::web_contents() {
193 return web_contents_
;
196 void WebContentsViewGuest::RestoreFocus() {
197 platform_view_
->RestoreFocus();
200 void WebContentsViewGuest::Focus() {
201 platform_view_
->Focus();
204 void WebContentsViewGuest::StoreFocus() {
205 platform_view_
->StoreFocus();
208 DropData
* WebContentsViewGuest::GetDropData() const {
213 void WebContentsViewGuest::UpdateDragCursor(WebDragOperation operation
) {
214 RenderViewHostImpl
* embedder_render_view_host
=
215 static_cast<RenderViewHostImpl
*>(
216 guest_
->embedder_web_contents()->GetRenderViewHost());
217 CHECK(embedder_render_view_host
);
218 RenderViewHostDelegateView
* view
=
219 embedder_render_view_host
->GetDelegate()->GetDelegateView();
221 view
->UpdateDragCursor(operation
);
224 void WebContentsViewGuest::GotFocus() {
227 void WebContentsViewGuest::TakeFocus(bool reverse
) {
230 void WebContentsViewGuest::ShowContextMenu(RenderFrameHost
* render_frame_host
,
231 const ContextMenuParams
& params
) {
232 platform_view_delegate_view_
->ShowContextMenu(
233 render_frame_host
, ConvertContextMenuParams(params
));
236 void WebContentsViewGuest::StartDragging(
237 const DropData
& drop_data
,
238 WebDragOperationsMask ops
,
239 const gfx::ImageSkia
& image
,
240 const gfx::Vector2d
& image_offset
,
241 const DragEventSourceInfo
& event_info
) {
242 WebContentsImpl
* embedder_web_contents
= guest_
->embedder_web_contents();
243 embedder_web_contents
->GetBrowserPluginEmbedder()->StartDrag(guest_
);
244 RenderViewHostImpl
* embedder_render_view_host
=
245 static_cast<RenderViewHostImpl
*>(
246 embedder_web_contents
->GetRenderViewHost());
247 CHECK(embedder_render_view_host
);
248 RenderViewHostDelegateView
* view
=
249 embedder_render_view_host
->GetDelegate()->GetDelegateView();
251 RecordAction(base::UserMetricsAction("BrowserPlugin.Guest.StartDrag"));
252 view
->StartDragging(drop_data
, ops
, image
, image_offset
, event_info
);
254 embedder_web_contents
->SystemDragEnded();
258 } // namespace content