Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(619)

Side by Side Diff: content/browser/web_contents/web_contents_drag_win.cc

Issue 14122008: Enable touch-initiated drag-drop work on Windows (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: hot fixing Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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) {
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 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 (touch_initiated_drag_drop) {
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 (msg->message == WM_RBUTTONUP)
dcheng 2013/05/28 20:34:02 Nit: I think you can just fold this into line 64-6
Hongbo Min 2013/05/29 06:08:04 Done.
girard 2013/05/29 18:57:52 Is this code even reachable given the test in line
Hongbo Min 2013/05/30 01:36:03 Fixed.
82 PostThreadMessage(drag_out_thread_id,
83 msg->message,
84 msg->wParam,
85 msg->lParam);
86
87 if (msg->message == WM_RBUTTONUP || !(GetKeyState(VK_RBUTTON) & 0x8000))
88 mouse_up_received = true;
89 }
90
74 return TRUE; 91 return TRUE;
75 } 92 }
76 } 93 }
77 return CallNextHookEx(msg_hook, code, wparam, lparam); 94 return CallNextHookEx(msg_hook, code, wparam, lparam);
78 } 95 }
79 96
80 void EnableBackgroundDraggingSupport(DWORD thread_id) { 97 void EnableBackgroundDraggingSupport(DWORD thread_id) {
81 // Install a hook procedure to monitor the messages so that we can forward 98 // Install a hook procedure to monitor the messages so that we can forward
82 // the appropriate ones to the background thread. 99 // the appropriate ones to the background thread.
83 drag_out_thread_id = thread_id; 100 drag_out_thread_id = thread_id;
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 171 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
155 DCHECK(!drag_drop_thread_.get()); 172 DCHECK(!drag_drop_thread_.get());
156 } 173 }
157 174
158 void WebContentsDragWin::StartDragging(const WebDropData& drop_data, 175 void WebContentsDragWin::StartDragging(const WebDropData& drop_data,
159 WebDragOperationsMask ops, 176 WebDragOperationsMask ops,
160 const gfx::ImageSkia& image, 177 const gfx::ImageSkia& image,
161 const gfx::Vector2d& image_offset) { 178 const gfx::Vector2d& image_offset) {
162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
163 180
164 drag_source_ = new WebDragSource(source_window_, web_contents_); 181 RenderWidgetHostViewWin* rwhv =
182 static_cast<RenderWidgetHostViewWin*>
183 (web_contents_->GetRenderWidgetHostView());
dcheng 2013/05/28 20:34:02 Nit: ( on previous line.
Hongbo Min 2013/05/29 06:08:04 Done.
184
185 ui::DragDropTypes::DragEventSource event_source;
186 if (rwhv->in_long_press_gesture()) {
187 event_source = ui::DragDropTypes::DRAG_EVENT_SOURCE_TOUCH;
188 touch_initiated_drag_drop = true;
189 }
190 else {
191 event_source = ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE;
192 touch_initiated_drag_drop = false;
193 }
194
195 drag_source_ = new WebDragSource(source_window_, web_contents_, event_source);
165 196
166 const GURL& page_url = web_contents_->GetURL(); 197 const GURL& page_url = web_contents_->GetURL();
167 const std::string& page_encoding = web_contents_->GetEncoding(); 198 const std::string& page_encoding = web_contents_->GetEncoding();
168 199
169 // If it is not drag-out, do the drag-and-drop in the current UI thread. 200 // If it is not drag-out, do the drag-and-drop in the current UI thread.
170 if (drop_data.download_metadata.empty()) { 201 if (drop_data.download_metadata.empty()) {
171 if (DoDragging(drop_data, ops, page_url, page_encoding, 202 if (DoDragging(drop_data, ops, page_url, page_encoding,
172 image, image_offset)) 203 image, image_offset))
173 EndDragging(); 204 EndDragging();
174 return; 205 return;
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 } 339 }
309 340
310 bool WebContentsDragWin::DoDragging(const WebDropData& drop_data, 341 bool WebContentsDragWin::DoDragging(const WebDropData& drop_data,
311 WebDragOperationsMask ops, 342 WebDragOperationsMask ops,
312 const GURL& page_url, 343 const GURL& page_url,
313 const std::string& page_encoding, 344 const std::string& page_encoding,
314 const gfx::ImageSkia& image, 345 const gfx::ImageSkia& image,
315 const gfx::Vector2d& image_offset) { 346 const gfx::Vector2d& image_offset) {
316 ui::OSExchangeData data; 347 ui::OSExchangeData data;
317 348
349 RenderWidgetHostViewWin* rwhv =
350 static_cast<RenderWidgetHostViewWin*>(
351 web_contents_->GetRenderWidgetHostView());
352 // If RWHV has a valid long press gesture, since DoDragDrop will run into
353 // itself loop and start a dragging session if and only if a mouse button
354 // is down and then moves, so we need to programmatically send out
355 // mouse down event and adjust the cursor position for DoDragDrop.
356 if (rwhv->in_long_press_gesture())
357 SendMouseEventForTouchDnD();
358
318 if (!drop_data.download_metadata.empty()) { 359 if (!drop_data.download_metadata.empty()) {
319 PrepareDragForDownload(drop_data, &data, page_url, page_encoding); 360 PrepareDragForDownload(drop_data, &data, page_url, page_encoding);
320 361
321 // Set the observer. 362 // Set the observer.
322 ui::OSExchangeDataProviderWin::GetDataObjectImpl(data)->set_observer(this); 363 ui::OSExchangeDataProviderWin::GetDataObjectImpl(data)->set_observer(this);
323 } 364 }
324 365
325 // We set the file contents before the URL because the URL also sets file 366 // 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 367 // contents (to a .URL shortcut). We want to prefer file content data over
327 // a shortcut so we add it first. 368 // a shortcut so we add it first.
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 446
406 drag_source_->CancelDrag(); 447 drag_source_->CancelDrag();
407 } 448 }
408 449
409 void WebContentsDragWin::CloseThread() { 450 void WebContentsDragWin::CloseThread() {
410 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 451 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
411 452
412 drag_drop_thread_.reset(); 453 drag_drop_thread_.reset();
413 } 454 }
414 455
456 void WebContentsDragWin::SendMouseEventForTouchDnD() {
457 static const int kMaxAbsoluteCoordinate = 65535;
458
459 RenderWidgetHostViewWin* rwhv =
460 static_cast<RenderWidgetHostViewWin*>(
461 web_contents_->GetRenderWidgetHostView());
462
463 if (!rwhv)
464 return;
465
466 int screen_width = ::GetSystemMetrics(SM_CXSCREEN);
467 int screen_height = ::GetSystemMetrics(SM_CYSCREEN);
dcheng 2013/05/28 20:34:02 Nit: extra space.
Hongbo Min 2013/05/29 06:08:04 Use gfx::Screen and gfx::Display API instead now.
468 gfx::Point last_touch_position = rwhv->GetLastTouchEventLocation();
469
470 // Map the screen coordinate to the normalized absolute coordinate.
471 int absolute_x =
472 last_touch_position.x() * kMaxAbsoluteCoordinate / screen_width;
473 int absolute_y =
474 last_touch_position.y() * kMaxAbsoluteCoordinate / screen_height;
475
476 // Send MOUSEEVENTF_RIGHTDOWN event followed by MOUSEEVENTF_MOVE event.
477 // The reason why not to merge these 2 mouse events into one is, we don't
478 // want DoDragDrop to get mouse event firstly which lead to drop the drag
479 // source. Once a touch point is removed from screen after long press,
480 // a MOUSEEVENTF_RIGHTUP event will be sent out, so we send the down event
481 // paired with it to complete DoDragDrop session.
482 INPUT input = { 0 };
483 input.type = INPUT_MOUSE;
484 input.mi.dwFlags = MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_ABSOLUTE;
485 input.mi.dx = absolute_x;
486 input.mi.dy = absolute_y;
487 ::SendInput(1, &input, sizeof(input));
488
489 // Send MOUSEEVENTF_MOVE input event to set cursor to the right position.
490 input.mi.dwFlags = MOUSEEVENTF_MOVE | MOUSEEVENTF_ABSOLUTE;
491 ::SendInput(1, &input, sizeof(input));
492 }
493
415 void WebContentsDragWin::OnWaitForData() { 494 void WebContentsDragWin::OnWaitForData() {
416 DCHECK(drag_drop_thread_id_ == base::PlatformThread::CurrentId()); 495 DCHECK(drag_drop_thread_id_ == base::PlatformThread::CurrentId());
417 496
418 // When the left button is release and we start to wait for the data, end 497 // 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 498 // 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. 499 // mode so that it can start to process the normal input events.
421 BrowserThread::PostTask( 500 BrowserThread::PostTask(
422 BrowserThread::UI, 501 BrowserThread::UI,
423 FROM_HERE, 502 FROM_HERE,
424 base::Bind(&WebContentsDragWin::EndDragging, this)); 503 base::Bind(&WebContentsDragWin::EndDragging, this));
425 } 504 }
426 505
427 void WebContentsDragWin::OnDataObjectDisposed() { 506 void WebContentsDragWin::OnDataObjectDisposed() {
428 DCHECK(drag_drop_thread_id_ == base::PlatformThread::CurrentId()); 507 DCHECK(drag_drop_thread_id_ == base::PlatformThread::CurrentId());
429 508
430 // The drag-and-drop thread is only closed after OLE is done with 509 // The drag-and-drop thread is only closed after OLE is done with
431 // DataObjectImpl. 510 // DataObjectImpl.
432 BrowserThread::PostTask( 511 BrowserThread::PostTask(
433 BrowserThread::UI, 512 BrowserThread::UI,
434 FROM_HERE, 513 FROM_HERE,
435 base::Bind(&WebContentsDragWin::CloseThread, this)); 514 base::Bind(&WebContentsDragWin::CloseThread, this));
436 } 515 }
437 516
438 // static 517 // static
439 void WebContentsDragWin::DisableDragDropForTesting() { 518 void WebContentsDragWin::DisableDragDropForTesting() {
440 run_do_drag_drop = false; 519 run_do_drag_drop = false;
441 } 520 }
442 521
443 } // namespace content 522 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698