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