OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/web_contents/web_contents_view_aura.h" | 5 #include "content/browser/web_contents/web_contents_view_aura.h" |
6 | 6 |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "content/browser/renderer_host/dip_util.h" | 8 #include "content/browser/renderer_host/dip_util.h" |
9 #include "content/browser/renderer_host/render_view_host_factory.h" | 9 #include "content/browser/renderer_host/render_view_host_factory.h" |
10 #include "content/browser/web_contents/interstitial_page_impl.h" | 10 #include "content/browser/web_contents/interstitial_page_impl.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 | 37 |
38 namespace content { | 38 namespace content { |
39 WebContentsView* CreateWebContentsView( | 39 WebContentsView* CreateWebContentsView( |
40 WebContentsImpl* web_contents, | 40 WebContentsImpl* web_contents, |
41 WebContentsViewDelegate* delegate, | 41 WebContentsViewDelegate* delegate, |
42 RenderViewHostDelegateView** render_view_host_delegate_view) { | 42 RenderViewHostDelegateView** render_view_host_delegate_view) { |
43 WebContentsViewAura* rv = new WebContentsViewAura(web_contents, delegate); | 43 WebContentsViewAura* rv = new WebContentsViewAura(web_contents, delegate); |
44 *render_view_host_delegate_view = rv; | 44 *render_view_host_delegate_view = rv; |
45 return rv; | 45 return rv; |
46 } | 46 } |
47 } | |
48 | 47 |
49 namespace { | 48 namespace { |
50 | 49 |
51 // Listens to all mouse drag events during a drag and drop and sends them to | 50 // Listens to all mouse drag events during a drag and drop and sends them to |
52 // the renderer. | 51 // the renderer. |
53 class WebDragSourceAura : public MessageLoopForUI::Observer, | 52 class WebDragSourceAura : public MessageLoopForUI::Observer, |
54 public content::NotificationObserver { | 53 public NotificationObserver { |
55 public: | 54 public: |
56 WebDragSourceAura(aura::Window* window, WebContentsImpl* contents) | 55 WebDragSourceAura(aura::Window* window, WebContentsImpl* contents) |
57 : window_(window), | 56 : window_(window), |
58 contents_(contents) { | 57 contents_(contents) { |
59 MessageLoopForUI::current()->AddObserver(this); | 58 MessageLoopForUI::current()->AddObserver(this); |
60 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED, | 59 registrar_.Add(this, NOTIFICATION_WEB_CONTENTS_DISCONNECTED, |
61 content::Source<content::WebContents>(contents)); | 60 Source<WebContents>(contents)); |
62 } | 61 } |
63 | 62 |
64 virtual ~WebDragSourceAura() { | 63 virtual ~WebDragSourceAura() { |
65 MessageLoopForUI::current()->RemoveObserver(this); | 64 MessageLoopForUI::current()->RemoveObserver(this); |
66 } | 65 } |
67 | 66 |
68 // MessageLoop::Observer implementation: | 67 // MessageLoop::Observer implementation: |
69 virtual base::EventStatus WillProcessEvent( | 68 virtual base::EventStatus WillProcessEvent( |
70 const base::NativeEvent& event) OVERRIDE { | 69 const base::NativeEvent& event) OVERRIDE { |
71 return base::EVENT_CONTINUE; | 70 return base::EVENT_CONTINUE; |
72 } | 71 } |
73 virtual void DidProcessEvent(const base::NativeEvent& event) OVERRIDE { | 72 virtual void DidProcessEvent(const base::NativeEvent& event) OVERRIDE { |
74 if (!contents_) | 73 if (!contents_) |
75 return; | 74 return; |
76 ui::EventType type = ui::EventTypeFromNative(event); | 75 ui::EventType type = ui::EventTypeFromNative(event); |
77 content::RenderViewHost* rvh = NULL; | 76 RenderViewHost* rvh = NULL; |
78 switch (type) { | 77 switch (type) { |
79 case ui::ET_MOUSE_DRAGGED: | 78 case ui::ET_MOUSE_DRAGGED: |
80 rvh = contents_->GetRenderViewHost(); | 79 rvh = contents_->GetRenderViewHost(); |
81 if (rvh) { | 80 if (rvh) { |
82 gfx::Point screen_loc_in_pixel = ui::EventLocationFromNative(event); | 81 gfx::Point screen_loc_in_pixel = ui::EventLocationFromNative(event); |
83 gfx::Point screen_loc = content::ConvertPointToDIP(rvh->GetView(), | 82 gfx::Point screen_loc = ConvertPointToDIP(rvh->GetView(), |
84 screen_loc_in_pixel); | 83 screen_loc_in_pixel); |
85 gfx::Point client_loc = screen_loc; | 84 gfx::Point client_loc = screen_loc; |
86 aura::Window* window = rvh->GetView()->GetNativeView(); | 85 aura::Window* window = rvh->GetView()->GetNativeView(); |
87 aura::Window::ConvertPointToTarget(window->GetRootWindow(), | 86 aura::Window::ConvertPointToTarget(window->GetRootWindow(), |
88 window, &client_loc); | 87 window, &client_loc); |
89 rvh->DragSourceMovedTo(client_loc.x(), client_loc.y(), | 88 rvh->DragSourceMovedTo(client_loc.x(), client_loc.y(), |
90 screen_loc.x(), screen_loc.y()); | 89 screen_loc.x(), screen_loc.y()); |
91 } | 90 } |
92 break; | 91 break; |
93 default: | 92 default: |
94 break; | 93 break; |
95 } | 94 } |
96 } | 95 } |
97 | 96 |
98 virtual void Observe(int type, | 97 virtual void Observe(int type, |
99 const content::NotificationSource& source, | 98 const NotificationSource& source, |
100 const content::NotificationDetails& details) OVERRIDE { | 99 const NotificationDetails& details) OVERRIDE { |
101 if (type != content::NOTIFICATION_WEB_CONTENTS_DISCONNECTED) | 100 if (type != NOTIFICATION_WEB_CONTENTS_DISCONNECTED) |
102 return; | 101 return; |
103 | 102 |
104 // Cancel the drag if it is still in progress. | 103 // Cancel the drag if it is still in progress. |
105 aura::client::DragDropClient* dnd_client = | 104 aura::client::DragDropClient* dnd_client = |
106 aura::client::GetDragDropClient(window_->GetRootWindow()); | 105 aura::client::GetDragDropClient(window_->GetRootWindow()); |
107 if (dnd_client && dnd_client->IsDragDropInProgress()) | 106 if (dnd_client && dnd_client->IsDragDropInProgress()) |
108 dnd_client->DragCancel(); | 107 dnd_client->DragCancel(); |
109 | 108 |
110 window_ = NULL; | 109 window_ = NULL; |
111 contents_ = NULL; | 110 contents_ = NULL; |
112 } | 111 } |
113 | 112 |
114 aura::Window* window() const { return window_; } | 113 aura::Window* window() const { return window_; } |
115 | 114 |
116 private: | 115 private: |
117 aura::Window* window_; | 116 aura::Window* window_; |
118 WebContentsImpl* contents_; | 117 WebContentsImpl* contents_; |
119 content::NotificationRegistrar registrar_; | 118 NotificationRegistrar registrar_; |
120 | 119 |
121 DISALLOW_COPY_AND_ASSIGN(WebDragSourceAura); | 120 DISALLOW_COPY_AND_ASSIGN(WebDragSourceAura); |
122 }; | 121 }; |
123 | 122 |
124 // Utility to fill a ui::OSExchangeDataProviderAura object from WebDropData. | 123 // Utility to fill a ui::OSExchangeDataProviderAura object from WebDropData. |
125 void PrepareDragData(const WebDropData& drop_data, | 124 void PrepareDragData(const WebDropData& drop_data, |
126 ui::OSExchangeDataProviderAura* provider) { | 125 ui::OSExchangeDataProviderAura* provider) { |
127 if (!drop_data.text.string().empty()) | 126 if (!drop_data.text.string().empty()) |
128 provider->SetString(drop_data.text.string()); | 127 provider->SetString(drop_data.text.string()); |
129 if (drop_data.url.is_valid()) | 128 if (drop_data.url.is_valid()) |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 } | 229 } |
231 | 230 |
232 } // namespace | 231 } // namespace |
233 | 232 |
234 | 233 |
235 //////////////////////////////////////////////////////////////////////////////// | 234 //////////////////////////////////////////////////////////////////////////////// |
236 // WebContentsViewAura, public: | 235 // WebContentsViewAura, public: |
237 | 236 |
238 WebContentsViewAura::WebContentsViewAura( | 237 WebContentsViewAura::WebContentsViewAura( |
239 WebContentsImpl* web_contents, | 238 WebContentsImpl* web_contents, |
240 content::WebContentsViewDelegate* delegate) | 239 WebContentsViewDelegate* delegate) |
241 : web_contents_(web_contents), | 240 : web_contents_(web_contents), |
242 view_(NULL), | 241 view_(NULL), |
243 delegate_(delegate), | 242 delegate_(delegate), |
244 current_drag_op_(WebKit::WebDragOperationNone), | 243 current_drag_op_(WebKit::WebDragOperationNone), |
245 drag_dest_delegate_(NULL), | 244 drag_dest_delegate_(NULL), |
246 current_rvh_for_drag_(NULL) { | 245 current_rvh_for_drag_(NULL) { |
247 } | 246 } |
248 | 247 |
249 //////////////////////////////////////////////////////////////////////////////// | 248 //////////////////////////////////////////////////////////////////////////////// |
250 // WebContentsViewAura, private: | 249 // WebContentsViewAura, private: |
251 | 250 |
252 WebContentsViewAura::~WebContentsViewAura() { | 251 WebContentsViewAura::~WebContentsViewAura() { |
253 // Window needs a valid delegate during its destructor, so we explicitly | 252 // Window needs a valid delegate during its destructor, so we explicitly |
254 // delete it here. | 253 // delete it here. |
255 window_.reset(); | 254 window_.reset(); |
256 } | 255 } |
257 | 256 |
258 void WebContentsViewAura::SizeChangedCommon(const gfx::Size& size) { | 257 void WebContentsViewAura::SizeChangedCommon(const gfx::Size& size) { |
259 if (web_contents_->GetInterstitialPage()) | 258 if (web_contents_->GetInterstitialPage()) |
260 web_contents_->GetInterstitialPage()->SetSize(size); | 259 web_contents_->GetInterstitialPage()->SetSize(size); |
261 content::RenderWidgetHostView* rwhv = | 260 RenderWidgetHostView* rwhv = |
262 web_contents_->GetRenderWidgetHostView(); | 261 web_contents_->GetRenderWidgetHostView(); |
263 if (rwhv) | 262 if (rwhv) |
264 rwhv->SetSize(size); | 263 rwhv->SetSize(size); |
265 } | 264 } |
266 | 265 |
267 void WebContentsViewAura::EndDrag(WebKit::WebDragOperationsMask ops) { | 266 void WebContentsViewAura::EndDrag(WebKit::WebDragOperationsMask ops) { |
268 aura::RootWindow* root_window = GetNativeView()->GetRootWindow(); | 267 aura::RootWindow* root_window = GetNativeView()->GetRootWindow(); |
269 gfx::Point screen_loc = | 268 gfx::Point screen_loc = |
270 gfx::Screen::GetScreenFor(GetNativeView())->GetCursorScreenPoint(); | 269 gfx::Screen::GetScreenFor(GetNativeView())->GetCursorScreenPoint(); |
271 gfx::Point client_loc = screen_loc; | 270 gfx::Point client_loc = screen_loc; |
272 content::RenderViewHost* rvh = web_contents_->GetRenderViewHost(); | 271 RenderViewHost* rvh = web_contents_->GetRenderViewHost(); |
273 aura::Window* window = rvh->GetView()->GetNativeView(); | 272 aura::Window* window = rvh->GetView()->GetNativeView(); |
274 aura::Window::ConvertPointToTarget(root_window, window, &client_loc); | 273 aura::Window::ConvertPointToTarget(root_window, window, &client_loc); |
275 rvh->DragSourceEndedAt(client_loc.x(), client_loc.y(), screen_loc.x(), | 274 rvh->DragSourceEndedAt(client_loc.x(), client_loc.y(), screen_loc.x(), |
276 screen_loc.y(), ops); | 275 screen_loc.y(), ops); |
277 } | 276 } |
278 | 277 |
279 //////////////////////////////////////////////////////////////////////////////// | 278 //////////////////////////////////////////////////////////////////////////////// |
280 // WebContentsViewAura, WebContentsView implementation: | 279 // WebContentsViewAura, WebContentsView implementation: |
281 | 280 |
282 void WebContentsViewAura::CreateView(const gfx::Size& initial_size) { | 281 void WebContentsViewAura::CreateView(const gfx::Size& initial_size) { |
(...skipping 10 matching lines...) Expand all Loading... |
293 window_->layer()->SetMasksToBounds(true); | 292 window_->layer()->SetMasksToBounds(true); |
294 window_->SetName("WebContentsViewAura"); | 293 window_->SetName("WebContentsViewAura"); |
295 | 294 |
296 // delegate_->GetDragDestDelegate() creates a new delegate on every call. | 295 // delegate_->GetDragDestDelegate() creates a new delegate on every call. |
297 // Hence, we save a reference to it locally. Similar model is used on other | 296 // Hence, we save a reference to it locally. Similar model is used on other |
298 // platforms as well. | 297 // platforms as well. |
299 if (delegate_.get()) | 298 if (delegate_.get()) |
300 drag_dest_delegate_ = delegate_->GetDragDestDelegate(); | 299 drag_dest_delegate_ = delegate_->GetDragDestDelegate(); |
301 } | 300 } |
302 | 301 |
303 content::RenderWidgetHostView* WebContentsViewAura::CreateViewForWidget( | 302 RenderWidgetHostView* WebContentsViewAura::CreateViewForWidget( |
304 content::RenderWidgetHost* render_widget_host) { | 303 RenderWidgetHost* render_widget_host) { |
305 if (render_widget_host->GetView()) { | 304 if (render_widget_host->GetView()) { |
306 // During testing, the view will already be set up in most cases to the | 305 // During testing, the view will already be set up in most cases to the |
307 // test view, so we don't want to clobber it with a real one. To verify that | 306 // test view, so we don't want to clobber it with a real one. To verify that |
308 // this actually is happening (and somebody isn't accidentally creating the | 307 // this actually is happening (and somebody isn't accidentally creating the |
309 // view twice), we check for the RVH Factory, which will be set when we're | 308 // view twice), we check for the RVH Factory, which will be set when we're |
310 // making special ones (which go along with the special views). | 309 // making special ones (which go along with the special views). |
311 DCHECK(RenderViewHostFactory::has_factory()); | 310 DCHECK(RenderViewHostFactory::has_factory()); |
312 return render_widget_host->GetView(); | 311 return render_widget_host->GetView(); |
313 } | 312 } |
314 | 313 |
315 view_ = content::RenderWidgetHostView::CreateViewForWidget( | 314 view_ = RenderWidgetHostView::CreateViewForWidget( |
316 render_widget_host); | 315 render_widget_host); |
317 view_->InitAsChild(NULL); | 316 view_->InitAsChild(NULL); |
318 GetNativeView()->AddChild(view_->GetNativeView()); | 317 GetNativeView()->AddChild(view_->GetNativeView()); |
319 view_->Show(); | 318 view_->Show(); |
320 | 319 |
321 // We listen to drag drop events in the newly created view's window. | 320 // We listen to drag drop events in the newly created view's window. |
322 aura::client::SetDragDropDelegate(view_->GetNativeView(), this); | 321 aura::client::SetDragDropDelegate(view_->GetNativeView(), this); |
323 return view_; | 322 return view_; |
324 } | 323 } |
325 | 324 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 if (bounds.size() != size) { | 356 if (bounds.size() != size) { |
358 bounds.set_size(size); | 357 bounds.set_size(size); |
359 window_->SetBounds(bounds); | 358 window_->SetBounds(bounds); |
360 } else { | 359 } else { |
361 // Our size matches what we want but the renderers size may not match. | 360 // Our size matches what we want but the renderers size may not match. |
362 // Pretend we were resized so that the renderers size is updated too. | 361 // Pretend we were resized so that the renderers size is updated too. |
363 SizeChangedCommon(size); | 362 SizeChangedCommon(size); |
364 } | 363 } |
365 } | 364 } |
366 | 365 |
367 void WebContentsViewAura::RenderViewCreated(content::RenderViewHost* host) { | 366 void WebContentsViewAura::RenderViewCreated(RenderViewHost* host) { |
368 } | 367 } |
369 | 368 |
370 void WebContentsViewAura::Focus() { | 369 void WebContentsViewAura::Focus() { |
371 if (web_contents_->GetInterstitialPage()) { | 370 if (web_contents_->GetInterstitialPage()) { |
372 web_contents_->GetInterstitialPage()->Focus(); | 371 web_contents_->GetInterstitialPage()->Focus(); |
373 return; | 372 return; |
374 } | 373 } |
375 | 374 |
376 if (delegate_.get() && delegate_->Focus()) | 375 if (delegate_.get() && delegate_->Focus()) |
377 return; | 376 return; |
378 | 377 |
379 content::RenderWidgetHostView* rwhv = | 378 RenderWidgetHostView* rwhv = web_contents_->GetRenderWidgetHostView(); |
380 web_contents_->GetRenderWidgetHostView(); | |
381 if (rwhv) | 379 if (rwhv) |
382 rwhv->Focus(); | 380 rwhv->Focus(); |
383 } | 381 } |
384 | 382 |
385 void WebContentsViewAura::SetInitialFocus() { | 383 void WebContentsViewAura::SetInitialFocus() { |
386 if (web_contents_->FocusLocationBarByDefault()) | 384 if (web_contents_->FocusLocationBarByDefault()) |
387 web_contents_->SetFocusToLocationBar(false); | 385 web_contents_->SetFocusToLocationBar(false); |
388 else | 386 else |
389 Focus(); | 387 Focus(); |
390 } | 388 } |
(...skipping 20 matching lines...) Expand all Loading... |
411 } | 409 } |
412 | 410 |
413 gfx::Rect WebContentsViewAura::GetViewBounds() const { | 411 gfx::Rect WebContentsViewAura::GetViewBounds() const { |
414 return window_->GetBoundsInRootWindow(); | 412 return window_->GetBoundsInRootWindow(); |
415 } | 413 } |
416 | 414 |
417 //////////////////////////////////////////////////////////////////////////////// | 415 //////////////////////////////////////////////////////////////////////////////// |
418 // WebContentsViewAura, RenderViewHostDelegateView implementation: | 416 // WebContentsViewAura, RenderViewHostDelegateView implementation: |
419 | 417 |
420 void WebContentsViewAura::ShowContextMenu( | 418 void WebContentsViewAura::ShowContextMenu( |
421 const content::ContextMenuParams& params, | 419 const ContextMenuParams& params, |
422 content::ContextMenuSourceType type) { | 420 ContextMenuSourceType type) { |
423 if (delegate_.get()) | 421 if (delegate_.get()) |
424 delegate_->ShowContextMenu(params, type); | 422 delegate_->ShowContextMenu(params, type); |
425 } | 423 } |
426 | 424 |
427 void WebContentsViewAura::ShowPopupMenu(const gfx::Rect& bounds, | 425 void WebContentsViewAura::ShowPopupMenu(const gfx::Rect& bounds, |
428 int item_height, | 426 int item_height, |
429 double item_font_size, | 427 double item_font_size, |
430 int selected_item, | 428 int selected_item, |
431 const std::vector<WebMenuItem>& items, | 429 const std::vector<WebMenuItem>& items, |
432 bool right_aligned, | 430 bool right_aligned, |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
682 OnDragEntered(event); | 680 OnDragEntered(event); |
683 | 681 |
684 web_contents_->GetRenderViewHost()->DragTargetDrop( | 682 web_contents_->GetRenderViewHost()->DragTargetDrop( |
685 event.location(), | 683 event.location(), |
686 gfx::Screen::GetScreenFor(GetNativeView())->GetCursorScreenPoint(), | 684 gfx::Screen::GetScreenFor(GetNativeView())->GetCursorScreenPoint(), |
687 ConvertAuraEventFlagsToWebInputEventModifiers(event.flags())); | 685 ConvertAuraEventFlagsToWebInputEventModifiers(event.flags())); |
688 if (drag_dest_delegate_) | 686 if (drag_dest_delegate_) |
689 drag_dest_delegate_->OnDrop(); | 687 drag_dest_delegate_->OnDrop(); |
690 return current_drag_op_; | 688 return current_drag_op_; |
691 } | 689 } |
| 690 |
| 691 } // namespace content |
OLD | NEW |