web_contents_view_android.cc revision b2df76ea8fec9e32f6f3718986dba0d95315b29c
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::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
65gfx::NativeView WebContentsViewAndroid::GetNativeView() const {
66  return content_view_core_->GetViewAndroid();
67}
68
69gfx::NativeView WebContentsViewAndroid::GetContentNativeView() const {
70  return content_view_core_->GetViewAndroid();
71}
72
73gfx::NativeWindow WebContentsViewAndroid::GetTopLevelNativeWindow() const {
74  return content_view_core_->GetWindowAndroid();
75}
76
77void WebContentsViewAndroid::GetContainerBounds(gfx::Rect* out) const {
78  RenderWidgetHostView* rwhv = web_contents_->GetRenderWidgetHostView();
79  if (rwhv)
80    *out = rwhv->GetViewBounds();
81}
82
83void WebContentsViewAndroid::SetPageTitle(const string16& title) {
84  if (content_view_core_)
85    content_view_core_->SetTitle(title);
86}
87
88void 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
98void 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
105void WebContentsViewAndroid::Focus() {
106  if (web_contents_->ShowingInterstitialPage())
107    web_contents_->GetInterstitialPage()->Focus();
108  else
109    web_contents_->GetRenderWidgetHostView()->Focus();
110}
111
112void WebContentsViewAndroid::SetInitialFocus() {
113  if (web_contents_->FocusLocationBarByDefault())
114    web_contents_->SetFocusToLocationBar(false);
115  else
116    Focus();
117}
118
119void WebContentsViewAndroid::StoreFocus() {
120  NOTIMPLEMENTED();
121}
122
123void WebContentsViewAndroid::RestoreFocus() {
124  NOTIMPLEMENTED();
125}
126
127WebDropData* WebContentsViewAndroid::GetDropData() const {
128  NOTIMPLEMENTED();
129  return NULL;
130}
131
132gfx::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
140void WebContentsViewAndroid::CreateView(
141    const gfx::Size& initial_size, gfx::NativeView context) {
142}
143
144RenderWidgetHostView* 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
166RenderWidgetHostView* WebContentsViewAndroid::CreateViewForPopupWidget(
167    RenderWidgetHost* render_widget_host) {
168  return RenderWidgetHostViewPort::CreateViewForWidget(render_widget_host);
169}
170
171void WebContentsViewAndroid::RenderViewCreated(RenderViewHost* host) {
172}
173
174void WebContentsViewAndroid::RenderViewSwappedIn(RenderViewHost* host) {
175}
176
177void WebContentsViewAndroid::SetOverscrollControllerEnabled(bool enabled) {
178}
179
180void WebContentsViewAndroid::ShowContextMenu(
181    const ContextMenuParams& params,
182    ContextMenuSourceType type) {
183  if (delegate_)
184    delegate_->ShowContextMenu(params, type);
185}
186
187void WebContentsViewAndroid::ShowPopupMenu(
188    const gfx::Rect& bounds,
189    int item_height,
190    double item_font_size,
191    int selected_item,
192    const std::vector<WebMenuItem>& items,
193    bool right_aligned,
194    bool allow_multiple_selection) {
195  if (content_view_core_) {
196    content_view_core_->ShowSelectPopupMenu(
197        items, selected_item, allow_multiple_selection);
198  }
199}
200
201void WebContentsViewAndroid::StartDragging(
202    const WebDropData& drop_data,
203    WebKit::WebDragOperationsMask allowed_ops,
204    const gfx::ImageSkia& image,
205    const gfx::Vector2d& image_offset,
206    const DragEventSourceInfo& event_info) {
207  NOTIMPLEMENTED();
208}
209
210void WebContentsViewAndroid::UpdateDragCursor(WebKit::WebDragOperation op) {
211  NOTIMPLEMENTED();
212}
213
214void WebContentsViewAndroid::GotFocus() {
215  // This is only used in the views FocusManager stuff but it bleeds through
216  // all subclasses. http://crbug.com/21875
217}
218
219// This is called when we the renderer asks us to take focus back (i.e., it has
220// iterated past the last focusable element on the page).
221void WebContentsViewAndroid::TakeFocus(bool reverse) {
222  if (web_contents_->GetDelegate() &&
223      web_contents_->GetDelegate()->TakeFocus(web_contents_, reverse))
224    return;
225  web_contents_->GetRenderWidgetHostView()->Focus();
226}
227
228} // namespace content
229