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