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/renderer_host/render_view_host_factory.h"
12 #include "content/browser/renderer_host/render_view_host_impl.h"
13 #include "content/browser/renderer_host/render_widget_host_view_guest.h"
14 #include "content/browser/web_contents/web_contents_impl.h"
15 #include "content/common/drag_messages.h"
16 #include "content/public/browser/web_contents_delegate.h"
17 #include "content/public/common/context_menu_params.h"
18 #include "content/public/common/drop_data.h"
19 #include "ui/gfx/image/image_skia.h"
20 #include "ui/gfx/point.h"
21 #include "ui/gfx/rect.h"
22 #include "ui/gfx/size.h"
25 #include "ui/aura/window.h"
28 using WebKit::WebDragOperation;
29 using WebKit::WebDragOperationsMask;
33 WebContentsViewGuest::WebContentsViewGuest(
34 WebContentsImpl* web_contents,
35 BrowserPluginGuest* guest,
36 scoped_ptr<WebContentsViewPort> platform_view,
37 RenderViewHostDelegateView* platform_view_delegate_view)
38 : web_contents_(web_contents),
40 platform_view_(platform_view.Pass()),
41 platform_view_delegate_view_(platform_view_delegate_view) {
44 WebContentsViewGuest::~WebContentsViewGuest() {
47 gfx::NativeView WebContentsViewGuest::GetNativeView() const {
48 return platform_view_->GetNativeView();
51 gfx::NativeView WebContentsViewGuest::GetContentNativeView() const {
52 RenderWidgetHostView* rwhv = web_contents_->GetRenderWidgetHostView();
55 return rwhv->GetNativeView();
58 gfx::NativeWindow WebContentsViewGuest::GetTopLevelNativeWindow() const {
59 return guest_->embedder_web_contents()->GetView()->GetTopLevelNativeWindow();
62 void WebContentsViewGuest::OnGuestInitialized(WebContentsView* parent_view) {
63 #if defined(USE_AURA) || defined(OS_WIN)
64 // In aura and windows, ScreenPositionClient doesn't work properly if we do
65 // not have the native view associated with this WebContentsViewGuest in the
66 // view hierarchy. We add this view as embedder's child here.
67 // This would go in WebContentsViewGuest::CreateView, but that is too early to
68 // access embedder_web_contents(). Therefore, we do it here.
70 // This can be win aura or chromeos.
71 parent_view->GetNativeView()->AddChild(platform_view_->GetNativeView());
73 SetParent(platform_view_->GetNativeView(), parent_view->GetNativeView());
75 #endif // defined(USE_AURA) || defined(OS_WIN)
78 void WebContentsViewGuest::GetContainerBounds(gfx::Rect* out) const {
79 // We need embedder container's bounds to calculate our bounds.
80 guest_->embedder_web_contents()->GetView()->GetContainerBounds(out);
81 gfx::Point guest_coordinates = guest_->GetScreenCoordinates(gfx::Point());
82 out->Offset(guest_coordinates.x(), guest_coordinates.y());
86 void WebContentsViewGuest::SizeContents(const gfx::Size& size) {
88 RenderWidgetHostView* rwhv = web_contents_->GetRenderWidgetHostView();
93 void WebContentsViewGuest::SetInitialFocus() {
94 platform_view_->SetInitialFocus();
97 gfx::Rect WebContentsViewGuest::GetViewBounds() const {
98 return gfx::Rect(size_);
101 #if defined(OS_MACOSX)
102 void WebContentsViewGuest::SetAllowOverlappingViews(bool overlapping) {
103 platform_view_->SetAllowOverlappingViews(overlapping);
106 bool WebContentsViewGuest::GetAllowOverlappingViews() const {
107 return platform_view_->GetAllowOverlappingViews();
111 void WebContentsViewGuest::CreateView(const gfx::Size& initial_size,
112 gfx::NativeView context) {
113 platform_view_->CreateView(initial_size, context);
114 size_ = initial_size;
117 RenderWidgetHostView* WebContentsViewGuest::CreateViewForWidget(
118 RenderWidgetHost* render_widget_host) {
119 if (render_widget_host->GetView()) {
120 // During testing, the view will already be set up in most cases to the
121 // test view, so we don't want to clobber it with a real one. To verify that
122 // this actually is happening (and somebody isn't accidentally creating the
123 // view twice), we check for the RVH Factory, which will be set when we're
124 // making special ones (which go along with the special views).
125 DCHECK(RenderViewHostFactory::has_factory());
126 return render_widget_host->GetView();
129 RenderWidgetHostView* platform_widget = NULL;
130 platform_widget = platform_view_->CreateViewForWidget(render_widget_host);
132 RenderWidgetHostView* view = new RenderWidgetHostViewGuest(
140 RenderWidgetHostView* WebContentsViewGuest::CreateViewForPopupWidget(
141 RenderWidgetHost* render_widget_host) {
142 return RenderWidgetHostViewPort::CreateViewForWidget(render_widget_host);
145 void WebContentsViewGuest::SetPageTitle(const string16& title) {
148 void WebContentsViewGuest::RenderViewCreated(RenderViewHost* host) {
149 platform_view_->RenderViewCreated(host);
152 void WebContentsViewGuest::RenderViewSwappedIn(RenderViewHost* host) {
153 platform_view_->RenderViewSwappedIn(host);
156 void WebContentsViewGuest::SetOverscrollControllerEnabled(bool enabled) {
157 // This should never override the setting of the embedder view.
160 #if defined(OS_MACOSX)
161 bool WebContentsViewGuest::IsEventTracking() const {
165 void WebContentsViewGuest::CloseTabAfterEventTracking() {
169 WebContents* WebContentsViewGuest::web_contents() {
170 return web_contents_;
173 void WebContentsViewGuest::RestoreFocus() {
174 platform_view_->RestoreFocus();
177 void WebContentsViewGuest::OnTabCrashed(base::TerminationStatus status,
181 void WebContentsViewGuest::Focus() {
182 platform_view_->Focus();
185 void WebContentsViewGuest::StoreFocus() {
186 platform_view_->StoreFocus();
189 DropData* WebContentsViewGuest::GetDropData() const {
194 void WebContentsViewGuest::UpdateDragCursor(WebDragOperation operation) {
195 RenderViewHostImpl* embedder_render_view_host =
196 static_cast<RenderViewHostImpl*>(
197 guest_->embedder_web_contents()->GetRenderViewHost());
198 CHECK(embedder_render_view_host);
199 RenderViewHostDelegateView* view =
200 embedder_render_view_host->GetDelegate()->GetDelegateView();
202 view->UpdateDragCursor(operation);
205 void WebContentsViewGuest::GotFocus() {
208 void WebContentsViewGuest::TakeFocus(bool reverse) {
211 void WebContentsViewGuest::ShowContextMenu(const ContextMenuParams& params) {
212 #if defined(USE_AURA) || defined(OS_WIN)
213 // Context menu uses ScreenPositionClient::ConvertPointToScreen() in aura and
214 // windows to calculate popup position. Guest's native view
215 // (platform_view_->GetNativeView()) is part of the embedder's view hierarchy,
216 // but is placed at (0, 0) w.r.t. the embedder's position. Therefore, |offset|
217 // is added to |params|.
218 gfx::Rect embedder_bounds;
219 guest_->embedder_web_contents()->GetView()->GetContainerBounds(
221 gfx::Rect guest_bounds;
222 GetContainerBounds(&guest_bounds);
224 gfx::Vector2d offset = guest_bounds.origin() - embedder_bounds.origin();
225 ContextMenuParams params_in_embedder = params;
226 params_in_embedder.x += offset.x();
227 params_in_embedder.y += offset.y();
228 platform_view_delegate_view_->ShowContextMenu(params_in_embedder);
230 platform_view_delegate_view_->ShowContextMenu(params);
231 #endif // defined(USE_AURA) || defined(OS_WIN)
234 void WebContentsViewGuest::ShowPopupMenu(const gfx::Rect& bounds,
236 double item_font_size,
238 const std::vector<MenuItem>& items,
240 bool allow_multiple_selection) {
241 // External popup menus are only used on Mac and Android.
245 void WebContentsViewGuest::StartDragging(
246 const DropData& drop_data,
247 WebDragOperationsMask ops,
248 const gfx::ImageSkia& image,
249 const gfx::Vector2d& image_offset,
250 const DragEventSourceInfo& event_info) {
251 WebContentsImpl* embedder_web_contents = guest_->embedder_web_contents();
252 embedder_web_contents->GetBrowserPluginEmbedder()->StartDrag(guest_);
253 RenderViewHostImpl* embedder_render_view_host =
254 static_cast<RenderViewHostImpl*>(
255 embedder_web_contents->GetRenderViewHost());
256 CHECK(embedder_render_view_host);
257 RenderViewHostDelegateView* view =
258 embedder_render_view_host->GetDelegate()->GetDelegateView();
260 view->StartDragging(drop_data, ops, image, image_offset, event_info);
262 embedder_web_contents->SystemDragEnded();
265 } // namespace content