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