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