| 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_drag_source_win.h" | 5 #include "content/browser/web_contents/web_drag_source_win.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "content/browser/renderer_host/render_view_host_impl.h" | 8 #include "content/browser/renderer_host/render_view_host_impl.h" |
| 9 #include "content/browser/web_contents/web_contents_impl.h" |
| 9 #include "content/browser/web_contents/web_drag_utils_win.h" | 10 #include "content/browser/web_contents/web_drag_utils_win.h" |
| 10 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/notification_source.h" | 12 #include "content/public/browser/notification_source.h" |
| 12 #include "content/public/browser/notification_types.h" | 13 #include "content/public/browser/notification_types.h" |
| 13 #include "content/public/browser/web_contents.h" | |
| 14 #include "ui/base/dragdrop/os_exchange_data.h" | 14 #include "ui/base/dragdrop/os_exchange_data.h" |
| 15 | 15 |
| 16 using WebKit::WebDragOperationNone; | 16 using WebKit::WebDragOperationNone; |
| 17 | 17 |
| 18 namespace content { | 18 namespace content { |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 static void GetCursorPositions(gfx::NativeWindow wnd, gfx::Point* client, | 21 static void GetCursorPositions(gfx::NativeWindow wnd, gfx::Point* client, |
| 22 gfx::Point* screen) { | 22 gfx::Point* screen) { |
| 23 POINT cursor_pos; | 23 POINT cursor_pos; |
| 24 GetCursorPos(&cursor_pos); | 24 GetCursorPos(&cursor_pos); |
| 25 screen->SetPoint(cursor_pos.x, cursor_pos.y); | 25 screen->SetPoint(cursor_pos.x, cursor_pos.y); |
| 26 ScreenToClient(wnd, &cursor_pos); | 26 ScreenToClient(wnd, &cursor_pos); |
| 27 client->SetPoint(cursor_pos.x, cursor_pos.y); | 27 client->SetPoint(cursor_pos.x, cursor_pos.y); |
| 28 } | 28 } |
| 29 | 29 |
| 30 } // namespace | 30 } // namespace |
| 31 | 31 |
| 32 /////////////////////////////////////////////////////////////////////////////// | 32 /////////////////////////////////////////////////////////////////////////////// |
| 33 // WebDragSource, public: | 33 // WebDragSource, public: |
| 34 | 34 |
| 35 WebDragSource::WebDragSource(gfx::NativeWindow source_wnd, | 35 WebDragSource::WebDragSource(gfx::NativeWindow source_wnd, |
| 36 WebContents* web_contents) | 36 WebContents* web_contents) |
| 37 : ui::DragSourceWin(), | 37 : ui::DragSourceWin(), |
| 38 source_wnd_(source_wnd), | 38 source_wnd_(source_wnd), |
| 39 render_view_host_(web_contents->GetRenderViewHost()), | 39 web_contents_(static_cast<WebContentsImpl*>(web_contents)), |
| 40 effect_(DROPEFFECT_NONE), | 40 effect_(DROPEFFECT_NONE), |
| 41 data_(NULL) { | 41 data_(NULL) { |
| 42 registrar_.Add(this, NOTIFICATION_WEB_CONTENTS_SWAPPED, | 42 registrar_.Add(this, NOTIFICATION_WEB_CONTENTS_SWAPPED, |
| 43 Source<WebContents>(web_contents)); | 43 Source<WebContents>(web_contents)); |
| 44 registrar_.Add(this, NOTIFICATION_WEB_CONTENTS_DISCONNECTED, | 44 registrar_.Add(this, NOTIFICATION_WEB_CONTENTS_DISCONNECTED, |
| 45 Source<WebContents>(web_contents)); | 45 Source<WebContents>(web_contents)); |
| 46 } | 46 } |
| 47 | 47 |
| 48 WebDragSource::~WebDragSource() { | 48 WebDragSource::~WebDragSource() { |
| 49 } | 49 } |
| 50 | 50 |
| 51 void WebDragSource::OnDragSourceCancel() { | 51 void WebDragSource::OnDragSourceCancel() { |
| 52 // Delegate to the UI thread if we do drag-and-drop in the background thread. | 52 // Delegate to the UI thread if we do drag-and-drop in the background thread. |
| 53 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 53 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 54 BrowserThread::PostTask( | 54 BrowserThread::PostTask( |
| 55 BrowserThread::UI, FROM_HERE, | 55 BrowserThread::UI, FROM_HERE, |
| 56 base::Bind(&WebDragSource::OnDragSourceCancel, this)); | 56 base::Bind(&WebDragSource::OnDragSourceCancel, this)); |
| 57 return; | 57 return; |
| 58 } | 58 } |
| 59 | 59 |
| 60 if (!render_view_host_) | 60 if (!web_contents_) |
| 61 return; | 61 return; |
| 62 | 62 |
| 63 gfx::Point client; | 63 gfx::Point client; |
| 64 gfx::Point screen; | 64 gfx::Point screen; |
| 65 GetCursorPositions(source_wnd_, &client, &screen); | 65 GetCursorPositions(source_wnd_, &client, &screen); |
| 66 render_view_host_->DragSourceEndedAt(client.x(), client.y(), | 66 web_contents_->DragSourceEndedAt(client.x(), client.y(), |
| 67 screen.x(), screen.y(), | 67 screen.x(), screen.y(), |
| 68 WebDragOperationNone); | 68 WebDragOperationNone); |
| 69 } | 69 } |
| 70 | 70 |
| 71 void WebDragSource::OnDragSourceDrop() { | 71 void WebDragSource::OnDragSourceDrop() { |
| 72 DCHECK(data_); | 72 DCHECK(data_); |
| 73 data_->SetInDragLoop(false); | 73 data_->SetInDragLoop(false); |
| 74 // On Windows, we check for drag end in IDropSource::QueryContinueDrag which | 74 // On Windows, we check for drag end in IDropSource::QueryContinueDrag which |
| 75 // happens before IDropTarget::Drop is called. HTML5 requires the "dragend" | 75 // happens before IDropTarget::Drop is called. HTML5 requires the "dragend" |
| 76 // event to happen after the "drop" event. Since Windows calls these two | 76 // event to happen after the "drop" event. Since Windows calls these two |
| 77 // directly after each other we can just post a task to handle the | 77 // directly after each other we can just post a task to handle the |
| 78 // OnDragSourceDrop after the current task. | 78 // OnDragSourceDrop after the current task. |
| 79 BrowserThread::PostTask( | 79 BrowserThread::PostTask( |
| 80 BrowserThread::UI, FROM_HERE, | 80 BrowserThread::UI, FROM_HERE, |
| 81 base::Bind(&WebDragSource::DelayedOnDragSourceDrop, this)); | 81 base::Bind(&WebDragSource::DelayedOnDragSourceDrop, this)); |
| 82 } | 82 } |
| 83 | 83 |
| 84 void WebDragSource::DelayedOnDragSourceDrop() { | 84 void WebDragSource::DelayedOnDragSourceDrop() { |
| 85 if (!render_view_host_) | 85 if (!web_contents_) |
| 86 return; | 86 return; |
| 87 | 87 |
| 88 gfx::Point client; | 88 gfx::Point client; |
| 89 gfx::Point screen; | 89 gfx::Point screen; |
| 90 GetCursorPositions(source_wnd_, &client, &screen); | 90 GetCursorPositions(source_wnd_, &client, &screen); |
| 91 render_view_host_->DragSourceEndedAt( | 91 web_contents_->DragSourceEndedAt(client.x(), client.y(), screen.x(), |
| 92 client.x(), client.y(), screen.x(), screen.y(), | 92 screen.y(), WinDragOpToWebDragOp(effect_)); |
| 93 WinDragOpToWebDragOp(effect_)); | |
| 94 } | 93 } |
| 95 | 94 |
| 96 void WebDragSource::OnDragSourceMove() { | 95 void WebDragSource::OnDragSourceMove() { |
| 97 // Delegate to the UI thread if we do drag-and-drop in the background thread. | 96 // Delegate to the UI thread if we do drag-and-drop in the background thread. |
| 98 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 97 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 99 BrowserThread::PostTask( | 98 BrowserThread::PostTask( |
| 100 BrowserThread::UI, FROM_HERE, | 99 BrowserThread::UI, FROM_HERE, |
| 101 base::Bind(&WebDragSource::OnDragSourceMove, this)); | 100 base::Bind(&WebDragSource::OnDragSourceMove, this)); |
| 102 return; | 101 return; |
| 103 } | 102 } |
| 104 | 103 |
| 105 if (!render_view_host_) | 104 if (!web_contents_) |
| 106 return; | 105 return; |
| 107 | 106 |
| 108 gfx::Point client; | 107 gfx::Point client; |
| 109 gfx::Point screen; | 108 gfx::Point screen; |
| 110 GetCursorPositions(source_wnd_, &client, &screen); | 109 GetCursorPositions(source_wnd_, &client, &screen); |
| 111 render_view_host_->DragSourceMovedTo(client.x(), client.y(), | 110 web_contents_->DragSourceMovedTo(client.x(), client.y(), |
| 112 screen.x(), screen.y()); | 111 screen.x(), screen.y()); |
| 113 } | 112 } |
| 114 | 113 |
| 115 void WebDragSource::Observe(int type, | 114 void WebDragSource::Observe(int type, |
| 116 const NotificationSource& source, | 115 const NotificationSource& source, |
| 117 const NotificationDetails& details) { | 116 const NotificationDetails& details) { |
| 118 if (type == NOTIFICATION_WEB_CONTENTS_SWAPPED) { | 117 if (type == NOTIFICATION_WEB_CONTENTS_SWAPPED) { |
| 119 // When the WebContents get swapped, our render view host goes away. | 118 // When the WebContents get swapped, our render view host goes away. |
| 120 // That's OK, we can continue the drag, we just can't send messages back to | 119 // That's OK, we can continue the drag, we just can't send messages back to |
| 121 // our drag source. | 120 // our drag source. |
| 122 render_view_host_ = NULL; | 121 web_contents_ = NULL; |
| 123 } else if (type == NOTIFICATION_WEB_CONTENTS_DISCONNECTED) { | 122 } else if (type == NOTIFICATION_WEB_CONTENTS_DISCONNECTED) { |
| 124 // This could be possible when we close the tab and the source is still | 123 // This could be possible when we close the tab and the source is still |
| 125 // being used in DoDragDrop at the time that the virtual file is being | 124 // being used in DoDragDrop at the time that the virtual file is being |
| 126 // downloaded. | 125 // downloaded. |
| 127 render_view_host_ = NULL; | 126 web_contents_ = NULL; |
| 128 } | 127 } |
| 129 } | 128 } |
| 130 | 129 |
| 131 } // namespace content | 130 } // namespace content |
| OLD | NEW |