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 "chrome/browser/ui/views/tab_contents/tab_contents_drag_win.h" | 5 #include "chrome/browser/ui/views/tab_contents/tab_contents_drag_win.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
14 #include "base/pickle.h" | 14 #include "base/pickle.h" |
15 #include "base/threading/platform_thread.h" | 15 #include "base/threading/platform_thread.h" |
16 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
17 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
18 #include "chrome/browser/bookmarks/bookmark_node_data.h" | |
19 #include "chrome/browser/ui/views/tab_contents/native_tab_contents_view_win.h" | |
20 #include "chrome/common/url_constants.h" | |
21 #include "content/browser/download/drag_download_file.h" | 18 #include "content/browser/download/drag_download_file.h" |
22 #include "content/browser/download/drag_download_util.h" | 19 #include "content/browser/download/drag_download_util.h" |
| 20 #include "content/browser/tab_contents/web_drag_dest_delegate.h" |
23 #include "content/browser/tab_contents/web_drag_dest_win.h" | 21 #include "content/browser/tab_contents/web_drag_dest_win.h" |
24 #include "content/browser/tab_contents/web_drag_source_win.h" | 22 #include "content/browser/tab_contents/web_drag_source_win.h" |
25 #include "content/browser/tab_contents/web_drag_utils_win.h" | 23 #include "content/browser/tab_contents/web_drag_utils_win.h" |
26 #include "content/public/browser/browser_thread.h" | 24 #include "content/public/browser/browser_thread.h" |
27 #include "content/public/browser/content_browser_client.h" | 25 #include "content/public/browser/content_browser_client.h" |
28 #include "content/public/browser/web_contents.h" | 26 #include "content/public/browser/web_contents.h" |
29 #include "net/base/net_util.h" | 27 #include "net/base/net_util.h" |
30 #include "ui/base/clipboard/clipboard_util_win.h" | 28 #include "ui/base/clipboard/clipboard_util_win.h" |
31 #include "ui/base/clipboard/custom_data_helper.h" | 29 #include "ui/base/clipboard/custom_data_helper.h" |
| 30 #include "ui/gfx/size.h" |
32 #include "ui/views/drag_utils.h" | 31 #include "ui/views/drag_utils.h" |
33 #include "webkit/glue/webdropdata.h" | 32 #include "webkit/glue/webdropdata.h" |
34 | 33 |
35 using content::BrowserThread; | 34 using content::BrowserThread; |
36 using WebKit::WebDragOperationsMask; | 35 using WebKit::WebDragOperationsMask; |
37 using WebKit::WebDragOperationCopy; | 36 using WebKit::WebDragOperationCopy; |
38 using WebKit::WebDragOperationLink; | 37 using WebKit::WebDragOperationLink; |
39 using WebKit::WebDragOperationMove; | 38 using WebKit::WebDragOperationMove; |
40 | 39 |
41 namespace { | 40 namespace { |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 } | 93 } |
95 | 94 |
96 private: | 95 private: |
97 // Hold a reference count to TabContentsDragWin to make sure that it is always | 96 // Hold a reference count to TabContentsDragWin to make sure that it is always |
98 // alive in the thread lifetime. | 97 // alive in the thread lifetime. |
99 scoped_refptr<TabContentsDragWin> drag_handler_; | 98 scoped_refptr<TabContentsDragWin> drag_handler_; |
100 | 99 |
101 DISALLOW_COPY_AND_ASSIGN(DragDropThread); | 100 DISALLOW_COPY_AND_ASSIGN(DragDropThread); |
102 }; | 101 }; |
103 | 102 |
104 TabContentsDragWin::TabContentsDragWin(NativeTabContentsViewWin* view) | 103 TabContentsDragWin::TabContentsDragWin( |
| 104 gfx::NativeWindow source_wnd, |
| 105 content::WebContents* web_contents, |
| 106 WebDragDest* drag_dest, |
| 107 const base::Callback<void()>& drag_end_callback) |
105 : drag_drop_thread_id_(0), | 108 : drag_drop_thread_id_(0), |
106 view_(view), | 109 source_wnd_(source_wnd), |
| 110 web_contents_(web_contents), |
| 111 drag_dest_(drag_dest), |
107 drag_ended_(false), | 112 drag_ended_(false), |
108 old_drop_target_suspended_state_(false) { | 113 old_drop_target_suspended_state_(false), |
| 114 drag_end_callback_(drag_end_callback) { |
109 } | 115 } |
110 | 116 |
111 TabContentsDragWin::~TabContentsDragWin() { | 117 TabContentsDragWin::~TabContentsDragWin() { |
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
113 DCHECK(!drag_drop_thread_.get()); | 119 DCHECK(!drag_drop_thread_.get()); |
114 } | 120 } |
115 | 121 |
116 void TabContentsDragWin::StartDragging(const WebDropData& drop_data, | 122 void TabContentsDragWin::StartDragging(const WebDropData& drop_data, |
117 WebDragOperationsMask ops, | 123 WebDragOperationsMask ops, |
118 const SkBitmap& image, | 124 const SkBitmap& image, |
119 const gfx::Point& image_offset) { | 125 const gfx::Point& image_offset) { |
120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
121 | 127 |
122 drag_source_ = new WebDragSource(view_->GetNativeView(), | 128 drag_source_ = new WebDragSource(source_wnd_, web_contents_); |
123 view_->GetWebContents()); | |
124 | 129 |
125 const GURL& page_url = view_->GetWebContents()->GetURL(); | 130 const GURL& page_url = web_contents_->GetURL(); |
126 const std::string& page_encoding = view_->GetWebContents()->GetEncoding(); | 131 const std::string& page_encoding = web_contents_->GetEncoding(); |
127 | 132 |
128 // If it is not drag-out, do the drag-and-drop in the current UI thread. | 133 // If it is not drag-out, do the drag-and-drop in the current UI thread. |
129 if (drop_data.download_metadata.empty()) { | 134 if (drop_data.download_metadata.empty()) { |
130 DoDragging(drop_data, ops, page_url, page_encoding, image, image_offset); | 135 DoDragging(drop_data, ops, page_url, page_encoding, image, image_offset); |
131 EndDragging(false); | 136 EndDragging(false); |
132 return; | 137 return; |
133 } | 138 } |
134 | 139 |
135 // We do not want to drag and drop the download to itself. | 140 // We do not want to drag and drop the download to itself. |
136 old_drop_target_suspended_state_ = view_->drag_dest()->suspended(); | 141 old_drop_target_suspended_state_ = drag_dest_->suspended(); |
137 view_->drag_dest()->set_suspended(true); | 142 drag_dest_->set_suspended(true); |
138 | 143 |
139 // Start a background thread to do the drag-and-drop. | 144 // Start a background thread to do the drag-and-drop. |
140 DCHECK(!drag_drop_thread_.get()); | 145 DCHECK(!drag_drop_thread_.get()); |
141 drag_drop_thread_.reset(new DragDropThread(this)); | 146 drag_drop_thread_.reset(new DragDropThread(this)); |
142 base::Thread::Options options; | 147 base::Thread::Options options; |
143 options.message_loop_type = MessageLoop::TYPE_UI; | 148 options.message_loop_type = MessageLoop::TYPE_UI; |
144 if (drag_drop_thread_->StartWithOptions(options)) { | 149 if (drag_drop_thread_->StartWithOptions(options)) { |
145 drag_drop_thread_->message_loop()->PostTask( | 150 drag_drop_thread_->message_loop()->PostTask( |
146 FROM_HERE, | 151 FROM_HERE, |
147 base::Bind(&TabContentsDragWin::StartBackgroundDragging, this, | 152 base::Bind(&TabContentsDragWin::StartBackgroundDragging, this, |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 | 213 |
209 // Provide the data as file (CF_HDROP). A temporary download file with the | 214 // Provide the data as file (CF_HDROP). A temporary download file with the |
210 // Zone.Identifier ADS (Alternate Data Stream) attached will be created. | 215 // Zone.Identifier ADS (Alternate Data Stream) attached will be created. |
211 linked_ptr<net::FileStream> empty_file_stream; | 216 linked_ptr<net::FileStream> empty_file_stream; |
212 scoped_refptr<DragDownloadFile> download_file = | 217 scoped_refptr<DragDownloadFile> download_file = |
213 new DragDownloadFile(generated_download_file_name, | 218 new DragDownloadFile(generated_download_file_name, |
214 empty_file_stream, | 219 empty_file_stream, |
215 download_url, | 220 download_url, |
216 page_url, | 221 page_url, |
217 page_encoding, | 222 page_encoding, |
218 view_->GetWebContents()); | 223 web_contents_); |
219 ui::OSExchangeData::DownloadFileInfo file_download(FilePath(), | 224 ui::OSExchangeData::DownloadFileInfo file_download(FilePath(), |
220 download_file.get()); | 225 download_file.get()); |
221 data->SetDownloadFileInfo(file_download); | 226 data->SetDownloadFileInfo(file_download); |
222 | 227 |
223 // Enable asynchronous operation. | 228 // Enable asynchronous operation. |
224 ui::OSExchangeDataProviderWin::GetIAsyncOperation(*data)->SetAsyncMode(TRUE); | 229 ui::OSExchangeDataProviderWin::GetIAsyncOperation(*data)->SetAsyncMode(TRUE); |
225 } | 230 } |
226 | 231 |
227 void TabContentsDragWin::PrepareDragForFileContents( | 232 void TabContentsDragWin::PrepareDragForFileContents( |
228 const WebDropData& drop_data, ui::OSExchangeData* data) { | 233 const WebDropData& drop_data, ui::OSExchangeData* data) { |
(...skipping 11 matching lines...) Expand all Loading... |
240 file_name = FilePath(file_name.value().substr( | 245 file_name = FilePath(file_name.value().substr( |
241 0, kMaxFilenameLength - extension.size())); | 246 0, kMaxFilenameLength - extension.size())); |
242 } | 247 } |
243 } | 248 } |
244 file_name = file_name.ReplaceExtension(extension); | 249 file_name = file_name.ReplaceExtension(extension); |
245 data->SetFileContents(file_name, drop_data.file_contents); | 250 data->SetFileContents(file_name, drop_data.file_contents); |
246 } | 251 } |
247 | 252 |
248 void TabContentsDragWin::PrepareDragForUrl(const WebDropData& drop_data, | 253 void TabContentsDragWin::PrepareDragForUrl(const WebDropData& drop_data, |
249 ui::OSExchangeData* data) { | 254 ui::OSExchangeData* data) { |
250 if (drop_data.url.SchemeIs(chrome::kJavaScriptScheme)) { | 255 if (drag_dest_->delegate()->AddDragData(drop_data, data)) |
251 // We don't want to allow javascript URLs to be dragged to the desktop, | 256 return; |
252 // but we do want to allow them to be added to the bookmarks bar | |
253 // (bookmarklets). So we create a fake bookmark entry (BookmarkNodeData | |
254 // object) which explorer.exe cannot handle, and write the entry to data. | |
255 BookmarkNodeData::Element bm_elt; | |
256 bm_elt.is_url = true; | |
257 bm_elt.url = drop_data.url; | |
258 bm_elt.title = drop_data.url_title; | |
259 | 257 |
260 BookmarkNodeData bm_drag_data; | 258 data->SetURL(drop_data.url, drop_data.url_title); |
261 bm_drag_data.elements.push_back(bm_elt); | |
262 | |
263 // Pass in NULL as the profile so that the bookmark always adds the url | |
264 // rather than trying to move an existing url. | |
265 bm_drag_data.Write(NULL, data); | |
266 } else { | |
267 data->SetURL(drop_data.url, drop_data.url_title); | |
268 } | |
269 } | 259 } |
270 | 260 |
271 void TabContentsDragWin::DoDragging(const WebDropData& drop_data, | 261 void TabContentsDragWin::DoDragging(const WebDropData& drop_data, |
272 WebDragOperationsMask ops, | 262 WebDragOperationsMask ops, |
273 const GURL& page_url, | 263 const GURL& page_url, |
274 const std::string& page_encoding, | 264 const std::string& page_encoding, |
275 const SkBitmap& image, | 265 const SkBitmap& image, |
276 const gfx::Point& image_offset) { | 266 const gfx::Point& image_offset) { |
277 ui::OSExchangeData data; | 267 ui::OSExchangeData data; |
278 | 268 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 } | 317 } |
328 | 318 |
329 void TabContentsDragWin::EndDragging(bool restore_suspended_state) { | 319 void TabContentsDragWin::EndDragging(bool restore_suspended_state) { |
330 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 320 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
331 | 321 |
332 if (drag_ended_) | 322 if (drag_ended_) |
333 return; | 323 return; |
334 drag_ended_ = true; | 324 drag_ended_ = true; |
335 | 325 |
336 if (restore_suspended_state) | 326 if (restore_suspended_state) |
337 view_->drag_dest()->set_suspended(old_drop_target_suspended_state_); | 327 drag_dest_->set_suspended(old_drop_target_suspended_state_); |
338 | 328 |
339 if (msg_hook) { | 329 if (msg_hook) { |
340 AttachThreadInput(drag_out_thread_id, GetCurrentThreadId(), FALSE); | 330 AttachThreadInput(drag_out_thread_id, GetCurrentThreadId(), FALSE); |
341 UnhookWindowsHookEx(msg_hook); | 331 UnhookWindowsHookEx(msg_hook); |
342 msg_hook = NULL; | 332 msg_hook = NULL; |
343 } | 333 } |
344 | 334 |
345 view_->EndDragging(); | 335 drag_end_callback_.Run(); |
346 } | 336 } |
347 | 337 |
348 void TabContentsDragWin::CancelDrag() { | 338 void TabContentsDragWin::CancelDrag() { |
349 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 339 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
350 | 340 |
351 drag_source_->CancelDrag(); | 341 drag_source_->CancelDrag(); |
352 } | 342 } |
353 | 343 |
354 void TabContentsDragWin::CloseThread() { | 344 void TabContentsDragWin::CloseThread() { |
355 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 345 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
(...skipping 16 matching lines...) Expand all Loading... |
372 void TabContentsDragWin::OnDataObjectDisposed() { | 362 void TabContentsDragWin::OnDataObjectDisposed() { |
373 DCHECK(drag_drop_thread_id_ == base::PlatformThread::CurrentId()); | 363 DCHECK(drag_drop_thread_id_ == base::PlatformThread::CurrentId()); |
374 | 364 |
375 // The drag-and-drop thread is only closed after OLE is done with | 365 // The drag-and-drop thread is only closed after OLE is done with |
376 // DataObjectImpl. | 366 // DataObjectImpl. |
377 BrowserThread::PostTask( | 367 BrowserThread::PostTask( |
378 BrowserThread::UI, | 368 BrowserThread::UI, |
379 FROM_HERE, | 369 FROM_HERE, |
380 base::Bind(&TabContentsDragWin::CloseThread, this)); | 370 base::Bind(&TabContentsDragWin::CloseThread, this)); |
381 } | 371 } |
OLD | NEW |