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