| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/tab_contents/web_drag_source_win.h" | 5 #include "chrome/browser/tab_contents/web_drag_source_win.h" |
| 6 | 6 |
| 7 #include "base/task.h" |
| 8 #include "chrome/browser/chrome_thread.h" |
| 7 #include "chrome/browser/renderer_host/render_view_host.h" | 9 #include "chrome/browser/renderer_host/render_view_host.h" |
| 8 #include "chrome/browser/tab_contents/tab_contents.h" | 10 #include "chrome/browser/tab_contents/tab_contents.h" |
| 9 #include "chrome/common/notification_type.h" | 11 #include "chrome/common/notification_type.h" |
| 10 #include "chrome/common/notification_service.h" | 12 #include "chrome/common/notification_service.h" |
| 11 | 13 |
| 12 using WebKit::WebDragOperationNone; | 14 using WebKit::WebDragOperationNone; |
| 13 using WebKit::WebDragOperationCopy; | 15 using WebKit::WebDragOperationCopy; |
| 14 | 16 |
| 15 namespace { | 17 namespace { |
| 16 | 18 |
| 17 static void GetCursorPositions(gfx::NativeWindow wnd, gfx::Point* client, | 19 static void GetCursorPositions(gfx::NativeWindow wnd, gfx::Point* client, |
| 18 gfx::Point* screen) { | 20 gfx::Point* screen) { |
| 19 POINT cursor_pos; | 21 POINT cursor_pos; |
| 20 GetCursorPos(&cursor_pos); | 22 GetCursorPos(&cursor_pos); |
| 21 screen->SetPoint(cursor_pos.x, cursor_pos.y); | 23 screen->SetPoint(cursor_pos.x, cursor_pos.y); |
| 22 ScreenToClient(wnd, &cursor_pos); | 24 ScreenToClient(wnd, &cursor_pos); |
| 23 client->SetPoint(cursor_pos.x, cursor_pos.y); | 25 client->SetPoint(cursor_pos.x, cursor_pos.y); |
| 24 } | 26 } |
| 25 | 27 |
| 26 } // namespace | 28 } // namespace |
| 29 |
| 27 /////////////////////////////////////////////////////////////////////////////// | 30 /////////////////////////////////////////////////////////////////////////////// |
| 28 // WebDragSource, public: | 31 // WebDragSource, public: |
| 29 | 32 |
| 30 WebDragSource::WebDragSource(gfx::NativeWindow source_wnd, | 33 WebDragSource::WebDragSource(gfx::NativeWindow source_wnd, |
| 31 TabContents* tab_contents) | 34 TabContents* tab_contents) |
| 32 : BaseDragSource(), | 35 : BaseDragSource(), |
| 33 source_wnd_(source_wnd), | 36 source_wnd_(source_wnd), |
| 34 render_view_host_(tab_contents->render_view_host()) { | 37 render_view_host_(tab_contents->render_view_host()) { |
| 35 registrar_.Add(this, NotificationType::TAB_CONTENTS_SWAPPED, | 38 registrar_.Add(this, NotificationType::TAB_CONTENTS_SWAPPED, |
| 36 Source<TabContents>(tab_contents)); | 39 Source<TabContents>(tab_contents)); |
| 37 registrar_.Add(this, NotificationType::TAB_CONTENTS_DISCONNECTED, | 40 registrar_.Add(this, NotificationType::TAB_CONTENTS_DISCONNECTED, |
| 38 Source<TabContents>(tab_contents)); | 41 Source<TabContents>(tab_contents)); |
| 39 } | 42 } |
| 40 | 43 |
| 41 void WebDragSource::OnDragSourceCancel() { | 44 void WebDragSource::OnDragSourceCancel() { |
| 45 // Delegate to the UI thread if we do drag-and-drop in the background thread. |
| 46 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { |
| 47 ChromeThread::PostTask( |
| 48 ChromeThread::UI, FROM_HERE, |
| 49 NewRunnableMethod(this, &WebDragSource::OnDragSourceCancel)); |
| 50 return; |
| 51 } |
| 52 |
| 42 if (!render_view_host_) | 53 if (!render_view_host_) |
| 43 return; | 54 return; |
| 44 | 55 |
| 45 gfx::Point client; | 56 gfx::Point client; |
| 46 gfx::Point screen; | 57 gfx::Point screen; |
| 47 GetCursorPositions(source_wnd_, &client, &screen); | 58 GetCursorPositions(source_wnd_, &client, &screen); |
| 48 render_view_host_->DragSourceEndedAt(client.x(), client.y(), | 59 render_view_host_->DragSourceEndedAt(client.x(), client.y(), |
| 49 screen.x(), screen.y(), | 60 screen.x(), screen.y(), |
| 50 WebDragOperationNone); | 61 WebDragOperationNone); |
| 51 } | 62 } |
| 52 | 63 |
| 53 void WebDragSource::OnDragSourceDrop() { | 64 void WebDragSource::OnDragSourceDrop() { |
| 65 // Delegate to the UI thread if we do drag-and-drop in the background thread. |
| 66 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { |
| 67 ChromeThread::PostTask( |
| 68 ChromeThread::UI, FROM_HERE, |
| 69 NewRunnableMethod(this, &WebDragSource::OnDragSourceDrop)); |
| 70 return; |
| 71 } |
| 72 |
| 54 if (!render_view_host_) | 73 if (!render_view_host_) |
| 55 return; | 74 return; |
| 56 | 75 |
| 57 gfx::Point client; | 76 gfx::Point client; |
| 58 gfx::Point screen; | 77 gfx::Point screen; |
| 59 GetCursorPositions(source_wnd_, &client, &screen); | 78 GetCursorPositions(source_wnd_, &client, &screen); |
| 60 render_view_host_->DragSourceEndedAt(client.x(), client.y(), | 79 render_view_host_->DragSourceEndedAt(client.x(), client.y(), |
| 61 screen.x(), screen.y(), | 80 screen.x(), screen.y(), |
| 62 WebDragOperationCopy); | 81 WebDragOperationCopy); |
| 63 // TODO(jpa): This needs to be fixed to send the actual drag operation. | 82 // TODO(jpa): This needs to be fixed to send the actual drag operation. |
| 64 } | 83 } |
| 65 | 84 |
| 66 void WebDragSource::OnDragSourceMove() { | 85 void WebDragSource::OnDragSourceMove() { |
| 86 // Delegate to the UI thread if we do drag-and-drop in the background thread. |
| 87 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { |
| 88 ChromeThread::PostTask( |
| 89 ChromeThread::UI, FROM_HERE, |
| 90 NewRunnableMethod(this, &WebDragSource::OnDragSourceMove)); |
| 91 return; |
| 92 } |
| 93 |
| 67 if (!render_view_host_) | 94 if (!render_view_host_) |
| 68 return; | 95 return; |
| 69 | 96 |
| 70 gfx::Point client; | 97 gfx::Point client; |
| 71 gfx::Point screen; | 98 gfx::Point screen; |
| 72 GetCursorPositions(source_wnd_, &client, &screen); | 99 GetCursorPositions(source_wnd_, &client, &screen); |
| 73 render_view_host_->DragSourceMovedTo(client.x(), client.y(), | 100 render_view_host_->DragSourceMovedTo(client.x(), client.y(), |
| 74 screen.x(), screen.y()); | 101 screen.x(), screen.y()); |
| 75 } | 102 } |
| 76 | 103 |
| 77 void WebDragSource::Observe(NotificationType type, | 104 void WebDragSource::Observe(NotificationType type, |
| 78 const NotificationSource& source, const NotificationDetails& details) { | 105 const NotificationSource& source, const NotificationDetails& details) { |
| 79 if (NotificationType::TAB_CONTENTS_SWAPPED == type) { | 106 if (NotificationType::TAB_CONTENTS_SWAPPED == type) { |
| 80 // When the tab contents get swapped, our render view host goes away. | 107 // When the tab contents get swapped, our render view host goes away. |
| 81 // That's OK, we can continue the drag, we just can't send messages back to | 108 // That's OK, we can continue the drag, we just can't send messages back to |
| 82 // our drag source. | 109 // our drag source. |
| 83 render_view_host_ = NULL; | 110 render_view_host_ = NULL; |
| 84 } else if (NotificationType::TAB_CONTENTS_DISCONNECTED == type) { | 111 } else if (NotificationType::TAB_CONTENTS_DISCONNECTED == type) { |
| 85 NOTREACHED(); | 112 // This could be possible when we close the tab and the source is still |
| 113 // being used in DoDragDrop at the time that the virtual file is being |
| 114 // downloaded. |
| 115 render_view_host_ = NULL; |
| 86 } | 116 } |
| 87 } | 117 } |
| OLD | NEW |