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 3 // found in the LICENSE file. 4 5 #ifndef CONTENT_BROWSER_WEB_CONTENTS_WEB_CONTENTS_DRAG_WIN_H_ 6 #define CONTENT_BROWSER_WEB_CONTENTS_WEB_CONTENTS_DRAG_WIN_H_ 7 8 #include "base/callback.h" 9 #include "base/compiler_specific.h" 10 #include "base/memory/ref_counted.h" 11 #include "base/memory/scoped_ptr.h" 12 #include "base/threading/platform_thread.h" 13 #include "content/common/content_export.h" 14 #include "third_party/WebKit/public/web/WebDragOperation.h" 15 #include "ui/base/dragdrop/os_exchange_data_provider_win.h" 16 #include "ui/gfx/native_widget_types.h" 17 #include "ui/gfx/point.h" 18 19 class SkBitmap; 20 21 namespace gfx { 22 class ImageSkia; 23 } 24 25 namespace content { 26 class DragDropThread; 27 class WebContents; 28 class WebDragDest; 29 class WebDragSource; 30 struct DropData; 31 32 // Windows-specific drag-and-drop handling in WebContentsView. 33 // If we are dragging a virtual file out of the browser, we use a background 34 // thread to do the drag-and-drop because we do not want to run nested 35 // message loop in the UI thread. For all other cases, the drag-and-drop happens 36 // in the UI thread. 37 class CONTENT_EXPORT WebContentsDragWin 38 : NON_EXPORTED_BASE(public ui::DataObjectImpl::Observer), 39 public base::RefCountedThreadSafe<WebContentsDragWin> { 40 public: 41 WebContentsDragWin(gfx::NativeWindow source_window, 42 WebContents* web_contents, 43 WebDragDest* drag_dest, 44 const base::Callback<void()>& drag_end_callback); 45 virtual ~WebContentsDragWin(); 46 47 // Called on UI thread. 48 void StartDragging(const DropData& drop_data, 49 WebKit::WebDragOperationsMask ops, 50 const gfx::ImageSkia& image, 51 const gfx::Vector2d& image_offset); 52 void CancelDrag(); 53 54 // DataObjectImpl::Observer implementation. 55 // Called on drag-and-drop thread. 56 virtual void OnWaitForData(); 57 virtual void OnDataObjectDisposed(); 58 59 // Don't invoke OLE DoDragDrop during tests. 60 static void DisableDragDropForTesting(); 61 62 private: 63 // Called on either UI thread or drag-and-drop thread. 64 void PrepareDragForDownload(const DropData& drop_data, 65 ui::OSExchangeData* data, 66 const GURL& page_url, 67 const std::string& page_encoding); 68 void PrepareDragForFileContents(const DropData& drop_data, 69 ui::OSExchangeData* data); 70 void PrepareDragForUrl(const DropData& drop_data, 71 ui::OSExchangeData* data); 72 // Returns false if the source window becomes invalid when the drag ends. 73 // This could happen when the window gets destroyed when the drag is still in 74 // progress. No further processing should be done beyond this return point 75 // because the instance has been destroyed. 76 bool DoDragging(const DropData& drop_data, 77 WebKit::WebDragOperationsMask ops, 78 const GURL& page_url, 79 const std::string& page_encoding, 80 const gfx::ImageSkia& image, 81 const gfx::Vector2d& image_offset); 82 83 // Called on drag-and-drop thread. 84 void StartBackgroundDragging(const DropData& drop_data, 85 WebKit::WebDragOperationsMask ops, 86 const GURL& page_url, 87 const std::string& page_encoding, 88 const gfx::ImageSkia& image, 89 const gfx::Vector2d& image_offset); 90 // Called on UI thread. 91 void EndDragging(); 92 void CloseThread(); 93 94 // For debug check only. Access only on drag-and-drop thread. 95 base::PlatformThreadId drag_drop_thread_id_; 96 97 // All the member variables below are accessed on UI thread. 98 99 gfx::NativeWindow source_window_; 100 WebContents* web_contents_; 101 WebDragDest* drag_dest_; 102 103 // |drag_source_| is our callback interface passed to the system when we 104 // want to initiate a drag and drop operation. We use it to tell if a 105 // drag operation is happening. 106 scoped_refptr<WebDragSource> drag_source_; 107 108 // The thread used by the drag-out download. This is because we want to avoid 109 // running nested message loop in main UI thread. 110 scoped_ptr<DragDropThread> drag_drop_thread_; 111 112 // The flag to guard that EndDragging is not called twice. 113 bool drag_ended_; 114 115 base::Callback<void()> drag_end_callback_; 116 117 DISALLOW_COPY_AND_ASSIGN(WebContentsDragWin); 118 }; 119 120 } // namespace content 121 122 #endif // CONTENT_BROWSER_WEB_CONTENTS_WEB_CONTENTS_DRAG_WIN_H_ 123