web_contents_view_android.cc revision 0529e5d033099cbfc42635f6f6183833b09dff6e
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/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"
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
57gfx::NativeView WebContentsViewAndroid::GetNativeView() const {
58  return content_view_core_ ? content_view_core_->GetViewAndroid() : NULL;
59}
60
61gfx::NativeView WebContentsViewAndroid::GetContentNativeView() const {
62  return content_view_core_ ? content_view_core_->GetViewAndroid() : NULL;
63}
64
65gfx::NativeWindow WebContentsViewAndroid::GetTopLevelNativeWindow() const {
66  return content_view_core_ ? content_view_core_->GetWindowAndroid() : NULL;
67}
68
69void WebContentsViewAndroid::GetContainerBounds(gfx::Rect* out) const {
70  *out = content_view_core_ ? gfx::Rect(content_view_core_->GetViewSize())
71                            : gfx::Rect();
72}
73
74void WebContentsViewAndroid::SetPageTitle(const base::string16& title) {
75  if (content_view_core_)
76    content_view_core_->SetTitle(title);
77}
78
79void WebContentsViewAndroid::OnTabCrashed(base::TerminationStatus status,
80                                          int error_code) {
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();
87}
88
89void WebContentsViewAndroid::SizeContents(const gfx::Size& size) {
90  // TODO(klobag): Do we need to do anything else?
91  RenderWidgetHostView* rwhv = web_contents_->GetRenderWidgetHostView();
92  if (rwhv)
93    rwhv->SetSize(size);
94}
95
96void WebContentsViewAndroid::Focus() {
97  if (web_contents_->ShowingInterstitialPage())
98    web_contents_->GetInterstitialPage()->Focus();
99  else
100    web_contents_->GetRenderWidgetHostView()->Focus();
101}
102
103void WebContentsViewAndroid::SetInitialFocus() {
104  if (web_contents_->FocusLocationBarByDefault())
105    web_contents_->SetFocusToLocationBar(false);
106  else
107    Focus();
108}
109
110void WebContentsViewAndroid::StoreFocus() {
111  NOTIMPLEMENTED();
112}
113
114void WebContentsViewAndroid::RestoreFocus() {
115  NOTIMPLEMENTED();
116}
117
118DropData* WebContentsViewAndroid::GetDropData() const {
119  NOTIMPLEMENTED();
120  return NULL;
121}
122
123gfx::Rect WebContentsViewAndroid::GetViewBounds() const {
124  if (content_view_core_)
125    return gfx::Rect(content_view_core_->GetViewSize());
126
127  return gfx::Rect();
128}
129
130void WebContentsViewAndroid::CreateView(
131    const gfx::Size& initial_size, gfx::NativeView context) {
132}
133
134RenderWidgetHostView* 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();
144  }
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_);
153  return view;
154}
155
156RenderWidgetHostView* WebContentsViewAndroid::CreateViewForPopupWidget(
157    RenderWidgetHost* render_widget_host) {
158  return RenderWidgetHostViewPort::CreateViewForWidget(render_widget_host);
159}
160
161void WebContentsViewAndroid::RenderViewCreated(RenderViewHost* host) {
162}
163
164void WebContentsViewAndroid::RenderViewSwappedIn(RenderViewHost* host) {
165}
166
167void WebContentsViewAndroid::SetOverscrollControllerEnabled(bool enabled) {
168}
169
170void WebContentsViewAndroid::ShowContextMenu(
171    RenderFrameHost* render_frame_host, const ContextMenuParams& params) {
172  if (delegate_)
173    delegate_->ShowContextMenu(render_frame_host, params);
174}
175
176void WebContentsViewAndroid::ShowPopupMenu(
177    const gfx::Rect& bounds,
178    int item_height,
179    double item_font_size,
180    int selected_item,
181    const std::vector<MenuItem>& items,
182    bool right_aligned,
183    bool allow_multiple_selection) {
184  if (content_view_core_) {
185    content_view_core_->ShowSelectPopupMenu(
186        items, selected_item, allow_multiple_selection);
187  }
188}
189
190void WebContentsViewAndroid::HidePopupMenu() {
191  if (content_view_core_)
192    content_view_core_->HideSelectPopupMenu();
193}
194
195void WebContentsViewAndroid::StartDragging(
196    const DropData& drop_data,
197    blink::WebDragOperationsMask allowed_ops,
198    const gfx::ImageSkia& image,
199    const gfx::Vector2d& image_offset,
200    const DragEventSourceInfo& event_info) {
201  NOTIMPLEMENTED();
202}
203
204void WebContentsViewAndroid::UpdateDragCursor(blink::WebDragOperation op) {
205  NOTIMPLEMENTED();
206}
207
208void WebContentsViewAndroid::GotFocus() {
209  // This is only used in the views FocusManager stuff but it bleeds through
210  // all subclasses. http://crbug.com/21875
211}
212
213// This is called when we the renderer asks us to take focus back (i.e., it has
214// iterated past the last focusable element on the page).
215void WebContentsViewAndroid::TakeFocus(bool reverse) {
216  if (web_contents_->GetDelegate() &&
217      web_contents_->GetDelegate()->TakeFocus(web_contents_, reverse))
218    return;
219  web_contents_->GetRenderWidgetHostView()->Focus();
220}
221
222} // namespace content
223