OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/ui/views/tab_contents/native_tab_contents_view_aura.h" | 5 #include "chrome/browser/ui/views/tab_contents/native_tab_contents_view_aura.h" |
6 | 6 |
| 7 #include "base/event_types.h" |
| 8 #include "base/message_loop.h" |
7 #include "chrome/browser/ui/views/tab_contents/native_tab_contents_view_delegate
.h" | 9 #include "chrome/browser/ui/views/tab_contents/native_tab_contents_view_delegate
.h" |
| 10 #include "content/browser/renderer_host/render_view_host.h" |
8 #include "content/browser/renderer_host/render_widget_host_view_aura.h" | 11 #include "content/browser/renderer_host/render_widget_host_view_aura.h" |
9 #include "content/browser/tab_contents/tab_contents.h" | 12 #include "content/browser/tab_contents/tab_contents.h" |
10 #include "content/browser/tab_contents/tab_contents_view.h" | 13 #include "content/browser/tab_contents/tab_contents_view.h" |
| 14 #include "ui/aura/client/aura_constants.h" |
| 15 #include "ui/aura/client/drag_drop_client.h" |
| 16 #include "ui/aura/desktop.h" |
11 #include "ui/aura/event.h" | 17 #include "ui/aura/event.h" |
12 #include "ui/aura/window.h" | 18 #include "ui/aura/window.h" |
| 19 #include "ui/base/dragdrop/drag_drop_types.h" |
| 20 #include "ui/base/dragdrop/os_exchange_data.h" |
| 21 #include "ui/base/dragdrop/os_exchange_data_provider_aura.h" |
13 #include "ui/views/widget/widget.h" | 22 #include "ui/views/widget/widget.h" |
14 #include "views/views_delegate.h" | 23 #include "views/views_delegate.h" |
| 24 #include "webkit/glue/webdropdata.h" |
| 25 |
| 26 namespace { |
| 27 |
| 28 // Listens to all mouse drag events during a drag and drop and sends them to |
| 29 // the renderer. |
| 30 class WebDragSourceAura : public MessageLoopForUI::Observer { |
| 31 public: |
| 32 explicit WebDragSourceAura(NativeTabContentsViewAura* view) |
| 33 : view_(view) { |
| 34 MessageLoopForUI::current()->AddObserver(this); |
| 35 } |
| 36 |
| 37 virtual ~WebDragSourceAura() { |
| 38 MessageLoopForUI::current()->RemoveObserver(this); |
| 39 } |
| 40 |
| 41 // MessageLoop::Observer implementation: |
| 42 virtual base::EventStatus WillProcessEvent( |
| 43 const base::NativeEvent& event) OVERRIDE { |
| 44 return base::EVENT_CONTINUE; |
| 45 } |
| 46 virtual void DidProcessEvent(const base::NativeEvent& event) OVERRIDE { |
| 47 ui::EventType type = ui::EventTypeFromNative(event); |
| 48 RenderViewHost* rvh = NULL; |
| 49 switch (type) { |
| 50 case ui::ET_MOUSE_DRAGGED: |
| 51 rvh = view_->GetTabContents()->render_view_host(); |
| 52 if (rvh) { |
| 53 gfx::Point screen_loc = ui::EventLocationFromNative(event); |
| 54 gfx::Point client_loc = screen_loc; |
| 55 aura::Window* window = rvh->view()->GetNativeView(); |
| 56 aura::Window::ConvertPointToWindow(aura::Desktop::GetInstance(), |
| 57 window, &client_loc); |
| 58 rvh->DragSourceMovedTo(client_loc.x(), client_loc.y(), |
| 59 screen_loc.x(), screen_loc.y()); |
| 60 } |
| 61 break; |
| 62 default: |
| 63 break; |
| 64 } |
| 65 } |
| 66 |
| 67 private: |
| 68 NativeTabContentsViewAura* view_; |
| 69 |
| 70 DISALLOW_COPY_AND_ASSIGN(WebDragSourceAura); |
| 71 }; |
| 72 |
| 73 // Utility to fill a ui::OSExchangeDataProviderAura object from WebDropData. |
| 74 void PrepareDragData(const WebDropData& drop_data, |
| 75 ui::OSExchangeDataProviderAura* provider) { |
| 76 if (!drop_data.plain_text.empty()) |
| 77 provider->SetString(drop_data.plain_text); |
| 78 if (drop_data.url.is_valid()) |
| 79 provider->SetURL(drop_data.url, drop_data.url_title); |
| 80 // TODO(varunjain): support other formats. |
| 81 } |
| 82 |
| 83 // Utility to fill a WebDropData object from ui::OSExchangeData. |
| 84 void PrepareWebDropData(WebDropData* drop_data, |
| 85 const ui::OSExchangeData& data) { |
| 86 string16 plain_text, url_title; |
| 87 GURL url; |
| 88 data.GetString(&plain_text); |
| 89 if (!plain_text.empty()) |
| 90 drop_data->plain_text = plain_text; |
| 91 data.GetURLAndTitle(&url, &url_title); |
| 92 if (url.is_valid()) { |
| 93 drop_data->url = url; |
| 94 drop_data->url_title = url_title; |
| 95 } |
| 96 // TODO(varunjain): support other formats. |
| 97 } |
| 98 |
| 99 // Utilities to convert between WebKit::WebDragOperationsMask and |
| 100 // ui::DragDropTypes. |
| 101 int ConvertFromWeb(WebKit::WebDragOperationsMask ops) { |
| 102 int drag_op = ui::DragDropTypes::DRAG_NONE; |
| 103 if (ops & WebKit::WebDragOperationCopy) |
| 104 drag_op |= ui::DragDropTypes::DRAG_COPY; |
| 105 if (ops & WebKit::WebDragOperationMove) |
| 106 drag_op |= ui::DragDropTypes::DRAG_MOVE; |
| 107 if (ops & WebKit::WebDragOperationLink) |
| 108 drag_op |= ui::DragDropTypes::DRAG_LINK; |
| 109 return drag_op; |
| 110 } |
| 111 |
| 112 WebKit::WebDragOperationsMask ConvertToWeb(int drag_op) { |
| 113 int web_drag_op = WebKit::WebDragOperationNone; |
| 114 if (drag_op & ui::DragDropTypes::DRAG_COPY) |
| 115 web_drag_op |= WebKit::WebDragOperationCopy; |
| 116 if (drag_op & ui::DragDropTypes::DRAG_MOVE) |
| 117 web_drag_op |= WebKit::WebDragOperationMove; |
| 118 if (drag_op & ui::DragDropTypes::DRAG_LINK) |
| 119 web_drag_op |= WebKit::WebDragOperationLink; |
| 120 return (WebKit::WebDragOperationsMask) web_drag_op; |
| 121 } |
| 122 |
| 123 } // namespace |
15 | 124 |
16 //////////////////////////////////////////////////////////////////////////////// | 125 //////////////////////////////////////////////////////////////////////////////// |
17 // NativeTabContentsViewAura, public: | 126 // NativeTabContentsViewAura, public: |
18 | 127 |
19 NativeTabContentsViewAura::NativeTabContentsViewAura( | 128 NativeTabContentsViewAura::NativeTabContentsViewAura( |
20 internal::NativeTabContentsViewDelegate* delegate) | 129 internal::NativeTabContentsViewDelegate* delegate) |
21 : views::NativeWidgetAura(delegate->AsNativeWidgetDelegate()), | 130 : views::NativeWidgetAura(delegate->AsNativeWidgetDelegate()), |
22 delegate_(delegate) { | 131 delegate_(delegate), |
| 132 current_drag_op_(WebKit::WebDragOperationNone) { |
23 } | 133 } |
24 | 134 |
25 NativeTabContentsViewAura::~NativeTabContentsViewAura() { | 135 NativeTabContentsViewAura::~NativeTabContentsViewAura() { |
26 } | 136 } |
27 | 137 |
28 TabContents* NativeTabContentsViewAura::GetTabContents() const { | 138 TabContents* NativeTabContentsViewAura::GetTabContents() const { |
29 return delegate_->GetTabContents(); | 139 return delegate_->GetTabContents(); |
30 } | 140 } |
31 | 141 |
32 void NativeTabContentsViewAura::EndDragging() { | |
33 delegate_->OnNativeTabContentsViewDraggingEnded(); | |
34 // TODO(beng): | |
35 NOTIMPLEMENTED(); | |
36 } | |
37 | |
38 //////////////////////////////////////////////////////////////////////////////// | 142 //////////////////////////////////////////////////////////////////////////////// |
39 // NativeTabContentsViewAura, NativeTabContentsView implementation: | 143 // NativeTabContentsViewAura, NativeTabContentsView implementation: |
40 | 144 |
41 void NativeTabContentsViewAura::InitNativeTabContentsView() { | 145 void NativeTabContentsViewAura::InitNativeTabContentsView() { |
42 views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL); | 146 views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL); |
43 params.native_widget = this; | 147 params.native_widget = this; |
44 // We don't draw anything so we don't need a texture. | 148 // We don't draw anything so we don't need a texture. |
45 params.create_texture_for_layer = false; | 149 params.create_texture_for_layer = false; |
46 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 150 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
47 params.parent = NULL; | 151 params.parent = NULL; |
(...skipping 12 matching lines...) Expand all Loading... |
60 // Nothing to do. | 164 // Nothing to do. |
61 } | 165 } |
62 | 166 |
63 RenderWidgetHostView* NativeTabContentsViewAura::CreateRenderWidgetHostView( | 167 RenderWidgetHostView* NativeTabContentsViewAura::CreateRenderWidgetHostView( |
64 RenderWidgetHost* render_widget_host) { | 168 RenderWidgetHost* render_widget_host) { |
65 RenderWidgetHostViewAura* view = | 169 RenderWidgetHostViewAura* view = |
66 new RenderWidgetHostViewAura(render_widget_host); | 170 new RenderWidgetHostViewAura(render_widget_host); |
67 view->InitAsChild(); | 171 view->InitAsChild(); |
68 GetNativeView()->AddChild(view->GetNativeView()); | 172 GetNativeView()->AddChild(view->GetNativeView()); |
69 view->Show(); | 173 view->Show(); |
| 174 |
| 175 // We listen to drag drop events in the newly created view's window. |
| 176 aura::Window* window = static_cast<aura::Window*>(view->GetNativeView()); |
| 177 DCHECK(window); |
| 178 window->SetProperty(aura::kDragDropDelegateKey, |
| 179 static_cast<aura::WindowDragDropDelegate*>(this)); |
70 return view; | 180 return view; |
71 } | 181 } |
72 | 182 |
73 gfx::NativeWindow NativeTabContentsViewAura::GetTopLevelNativeWindow() const { | 183 gfx::NativeWindow NativeTabContentsViewAura::GetTopLevelNativeWindow() const { |
74 // TODO(beng): | 184 // TODO(beng): |
75 NOTIMPLEMENTED(); | 185 NOTIMPLEMENTED(); |
76 return NULL; | 186 return NULL; |
77 } | 187 } |
78 | 188 |
79 void NativeTabContentsViewAura::SetPageTitle(const string16& title) { | 189 void NativeTabContentsViewAura::SetPageTitle(const string16& title) { |
80 GetNativeView()->set_title(title); | 190 GetNativeView()->set_title(title); |
81 } | 191 } |
82 | 192 |
83 void NativeTabContentsViewAura::StartDragging(const WebDropData& drop_data, | 193 void NativeTabContentsViewAura::StartDragging(const WebDropData& drop_data, |
84 WebKit::WebDragOperationsMask ops, | 194 WebKit::WebDragOperationsMask ops, |
85 const SkBitmap& image, | 195 const SkBitmap& image, |
86 const gfx::Point& image_offset) { | 196 const gfx::Point& image_offset) { |
87 // TODO(beng): | 197 aura::DragDropClient* client = static_cast<aura::DragDropClient*>( |
88 NOTIMPLEMENTED(); | 198 aura::Desktop::GetInstance()->GetProperty( |
| 199 aura::kDesktopDragDropClientKey)); |
| 200 if (!client) |
| 201 return; |
| 202 |
| 203 ui::OSExchangeDataProviderAura* provider = new ui::OSExchangeDataProviderAura; |
| 204 PrepareDragData(drop_data, provider); |
| 205 if (!image.isNull()) |
| 206 provider->set_drag_image(image); |
| 207 ui::OSExchangeData data(provider); // takes ownership of |provider|. |
| 208 |
| 209 scoped_ptr<WebDragSourceAura> drag_source(new WebDragSourceAura(this)); |
| 210 |
| 211 // We need to enable recursive tasks on the message loop so we can get |
| 212 // updates while in the system DoDragDrop loop. |
| 213 bool old_state = MessageLoop::current()->NestableTasksAllowed(); |
| 214 MessageLoop::current()->SetNestableTasksAllowed(true); |
| 215 int result_op = client->StartDragAndDrop(data, ConvertFromWeb(ops)); |
| 216 MessageLoop::current()->SetNestableTasksAllowed(old_state); |
| 217 |
| 218 EndDrag(ConvertToWeb(result_op)); |
| 219 GetTabContents()->render_view_host()->DragSourceSystemDragEnded(); |
89 } | 220 } |
90 | 221 |
91 void NativeTabContentsViewAura::CancelDrag() { | 222 void NativeTabContentsViewAura::CancelDrag() { |
92 // TODO(beng): | 223 aura::DragDropClient* client = static_cast<aura::DragDropClient*>( |
93 NOTIMPLEMENTED(); | 224 aura::Desktop::GetInstance()->GetProperty( |
| 225 aura::kDesktopDragDropClientKey)); |
| 226 if (client) |
| 227 client->DragCancel(); |
94 } | 228 } |
95 | 229 |
96 bool NativeTabContentsViewAura::IsDoingDrag() const { | 230 bool NativeTabContentsViewAura::IsDoingDrag() const { |
97 // TODO(beng): | 231 aura::DragDropClient* client = static_cast<aura::DragDropClient*>( |
98 NOTIMPLEMENTED(); | 232 aura::Desktop::GetInstance()->GetProperty( |
| 233 aura::kDesktopDragDropClientKey)); |
| 234 if (client) |
| 235 return client->IsDragDropInProgress(); |
99 return false; | 236 return false; |
100 } | 237 } |
101 | 238 |
102 void NativeTabContentsViewAura::SetDragCursor( | 239 void NativeTabContentsViewAura::SetDragCursor( |
103 WebKit::WebDragOperation operation) { | 240 WebKit::WebDragOperation operation) { |
104 // TODO(beng): | 241 current_drag_op_ = operation; |
105 NOTIMPLEMENTED(); | |
106 } | 242 } |
107 | 243 |
108 views::NativeWidget* NativeTabContentsViewAura::AsNativeWidget() { | 244 views::NativeWidget* NativeTabContentsViewAura::AsNativeWidget() { |
109 return this; | 245 return this; |
110 } | 246 } |
111 | 247 |
112 //////////////////////////////////////////////////////////////////////////////// | 248 //////////////////////////////////////////////////////////////////////////////// |
113 // NativeTabContentsViewAura, views::NativeWidgetAura overrides: | 249 // NativeTabContentsViewAura, views::NativeWidgetAura overrides: |
114 | 250 |
115 void NativeTabContentsViewAura::OnBoundsChanged(const gfx::Rect& old_bounds, | 251 void NativeTabContentsViewAura::OnBoundsChanged(const gfx::Rect& old_bounds, |
(...skipping 13 matching lines...) Expand all Loading... |
129 break; | 265 break; |
130 default: | 266 default: |
131 // TODO(oshima): mouse wheel | 267 // TODO(oshima): mouse wheel |
132 break; | 268 break; |
133 } | 269 } |
134 } | 270 } |
135 // Pass all mouse event to renderer. | 271 // Pass all mouse event to renderer. |
136 return views::NativeWidgetAura::OnMouseEvent(event); | 272 return views::NativeWidgetAura::OnMouseEvent(event); |
137 } | 273 } |
138 | 274 |
| 275 void NativeTabContentsViewAura::OnDragEntered( |
| 276 const aura::DropTargetEvent& event) { |
| 277 WebDropData drop_data; |
| 278 PrepareWebDropData(&drop_data, event.data()); |
| 279 WebKit::WebDragOperationsMask op = ConvertToWeb(event.source_operations()); |
| 280 |
| 281 gfx::Point screen_pt = aura::Desktop::GetInstance()->last_mouse_location(); |
| 282 GetTabContents()->render_view_host()->DragTargetDragEnter( |
| 283 drop_data, event.location(), screen_pt, op); |
| 284 } |
| 285 |
| 286 int NativeTabContentsViewAura::OnDragUpdated( |
| 287 const aura::DropTargetEvent& event) { |
| 288 WebKit::WebDragOperationsMask op = ConvertToWeb(event.source_operations()); |
| 289 gfx::Point screen_pt = aura::Desktop::GetInstance()->last_mouse_location(); |
| 290 GetTabContents()->render_view_host()->DragTargetDragOver( |
| 291 event.location(), screen_pt, op); |
| 292 return ConvertFromWeb(current_drag_op_); |
| 293 } |
| 294 |
| 295 void NativeTabContentsViewAura::OnDragExited() { |
| 296 GetTabContents()->render_view_host()->DragTargetDragLeave(); |
| 297 } |
| 298 |
| 299 int NativeTabContentsViewAura::OnPerformDrop( |
| 300 const aura::DropTargetEvent& event) { |
| 301 GetTabContents()->render_view_host()->DragTargetDrop( |
| 302 event.location(), aura::Desktop::GetInstance()->last_mouse_location()); |
| 303 return current_drag_op_; |
| 304 } |
| 305 |
| 306 //////////////////////////////////////////////////////////////////////////////// |
| 307 // NativeTabContentsViewAura, private: |
| 308 |
| 309 void NativeTabContentsViewAura::EndDrag(WebKit::WebDragOperationsMask ops) { |
| 310 gfx::Point screen_loc = aura::Desktop::GetInstance()->last_mouse_location(); |
| 311 gfx::Point client_loc = screen_loc; |
| 312 RenderViewHost* rvh = GetTabContents()->render_view_host(); |
| 313 aura::Window* window = rvh->view()->GetNativeView(); |
| 314 aura::Window::ConvertPointToWindow(aura::Desktop::GetInstance(), |
| 315 window, &client_loc); |
| 316 rvh->DragSourceEndedAt(client_loc.x(), client_loc.y(), screen_loc.x(), |
| 317 screen_loc.y(), ops); |
| 318 } |
| 319 |
139 //////////////////////////////////////////////////////////////////////////////// | 320 //////////////////////////////////////////////////////////////////////////////// |
140 // NativeTabContentsView, public: | 321 // NativeTabContentsView, public: |
141 | 322 |
142 // static | 323 // static |
143 NativeTabContentsView* NativeTabContentsView::CreateNativeTabContentsView( | 324 NativeTabContentsView* NativeTabContentsView::CreateNativeTabContentsView( |
144 internal::NativeTabContentsViewDelegate* delegate) { | 325 internal::NativeTabContentsViewDelegate* delegate) { |
145 return new NativeTabContentsViewAura(delegate); | 326 return new NativeTabContentsViewAura(delegate); |
146 } | 327 } |
OLD | NEW |