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::SetAllowOtherViews(bool allow) {
131 platform_view_->SetAllowOtherViews(allow);
134 bool WebContentsViewGuest::GetAllowOtherViews() const {
135 return platform_view_->GetAllowOtherViews();
139 void WebContentsViewGuest::CreateView(const gfx::Size& initial_size,
140 gfx::NativeView context) {
141 platform_view_->CreateView(initial_size, context);
142 size_ = initial_size;
145 RenderWidgetHostViewBase* WebContentsViewGuest::CreateViewForWidget(
146 RenderWidgetHost* render_widget_host) {
147 if (render_widget_host->GetView()) {
148 // During testing, the view will already be set up in most cases to the
149 // test view, so we don't want to clobber it with a real one. To verify that
150 // this actually is happening (and somebody isn't accidentally creating the
151 // view twice), we check for the RVH Factory, which will be set when we're
152 // making special ones (which go along with the special views).
153 DCHECK(RenderViewHostFactory::has_factory());
154 return static_cast<RenderWidgetHostViewBase*>(
155 render_widget_host->GetView());
158 RenderWidgetHostViewBase* platform_widget =
159 platform_view_->CreateViewForWidget(render_widget_host);
161 RenderWidgetHostViewBase* view = new RenderWidgetHostViewGuest(
169 RenderWidgetHostViewBase* WebContentsViewGuest::CreateViewForPopupWidget(
170 RenderWidgetHost* render_widget_host) {
171 return platform_view_->CreateViewForPopupWidget(render_widget_host);
174 void WebContentsViewGuest::SetPageTitle(const base::string16& title) {
177 void WebContentsViewGuest::RenderViewCreated(RenderViewHost* host) {
178 platform_view_->RenderViewCreated(host);
181 void WebContentsViewGuest::RenderViewSwappedIn(RenderViewHost* host) {
182 platform_view_->RenderViewSwappedIn(host);
185 void WebContentsViewGuest::SetOverscrollControllerEnabled(bool enabled) {
186 // This should never override the setting of the embedder view.
189 #if defined(OS_MACOSX)
190 bool WebContentsViewGuest::IsEventTracking() const {
194 void WebContentsViewGuest::CloseTabAfterEventTracking() {
198 WebContents* WebContentsViewGuest::web_contents() {
199 return web_contents_;
202 void WebContentsViewGuest::RestoreFocus() {
203 platform_view_->RestoreFocus();
206 void WebContentsViewGuest::Focus() {
207 platform_view_->Focus();
210 void WebContentsViewGuest::StoreFocus() {
211 platform_view_->StoreFocus();
214 DropData* WebContentsViewGuest::GetDropData() const {
219 void WebContentsViewGuest::UpdateDragCursor(WebDragOperation operation) {
220 RenderViewHostImpl* embedder_render_view_host =
221 static_cast<RenderViewHostImpl*>(
222 guest_->embedder_web_contents()->GetRenderViewHost());
223 CHECK(embedder_render_view_host);
224 RenderViewHostDelegateView* view =
225 embedder_render_view_host->GetDelegate()->GetDelegateView();
227 view->UpdateDragCursor(operation);
230 void WebContentsViewGuest::GotFocus() {
233 void WebContentsViewGuest::TakeFocus(bool reverse) {
236 void WebContentsViewGuest::ShowContextMenu(RenderFrameHost* render_frame_host,
237 const ContextMenuParams& params) {
238 platform_view_delegate_view_->ShowContextMenu(
239 render_frame_host, ConvertContextMenuParams(params));
242 void WebContentsViewGuest::StartDragging(
243 const DropData& drop_data,
244 WebDragOperationsMask ops,
245 const gfx::ImageSkia& image,
246 const gfx::Vector2d& image_offset,
247 const DragEventSourceInfo& event_info) {
248 WebContentsImpl* embedder_web_contents = guest_->embedder_web_contents();
249 embedder_web_contents->GetBrowserPluginEmbedder()->StartDrag(guest_);
250 RenderViewHostImpl* embedder_render_view_host =
251 static_cast<RenderViewHostImpl*>(
252 embedder_web_contents->GetRenderViewHost());
253 CHECK(embedder_render_view_host);
254 RenderViewHostDelegateView* view =
255 embedder_render_view_host->GetDelegate()->GetDelegateView();
257 RecordAction(base::UserMetricsAction("BrowserPlugin.Guest.StartDrag"));
258 view->StartDragging(drop_data, ops, image, image_offset, event_info);
260 embedder_web_contents->SystemDragEnded();
264 } // namespace content