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_contents_drag_win.h" | 5 #include "content/browser/web_contents/web_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_util.h" | 12 #include "base/file_util.h" |
13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
15 #include "base/pickle.h" | 15 #include "base/pickle.h" |
16 #include "base/threading/platform_thread.h" | 16 #include "base/threading/platform_thread.h" |
17 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
18 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
19 #include "content/browser/download/drag_download_file.h" | 19 #include "content/browser/download/drag_download_file.h" |
20 #include "content/browser/download/drag_download_util.h" | 20 #include "content/browser/download/drag_download_util.h" |
21 #include "content/browser/renderer_host/render_widget_host_view_win.h" | |
21 #include "content/browser/web_contents/web_drag_dest_win.h" | 22 #include "content/browser/web_contents/web_drag_dest_win.h" |
22 #include "content/browser/web_contents/web_drag_source_win.h" | 23 #include "content/browser/web_contents/web_drag_source_win.h" |
23 #include "content/browser/web_contents/web_drag_utils_win.h" | 24 #include "content/browser/web_contents/web_drag_utils_win.h" |
24 #include "content/public/browser/browser_thread.h" | 25 #include "content/public/browser/browser_thread.h" |
25 #include "content/public/browser/content_browser_client.h" | 26 #include "content/public/browser/content_browser_client.h" |
26 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
27 #include "content/public/browser/web_contents_view.h" | 28 #include "content/public/browser/web_contents_view.h" |
28 #include "content/public/browser/web_drag_dest_delegate.h" | 29 #include "content/public/browser/web_drag_dest_delegate.h" |
29 #include "net/base/net_util.h" | 30 #include "net/base/net_util.h" |
30 #include "third_party/skia/include/core/SkBitmap.h" | 31 #include "third_party/skia/include/core/SkBitmap.h" |
(...skipping 13 matching lines...) Expand all Loading... | |
44 using WebKit::WebDragOperationMove; | 45 using WebKit::WebDragOperationMove; |
45 | 46 |
46 namespace content { | 47 namespace content { |
47 namespace { | 48 namespace { |
48 | 49 |
49 bool run_do_drag_drop = true; | 50 bool run_do_drag_drop = true; |
50 | 51 |
51 HHOOK msg_hook = NULL; | 52 HHOOK msg_hook = NULL; |
52 DWORD drag_out_thread_id = 0; | 53 DWORD drag_out_thread_id = 0; |
53 bool mouse_up_received = false; | 54 bool mouse_up_received = false; |
55 // True if the drag-drop is initiated by touch event. | |
56 bool touch_initiated_drag_drop = false; | |
54 | 57 |
55 LRESULT CALLBACK MsgFilterProc(int code, WPARAM wparam, LPARAM lparam) { | 58 LRESULT CALLBACK MsgFilterProc(int code, WPARAM wparam, LPARAM lparam) { |
56 if (code == base::MessagePumpForUI::kMessageFilterCode && | 59 if (code == base::MessagePumpForUI::kMessageFilterCode && |
57 !mouse_up_received) { | 60 !mouse_up_received) { |
58 MSG* msg = reinterpret_cast<MSG*>(lparam); | 61 MSG* msg = reinterpret_cast<MSG*>(lparam); |
59 // We do not care about WM_SYSKEYDOWN and WM_SYSKEYUP because when ALT key | 62 // We do not care about WM_SYSKEYDOWN and WM_SYSKEYUP because when ALT key |
60 // is pressed down on drag-and-drop, it means to create a link. | 63 // is pressed down on drag-and-drop, it means to create a link. |
61 if (msg->message == WM_MOUSEMOVE || msg->message == WM_LBUTTONUP || | 64 if (msg->message == WM_MOUSEMOVE || msg->message == WM_LBUTTONUP || |
62 msg->message == WM_KEYDOWN || msg->message == WM_KEYUP) { | 65 msg->message == WM_KEYDOWN || msg->message == WM_KEYUP || |
dcheng
2013/05/29 20:16:43
WM_LBUTTONUP here and lines 75 should probably be
Hongbo Min
2013/05/30 01:31:50
Done.
| |
66 (touch_initiated_drag_drop && msg->message == WM_RBUTTONUP)) { | |
63 // Forward the message from the UI thread to the drag-and-drop thread. | 67 // Forward the message from the UI thread to the drag-and-drop thread. |
64 PostThreadMessage(drag_out_thread_id, | 68 PostThreadMessage(drag_out_thread_id, |
65 msg->message, | 69 msg->message, |
66 msg->wParam, | 70 msg->wParam, |
67 msg->lParam); | 71 msg->lParam); |
68 | 72 |
69 // If the left button is up, we do not need to forward the message any | 73 // If the left button is up, we do not need to forward the message |
70 // more. | 74 // any more. |
71 if (msg->message == WM_LBUTTONUP || !(GetKeyState(VK_LBUTTON) & 0x8000)) | 75 if (msg->message == WM_LBUTTONUP || !(GetKeyState(VK_LBUTTON) & 0x8000)) |
72 mouse_up_received = true; | 76 mouse_up_received = true; |
73 | 77 |
78 // In case of touch-initiated drag, we need to take care of right | |
79 // mouse up event since a mouse right button down event is triggered | |
80 // programmatically for DoDragDrop running. | |
81 if (touch_initiated_drag_drop && (msg->message == WM_RBUTTONUP || | |
dcheng
2013/05/29 20:16:43
I think this should be (touch_initiated_drag_drop
Hongbo Min
2013/05/30 01:31:50
Why do we need additional '(...)' to wrap "msg->me
dcheng
2013/05/31 00:57:05
I mismatched the parens. See my next comment =)
| |
82 !(GetKeyState(VK_RBUTTON) & 0x8000))) | |
83 mouse_up_received = true; | |
84 | |
74 return TRUE; | 85 return TRUE; |
75 } | 86 } |
76 } | 87 } |
77 return CallNextHookEx(msg_hook, code, wparam, lparam); | 88 return CallNextHookEx(msg_hook, code, wparam, lparam); |
78 } | 89 } |
79 | 90 |
80 void EnableBackgroundDraggingSupport(DWORD thread_id) { | 91 void EnableBackgroundDraggingSupport(DWORD thread_id) { |
81 // Install a hook procedure to monitor the messages so that we can forward | 92 // Install a hook procedure to monitor the messages so that we can forward |
82 // the appropriate ones to the background thread. | 93 // the appropriate ones to the background thread. |
83 drag_out_thread_id = thread_id; | 94 drag_out_thread_id = thread_id; |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 165 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
155 DCHECK(!drag_drop_thread_.get()); | 166 DCHECK(!drag_drop_thread_.get()); |
156 } | 167 } |
157 | 168 |
158 void WebContentsDragWin::StartDragging(const WebDropData& drop_data, | 169 void WebContentsDragWin::StartDragging(const WebDropData& drop_data, |
159 WebDragOperationsMask ops, | 170 WebDragOperationsMask ops, |
160 const gfx::ImageSkia& image, | 171 const gfx::ImageSkia& image, |
161 const gfx::Vector2d& image_offset) { | 172 const gfx::Vector2d& image_offset) { |
162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 173 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
163 | 174 |
164 drag_source_ = new WebDragSource(source_window_, web_contents_); | 175 RenderWidgetHostViewWin* rwhv = |
176 static_cast<RenderWidgetHostViewWin*>( | |
177 web_contents_->GetRenderWidgetHostView()); | |
178 | |
179 ui::DragDropTypes::DragEventSource event_source; | |
180 if (rwhv->in_long_press_gesture()) { | |
181 event_source = ui::DragDropTypes::DRAG_EVENT_SOURCE_TOUCH; | |
182 touch_initiated_drag_drop = true; | |
183 } | |
184 else { | |
185 event_source = ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE; | |
186 touch_initiated_drag_drop = false; | |
187 } | |
188 | |
189 drag_source_ = new WebDragSource(source_window_, web_contents_, event_source); | |
165 | 190 |
166 const GURL& page_url = web_contents_->GetURL(); | 191 const GURL& page_url = web_contents_->GetURL(); |
167 const std::string& page_encoding = web_contents_->GetEncoding(); | 192 const std::string& page_encoding = web_contents_->GetEncoding(); |
168 | 193 |
169 // If it is not drag-out, do the drag-and-drop in the current UI thread. | 194 // If it is not drag-out, do the drag-and-drop in the current UI thread. |
170 if (drop_data.download_metadata.empty()) { | 195 if (drop_data.download_metadata.empty()) { |
171 if (DoDragging(drop_data, ops, page_url, page_encoding, | 196 if (DoDragging(drop_data, ops, page_url, page_encoding, |
172 image, image_offset)) | 197 image, image_offset)) |
173 EndDragging(); | 198 EndDragging(); |
174 return; | 199 return; |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
308 } | 333 } |
309 | 334 |
310 bool WebContentsDragWin::DoDragging(const WebDropData& drop_data, | 335 bool WebContentsDragWin::DoDragging(const WebDropData& drop_data, |
311 WebDragOperationsMask ops, | 336 WebDragOperationsMask ops, |
312 const GURL& page_url, | 337 const GURL& page_url, |
313 const std::string& page_encoding, | 338 const std::string& page_encoding, |
314 const gfx::ImageSkia& image, | 339 const gfx::ImageSkia& image, |
315 const gfx::Vector2d& image_offset) { | 340 const gfx::Vector2d& image_offset) { |
316 ui::OSExchangeData data; | 341 ui::OSExchangeData data; |
317 | 342 |
343 RenderWidgetHostViewWin* rwhv = | |
344 static_cast<RenderWidgetHostViewWin*>( | |
345 web_contents_->GetRenderWidgetHostView()); | |
346 // If RWHV has a valid long press gesture, since DoDragDrop will run into | |
347 // itself loop and start a dragging session if and only if a mouse button | |
348 // is down and then moves, so we need to programmatically send out | |
349 // mouse down event and adjust the cursor position for DoDragDrop. | |
350 if (rwhv->in_long_press_gesture()) | |
351 SendMouseEventForTouchDnD(); | |
352 | |
318 if (!drop_data.download_metadata.empty()) { | 353 if (!drop_data.download_metadata.empty()) { |
319 PrepareDragForDownload(drop_data, &data, page_url, page_encoding); | 354 PrepareDragForDownload(drop_data, &data, page_url, page_encoding); |
320 | 355 |
321 // Set the observer. | 356 // Set the observer. |
322 ui::OSExchangeDataProviderWin::GetDataObjectImpl(data)->set_observer(this); | 357 ui::OSExchangeDataProviderWin::GetDataObjectImpl(data)->set_observer(this); |
323 } | 358 } |
324 | 359 |
325 // We set the file contents before the URL because the URL also sets file | 360 // We set the file contents before the URL because the URL also sets file |
326 // contents (to a .URL shortcut). We want to prefer file content data over | 361 // contents (to a .URL shortcut). We want to prefer file content data over |
327 // a shortcut so we add it first. | 362 // a shortcut so we add it first. |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
405 | 440 |
406 drag_source_->CancelDrag(); | 441 drag_source_->CancelDrag(); |
407 } | 442 } |
408 | 443 |
409 void WebContentsDragWin::CloseThread() { | 444 void WebContentsDragWin::CloseThread() { |
410 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 445 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
411 | 446 |
412 drag_drop_thread_.reset(); | 447 drag_drop_thread_.reset(); |
413 } | 448 } |
414 | 449 |
450 void WebContentsDragWin::SendMouseEventForTouchDnD() { | |
451 static const int kMaxAbsoluteCoordinate = 65535; | |
452 | |
453 RenderWidgetHostViewWin* rwhv = | |
454 static_cast<RenderWidgetHostViewWin*>( | |
455 web_contents_->GetRenderWidgetHostView()); | |
456 | |
457 if (!rwhv) | |
458 return; | |
459 | |
460 gfx::Point last_touch_position = rwhv->GetLastTouchEventLocation(); | |
461 gfx::Screen* screen = gfx::Screen::GetScreenFor(rwhv->GetNativeView()); | |
462 gfx::Display display = screen->GetDisplayNearestPoint(last_touch_position); | |
oshima
2013/05/29 14:41:01
It may be better to use GetDisplayNearestWindow as
Hongbo Min
2013/05/30 01:36:03
Thanks for your information. I still think it shou
| |
463 int screen_width = display.size().width(); | |
464 int screen_height = display.size().height(); | |
465 | |
466 // Map the screen coordinate to the normalized absolute coordinate. | |
467 int absolute_x = | |
468 last_touch_position.x() * kMaxAbsoluteCoordinate / screen_width; | |
469 int absolute_y = | |
470 last_touch_position.y() * kMaxAbsoluteCoordinate / screen_height; | |
471 | |
472 // Send MOUSEEVENTF_RIGHTDOWN event followed by MOUSEEVENTF_MOVE event. | |
473 // The reason why not to merge these 2 mouse events into one is, we don't | |
474 // want DoDragDrop to get mouse event firstly which lead to drop the drag | |
475 // source. Once a touch point is removed from screen after long press, | |
476 // a MOUSEEVENTF_RIGHTUP event will be sent out, so we send the down event | |
477 // paired with it to complete DoDragDrop session. | |
478 INPUT input = { 0 }; | |
479 input.type = INPUT_MOUSE; | |
480 input.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_ABSOLUTE; | |
481 input.mi.dx = absolute_x; | |
482 input.mi.dy = absolute_y; | |
483 ::SendInput(1, &input, sizeof(input)); | |
484 | |
485 // Send MOUSEEVENTF_MOVE input event to set cursor to the right position. | |
486 input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE; | |
487 ::SendInput(1, &input, sizeof(input)); | |
488 } | |
489 | |
415 void WebContentsDragWin::OnWaitForData() { | 490 void WebContentsDragWin::OnWaitForData() { |
416 DCHECK(drag_drop_thread_id_ == base::PlatformThread::CurrentId()); | 491 DCHECK(drag_drop_thread_id_ == base::PlatformThread::CurrentId()); |
417 | 492 |
418 // When the left button is release and we start to wait for the data, end | 493 // When the left button is release and we start to wait for the data, end |
419 // the dragging before DoDragDrop returns. This makes the page leave the drag | 494 // the dragging before DoDragDrop returns. This makes the page leave the drag |
420 // mode so that it can start to process the normal input events. | 495 // mode so that it can start to process the normal input events. |
421 BrowserThread::PostTask( | 496 BrowserThread::PostTask( |
422 BrowserThread::UI, | 497 BrowserThread::UI, |
423 FROM_HERE, | 498 FROM_HERE, |
424 base::Bind(&WebContentsDragWin::EndDragging, this)); | 499 base::Bind(&WebContentsDragWin::EndDragging, this)); |
425 } | 500 } |
426 | 501 |
427 void WebContentsDragWin::OnDataObjectDisposed() { | 502 void WebContentsDragWin::OnDataObjectDisposed() { |
428 DCHECK(drag_drop_thread_id_ == base::PlatformThread::CurrentId()); | 503 DCHECK(drag_drop_thread_id_ == base::PlatformThread::CurrentId()); |
429 | 504 |
430 // The drag-and-drop thread is only closed after OLE is done with | 505 // The drag-and-drop thread is only closed after OLE is done with |
431 // DataObjectImpl. | 506 // DataObjectImpl. |
432 BrowserThread::PostTask( | 507 BrowserThread::PostTask( |
433 BrowserThread::UI, | 508 BrowserThread::UI, |
434 FROM_HERE, | 509 FROM_HERE, |
435 base::Bind(&WebContentsDragWin::CloseThread, this)); | 510 base::Bind(&WebContentsDragWin::CloseThread, this)); |
436 } | 511 } |
437 | 512 |
438 // static | 513 // static |
439 void WebContentsDragWin::DisableDragDropForTesting() { | 514 void WebContentsDragWin::DisableDragDropForTesting() { |
440 run_do_drag_drop = false; | 515 run_do_drag_drop = false; |
441 } | 516 } |
442 | 517 |
443 } // namespace content | 518 } // namespace content |
OLD | NEW |