web_contents_view_guest.cc revision 010d83a9304c5a91596085d917d248abff47903a
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. 4 5#include "content/browser/web_contents/web_contents_view_guest.h" 6 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" 24 25#if defined(USE_AURA) 26#include "ui/aura/window.h" 27#endif 28 29using blink::WebDragOperation; 30using blink::WebDragOperationsMask; 31 32namespace content { 33 34WebContentsViewGuest::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), 40 guest_(guest), 41 platform_view_(platform_view.Pass()), 42 platform_view_delegate_view_(platform_view_delegate_view) { 43} 44 45WebContentsViewGuest::~WebContentsViewGuest() { 46} 47 48gfx::NativeView WebContentsViewGuest::GetNativeView() const { 49 return platform_view_->GetNativeView(); 50} 51 52gfx::NativeView WebContentsViewGuest::GetContentNativeView() const { 53 RenderWidgetHostView* rwhv = web_contents_->GetRenderWidgetHostView(); 54 if (!rwhv) 55 return NULL; 56 return rwhv->GetNativeView(); 57} 58 59gfx::NativeWindow WebContentsViewGuest::GetTopLevelNativeWindow() const { 60 return guest_->embedder_web_contents()->GetTopLevelNativeWindow(); 61} 62 63void WebContentsViewGuest::OnGuestInitialized(WebContentsView* parent_view) { 64#if defined(USE_AURA) 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) 72} 73 74ContextMenuParams WebContentsViewGuest::ConvertContextMenuParams( 75 const ContextMenuParams& params) const { 76#if defined(USE_AURA) 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( 84 &embedder_bounds); 85 gfx::Rect guest_bounds; 86 GetContainerBounds(&guest_bounds); 87 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; 93#else 94 return params; 95#endif 96} 97 98void 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_); 104} 105 106void WebContentsViewGuest::SizeContents(const gfx::Size& size) { 107 size_ = size; 108 RenderWidgetHostView* rwhv = web_contents_->GetRenderWidgetHostView(); 109 if (rwhv) 110 rwhv->SetSize(size); 111} 112 113void WebContentsViewGuest::SetInitialFocus() { 114 platform_view_->SetInitialFocus(); 115} 116 117gfx::Rect WebContentsViewGuest::GetViewBounds() const { 118 return gfx::Rect(size_); 119} 120 121#if defined(OS_MACOSX) 122void WebContentsViewGuest::SetAllowOverlappingViews(bool overlapping) { 123 platform_view_->SetAllowOverlappingViews(overlapping); 124} 125 126bool WebContentsViewGuest::GetAllowOverlappingViews() const { 127 return platform_view_->GetAllowOverlappingViews(); 128} 129 130void WebContentsViewGuest::SetOverlayView( 131 WebContentsView* overlay, const gfx::Point& offset) { 132 platform_view_->SetOverlayView(overlay, offset); 133} 134 135void WebContentsViewGuest::RemoveOverlayView() { 136 platform_view_->RemoveOverlayView(); 137} 138#endif 139 140void WebContentsViewGuest::CreateView(const gfx::Size& initial_size, 141 gfx::NativeView context) { 142 platform_view_->CreateView(initial_size, context); 143 size_ = initial_size; 144} 145 146RenderWidgetHostViewBase* WebContentsViewGuest::CreateViewForWidget( 147 RenderWidgetHost* render_widget_host) { 148 if (render_widget_host->GetView()) { 149 // During testing, the view will already be set up in most cases to the 150 // test view, so we don't want to clobber it with a real one. To verify that 151 // this actually is happening (and somebody isn't accidentally creating the 152 // view twice), we check for the RVH Factory, which will be set when we're 153 // making special ones (which go along with the special views). 154 DCHECK(RenderViewHostFactory::has_factory()); 155 return static_cast<RenderWidgetHostViewBase*>( 156 render_widget_host->GetView()); 157 } 158 159 RenderWidgetHostViewBase* platform_widget = 160 platform_view_->CreateViewForWidget(render_widget_host); 161 162 RenderWidgetHostViewBase* view = new RenderWidgetHostViewGuest( 163 render_widget_host, 164 guest_, 165 platform_widget); 166 167 return view; 168} 169 170RenderWidgetHostViewBase* WebContentsViewGuest::CreateViewForPopupWidget( 171 RenderWidgetHost* render_widget_host) { 172 return platform_view_->CreateViewForWidget(render_widget_host); 173} 174 175void WebContentsViewGuest::SetPageTitle(const base::string16& title) { 176} 177 178void WebContentsViewGuest::RenderViewCreated(RenderViewHost* host) { 179 platform_view_->RenderViewCreated(host); 180} 181 182void WebContentsViewGuest::RenderViewSwappedIn(RenderViewHost* host) { 183 platform_view_->RenderViewSwappedIn(host); 184} 185 186void WebContentsViewGuest::SetOverscrollControllerEnabled(bool enabled) { 187 // This should never override the setting of the embedder view. 188} 189 190#if defined(OS_MACOSX) 191bool WebContentsViewGuest::IsEventTracking() const { 192 return false; 193} 194 195void WebContentsViewGuest::CloseTabAfterEventTracking() { 196} 197#endif 198 199WebContents* WebContentsViewGuest::web_contents() { 200 return web_contents_; 201} 202 203void WebContentsViewGuest::RestoreFocus() { 204 platform_view_->RestoreFocus(); 205} 206 207void WebContentsViewGuest::Focus() { 208 platform_view_->Focus(); 209} 210 211void WebContentsViewGuest::StoreFocus() { 212 platform_view_->StoreFocus(); 213} 214 215DropData* WebContentsViewGuest::GetDropData() const { 216 NOTIMPLEMENTED(); 217 return NULL; 218} 219 220void WebContentsViewGuest::UpdateDragCursor(WebDragOperation operation) { 221 RenderViewHostImpl* embedder_render_view_host = 222 static_cast<RenderViewHostImpl*>( 223 guest_->embedder_web_contents()->GetRenderViewHost()); 224 CHECK(embedder_render_view_host); 225 RenderViewHostDelegateView* view = 226 embedder_render_view_host->GetDelegate()->GetDelegateView(); 227 if (view) 228 view->UpdateDragCursor(operation); 229} 230 231void WebContentsViewGuest::GotFocus() { 232} 233 234void WebContentsViewGuest::TakeFocus(bool reverse) { 235} 236 237void WebContentsViewGuest::ShowContextMenu(RenderFrameHost* render_frame_host, 238 const ContextMenuParams& params) { 239 platform_view_delegate_view_->ShowContextMenu( 240 render_frame_host, ConvertContextMenuParams(params)); 241} 242 243void WebContentsViewGuest::StartDragging( 244 const DropData& drop_data, 245 WebDragOperationsMask ops, 246 const gfx::ImageSkia& image, 247 const gfx::Vector2d& image_offset, 248 const DragEventSourceInfo& event_info) { 249 WebContentsImpl* embedder_web_contents = guest_->embedder_web_contents(); 250 embedder_web_contents->GetBrowserPluginEmbedder()->StartDrag(guest_); 251 RenderViewHostImpl* embedder_render_view_host = 252 static_cast<RenderViewHostImpl*>( 253 embedder_web_contents->GetRenderViewHost()); 254 CHECK(embedder_render_view_host); 255 RenderViewHostDelegateView* view = 256 embedder_render_view_host->GetDelegate()->GetDelegateView(); 257 if (view) { 258 RecordAction(base::UserMetricsAction("BrowserPlugin.Guest.StartDrag")); 259 view->StartDragging(drop_data, ops, image, image_offset, event_info); 260 } else { 261 embedder_web_contents->SystemDragEnded(); 262 } 263} 264 265} // namespace content 266