OLD | NEW |
| (Empty) |
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 #include "chrome/browser/download/download_util.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "content/public/browser/download_item.h" | |
10 #include "net/base/mime_util.h" | |
11 #include "net/base/net_util.h" | |
12 #include "ui/gfx/image/image.h" | |
13 #include "ui/gfx/point.h" | |
14 #include "url/gurl.h" | |
15 | |
16 #if defined(TOOLKIT_VIEWS) | |
17 #include "ui/base/dragdrop/drag_drop_types.h" | |
18 #include "ui/base/dragdrop/drag_utils.h" | |
19 #include "ui/base/dragdrop/os_exchange_data.h" | |
20 #include "ui/gfx/screen.h" | |
21 #include "ui/views/widget/widget.h" | |
22 #endif | |
23 | |
24 #if defined(TOOLKIT_GTK) | |
25 #include "chrome/browser/ui/gtk/custom_drag.h" | |
26 #endif // defined(TOOLKIT_GTK) | |
27 | |
28 #if defined(OS_WIN) && !defined(USE_AURA) | |
29 #include "ui/base/dragdrop/drag_source_win.h" | |
30 #include "ui/base/dragdrop/os_exchange_data_provider_win.h" | |
31 #endif | |
32 | |
33 #if defined(USE_AURA) | |
34 #include "ui/aura/client/drag_drop_client.h" | |
35 #include "ui/aura/root_window.h" | |
36 #include "ui/aura/window.h" | |
37 #endif | |
38 | |
39 namespace download_util { | |
40 | |
41 using content::DownloadItem; | |
42 | |
43 #if defined(TOOLKIT_VIEWS) | |
44 // Download dragging | |
45 void DragDownload(const DownloadItem* download, | |
46 gfx::Image* icon, | |
47 gfx::NativeView view) { | |
48 DCHECK(download); | |
49 DCHECK_EQ(DownloadItem::COMPLETE, download->GetState()); | |
50 | |
51 // Set up our OLE machinery | |
52 ui::OSExchangeData data; | |
53 | |
54 if (icon) { | |
55 drag_utils::CreateDragImageForFile( | |
56 download->GetFileNameToReportUser(), icon->ToImageSkia(), &data); | |
57 } | |
58 | |
59 const base::FilePath full_path = download->GetTargetFilePath(); | |
60 data.SetFilename(full_path); | |
61 | |
62 std::string mime_type = download->GetMimeType(); | |
63 if (mime_type.empty()) | |
64 net::GetMimeTypeFromFile(full_path, &mime_type); | |
65 | |
66 // Add URL so that we can load supported files when dragged to WebContents. | |
67 if (net::IsSupportedMimeType(mime_type)) { | |
68 data.SetURL(net::FilePathToFileURL(full_path), | |
69 download->GetFileNameToReportUser().LossyDisplayName()); | |
70 } | |
71 | |
72 #if !defined(TOOLKIT_GTK) | |
73 #if defined(USE_AURA) | |
74 aura::RootWindow* root_window = view->GetRootWindow(); | |
75 if (!root_window || !aura::client::GetDragDropClient(root_window)) | |
76 return; | |
77 | |
78 gfx::Point location = gfx::Screen::GetScreenFor(view)->GetCursorScreenPoint(); | |
79 // TODO(varunjain): Properly determine and send DRAG_EVENT_SOURCE below. | |
80 aura::client::GetDragDropClient(root_window)->StartDragAndDrop( | |
81 data, | |
82 root_window, | |
83 view, | |
84 location, | |
85 ui::DragDropTypes::DRAG_COPY | ui::DragDropTypes::DRAG_LINK, | |
86 ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE); | |
87 #else // We are on WIN without AURA | |
88 // We cannot use Widget::RunShellDrag on WIN since the |view| is backed by a | |
89 // WebContentsViewWin, not a NativeWidgetWin. | |
90 scoped_refptr<ui::DragSourceWin> drag_source(new ui::DragSourceWin); | |
91 // Run the drag and drop loop | |
92 DWORD effects; | |
93 DoDragDrop(ui::OSExchangeDataProviderWin::GetIDataObject(data), | |
94 drag_source.get(), DROPEFFECT_COPY | DROPEFFECT_LINK, &effects); | |
95 #endif | |
96 | |
97 #else | |
98 GtkWidget* root = gtk_widget_get_toplevel(view); | |
99 if (!root) | |
100 return; | |
101 | |
102 views::NativeWidgetGtk* widget = static_cast<views::NativeWidgetGtk*>( | |
103 views::Widget::GetWidgetForNativeView(root)->native_widget()); | |
104 if (!widget) | |
105 return; | |
106 | |
107 widget->DoDrag(data, | |
108 ui::DragDropTypes::DRAG_COPY | ui::DragDropTypes::DRAG_LINK); | |
109 #endif // TOOLKIT_GTK | |
110 } | |
111 #elif defined(USE_X11) | |
112 void DragDownload(const DownloadItem* download, | |
113 gfx::Image* icon, | |
114 gfx::NativeView view) { | |
115 DownloadItemDrag::BeginDrag(download, icon); | |
116 } | |
117 #endif // USE_X11 | |
118 | |
119 } // namespace download_util | |
OLD | NEW |