Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / content / browser / web_contents / web_contents_view_guest.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_guest.h"
6
7 #include "build/build_config.h"
8 #include "content/browser/browser_plugin/browser_plugin_embedder.h"
9 #include "content/browser/browser_plugin/browser_plugin_guest.h"
10 #include "content/browser/frame_host/interstitial_page_impl.h"
11 #include "content/browser/frame_host/render_widget_host_view_guest.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/common/drag_messages.h"
16 #include "content/public/browser/user_metrics.h"
17 #include "content/public/browser/web_contents_delegate.h"
18 #include "content/public/common/context_menu_params.h"
19 #include "content/public/common/drop_data.h"
20 #include "ui/gfx/image/image_skia.h"
21 #include "ui/gfx/point.h"
22 #include "ui/gfx/rect.h"
23 #include "ui/gfx/size.h"
24
25 #if defined(USE_AURA)
26 #include "ui/aura/window.h"
27 #endif
28
29 using blink::WebDragOperation;
30 using blink::WebDragOperationsMask;
31
32 namespace content {
33
34 WebContentsViewGuest::WebContentsViewGuest(
35     WebContentsImpl* web_contents,
36     BrowserPluginGuest* guest,
37     scoped_ptr<WebContentsView> platform_view,
38     RenderViewHostDelegateView* platform_view_delegate_view)
39     : web_contents_(web_contents),
40       guest_(guest),
41       platform_view_(platform_view.Pass()),
42       platform_view_delegate_view_(platform_view_delegate_view) {
43 }
44
45 WebContentsViewGuest::~WebContentsViewGuest() {
46 }
47
48 gfx::NativeView WebContentsViewGuest::GetNativeView() const {
49   return platform_view_->GetNativeView();
50 }
51
52 gfx::NativeView WebContentsViewGuest::GetContentNativeView() const {
53   RenderWidgetHostView* rwhv = web_contents_->GetRenderWidgetHostView();
54   if (!rwhv)
55     return NULL;
56   return rwhv->GetNativeView();
57 }
58
59 gfx::NativeWindow WebContentsViewGuest::GetTopLevelNativeWindow() const {
60   return guest_->embedder_web_contents()->GetTopLevelNativeWindow();
61 }
62
63 void WebContentsViewGuest::OnGuestInitialized(WebContentsView* parent_view) {
64 #if defined(USE_AURA)
65   // In aura, ScreenPositionClient doesn't work properly if we do
66   // not have the native view associated with this WebContentsViewGuest in the
67   // view hierarchy. We add this view as embedder's child here.
68   // This would go in WebContentsViewGuest::CreateView, but that is too early to
69   // access embedder_web_contents(). Therefore, we do it here.
70   parent_view->GetNativeView()->AddChild(platform_view_->GetNativeView());
71 #endif  // defined(USE_AURA)
72 }
73
74 ContextMenuParams WebContentsViewGuest::ConvertContextMenuParams(
75     const ContextMenuParams& params) const {
76 #if defined(USE_AURA)
77   // Context menu uses ScreenPositionClient::ConvertPointToScreen() in aura
78   // to calculate popup position. Guest's native view
79   // (platform_view_->GetNativeView()) is part of the embedder's view hierarchy,
80   // but is placed at (0, 0) w.r.t. the embedder's position. Therefore, |offset|
81   // is added to |params|.
82   gfx::Rect embedder_bounds;
83   guest_->embedder_web_contents()->GetView()->GetContainerBounds(
84       &embedder_bounds);
85   gfx::Rect guest_bounds;
86   GetContainerBounds(&guest_bounds);
87
88   gfx::Vector2d offset = guest_bounds.origin() - embedder_bounds.origin();
89   ContextMenuParams params_in_embedder = params;
90   params_in_embedder.x += offset.x();
91   params_in_embedder.y += offset.y();
92   return params_in_embedder;
93 #else
94   return params;
95 #endif
96 }
97
98 void WebContentsViewGuest::GetContainerBounds(gfx::Rect* out) const {
99   // We need embedder container's bounds to calculate our bounds.
100   guest_->embedder_web_contents()->GetView()->GetContainerBounds(out);
101   gfx::Point guest_coordinates = guest_->GetScreenCoordinates(gfx::Point());
102   out->Offset(guest_coordinates.x(), guest_coordinates.y());
103   out->set_size(size_);
104 }
105
106 void WebContentsViewGuest::SizeContents(const gfx::Size& size) {
107   size_ = size;
108   RenderWidgetHostView* rwhv = web_contents_->GetRenderWidgetHostView();
109   if (rwhv)
110     rwhv->SetSize(size);
111 }
112
113 void WebContentsViewGuest::SetInitialFocus() {
114   platform_view_->SetInitialFocus();
115 }
116
117 gfx::Rect WebContentsViewGuest::GetViewBounds() const {
118   return gfx::Rect(size_);
119 }
120
121 #if defined(OS_MACOSX)
122 void WebContentsViewGuest::SetAllowOverlappingViews(bool overlapping) {
123   platform_view_->SetAllowOverlappingViews(overlapping);
124 }
125
126 bool WebContentsViewGuest::GetAllowOverlappingViews() const {
127   return platform_view_->GetAllowOverlappingViews();
128 }
129
130 void WebContentsViewGuest::SetAllowOtherViews(bool allow) {
131   platform_view_->SetAllowOtherViews(allow);
132 }
133
134 bool WebContentsViewGuest::GetAllowOtherViews() const {
135   return platform_view_->GetAllowOtherViews();
136 }
137 #endif
138
139 void WebContentsViewGuest::CreateView(const gfx::Size& initial_size,
140                                       gfx::NativeView context) {
141   platform_view_->CreateView(initial_size, context);
142   size_ = initial_size;
143 }
144
145 RenderWidgetHostViewBase* WebContentsViewGuest::CreateViewForWidget(
146     RenderWidgetHost* render_widget_host) {
147   if (render_widget_host->GetView()) {
148     // During testing, the view will already be set up in most cases to the
149     // test view, so we don't want to clobber it with a real one. To verify that
150     // this actually is happening (and somebody isn't accidentally creating the
151     // view twice), we check for the RVH Factory, which will be set when we're
152     // making special ones (which go along with the special views).
153     DCHECK(RenderViewHostFactory::has_factory());
154     return static_cast<RenderWidgetHostViewBase*>(
155         render_widget_host->GetView());
156   }
157
158   RenderWidgetHostViewBase* platform_widget =
159       platform_view_->CreateViewForWidget(render_widget_host);
160
161   RenderWidgetHostViewBase* view = new RenderWidgetHostViewGuest(
162       render_widget_host,
163       guest_,
164       platform_widget);
165
166   return view;
167 }
168
169 RenderWidgetHostViewBase* WebContentsViewGuest::CreateViewForPopupWidget(
170     RenderWidgetHost* render_widget_host) {
171   return platform_view_->CreateViewForPopupWidget(render_widget_host);
172 }
173
174 void WebContentsViewGuest::SetPageTitle(const base::string16& title) {
175 }
176
177 void WebContentsViewGuest::RenderViewCreated(RenderViewHost* host) {
178   platform_view_->RenderViewCreated(host);
179 }
180
181 void WebContentsViewGuest::RenderViewSwappedIn(RenderViewHost* host) {
182   platform_view_->RenderViewSwappedIn(host);
183 }
184
185 void WebContentsViewGuest::SetOverscrollControllerEnabled(bool enabled) {
186   // This should never override the setting of the embedder view.
187 }
188
189 #if defined(OS_MACOSX)
190 bool WebContentsViewGuest::IsEventTracking() const {
191   return false;
192 }
193
194 void WebContentsViewGuest::CloseTabAfterEventTracking() {
195 }
196 #endif
197
198 WebContents* WebContentsViewGuest::web_contents() {
199   return web_contents_;
200 }
201
202 void WebContentsViewGuest::RestoreFocus() {
203   platform_view_->RestoreFocus();
204 }
205
206 void WebContentsViewGuest::Focus() {
207   platform_view_->Focus();
208 }
209
210 void WebContentsViewGuest::StoreFocus() {
211   platform_view_->StoreFocus();
212 }
213
214 DropData* WebContentsViewGuest::GetDropData() const {
215   NOTIMPLEMENTED();
216   return NULL;
217 }
218
219 void WebContentsViewGuest::UpdateDragCursor(WebDragOperation operation) {
220   RenderViewHostImpl* embedder_render_view_host =
221       static_cast<RenderViewHostImpl*>(
222           guest_->embedder_web_contents()->GetRenderViewHost());
223   CHECK(embedder_render_view_host);
224   RenderViewHostDelegateView* view =
225       embedder_render_view_host->GetDelegate()->GetDelegateView();
226   if (view)
227     view->UpdateDragCursor(operation);
228 }
229
230 void WebContentsViewGuest::GotFocus() {
231 }
232
233 void WebContentsViewGuest::TakeFocus(bool reverse) {
234 }
235
236 void WebContentsViewGuest::ShowContextMenu(RenderFrameHost* render_frame_host,
237                                            const ContextMenuParams& params) {
238   platform_view_delegate_view_->ShowContextMenu(
239       render_frame_host, ConvertContextMenuParams(params));
240 }
241
242 void WebContentsViewGuest::StartDragging(
243     const DropData& drop_data,
244     WebDragOperationsMask ops,
245     const gfx::ImageSkia& image,
246     const gfx::Vector2d& image_offset,
247     const DragEventSourceInfo& event_info) {
248   WebContentsImpl* embedder_web_contents = guest_->embedder_web_contents();
249   embedder_web_contents->GetBrowserPluginEmbedder()->StartDrag(guest_);
250   RenderViewHostImpl* embedder_render_view_host =
251       static_cast<RenderViewHostImpl*>(
252           embedder_web_contents->GetRenderViewHost());
253   CHECK(embedder_render_view_host);
254   RenderViewHostDelegateView* view =
255       embedder_render_view_host->GetDelegate()->GetDelegateView();
256   if (view) {
257     RecordAction(base::UserMetricsAction("BrowserPlugin.Guest.StartDrag"));
258     view->StartDragging(drop_data, ops, image, image_offset, event_info);
259   } else {
260     embedder_web_contents->SystemDragEnded();
261   }
262 }
263
264 }  // namespace content