web_contents_view_guest.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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/renderer_host/render_view_host_factory.h"
11#include "content/browser/renderer_host/render_view_host_impl.h"
12#include "content/browser/renderer_host/render_widget_host_view_guest.h"
13#include "content/browser/web_contents/interstitial_page_impl.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
24using WebKit::WebDragOperation;
25using WebKit::WebDragOperationsMask;
26
27namespace content {
28
29WebContentsViewGuest::WebContentsViewGuest(
30    WebContentsImpl* web_contents,
31    BrowserPluginGuest* guest,
32    WebContentsViewPort* platform_view,
33    RenderViewHostDelegateView* platform_view_delegate_view)
34    : web_contents_(web_contents),
35      guest_(guest),
36      platform_view_(platform_view),
37      platform_view_delegate_view_(platform_view_delegate_view) {
38}
39
40WebContentsViewGuest::~WebContentsViewGuest() {
41}
42
43gfx::NativeView WebContentsViewGuest::GetNativeView() const {
44  return platform_view_->GetNativeView();
45}
46
47gfx::NativeView WebContentsViewGuest::GetContentNativeView() const {
48  RenderWidgetHostView* rwhv = web_contents_->GetRenderWidgetHostView();
49  if (!rwhv)
50    return NULL;
51  return rwhv->GetNativeView();
52}
53
54gfx::NativeWindow WebContentsViewGuest::GetTopLevelNativeWindow() const {
55  return guest_->embedder_web_contents()->GetView()->GetTopLevelNativeWindow();
56}
57
58void WebContentsViewGuest::GetContainerBounds(gfx::Rect* out) const {
59  // We need embedder container's bounds to calculate our bounds.
60  guest_->embedder_web_contents()->GetView()->GetContainerBounds(out);
61  gfx::Point guest_coordinates = guest_->GetScreenCoordinates(gfx::Point());
62  out->Offset(guest_coordinates.x(), guest_coordinates.y());
63  out->set_size(size_);
64}
65
66void WebContentsViewGuest::SizeContents(const gfx::Size& size) {
67  size_ = size;
68  RenderWidgetHostView* rwhv = web_contents_->GetRenderWidgetHostView();
69  if (rwhv)
70    rwhv->SetSize(size);
71}
72
73void WebContentsViewGuest::SetInitialFocus() {
74  platform_view_->SetInitialFocus();
75}
76
77gfx::Rect WebContentsViewGuest::GetViewBounds() const {
78  return gfx::Rect(size_);
79}
80
81#if defined(OS_MACOSX)
82void WebContentsViewGuest::SetAllowOverlappingViews(bool overlapping) {
83  platform_view_->SetAllowOverlappingViews(overlapping);
84}
85
86bool WebContentsViewGuest::GetAllowOverlappingViews() const {
87  return platform_view_->GetAllowOverlappingViews();
88}
89#endif
90
91void WebContentsViewGuest::CreateView(const gfx::Size& initial_size,
92                                      gfx::NativeView context) {
93  platform_view_->CreateView(initial_size, context);
94  size_ = initial_size;
95}
96
97RenderWidgetHostView* WebContentsViewGuest::CreateViewForWidget(
98    RenderWidgetHost* render_widget_host) {
99  if (render_widget_host->GetView()) {
100    // During testing, the view will already be set up in most cases to the
101    // test view, so we don't want to clobber it with a real one. To verify that
102    // this actually is happening (and somebody isn't accidentally creating the
103    // view twice), we check for the RVH Factory, which will be set when we're
104    // making special ones (which go along with the special views).
105    DCHECK(RenderViewHostFactory::has_factory());
106    return render_widget_host->GetView();
107  }
108
109  RenderWidgetHostView* platform_widget = NULL;
110  platform_widget = platform_view_->CreateViewForWidget(render_widget_host);
111
112  RenderWidgetHostView* view = new RenderWidgetHostViewGuest(
113      render_widget_host,
114      guest_,
115      platform_widget);
116
117  return view;
118}
119
120RenderWidgetHostView* WebContentsViewGuest::CreateViewForPopupWidget(
121    RenderWidgetHost* render_widget_host) {
122  return RenderWidgetHostViewPort::CreateViewForWidget(render_widget_host);
123}
124
125void WebContentsViewGuest::SetPageTitle(const string16& title) {
126}
127
128void WebContentsViewGuest::RenderViewCreated(RenderViewHost* host) {
129  platform_view_->RenderViewCreated(host);
130}
131
132void WebContentsViewGuest::RenderViewSwappedIn(RenderViewHost* host) {
133  platform_view_->RenderViewSwappedIn(host);
134}
135
136void WebContentsViewGuest::SetOverscrollControllerEnabled(bool enabled) {
137  // This should never override the setting of the embedder view.
138}
139
140#if defined(OS_MACOSX)
141bool WebContentsViewGuest::IsEventTracking() const {
142  return false;
143}
144
145void WebContentsViewGuest::CloseTabAfterEventTracking() {
146}
147#endif
148
149WebContents* WebContentsViewGuest::web_contents() {
150  return web_contents_;
151}
152
153void WebContentsViewGuest::RestoreFocus() {
154  platform_view_->RestoreFocus();
155}
156
157void WebContentsViewGuest::OnTabCrashed(base::TerminationStatus status,
158                                        int error_code) {
159}
160
161void WebContentsViewGuest::Focus() {
162  platform_view_->Focus();
163}
164
165void WebContentsViewGuest::StoreFocus() {
166  platform_view_->StoreFocus();
167}
168
169DropData* WebContentsViewGuest::GetDropData() const {
170  NOTIMPLEMENTED();
171  return NULL;
172}
173
174void WebContentsViewGuest::UpdateDragCursor(WebDragOperation operation) {
175  RenderViewHostImpl* embedder_render_view_host =
176      static_cast<RenderViewHostImpl*>(
177          guest_->embedder_web_contents()->GetRenderViewHost());
178  CHECK(embedder_render_view_host);
179  RenderViewHostDelegateView* view =
180      embedder_render_view_host->GetDelegate()->GetDelegateView();
181  if (view)
182    view->UpdateDragCursor(operation);
183}
184
185void WebContentsViewGuest::GotFocus() {
186}
187
188void WebContentsViewGuest::TakeFocus(bool reverse) {
189}
190
191void WebContentsViewGuest::ShowContextMenu(const ContextMenuParams& params) {
192  platform_view_delegate_view_->ShowContextMenu(params);
193}
194
195void WebContentsViewGuest::ShowPopupMenu(const gfx::Rect& bounds,
196                                         int item_height,
197                                         double item_font_size,
198                                         int selected_item,
199                                         const std::vector<WebMenuItem>& items,
200                                         bool right_aligned,
201                                         bool allow_multiple_selection) {
202  // External popup menus are only used on Mac and Android.
203  NOTIMPLEMENTED();
204}
205
206void WebContentsViewGuest::StartDragging(
207    const DropData& drop_data,
208    WebDragOperationsMask ops,
209    const gfx::ImageSkia& image,
210    const gfx::Vector2d& image_offset,
211    const DragEventSourceInfo& event_info) {
212  WebContentsImpl* embedder_web_contents = guest_->embedder_web_contents();
213  embedder_web_contents->GetBrowserPluginEmbedder()->StartDrag(guest_);
214  RenderViewHostImpl* embedder_render_view_host =
215      static_cast<RenderViewHostImpl*>(
216          embedder_web_contents->GetRenderViewHost());
217  CHECK(embedder_render_view_host);
218  RenderViewHostDelegateView* view =
219      embedder_render_view_host->GetDelegate()->GetDelegateView();
220  if (view)
221    view->StartDragging(drop_data, ops, image, image_offset, event_info);
222  else
223    embedder_web_contents->SystemDragEnded();
224}
225
226}  // namespace content
227