Chromium Code Reviews| 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 = static_cast<aura::Window*>( | |
|
Ben Goodger (Google)
2011/11/28 17:07:44
Cast unnecessary.
varunjain
2011/11/28 17:32:29
Done.
| |
| 56 rvh->view()->GetNativeView()); | |
| 57 aura::Window::ConvertPointToWindow(aura::Desktop::GetInstance(), | |
| 58 window, &client_loc); | |
| 59 rvh->DragSourceMovedTo(client_loc.x(), client_loc.y(), | |
| 60 screen_loc.x(), screen_loc.y()); | |
| 61 } | |
| 62 break; | |
| 63 default: | |
| 64 break; | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 private: | |
| 69 NativeTabContentsViewAura* view_; | |
| 70 | |
| 71 DISALLOW_COPY_AND_ASSIGN(WebDragSourceAura); | |
| 72 }; | |
| 73 | |
| 74 } // namespace | |
| 15 | 75 |
| 16 //////////////////////////////////////////////////////////////////////////////// | 76 //////////////////////////////////////////////////////////////////////////////// |
| 17 // NativeTabContentsViewAura, public: | 77 // NativeTabContentsViewAura, public: |
| 18 | 78 |
| 19 NativeTabContentsViewAura::NativeTabContentsViewAura( | 79 NativeTabContentsViewAura::NativeTabContentsViewAura( |
| 20 internal::NativeTabContentsViewDelegate* delegate) | 80 internal::NativeTabContentsViewDelegate* delegate) |
| 21 : views::NativeWidgetAura(delegate->AsNativeWidgetDelegate()), | 81 : views::NativeWidgetAura(delegate->AsNativeWidgetDelegate()), |
| 22 delegate_(delegate) { | 82 delegate_(delegate), |
| 83 current_drag_op_(WebKit::WebDragOperationNone) { | |
| 23 } | 84 } |
| 24 | 85 |
| 25 NativeTabContentsViewAura::~NativeTabContentsViewAura() { | 86 NativeTabContentsViewAura::~NativeTabContentsViewAura() { |
| 26 } | 87 } |
| 27 | 88 |
| 28 TabContents* NativeTabContentsViewAura::GetTabContents() const { | 89 TabContents* NativeTabContentsViewAura::GetTabContents() const { |
| 29 return delegate_->GetTabContents(); | 90 return delegate_->GetTabContents(); |
| 30 } | 91 } |
| 31 | 92 |
| 32 void NativeTabContentsViewAura::EndDragging() { | |
| 33 delegate_->OnNativeTabContentsViewDraggingEnded(); | |
| 34 // TODO(beng): | |
| 35 NOTIMPLEMENTED(); | |
| 36 } | |
| 37 | |
| 38 //////////////////////////////////////////////////////////////////////////////// | 93 //////////////////////////////////////////////////////////////////////////////// |
| 39 // NativeTabContentsViewAura, NativeTabContentsView implementation: | 94 // NativeTabContentsViewAura, NativeTabContentsView implementation: |
| 40 | 95 |
| 41 void NativeTabContentsViewAura::InitNativeTabContentsView() { | 96 void NativeTabContentsViewAura::InitNativeTabContentsView() { |
| 42 views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL); | 97 views::Widget::InitParams params(views::Widget::InitParams::TYPE_CONTROL); |
| 43 params.native_widget = this; | 98 params.native_widget = this; |
| 44 // We don't draw anything so we don't need a texture. | 99 // We don't draw anything so we don't need a texture. |
| 45 params.create_texture_for_layer = false; | 100 params.create_texture_for_layer = false; |
| 46 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; | 101 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
| 47 params.parent = NULL; | 102 params.parent = NULL; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 60 // Nothing to do. | 115 // Nothing to do. |
| 61 } | 116 } |
| 62 | 117 |
| 63 RenderWidgetHostView* NativeTabContentsViewAura::CreateRenderWidgetHostView( | 118 RenderWidgetHostView* NativeTabContentsViewAura::CreateRenderWidgetHostView( |
| 64 RenderWidgetHost* render_widget_host) { | 119 RenderWidgetHost* render_widget_host) { |
| 65 RenderWidgetHostViewAura* view = | 120 RenderWidgetHostViewAura* view = |
| 66 new RenderWidgetHostViewAura(render_widget_host); | 121 new RenderWidgetHostViewAura(render_widget_host); |
| 67 view->InitAsChild(); | 122 view->InitAsChild(); |
| 68 GetNativeView()->AddChild(view->GetNativeView()); | 123 GetNativeView()->AddChild(view->GetNativeView()); |
| 69 view->Show(); | 124 view->Show(); |
| 125 | |
| 126 // We listen to drag drop events in the newly created view's window. | |
| 127 aura::Window* window = static_cast<aura::Window*>(view->GetNativeView()); | |
| 128 DCHECK(window); | |
| 129 window->SetProperty(aura::kDragDropDelegateKey, | |
| 130 static_cast<aura::WindowDragDropDelegate*>(this)); | |
| 70 return view; | 131 return view; |
| 71 } | 132 } |
| 72 | 133 |
| 73 gfx::NativeWindow NativeTabContentsViewAura::GetTopLevelNativeWindow() const { | 134 gfx::NativeWindow NativeTabContentsViewAura::GetTopLevelNativeWindow() const { |
| 74 // TODO(beng): | 135 // TODO(beng): |
| 75 NOTIMPLEMENTED(); | 136 NOTIMPLEMENTED(); |
| 76 return NULL; | 137 return NULL; |
| 77 } | 138 } |
| 78 | 139 |
| 79 void NativeTabContentsViewAura::SetPageTitle(const string16& title) { | 140 void NativeTabContentsViewAura::SetPageTitle(const string16& title) { |
| 80 GetNativeView()->set_title(title); | 141 GetNativeView()->set_title(title); |
| 81 } | 142 } |
| 82 | 143 |
| 83 void NativeTabContentsViewAura::StartDragging(const WebDropData& drop_data, | 144 void NativeTabContentsViewAura::StartDragging(const WebDropData& drop_data, |
| 84 WebKit::WebDragOperationsMask ops, | 145 WebKit::WebDragOperationsMask ops, |
| 85 const SkBitmap& image, | 146 const SkBitmap& image, |
| 86 const gfx::Point& image_offset) { | 147 const gfx::Point& image_offset) { |
| 87 // TODO(beng): | 148 aura::DragDropClient* client = static_cast<aura::DragDropClient*>( |
| 88 NOTIMPLEMENTED(); | 149 aura::Desktop::GetInstance()->GetProperty( |
| 150 aura::kDesktopDragDropClientKey)); | |
| 151 if (!client) | |
| 152 return; | |
| 153 | |
| 154 ui::OSExchangeDataProviderAura* provider = new ui::OSExchangeDataProviderAura; | |
| 155 PrepareDragData(drop_data, provider); | |
| 156 if (!image.isNull()) | |
| 157 provider->set_drag_image(image); | |
| 158 ui::OSExchangeData data(provider); // takes ownership of |provider|. | |
| 159 | |
| 160 scoped_ptr<WebDragSourceAura> drag_source(new WebDragSourceAura(this)); | |
| 161 | |
| 162 // We need to enable recursive tasks on the message loop so we can get | |
| 163 // updates while in the system DoDragDrop loop. | |
| 164 bool old_state = MessageLoop::current()->NestableTasksAllowed(); | |
| 165 MessageLoop::current()->SetNestableTasksAllowed(true); | |
| 166 int result_op = client->StartDragAndDrop(data, ConvertFromWeb(ops)); | |
| 167 MessageLoop::current()->SetNestableTasksAllowed(old_state); | |
| 168 | |
| 169 EndDrag(ConvertToWeb(result_op)); | |
| 170 GetTabContents()->render_view_host()->DragSourceSystemDragEnded(); | |
| 89 } | 171 } |
| 90 | 172 |
| 91 void NativeTabContentsViewAura::CancelDrag() { | 173 void NativeTabContentsViewAura::CancelDrag() { |
| 92 // TODO(beng): | 174 aura::DragDropClient* client = static_cast<aura::DragDropClient*>( |
| 93 NOTIMPLEMENTED(); | 175 aura::Desktop::GetInstance()->GetProperty( |
| 176 aura::kDesktopDragDropClientKey)); | |
| 177 if (client) | |
| 178 client->DragCancel(); | |
| 94 } | 179 } |
| 95 | 180 |
| 96 bool NativeTabContentsViewAura::IsDoingDrag() const { | 181 bool NativeTabContentsViewAura::IsDoingDrag() const { |
| 97 // TODO(beng): | 182 aura::DragDropClient* client = static_cast<aura::DragDropClient*>( |
| 98 NOTIMPLEMENTED(); | 183 aura::Desktop::GetInstance()->GetProperty( |
| 184 aura::kDesktopDragDropClientKey)); | |
| 185 if (client) | |
| 186 return client->IsDragDropInProgress(); | |
| 99 return false; | 187 return false; |
| 100 } | 188 } |
| 101 | 189 |
| 102 void NativeTabContentsViewAura::SetDragCursor( | 190 void NativeTabContentsViewAura::SetDragCursor( |
| 103 WebKit::WebDragOperation operation) { | 191 WebKit::WebDragOperation operation) { |
| 104 // TODO(beng): | 192 current_drag_op_ = operation; |
| 105 NOTIMPLEMENTED(); | |
| 106 } | 193 } |
| 107 | 194 |
| 108 views::NativeWidget* NativeTabContentsViewAura::AsNativeWidget() { | 195 views::NativeWidget* NativeTabContentsViewAura::AsNativeWidget() { |
| 109 return this; | 196 return this; |
| 110 } | 197 } |
| 111 | 198 |
| 112 //////////////////////////////////////////////////////////////////////////////// | 199 //////////////////////////////////////////////////////////////////////////////// |
| 113 // NativeTabContentsViewAura, views::NativeWidgetAura overrides: | 200 // NativeTabContentsViewAura, views::NativeWidgetAura overrides: |
| 114 | 201 |
| 115 void NativeTabContentsViewAura::OnBoundsChanged(const gfx::Rect& old_bounds, | 202 void NativeTabContentsViewAura::OnBoundsChanged(const gfx::Rect& old_bounds, |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 129 break; | 216 break; |
| 130 default: | 217 default: |
| 131 // TODO(oshima): mouse wheel | 218 // TODO(oshima): mouse wheel |
| 132 break; | 219 break; |
| 133 } | 220 } |
| 134 } | 221 } |
| 135 // Pass all mouse event to renderer. | 222 // Pass all mouse event to renderer. |
| 136 return views::NativeWidgetAura::OnMouseEvent(event); | 223 return views::NativeWidgetAura::OnMouseEvent(event); |
| 137 } | 224 } |
| 138 | 225 |
| 226 void NativeTabContentsViewAura::OnDragEntered( | |
| 227 const aura::DropTargetEvent& event) { | |
| 228 WebDropData drop_data; | |
| 229 PrepareWebDropData(&drop_data, event.data()); | |
| 230 WebKit::WebDragOperationsMask op = ConvertToWeb(event.source_operations()); | |
| 231 | |
| 232 gfx::Point screen_pt = aura::Desktop::GetInstance()->last_mouse_location(); | |
| 233 GetTabContents()->render_view_host()->DragTargetDragEnter( | |
| 234 drop_data, event.location(), screen_pt, op); | |
| 235 } | |
| 236 | |
| 237 int NativeTabContentsViewAura::OnDragUpdated( | |
| 238 const aura::DropTargetEvent& event) { | |
| 239 WebKit::WebDragOperationsMask op = ConvertToWeb(event.source_operations()); | |
| 240 gfx::Point screen_pt = aura::Desktop::GetInstance()->last_mouse_location(); | |
| 241 GetTabContents()->render_view_host()->DragTargetDragOver( | |
| 242 event.location(), screen_pt, op); | |
| 243 return ConvertFromWeb(current_drag_op_); | |
| 244 } | |
| 245 | |
| 246 void NativeTabContentsViewAura::OnDragExited() { | |
| 247 GetTabContents()->render_view_host()->DragTargetDragLeave(); | |
| 248 } | |
| 249 | |
| 250 int NativeTabContentsViewAura::OnPerformDrop( | |
| 251 const aura::DropTargetEvent& event) { | |
| 252 GetTabContents()->render_view_host()->DragTargetDrop( | |
| 253 event.location(), aura::Desktop::GetInstance()->last_mouse_location()); | |
| 254 return current_drag_op_; | |
| 255 } | |
| 256 | |
|
Ben Goodger (Google)
2011/11/28 17:07:44
Can we add a section marker here:
/////...etc
//
varunjain
2011/11/28 17:32:29
Done.
| |
| 257 void NativeTabContentsViewAura::PrepareDragData( | |
|
Ben Goodger (Google)
2011/11/28 17:07:44
Any of these functions that don't work with local
varunjain
2011/11/28 17:32:29
Done.
| |
| 258 const WebDropData& drop_data, | |
| 259 ui::OSExchangeDataProviderAura* provider) { | |
| 260 if (!drop_data.plain_text.empty()) | |
| 261 provider->SetString(drop_data.plain_text); | |
| 262 if (drop_data.url.is_valid()) | |
| 263 provider->SetURL(drop_data.url, drop_data.url_title); | |
| 264 // TODO(varunjain): support other formats. | |
| 265 } | |
| 266 | |
| 267 void NativeTabContentsViewAura::PrepareWebDropData( | |
| 268 WebDropData* drop_data, | |
| 269 const ui::OSExchangeData& data) { | |
|
Ben Goodger (Google)
2011/11/28 17:07:44
ditto
varunjain
2011/11/28 17:32:29
Done.
| |
| 270 string16 plain_text, url_title; | |
| 271 GURL url; | |
| 272 data.GetString(&plain_text); | |
| 273 if (!plain_text.empty()) | |
| 274 drop_data->plain_text = plain_text; | |
| 275 data.GetURLAndTitle(&url, &url_title); | |
| 276 if (url.is_valid()) { | |
| 277 drop_data->url = url; | |
| 278 drop_data->url_title = url_title; | |
| 279 } | |
| 280 // TODO(varunjain): support other formats. | |
| 281 } | |
| 282 | |
| 283 int NativeTabContentsViewAura::ConvertFromWeb( | |
|
Ben Goodger (Google)
2011/11/28 17:07:44
ditto
varunjain
2011/11/28 17:32:29
Done.
| |
| 284 WebKit::WebDragOperationsMask ops) { | |
| 285 int drag_op = ui::DragDropTypes::DRAG_NONE; | |
| 286 if (ops & WebKit::WebDragOperationCopy) | |
| 287 drag_op |= ui::DragDropTypes::DRAG_COPY; | |
| 288 if (ops & WebKit::WebDragOperationMove) | |
| 289 drag_op |= ui::DragDropTypes::DRAG_MOVE; | |
| 290 if (ops & WebKit::WebDragOperationLink) | |
| 291 drag_op |= ui::DragDropTypes::DRAG_LINK; | |
| 292 return drag_op; | |
| 293 } | |
| 294 | |
| 295 WebKit::WebDragOperationsMask NativeTabContentsViewAura::ConvertToWeb( | |
| 296 int drag_op) { | |
|
Ben Goodger (Google)
2011/11/28 17:07:44
ditto
varunjain
2011/11/28 17:32:29
Done.
| |
| 297 int web_drag_op = WebKit::WebDragOperationNone; | |
| 298 if (drag_op & ui::DragDropTypes::DRAG_COPY) | |
| 299 web_drag_op |= WebKit::WebDragOperationCopy; | |
| 300 if (drag_op & ui::DragDropTypes::DRAG_MOVE) | |
| 301 web_drag_op |= WebKit::WebDragOperationMove; | |
| 302 if (drag_op & ui::DragDropTypes::DRAG_LINK) | |
| 303 web_drag_op |= WebKit::WebDragOperationLink; | |
| 304 return (WebKit::WebDragOperationsMask) web_drag_op; | |
| 305 } | |
| 306 | |
| 307 void NativeTabContentsViewAura::EndDrag(WebKit::WebDragOperationsMask ops) { | |
| 308 gfx::Point screen_loc = aura::Desktop::GetInstance()->last_mouse_location(); | |
| 309 gfx::Point client_loc = screen_loc; | |
| 310 RenderViewHost* rvh = GetTabContents()->render_view_host(); | |
| 311 aura::Window* window = static_cast<aura::Window*>( | |
|
Ben Goodger (Google)
2011/11/28 17:07:44
This cast isn't necessary. NativeView typedefs to
varunjain
2011/11/28 17:32:29
Done.
| |
| 312 rvh->view()->GetNativeView()); | |
| 313 aura::Window::ConvertPointToWindow(aura::Desktop::GetInstance(), | |
| 314 window, &client_loc); | |
| 315 rvh->DragSourceEndedAt(client_loc.x(), client_loc.y(), screen_loc.x(), | |
| 316 screen_loc.y(), ops); | |
| 317 } | |
| 318 | |
| 139 //////////////////////////////////////////////////////////////////////////////// | 319 //////////////////////////////////////////////////////////////////////////////// |
| 140 // NativeTabContentsView, public: | 320 // NativeTabContentsView, public: |
| 141 | 321 |
| 142 // static | 322 // static |
| 143 NativeTabContentsView* NativeTabContentsView::CreateNativeTabContentsView( | 323 NativeTabContentsView* NativeTabContentsView::CreateNativeTabContentsView( |
| 144 internal::NativeTabContentsViewDelegate* delegate) { | 324 internal::NativeTabContentsViewDelegate* delegate) { |
| 145 return new NativeTabContentsViewAura(delegate); | 325 return new NativeTabContentsViewAura(delegate); |
| 146 } | 326 } |
| OLD | NEW |