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/renderer_host/render_view_host_factory.h" 11 #include "content/browser/renderer_host/render_view_host_impl.h" 12 #include "content/browser/renderer_host/render_widget_host_view_guest.h" 13 #include "content/browser/web_contents/interstitial_page_impl.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" 23 24 #if defined(USE_AURA) 25 #include "ui/aura/window.h" 26 #endif 27 28 using WebKit::WebDragOperation; 29 using WebKit::WebDragOperationsMask; 30 31 namespace content { 32 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), 39 guest_(guest), 40 platform_view_(platform_view.Pass()), 41 platform_view_delegate_view_(platform_view_delegate_view) { 42 } 43 44 WebContentsViewGuest::~WebContentsViewGuest() { 45 } 46 47 gfx::NativeView WebContentsViewGuest::GetNativeView() const { 48 return platform_view_->GetNativeView(); 49 } 50 51 gfx::NativeView WebContentsViewGuest::GetContentNativeView() const { 52 RenderWidgetHostView* rwhv = web_contents_->GetRenderWidgetHostView(); 53 if (!rwhv) 54 return NULL; 55 return rwhv->GetNativeView(); 56 } 57 58 gfx::NativeWindow WebContentsViewGuest::GetTopLevelNativeWindow() const { 59 return guest_->embedder_web_contents()->GetView()->GetTopLevelNativeWindow(); 60 } 61 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. 69 #if defined(USE_AURA) 70 // This can be win aura or chromeos. 71 parent_view->GetNativeView()->AddChild(platform_view_->GetNativeView()); 72 #elif defined(OS_WIN) 73 SetParent(platform_view_->GetNativeView(), parent_view->GetNativeView()); 74 #endif 75 #endif // defined(USE_AURA) || defined(OS_WIN) 76 } 77 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()); 83 out->set_size(size_); 84 } 85 86 void WebContentsViewGuest::SizeContents(const gfx::Size& size) { 87 size_ = size; 88 RenderWidgetHostView* rwhv = web_contents_->GetRenderWidgetHostView(); 89 if (rwhv) 90 rwhv->SetSize(size); 91 } 92 93 void WebContentsViewGuest::SetInitialFocus() { 94 platform_view_->SetInitialFocus(); 95 } 96 97 gfx::Rect WebContentsViewGuest::GetViewBounds() const { 98 return gfx::Rect(size_); 99 } 100 101 #if defined(OS_MACOSX) 102 void WebContentsViewGuest::SetAllowOverlappingViews(bool overlapping) { 103 platform_view_->SetAllowOverlappingViews(overlapping); 104 } 105 106 bool WebContentsViewGuest::GetAllowOverlappingViews() const { 107 return platform_view_->GetAllowOverlappingViews(); 108 } 109 #endif 110 111 void WebContentsViewGuest::CreateView(const gfx::Size& initial_size, 112 gfx::NativeView context) { 113 platform_view_->CreateView(initial_size, context); 114 size_ = initial_size; 115 } 116 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(); 127 } 128 129 RenderWidgetHostView* platform_widget = NULL; 130 platform_widget = platform_view_->CreateViewForWidget(render_widget_host); 131 132 RenderWidgetHostView* view = new RenderWidgetHostViewGuest( 133 render_widget_host, 134 guest_, 135 platform_widget); 136 137 return view; 138 } 139 140 RenderWidgetHostView* WebContentsViewGuest::CreateViewForPopupWidget( 141 RenderWidgetHost* render_widget_host) { 142 return RenderWidgetHostViewPort::CreateViewForWidget(render_widget_host); 143 } 144 145 void WebContentsViewGuest::SetPageTitle(const string16& title) { 146 } 147 148 void WebContentsViewGuest::RenderViewCreated(RenderViewHost* host) { 149 platform_view_->RenderViewCreated(host); 150 } 151 152 void WebContentsViewGuest::RenderViewSwappedIn(RenderViewHost* host) { 153 platform_view_->RenderViewSwappedIn(host); 154 } 155 156 void WebContentsViewGuest::SetOverscrollControllerEnabled(bool enabled) { 157 // This should never override the setting of the embedder view. 158 } 159 160 #if defined(OS_MACOSX) 161 bool WebContentsViewGuest::IsEventTracking() const { 162 return false; 163 } 164 165 void WebContentsViewGuest::CloseTabAfterEventTracking() { 166 } 167 #endif 168 169 WebContents* WebContentsViewGuest::web_contents() { 170 return web_contents_; 171 } 172 173 void WebContentsViewGuest::RestoreFocus() { 174 platform_view_->RestoreFocus(); 175 } 176 177 void WebContentsViewGuest::OnTabCrashed(base::TerminationStatus status, 178 int error_code) { 179 } 180 181 void WebContentsViewGuest::Focus() { 182 platform_view_->Focus(); 183 } 184 185 void WebContentsViewGuest::StoreFocus() { 186 platform_view_->StoreFocus(); 187 } 188 189 DropData* WebContentsViewGuest::GetDropData() const { 190 NOTIMPLEMENTED(); 191 return NULL; 192 } 193 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(); 201 if (view) 202 view->UpdateDragCursor(operation); 203 } 204 205 void WebContentsViewGuest::GotFocus() { 206 } 207 208 void WebContentsViewGuest::TakeFocus(bool reverse) { 209 } 210 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( 220 &embedder_bounds); 221 gfx::Rect guest_bounds; 222 GetContainerBounds(&guest_bounds); 223 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); 229 #else 230 platform_view_delegate_view_->ShowContextMenu(params); 231 #endif // defined(USE_AURA) || defined(OS_WIN) 232 } 233 234 void WebContentsViewGuest::ShowPopupMenu(const gfx::Rect& bounds, 235 int item_height, 236 double item_font_size, 237 int selected_item, 238 const std::vector<MenuItem>& items, 239 bool right_aligned, 240 bool allow_multiple_selection) { 241 // External popup menus are only used on Mac and Android. 242 NOTIMPLEMENTED(); 243 } 244 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(); 259 if (view) 260 view->StartDragging(drop_data, ops, image, image_offset, event_info); 261 else 262 embedder_web_contents->SystemDragEnded(); 263 } 264 265 } // namespace content 266