Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / content / browser / web_contents / web_contents_view_android.cc
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
17 namespace content {
18 WebContentsViewPort* 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
28 WebContentsViewAndroid::WebContentsViewAndroid(
29     WebContentsImpl* web_contents,
30     WebContentsViewDelegate* delegate)
31     : web_contents_(web_contents),
32       content_view_core_(NULL),
33       delegate_(delegate) {
34 }
35
36 WebContentsViewAndroid::~WebContentsViewAndroid() {
37 }
38
39 void 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 gfx::NativeView WebContentsViewAndroid::GetNativeView() const {
58   return content_view_core_ ? content_view_core_->GetViewAndroid() : NULL;
59 }
60
61 gfx::NativeView WebContentsViewAndroid::GetContentNativeView() const {
62   return content_view_core_ ? content_view_core_->GetViewAndroid() : NULL;
63 }
64
65 gfx::NativeWindow WebContentsViewAndroid::GetTopLevelNativeWindow() const {
66   return content_view_core_ ? content_view_core_->GetWindowAndroid() : NULL;
67 }
68
69 void WebContentsViewAndroid::GetContainerBounds(gfx::Rect* out) const {
70   *out = content_view_core_ ? gfx::Rect(content_view_core_->GetViewSize())
71                             : gfx::Rect();
72 }
73
74 void WebContentsViewAndroid::SetPageTitle(const base::string16& title) {
75   if (content_view_core_)
76     content_view_core_->SetTitle(title);
77 }
78
79 void 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
89 void 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
96 void WebContentsViewAndroid::Focus() {
97   if (web_contents_->ShowingInterstitialPage())
98     web_contents_->GetInterstitialPage()->Focus();
99   else
100     web_contents_->GetRenderWidgetHostView()->Focus();
101 }
102
103 void WebContentsViewAndroid::SetInitialFocus() {
104   if (web_contents_->FocusLocationBarByDefault())
105     web_contents_->SetFocusToLocationBar(false);
106   else
107     Focus();
108 }
109
110 void WebContentsViewAndroid::StoreFocus() {
111   NOTIMPLEMENTED();
112 }
113
114 void WebContentsViewAndroid::RestoreFocus() {
115   NOTIMPLEMENTED();
116 }
117
118 DropData* WebContentsViewAndroid::GetDropData() const {
119   NOTIMPLEMENTED();
120   return NULL;
121 }
122
123 gfx::Rect WebContentsViewAndroid::GetViewBounds() const {
124   if (content_view_core_)
125     return gfx::Rect(content_view_core_->GetViewSize());
126
127   return gfx::Rect();
128 }
129
130 void WebContentsViewAndroid::CreateView(
131     const gfx::Size& initial_size, gfx::NativeView context) {
132 }
133
134 RenderWidgetHostView* 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
156 RenderWidgetHostView* WebContentsViewAndroid::CreateViewForPopupWidget(
157     RenderWidgetHost* render_widget_host) {
158   return RenderWidgetHostViewPort::CreateViewForWidget(render_widget_host);
159 }
160
161 void WebContentsViewAndroid::RenderViewCreated(RenderViewHost* host) {
162 }
163
164 void WebContentsViewAndroid::RenderViewSwappedIn(RenderViewHost* host) {
165 }
166
167 void WebContentsViewAndroid::SetOverscrollControllerEnabled(bool enabled) {
168 }
169
170 void WebContentsViewAndroid::ShowContextMenu(
171     RenderFrameHost* render_frame_host, const ContextMenuParams& params) {
172   if (delegate_)
173     delegate_->ShowContextMenu(render_frame_host, params);
174 }
175
176 void 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
190 void WebContentsViewAndroid::HidePopupMenu() {
191   // TODO(tkent): implement
192 }
193
194 void WebContentsViewAndroid::StartDragging(
195     const DropData& drop_data,
196     blink::WebDragOperationsMask allowed_ops,
197     const gfx::ImageSkia& image,
198     const gfx::Vector2d& image_offset,
199     const DragEventSourceInfo& event_info) {
200   NOTIMPLEMENTED();
201 }
202
203 void WebContentsViewAndroid::UpdateDragCursor(blink::WebDragOperation op) {
204   NOTIMPLEMENTED();
205 }
206
207 void WebContentsViewAndroid::GotFocus() {
208   // This is only used in the views FocusManager stuff but it bleeds through
209   // all subclasses. http://crbug.com/21875
210 }
211
212 // This is called when we the renderer asks us to take focus back (i.e., it has
213 // iterated past the last focusable element on the page).
214 void WebContentsViewAndroid::TakeFocus(bool reverse) {
215   if (web_contents_->GetDelegate() &&
216       web_contents_->GetDelegate()->TakeFocus(web_contents_, reverse))
217     return;
218   web_contents_->GetRenderWidgetHostView()->Focus();
219 }
220
221 } // namespace content