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 const int kMaxAbsoluteCoordinate = 65535; |
54 | 56 |
55 LRESULT CALLBACK MsgFilterProc(int code, WPARAM wparam, LPARAM lparam) { | 57 LRESULT CALLBACK MsgFilterProc(int code, WPARAM wparam, LPARAM lparam) { |
56 if (code == base::MessagePumpForUI::kMessageFilterCode && | 58 if (code == base::MessagePumpForUI::kMessageFilterCode && |
57 !mouse_up_received) { | 59 !mouse_up_received) { |
58 MSG* msg = reinterpret_cast<MSG*>(lparam); | 60 MSG* msg = reinterpret_cast<MSG*>(lparam); |
59 // We do not care about WM_SYSKEYDOWN and WM_SYSKEYUP because when ALT key | 61 // 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. | 62 // is pressed down on drag-and-drop, it means to create a link. |
61 if (msg->message == WM_MOUSEMOVE || msg->message == WM_LBUTTONUP || | 63 if (msg->message == WM_MOUSEMOVE || msg->message == WM_LBUTTONUP || |
62 msg->message == WM_KEYDOWN || msg->message == WM_KEYUP) { | 64 msg->message == WM_KEYDOWN || msg->message == WM_KEYUP || |
| 65 msg->message == WM_RBUTTONUP) { |
63 // Forward the message from the UI thread to the drag-and-drop thread. | 66 // Forward the message from the UI thread to the drag-and-drop thread. |
64 PostThreadMessage(drag_out_thread_id, | 67 PostThreadMessage(drag_out_thread_id, |
65 msg->message, | 68 msg->message, |
66 msg->wParam, | 69 msg->wParam, |
67 msg->lParam); | 70 msg->lParam); |
68 | 71 |
69 // If the left button is up, we do not need to forward the message any | 72 // If the left/right button is up, we do not need to forward the message |
70 // more. | 73 // any more. |
71 if (msg->message == WM_LBUTTONUP || !(GetKeyState(VK_LBUTTON) & 0x8000)) | 74 if (msg->message == WM_LBUTTONUP || !(GetKeyState(VK_LBUTTON) & 0x8000)) |
72 mouse_up_received = true; | 75 mouse_up_received = true; |
73 | 76 |
| 77 if (msg->message == WM_RBUTTONUP || !(GetKeyState(VK_RBUTTON) & 0x8000)) |
| 78 mouse_up_received = true; |
| 79 |
74 return TRUE; | 80 return TRUE; |
75 } | 81 } |
76 } | 82 } |
77 return CallNextHookEx(msg_hook, code, wparam, lparam); | 83 return CallNextHookEx(msg_hook, code, wparam, lparam); |
78 } | 84 } |
79 | 85 |
80 void EnableBackgroundDraggingSupport(DWORD thread_id) { | 86 void EnableBackgroundDraggingSupport(DWORD thread_id) { |
81 // Install a hook procedure to monitor the messages so that we can forward | 87 // Install a hook procedure to monitor the messages so that we can forward |
82 // the appropriate ones to the background thread. | 88 // the appropriate ones to the background thread. |
83 drag_out_thread_id = thread_id; | 89 drag_out_thread_id = thread_id; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 WebDragOperationsMask ops, | 165 WebDragOperationsMask ops, |
160 const gfx::ImageSkia& image, | 166 const gfx::ImageSkia& image, |
161 const gfx::Vector2d& image_offset) { | 167 const gfx::Vector2d& image_offset) { |
162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 168 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
163 | 169 |
164 drag_source_ = new WebDragSource(source_window_, web_contents_); | 170 drag_source_ = new WebDragSource(source_window_, web_contents_); |
165 | 171 |
166 const GURL& page_url = web_contents_->GetURL(); | 172 const GURL& page_url = web_contents_->GetURL(); |
167 const std::string& page_encoding = web_contents_->GetEncoding(); | 173 const std::string& page_encoding = web_contents_->GetEncoding(); |
168 | 174 |
| 175 RenderWidgetHostViewWin* rwhv = |
| 176 static_cast<RenderWidgetHostViewWin*> |
| 177 (web_contents_->GetRenderWidgetHostView()); |
169 // If it is not drag-out, do the drag-and-drop in the current UI thread. | 178 // If it is not drag-out, do the drag-and-drop in the current UI thread. |
170 if (drop_data.download_metadata.empty()) { | 179 if (drop_data.download_metadata.empty()) { |
| 180 // If RWHV has a valid long press gesture, since DoDragDrop will run into |
| 181 // itself loop and start a dragging session if and only if a mouse button |
| 182 // is down and then moves, so we need to programmatically send out |
| 183 // mouse down event and adjust the cursor position for DoDragDrop. |
| 184 if (rwhv->in_long_press_gesture()) |
| 185 SendMouseEventForTouchDnD(); |
| 186 |
171 if (DoDragging(drop_data, ops, page_url, page_encoding, | 187 if (DoDragging(drop_data, ops, page_url, page_encoding, |
172 image, image_offset)) | 188 image, image_offset)) |
173 EndDragging(); | 189 EndDragging(); |
174 return; | 190 return; |
175 } | 191 } |
176 | 192 |
177 // Start a background thread to do the drag-and-drop. | 193 // Start a background thread to do the drag-and-drop. |
178 DCHECK(!drag_drop_thread_.get()); | 194 DCHECK(!drag_drop_thread_.get()); |
179 drag_drop_thread_.reset(new DragDropThread(this)); | 195 drag_drop_thread_.reset(new DragDropThread(this)); |
180 base::Thread::Options options; | 196 base::Thread::Options options; |
181 options.message_loop_type = base::MessageLoop::TYPE_UI; | 197 options.message_loop_type = base::MessageLoop::TYPE_UI; |
182 if (drag_drop_thread_->StartWithOptions(options)) { | 198 if (drag_drop_thread_->StartWithOptions(options)) { |
183 drag_drop_thread_->message_loop()->PostTask( | 199 drag_drop_thread_->message_loop()->PostTask( |
184 FROM_HERE, | 200 FROM_HERE, |
185 base::Bind(&WebContentsDragWin::StartBackgroundDragging, this, | 201 base::Bind(&WebContentsDragWin::StartBackgroundDragging, this, |
186 drop_data, ops, page_url, page_encoding, | 202 drop_data, ops, page_url, page_encoding, |
187 image, image_offset)); | 203 image, image_offset)); |
188 } | 204 } |
189 | 205 |
190 EnableBackgroundDraggingSupport(drag_drop_thread_->thread_id()); | 206 EnableBackgroundDraggingSupport(drag_drop_thread_->thread_id()); |
| 207 if (rwhv->in_long_press_gesture()) |
| 208 SendMouseEventForTouchDnD(); |
191 } | 209 } |
192 | 210 |
193 void WebContentsDragWin::StartBackgroundDragging( | 211 void WebContentsDragWin::StartBackgroundDragging( |
194 const WebDropData& drop_data, | 212 const WebDropData& drop_data, |
195 WebDragOperationsMask ops, | 213 WebDragOperationsMask ops, |
196 const GURL& page_url, | 214 const GURL& page_url, |
197 const std::string& page_encoding, | 215 const std::string& page_encoding, |
198 const gfx::ImageSkia& image, | 216 const gfx::ImageSkia& image, |
199 const gfx::Vector2d& image_offset) { | 217 const gfx::Vector2d& image_offset) { |
200 drag_drop_thread_id_ = base::PlatformThread::CurrentId(); | 218 drag_drop_thread_id_ = base::PlatformThread::CurrentId(); |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 | 423 |
406 drag_source_->CancelDrag(); | 424 drag_source_->CancelDrag(); |
407 } | 425 } |
408 | 426 |
409 void WebContentsDragWin::CloseThread() { | 427 void WebContentsDragWin::CloseThread() { |
410 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 428 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
411 | 429 |
412 drag_drop_thread_.reset(); | 430 drag_drop_thread_.reset(); |
413 } | 431 } |
414 | 432 |
| 433 void WebContentsDragWin::SendMouseEventForTouchDnD() { |
| 434 RenderWidgetHostViewWin* rwhv = |
| 435 static_cast<RenderWidgetHostViewWin*> |
| 436 (web_contents_->GetRenderWidgetHostView()); |
| 437 |
| 438 if (!rwhv) |
| 439 return; |
| 440 |
| 441 RECT screen_rect; |
| 442 ::GetWindowRect(::GetDesktopWindow(), &screen_rect); |
| 443 int screen_width = screen_rect.right - screen_rect.left; |
| 444 int screen_height = screen_rect.bottom - screen_rect.top; |
| 445 gfx::Point last_touch_position = rwhv->GetLastTouchEventLocation(); |
| 446 |
| 447 // Map the screen coordinate to the normalized absolute coordinate. |
| 448 int absolute_x = |
| 449 last_touch_position.x() * kMaxAbsoluteCoordinate / screen_width; |
| 450 int absolute_y = |
| 451 last_touch_position.y() * kMaxAbsoluteCoordinate / screen_height; |
| 452 |
| 453 // Send MOUSEEVENTF_RIGHTDOWN event followed by MOUSEEVENTF_MOVE event. |
| 454 // The reason why not to merge these 2 mouse events into one is, we don't |
| 455 // want DoDragDrop to get mouse event firstly which lead to drop the drag |
| 456 // source. Once a touch point is removed from screen after long press, |
| 457 // a MOUSEEVENTF_RIGHTUP event will be sent out, so we send the down event |
| 458 // paired with it to complete DoDragDrop session. |
| 459 INPUT ip = { 0 }; |
| 460 ip.type = INPUT_MOUSE; |
| 461 ip.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_ABSOLUTE; |
| 462 ip.mi.dx = absolute_x; |
| 463 ip.mi.dy = absolute_y; |
| 464 ::SendInput(1, &ip, sizeof(INPUT)); |
| 465 |
| 466 // Send MOUSEEVENTF_MOVE input event to set cursor to the right position. |
| 467 ip.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE; |
| 468 ::SendInput(1, &ip, sizeof(INPUT)); |
| 469 } |
| 470 |
415 void WebContentsDragWin::OnWaitForData() { | 471 void WebContentsDragWin::OnWaitForData() { |
416 DCHECK(drag_drop_thread_id_ == base::PlatformThread::CurrentId()); | 472 DCHECK(drag_drop_thread_id_ == base::PlatformThread::CurrentId()); |
417 | 473 |
| 474 LOG(INFO) << "call WebContentsDragWin::OnWaitForData"; |
| 475 |
418 // When the left button is release and we start to wait for the data, end | 476 // 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 | 477 // 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. | 478 // mode so that it can start to process the normal input events. |
421 BrowserThread::PostTask( | 479 BrowserThread::PostTask( |
422 BrowserThread::UI, | 480 BrowserThread::UI, |
423 FROM_HERE, | 481 FROM_HERE, |
424 base::Bind(&WebContentsDragWin::EndDragging, this)); | 482 base::Bind(&WebContentsDragWin::EndDragging, this)); |
425 } | 483 } |
426 | 484 |
427 void WebContentsDragWin::OnDataObjectDisposed() { | 485 void WebContentsDragWin::OnDataObjectDisposed() { |
428 DCHECK(drag_drop_thread_id_ == base::PlatformThread::CurrentId()); | 486 DCHECK(drag_drop_thread_id_ == base::PlatformThread::CurrentId()); |
429 | 487 |
430 // The drag-and-drop thread is only closed after OLE is done with | 488 // The drag-and-drop thread is only closed after OLE is done with |
431 // DataObjectImpl. | 489 // DataObjectImpl. |
432 BrowserThread::PostTask( | 490 BrowserThread::PostTask( |
433 BrowserThread::UI, | 491 BrowserThread::UI, |
434 FROM_HERE, | 492 FROM_HERE, |
435 base::Bind(&WebContentsDragWin::CloseThread, this)); | 493 base::Bind(&WebContentsDragWin::CloseThread, this)); |
436 } | 494 } |
437 | 495 |
438 // static | 496 // static |
439 void WebContentsDragWin::DisableDragDropForTesting() { | 497 void WebContentsDragWin::DisableDragDropForTesting() { |
440 run_do_drag_drop = false; | 498 run_do_drag_drop = false; |
441 } | 499 } |
442 | 500 |
443 } // namespace content | 501 } // namespace content |
OLD | NEW |