| 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 ((!touch_initiated_drag_drop && msg->message == WM_LBUTTONUP) || |
| 62 msg->message == WM_KEYDOWN || msg->message == WM_KEYUP) { | 65 (touch_initiated_drag_drop && msg->message == WM_RBUTTONUP) || |
| 66 msg->message == WM_MOUSEMOVE || msg->message == WM_KEYDOWN || |
| 67 msg->message == WM_KEYUP) { |
| 63 // Forward the message from the UI thread to the drag-and-drop thread. | 68 // Forward the message from the UI thread to the drag-and-drop thread. |
| 64 PostThreadMessage(drag_out_thread_id, | 69 PostThreadMessage(drag_out_thread_id, |
| 65 msg->message, | 70 msg->message, |
| 66 msg->wParam, | 71 msg->wParam, |
| 67 msg->lParam); | 72 msg->lParam); |
| 68 | 73 |
| 69 // If the left button is up, we do not need to forward the message any | 74 if (!touch_initiated_drag_drop) { |
| 70 // more. | 75 // If the left button is up, we do not need to forward the message |
| 71 if (msg->message == WM_LBUTTONUP || !(GetKeyState(VK_LBUTTON) & 0x8000)) | 76 // any more. |
| 72 mouse_up_received = true; | 77 if (msg->message == WM_LBUTTONUP || !(GetKeyState(VK_LBUTTON) & 0x8000)) |
| 78 mouse_up_received = true; |
| 79 } else { |
| 80 // In case of touch-initiated drag, we need to take care of right |
| 81 // mouse up event since a mouse right button down event is triggered |
| 82 // programmatically for DoDragDrop running. |
| 83 if (msg->message == WM_RBUTTONUP || !(GetKeyState(VK_RBUTTON) & 0x8000)) |
| 84 mouse_up_received = true; |
| 85 } |
| 73 | 86 |
| 74 return TRUE; | 87 return TRUE; |
| 75 } | 88 } |
| 76 } | 89 } |
| 77 return CallNextHookEx(msg_hook, code, wparam, lparam); | 90 return CallNextHookEx(msg_hook, code, wparam, lparam); |
| 78 } | 91 } |
| 79 | 92 |
| 80 void EnableBackgroundDraggingSupport(DWORD thread_id) { | 93 void EnableBackgroundDraggingSupport(DWORD thread_id) { |
| 81 // Install a hook procedure to monitor the messages so that we can forward | 94 // Install a hook procedure to monitor the messages so that we can forward |
| 82 // the appropriate ones to the background thread. | 95 // the appropriate ones to the background thread. |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 167 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 155 DCHECK(!drag_drop_thread_.get()); | 168 DCHECK(!drag_drop_thread_.get()); |
| 156 } | 169 } |
| 157 | 170 |
| 158 void WebContentsDragWin::StartDragging(const WebDropData& drop_data, | 171 void WebContentsDragWin::StartDragging(const WebDropData& drop_data, |
| 159 WebDragOperationsMask ops, | 172 WebDragOperationsMask ops, |
| 160 const gfx::ImageSkia& image, | 173 const gfx::ImageSkia& image, |
| 161 const gfx::Vector2d& image_offset) { | 174 const gfx::Vector2d& image_offset) { |
| 162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 175 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 163 | 176 |
| 164 drag_source_ = new WebDragSource(source_window_, web_contents_); | 177 RenderWidgetHostViewWin* rwhv = |
| 178 static_cast<RenderWidgetHostViewWin*>( |
| 179 web_contents_->GetRenderWidgetHostView()); |
| 180 |
| 181 ui::DragDropTypes::DragEventSource event_source; |
| 182 if (rwhv->in_long_press_gesture()) { |
| 183 event_source = ui::DragDropTypes::DRAG_EVENT_SOURCE_TOUCH; |
| 184 touch_initiated_drag_drop = true; |
| 185 } |
| 186 else { |
| 187 event_source = ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE; |
| 188 touch_initiated_drag_drop = false; |
| 189 } |
| 190 |
| 191 drag_source_ = new WebDragSource(source_window_, web_contents_, event_source); |
| 165 | 192 |
| 166 const GURL& page_url = web_contents_->GetURL(); | 193 const GURL& page_url = web_contents_->GetURL(); |
| 167 const std::string& page_encoding = web_contents_->GetEncoding(); | 194 const std::string& page_encoding = web_contents_->GetEncoding(); |
| 168 | 195 |
| 169 // If it is not drag-out, do the drag-and-drop in the current UI thread. | 196 // If it is not drag-out, do the drag-and-drop in the current UI thread. |
| 170 if (drop_data.download_metadata.empty()) { | 197 if (drop_data.download_metadata.empty()) { |
| 171 if (DoDragging(drop_data, ops, page_url, page_encoding, | 198 if (DoDragging(drop_data, ops, page_url, page_encoding, |
| 172 image, image_offset)) | 199 image, image_offset)) |
| 173 EndDragging(); | 200 EndDragging(); |
| 174 return; | 201 return; |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 308 } | 335 } |
| 309 | 336 |
| 310 bool WebContentsDragWin::DoDragging(const WebDropData& drop_data, | 337 bool WebContentsDragWin::DoDragging(const WebDropData& drop_data, |
| 311 WebDragOperationsMask ops, | 338 WebDragOperationsMask ops, |
| 312 const GURL& page_url, | 339 const GURL& page_url, |
| 313 const std::string& page_encoding, | 340 const std::string& page_encoding, |
| 314 const gfx::ImageSkia& image, | 341 const gfx::ImageSkia& image, |
| 315 const gfx::Vector2d& image_offset) { | 342 const gfx::Vector2d& image_offset) { |
| 316 ui::OSExchangeData data; | 343 ui::OSExchangeData data; |
| 317 | 344 |
| 345 RenderWidgetHostViewWin* rwhv = |
| 346 static_cast<RenderWidgetHostViewWin*>( |
| 347 web_contents_->GetRenderWidgetHostView()); |
| 348 // If RWHV has a valid long press gesture, since DoDragDrop will run into |
| 349 // itself loop and start a dragging session if and only if a mouse button |
| 350 // is down and then moves, so we need to programmatically send out |
| 351 // mouse down event and adjust the cursor position for DoDragDrop. |
| 352 if (rwhv->in_long_press_gesture()) |
| 353 SendMouseEventForTouchDnD(); |
| 354 |
| 318 if (!drop_data.download_metadata.empty()) { | 355 if (!drop_data.download_metadata.empty()) { |
| 319 PrepareDragForDownload(drop_data, &data, page_url, page_encoding); | 356 PrepareDragForDownload(drop_data, &data, page_url, page_encoding); |
| 320 | 357 |
| 321 // Set the observer. | 358 // Set the observer. |
| 322 ui::OSExchangeDataProviderWin::GetDataObjectImpl(data)->set_observer(this); | 359 ui::OSExchangeDataProviderWin::GetDataObjectImpl(data)->set_observer(this); |
| 323 } | 360 } |
| 324 | 361 |
| 325 // We set the file contents before the URL because the URL also sets file | 362 // 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 | 363 // contents (to a .URL shortcut). We want to prefer file content data over |
| 327 // a shortcut so we add it first. | 364 // a shortcut so we add it first. |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 | 442 |
| 406 drag_source_->CancelDrag(); | 443 drag_source_->CancelDrag(); |
| 407 } | 444 } |
| 408 | 445 |
| 409 void WebContentsDragWin::CloseThread() { | 446 void WebContentsDragWin::CloseThread() { |
| 410 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 447 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 411 | 448 |
| 412 drag_drop_thread_.reset(); | 449 drag_drop_thread_.reset(); |
| 413 } | 450 } |
| 414 | 451 |
| 452 void WebContentsDragWin::SendMouseEventForTouchDnD() { |
| 453 static const int kMaxAbsoluteCoordinate = 65535; |
| 454 |
| 455 RenderWidgetHostViewWin* rwhv = |
| 456 static_cast<RenderWidgetHostViewWin*>( |
| 457 web_contents_->GetRenderWidgetHostView()); |
| 458 |
| 459 if (!rwhv) |
| 460 return; |
| 461 |
| 462 gfx::Point last_touch_position = rwhv->GetLastTouchEventLocation(); |
| 463 gfx::Screen* screen = gfx::Screen::GetScreenFor(rwhv->GetNativeView()); |
| 464 gfx::Display display = screen->GetDisplayNearestPoint(last_touch_position); |
| 465 int screen_width = display.size().width(); |
| 466 int screen_height = display.size().height(); |
| 467 |
| 468 // Map the screen coordinate to the normalized absolute coordinate. |
| 469 int absolute_x = |
| 470 last_touch_position.x() * kMaxAbsoluteCoordinate / screen_width; |
| 471 int absolute_y = |
| 472 last_touch_position.y() * kMaxAbsoluteCoordinate / screen_height; |
| 473 |
| 474 // Send MOUSEEVENTF_RIGHTDOWN event followed by MOUSEEVENTF_MOVE event. |
| 475 // The reason why not to merge these 2 mouse events into one is, we don't |
| 476 // want DoDragDrop to get mouse event firstly which lead to drop the drag |
| 477 // source. Once a touch point is removed from screen after long press, |
| 478 // a MOUSEEVENTF_RIGHTUP event will be sent out, so we send the down event |
| 479 // paired with it to complete DoDragDrop session. |
| 480 INPUT input = { 0 }; |
| 481 input.type = INPUT_MOUSE; |
| 482 input.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_ABSOLUTE; |
| 483 input.mi.dx = absolute_x; |
| 484 input.mi.dy = absolute_y; |
| 485 ::SendInput(1, &input, sizeof(input)); |
| 486 |
| 487 // Send MOUSEEVENTF_MOVE input event to set cursor to the right position. |
| 488 input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE; |
| 489 ::SendInput(1, &input, sizeof(input)); |
| 490 } |
| 491 |
| 415 void WebContentsDragWin::OnWaitForData() { | 492 void WebContentsDragWin::OnWaitForData() { |
| 416 DCHECK(drag_drop_thread_id_ == base::PlatformThread::CurrentId()); | 493 DCHECK(drag_drop_thread_id_ == base::PlatformThread::CurrentId()); |
| 417 | 494 |
| 418 // When the left button is release and we start to wait for the data, end | 495 // 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 | 496 // 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. | 497 // mode so that it can start to process the normal input events. |
| 421 BrowserThread::PostTask( | 498 BrowserThread::PostTask( |
| 422 BrowserThread::UI, | 499 BrowserThread::UI, |
| 423 FROM_HERE, | 500 FROM_HERE, |
| 424 base::Bind(&WebContentsDragWin::EndDragging, this)); | 501 base::Bind(&WebContentsDragWin::EndDragging, this)); |
| 425 } | 502 } |
| 426 | 503 |
| 427 void WebContentsDragWin::OnDataObjectDisposed() { | 504 void WebContentsDragWin::OnDataObjectDisposed() { |
| 428 DCHECK(drag_drop_thread_id_ == base::PlatformThread::CurrentId()); | 505 DCHECK(drag_drop_thread_id_ == base::PlatformThread::CurrentId()); |
| 429 | 506 |
| 430 // The drag-and-drop thread is only closed after OLE is done with | 507 // The drag-and-drop thread is only closed after OLE is done with |
| 431 // DataObjectImpl. | 508 // DataObjectImpl. |
| 432 BrowserThread::PostTask( | 509 BrowserThread::PostTask( |
| 433 BrowserThread::UI, | 510 BrowserThread::UI, |
| 434 FROM_HERE, | 511 FROM_HERE, |
| 435 base::Bind(&WebContentsDragWin::CloseThread, this)); | 512 base::Bind(&WebContentsDragWin::CloseThread, this)); |
| 436 } | 513 } |
| 437 | 514 |
| 438 // static | 515 // static |
| 439 void WebContentsDragWin::DisableDragDropForTesting() { | 516 void WebContentsDragWin::DisableDragDropForTesting() { |
| 440 run_do_drag_drop = false; | 517 run_do_drag_drop = false; |
| 441 } | 518 } |
| 442 | 519 |
| 443 } // namespace content | 520 } // namespace content |
| OLD | NEW |