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_android.h" 6 7 #include "base/logging.h" 8 #include "content/browser/android/content_view_core_impl.h" 9 #include "content/browser/renderer_host/render_widget_host_view_android.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/web_contents/interstitial_page_impl.h" 13 #include "content/browser/web_contents/web_contents_impl.h" 14 #include "content/public/browser/web_contents_delegate.h" 15 #include "media/base/android/media_player_manager.h" 16 17 namespace content { 18 WebContentsViewPort* CreateWebContentsView( 19 WebContentsImpl* web_contents, 20 WebContentsViewDelegate* delegate, 21 RenderViewHostDelegateView** render_view_host_delegate_view) { 22 WebContentsViewAndroid* rv = new WebContentsViewAndroid( 23 web_contents, delegate); 24 *render_view_host_delegate_view = rv; 25 return rv; 26 } 27 28 WebContentsViewAndroid::WebContentsViewAndroid( 29 WebContentsImpl* web_contents, 30 WebContentsViewDelegate* delegate) 31 : web_contents_(web_contents), 32 content_view_core_(NULL), 33 delegate_(delegate) { 34 } 35 36 WebContentsViewAndroid::~WebContentsViewAndroid() { 37 } 38 39 void WebContentsViewAndroid::SetContentViewCore( 40 ContentViewCoreImpl* content_view_core) { 41 content_view_core_ = content_view_core; 42 RenderWidgetHostViewAndroid* rwhv = static_cast<RenderWidgetHostViewAndroid*>( 43 web_contents_->GetRenderWidgetHostView()); 44 if (rwhv) 45 rwhv->SetContentViewCore(content_view_core_); 46 47 if (web_contents_->ShowingInterstitialPage()) { 48 rwhv = static_cast<RenderWidgetHostViewAndroid*>( 49 static_cast<InterstitialPageImpl*>( 50 web_contents_->GetInterstitialPage())-> 51 GetRenderViewHost()->GetView()); 52 if (rwhv) 53 rwhv->SetContentViewCore(content_view_core_); 54 } 55 } 56 57 #if defined(GOOGLE_TV) 58 void WebContentsViewAndroid::NotifyExternalSurface( 59 int player_id, bool is_request, const gfx::RectF& rect) { 60 if (content_view_core_) 61 content_view_core_->NotifyExternalSurface(player_id, is_request, rect); 62 } 63 #endif 64 65 gfx::NativeView WebContentsViewAndroid::GetNativeView() const { 66 return content_view_core_ ? content_view_core_->GetViewAndroid() : NULL; 67 } 68 69 gfx::NativeView WebContentsViewAndroid::GetContentNativeView() const { 70 return content_view_core_ ? content_view_core_->GetViewAndroid() : NULL; 71 } 72 73 gfx::NativeWindow WebContentsViewAndroid::GetTopLevelNativeWindow() const { 74 return content_view_core_ ? content_view_core_->GetWindowAndroid() : NULL; 75 } 76 77 void WebContentsViewAndroid::GetContainerBounds(gfx::Rect* out) const { 78 RenderWidgetHostView* rwhv = web_contents_->GetRenderWidgetHostView(); 79 if (rwhv) 80 *out = rwhv->GetViewBounds(); 81 } 82 83 void WebContentsViewAndroid::SetPageTitle(const string16& title) { 84 if (content_view_core_) 85 content_view_core_->SetTitle(title); 86 } 87 88 void WebContentsViewAndroid::OnTabCrashed(base::TerminationStatus status, 89 int error_code) { 90 RenderViewHostImpl* rvh = static_cast<RenderViewHostImpl*>( 91 web_contents_->GetRenderViewHost()); 92 if (rvh->media_player_manager()) 93 rvh->media_player_manager()->DestroyAllMediaPlayers(); 94 if (content_view_core_) 95 content_view_core_->OnTabCrashed(); 96 } 97 98 void WebContentsViewAndroid::SizeContents(const gfx::Size& size) { 99 // TODO(klobag): Do we need to do anything else? 100 RenderWidgetHostView* rwhv = web_contents_->GetRenderWidgetHostView(); 101 if (rwhv) 102 rwhv->SetSize(size); 103 } 104 105 void WebContentsViewAndroid::Focus() { 106 if (web_contents_->ShowingInterstitialPage()) 107 web_contents_->GetInterstitialPage()->Focus(); 108 else 109 web_contents_->GetRenderWidgetHostView()->Focus(); 110 } 111 112 void WebContentsViewAndroid::SetInitialFocus() { 113 if (web_contents_->FocusLocationBarByDefault()) 114 web_contents_->SetFocusToLocationBar(false); 115 else 116 Focus(); 117 } 118 119 void WebContentsViewAndroid::StoreFocus() { 120 NOTIMPLEMENTED(); 121 } 122 123 void WebContentsViewAndroid::RestoreFocus() { 124 NOTIMPLEMENTED(); 125 } 126 127 DropData* WebContentsViewAndroid::GetDropData() const { 128 NOTIMPLEMENTED(); 129 return NULL; 130 } 131 132 gfx::Rect WebContentsViewAndroid::GetViewBounds() const { 133 RenderWidgetHostView* rwhv = web_contents_->GetRenderWidgetHostView(); 134 if (rwhv) 135 return rwhv->GetViewBounds(); 136 else 137 return gfx::Rect(); 138 } 139 140 void WebContentsViewAndroid::CreateView( 141 const gfx::Size& initial_size, gfx::NativeView context) { 142 } 143 144 RenderWidgetHostView* WebContentsViewAndroid::CreateViewForWidget( 145 RenderWidgetHost* render_widget_host) { 146 if (render_widget_host->GetView()) { 147 // During testing, the view will already be set up in most cases to the 148 // test view, so we don't want to clobber it with a real one. To verify that 149 // this actually is happening (and somebody isn't accidentally creating the 150 // view twice), we check for the RVH Factory, which will be set when we're 151 // making special ones (which go along with the special views). 152 DCHECK(RenderViewHostFactory::has_factory()); 153 return render_widget_host->GetView(); 154 } 155 // Note that while this instructs the render widget host to reference 156 // |native_view_|, this has no effect without also instructing the 157 // native view (i.e. ContentView) how to obtain a reference to this widget in 158 // order to paint it. See ContentView::GetRenderWidgetHostViewAndroid for an 159 // example of how this is achieved for InterstitialPages. 160 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(render_widget_host); 161 RenderWidgetHostView* view = new RenderWidgetHostViewAndroid( 162 rwhi, content_view_core_); 163 return view; 164 } 165 166 RenderWidgetHostView* WebContentsViewAndroid::CreateViewForPopupWidget( 167 RenderWidgetHost* render_widget_host) { 168 return RenderWidgetHostViewPort::CreateViewForWidget(render_widget_host); 169 } 170 171 void WebContentsViewAndroid::RenderViewCreated(RenderViewHost* host) { 172 } 173 174 void WebContentsViewAndroid::RenderViewSwappedIn(RenderViewHost* host) { 175 } 176 177 void WebContentsViewAndroid::SetOverscrollControllerEnabled(bool enabled) { 178 } 179 180 void WebContentsViewAndroid::ShowContextMenu( 181 const ContextMenuParams& params) { 182 if (delegate_) 183 delegate_->ShowContextMenu(params); 184 } 185 186 void WebContentsViewAndroid::ShowPopupMenu( 187 const gfx::Rect& bounds, 188 int item_height, 189 double item_font_size, 190 int selected_item, 191 const std::vector<MenuItem>& items, 192 bool right_aligned, 193 bool allow_multiple_selection) { 194 if (content_view_core_) { 195 content_view_core_->ShowSelectPopupMenu( 196 items, selected_item, allow_multiple_selection); 197 } 198 } 199 200 void WebContentsViewAndroid::StartDragging( 201 const DropData& drop_data, 202 WebKit::WebDragOperationsMask allowed_ops, 203 const gfx::ImageSkia& image, 204 const gfx::Vector2d& image_offset, 205 const DragEventSourceInfo& event_info) { 206 NOTIMPLEMENTED(); 207 } 208 209 void WebContentsViewAndroid::UpdateDragCursor(WebKit::WebDragOperation op) { 210 NOTIMPLEMENTED(); 211 } 212 213 void WebContentsViewAndroid::GotFocus() { 214 // This is only used in the views FocusManager stuff but it bleeds through 215 // all subclasses. http://crbug.com/21875 216 } 217 218 // This is called when we the renderer asks us to take focus back (i.e., it has 219 // iterated past the last focusable element on the page). 220 void WebContentsViewAndroid::TakeFocus(bool reverse) { 221 if (web_contents_->GetDelegate() && 222 web_contents_->GetDelegate()->TakeFocus(web_contents_, reverse)) 223 return; 224 web_contents_->GetRenderWidgetHostView()->Focus(); 225 } 226 227 } // namespace content 228