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<WebContentsViewPort> 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()->GetView()->GetTopLevelNativeWindow();
63 void WebContentsViewGuest::OnGuestInitialized(WebContentsView* parent_view) {
64 #if defined(USE_AURA) || defined(OS_WIN)
65 // In aura and windows, 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.
71 // This can be win aura or chromeos.
72 parent_view->GetNativeView()->AddChild(platform_view_->GetNativeView());
74 SetParent(platform_view_->GetNativeView(), parent_view->GetNativeView());
76 #endif // defined(USE_AURA) || defined(OS_WIN)
79 void WebContentsViewGuest::GetContainerBounds(gfx::Rect* out) const {
80 // We need embedder container's bounds to calculate our bounds.
81 guest_->embedder_web_contents()->GetView()->GetContainerBounds(out);
82 gfx::Point guest_coordinates = guest_->GetScreenCoordinates(gfx::Point());
83 out->Offset(guest_coordinates.x(), guest_coordinates.y());
87 void WebContentsViewGuest::SizeContents(const gfx::Size& size) {
89 RenderWidgetHostView* rwhv = web_contents_->GetRenderWidgetHostView();
94 void WebContentsViewGuest::SetInitialFocus() {
95 platform_view_->SetInitialFocus();
98 gfx::Rect WebContentsViewGuest::GetViewBounds() const {
99 return gfx::Rect(size_);
102 #if defined(OS_MACOSX)
103 void WebContentsViewGuest::SetAllowOverlappingViews(bool overlapping) {
104 platform_view_->SetAllowOverlappingViews(overlapping);
107 bool WebContentsViewGuest::GetAllowOverlappingViews() const {
108 return platform_view_->GetAllowOverlappingViews();
111 void WebContentsViewGuest::SetOverlayView(
112 WebContentsView* overlay, const gfx::Point& offset) {
113 platform_view_->SetOverlayView(overlay, offset);
116 void WebContentsViewGuest::RemoveOverlayView() {
117 platform_view_->RemoveOverlayView();
121 void WebContentsViewGuest::CreateView(const gfx::Size& initial_size,
122 gfx::NativeView context) {
123 platform_view_->CreateView(initial_size, context);
124 size_ = initial_size;
127 RenderWidgetHostView* WebContentsViewGuest::CreateViewForWidget(
128 RenderWidgetHost* render_widget_host) {
129 if (render_widget_host->GetView()) {
130 // During testing, the view will already be set up in most cases to the
131 // test view, so we don't want to clobber it with a real one. To verify that
132 // this actually is happening (and somebody isn't accidentally creating the
133 // view twice), we check for the RVH Factory, which will be set when we're
134 // making special ones (which go along with the special views).
135 DCHECK(RenderViewHostFactory::has_factory());
136 return render_widget_host->GetView();
139 RenderWidgetHostView* platform_widget = NULL;
140 platform_widget = platform_view_->CreateViewForWidget(render_widget_host);
142 RenderWidgetHostView* view = new RenderWidgetHostViewGuest(
150 RenderWidgetHostView* WebContentsViewGuest::CreateViewForPopupWidget(
151 RenderWidgetHost* render_widget_host) {
152 return RenderWidgetHostViewPort::CreateViewForWidget(render_widget_host);
155 void WebContentsViewGuest::SetPageTitle(const base::string16& title) {
158 void WebContentsViewGuest::RenderViewCreated(RenderViewHost* host) {
159 platform_view_->RenderViewCreated(host);
162 void WebContentsViewGuest::RenderViewSwappedIn(RenderViewHost* host) {
163 platform_view_->RenderViewSwappedIn(host);
166 void WebContentsViewGuest::SetOverscrollControllerEnabled(bool enabled) {
167 // This should never override the setting of the embedder view.
170 #if defined(OS_MACOSX)
171 bool WebContentsViewGuest::IsEventTracking() const {
175 void WebContentsViewGuest::CloseTabAfterEventTracking() {
179 WebContents* WebContentsViewGuest::web_contents() {
180 return web_contents_;
183 void WebContentsViewGuest::RestoreFocus() {
184 platform_view_->RestoreFocus();
187 void WebContentsViewGuest::OnTabCrashed(base::TerminationStatus status,
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 #if defined(USE_AURA) || defined(OS_WIN)
224 // Context menu uses ScreenPositionClient::ConvertPointToScreen() in aura and
225 // windows to calculate popup position. Guest's native view
226 // (platform_view_->GetNativeView()) is part of the embedder's view hierarchy,
227 // but is placed at (0, 0) w.r.t. the embedder's position. Therefore, |offset|
228 // is added to |params|.
229 gfx::Rect embedder_bounds;
230 guest_->embedder_web_contents()->GetView()->GetContainerBounds(
232 gfx::Rect guest_bounds;
233 GetContainerBounds(&guest_bounds);
235 gfx::Vector2d offset = guest_bounds.origin() - embedder_bounds.origin();
236 ContextMenuParams params_in_embedder = params;
237 params_in_embedder.x += offset.x();
238 params_in_embedder.y += offset.y();
239 platform_view_delegate_view_->ShowContextMenu(
240 render_frame_host, params_in_embedder);
242 platform_view_delegate_view_->ShowContextMenu(render_frame_host, params);
243 #endif // defined(USE_AURA) || defined(OS_WIN)
246 void WebContentsViewGuest::ShowPopupMenu(const gfx::Rect& bounds,
248 double item_font_size,
250 const std::vector<MenuItem>& items,
252 bool allow_multiple_selection) {
253 // External popup menus are only used on Mac and Android.
257 void WebContentsViewGuest::StartDragging(
258 const DropData& drop_data,
259 WebDragOperationsMask ops,
260 const gfx::ImageSkia& image,
261 const gfx::Vector2d& image_offset,
262 const DragEventSourceInfo& event_info) {
263 WebContentsImpl* embedder_web_contents = guest_->embedder_web_contents();
264 embedder_web_contents->GetBrowserPluginEmbedder()->StartDrag(guest_);
265 RenderViewHostImpl* embedder_render_view_host =
266 static_cast<RenderViewHostImpl*>(
267 embedder_web_contents->GetRenderViewHost());
268 CHECK(embedder_render_view_host);
269 RenderViewHostDelegateView* view =
270 embedder_render_view_host->GetDelegate()->GetDelegateView();
272 RecordAction(base::UserMetricsAction("BrowserPlugin.Guest.StartDrag"));
273 view->StartDragging(drop_data, ops, image, image_offset, event_info);
275 embedder_web_contents->SystemDragEnded();
279 } // namespace content