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