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_android.h"
7 #include "base/logging.h"
8 #include "content/browser/android/content_view_core_impl.h"
9 #include "content/browser/frame_host/interstitial_page_impl.h"
10 #include "content/browser/media/android/browser_media_player_manager.h"
11 #include "content/browser/renderer_host/render_widget_host_view_android.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/public/browser/web_contents_delegate.h"
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;
28 WebContentsViewAndroid::WebContentsViewAndroid(
29 WebContentsImpl* web_contents,
30 WebContentsViewDelegate* delegate)
31 : web_contents_(web_contents),
32 content_view_core_(NULL),
36 WebContentsViewAndroid::~WebContentsViewAndroid() {
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());
45 rwhv->SetContentViewCore(content_view_core_);
47 if (web_contents_->ShowingInterstitialPage()) {
48 rwhv = static_cast<RenderWidgetHostViewAndroid*>(
49 static_cast<InterstitialPageImpl*>(
50 web_contents_->GetInterstitialPage())->
51 GetRenderViewHost()->GetView());
53 rwhv->SetContentViewCore(content_view_core_);
57 gfx::NativeView WebContentsViewAndroid::GetNativeView() const {
58 return content_view_core_ ? content_view_core_->GetViewAndroid() : NULL;
61 gfx::NativeView WebContentsViewAndroid::GetContentNativeView() const {
62 return content_view_core_ ? content_view_core_->GetViewAndroid() : NULL;
65 gfx::NativeWindow WebContentsViewAndroid::GetTopLevelNativeWindow() const {
66 return content_view_core_ ? content_view_core_->GetWindowAndroid() : NULL;
69 void WebContentsViewAndroid::GetContainerBounds(gfx::Rect* out) const {
70 *out = content_view_core_ ? gfx::Rect(content_view_core_->GetViewSize())
74 void WebContentsViewAndroid::SetPageTitle(const base::string16& title) {
75 if (content_view_core_)
76 content_view_core_->SetTitle(title);
79 void WebContentsViewAndroid::OnTabCrashed(base::TerminationStatus status,
81 RenderViewHostImpl* rvh = static_cast<RenderViewHostImpl*>(
82 web_contents_->GetRenderViewHost());
83 if (rvh->media_player_manager())
84 rvh->media_player_manager()->DestroyAllMediaPlayers();
85 if (content_view_core_)
86 content_view_core_->OnTabCrashed();
89 void WebContentsViewAndroid::SizeContents(const gfx::Size& size) {
90 // TODO(klobag): Do we need to do anything else?
91 RenderWidgetHostView* rwhv = web_contents_->GetRenderWidgetHostView();
96 void WebContentsViewAndroid::Focus() {
97 if (web_contents_->ShowingInterstitialPage())
98 web_contents_->GetInterstitialPage()->Focus();
100 web_contents_->GetRenderWidgetHostView()->Focus();
103 void WebContentsViewAndroid::SetInitialFocus() {
104 if (web_contents_->FocusLocationBarByDefault())
105 web_contents_->SetFocusToLocationBar(false);
110 void WebContentsViewAndroid::StoreFocus() {
114 void WebContentsViewAndroid::RestoreFocus() {
118 DropData* WebContentsViewAndroid::GetDropData() const {
123 gfx::Rect WebContentsViewAndroid::GetViewBounds() const {
124 if (content_view_core_)
125 return gfx::Rect(content_view_core_->GetViewSize());
130 void WebContentsViewAndroid::CreateView(
131 const gfx::Size& initial_size, gfx::NativeView context) {
134 RenderWidgetHostView* WebContentsViewAndroid::CreateViewForWidget(
135 RenderWidgetHost* render_widget_host) {
136 if (render_widget_host->GetView()) {
137 // During testing, the view will already be set up in most cases to the
138 // test view, so we don't want to clobber it with a real one. To verify that
139 // this actually is happening (and somebody isn't accidentally creating the
140 // view twice), we check for the RVH Factory, which will be set when we're
141 // making special ones (which go along with the special views).
142 DCHECK(RenderViewHostFactory::has_factory());
143 return render_widget_host->GetView();
145 // Note that while this instructs the render widget host to reference
146 // |native_view_|, this has no effect without also instructing the
147 // native view (i.e. ContentView) how to obtain a reference to this widget in
148 // order to paint it. See ContentView::GetRenderWidgetHostViewAndroid for an
149 // example of how this is achieved for InterstitialPages.
150 RenderWidgetHostImpl* rwhi = RenderWidgetHostImpl::From(render_widget_host);
151 RenderWidgetHostView* view = new RenderWidgetHostViewAndroid(
152 rwhi, content_view_core_);
156 RenderWidgetHostView* WebContentsViewAndroid::CreateViewForPopupWidget(
157 RenderWidgetHost* render_widget_host) {
158 return RenderWidgetHostViewPort::CreateViewForWidget(render_widget_host);
161 void WebContentsViewAndroid::RenderViewCreated(RenderViewHost* host) {
164 void WebContentsViewAndroid::RenderViewSwappedIn(RenderViewHost* host) {
167 void WebContentsViewAndroid::SetOverscrollControllerEnabled(bool enabled) {
170 void WebContentsViewAndroid::ShowContextMenu(
171 RenderFrameHost* render_frame_host, const ContextMenuParams& params) {
173 delegate_->ShowContextMenu(render_frame_host, params);
176 void WebContentsViewAndroid::ShowPopupMenu(
177 const gfx::Rect& bounds,
179 double item_font_size,
181 const std::vector<MenuItem>& items,
183 bool allow_multiple_selection) {
184 if (content_view_core_) {
185 content_view_core_->ShowSelectPopupMenu(
186 items, selected_item, allow_multiple_selection);
190 void WebContentsViewAndroid::HidePopupMenu() {
191 // TODO(tkent): implement
194 void WebContentsViewAndroid::StartDragging(
195 const DropData& drop_data,
196 blink::WebDragOperationsMask allowed_ops,
197 const gfx::ImageSkia& image,
198 const gfx::Vector2d& image_offset,
199 const DragEventSourceInfo& event_info) {
203 void WebContentsViewAndroid::UpdateDragCursor(blink::WebDragOperation op) {
207 void WebContentsViewAndroid::GotFocus() {
208 // This is only used in the views FocusManager stuff but it bleeds through
209 // all subclasses. http://crbug.com/21875
212 // This is called when we the renderer asks us to take focus back (i.e., it has
213 // iterated past the last focusable element on the page).
214 void WebContentsViewAndroid::TakeFocus(bool reverse) {
215 if (web_contents_->GetDelegate() &&
216 web_contents_->GetDelegate()->TakeFocus(web_contents_, reverse))
218 web_contents_->GetRenderWidgetHostView()->Focus();
221 } // namespace content