1 // Copyright 2013 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.
5 #include "chrome/browser/download/drag_download_item.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "content/public/browser/download_item.h"
11 #include "net/base/mime_util.h"
12 #include "net/base/net_util.h"
13 #include "ui/base/dragdrop/drag_drop_types.h"
14 #include "ui/base/dragdrop/drag_utils.h"
15 #include "ui/base/dragdrop/os_exchange_data.h"
16 #include "ui/gfx/image/image.h"
17 #include "ui/gfx/point.h"
18 #include "ui/gfx/screen.h"
19 #include "ui/views/widget/widget.h"
23 #include "ui/aura/client/drag_drop_client.h"
24 #include "ui/aura/root_window.h"
25 #include "ui/aura/window.h"
28 #if defined(OS_CHROMEOS)
29 #include "chrome/browser/chromeos/drive/download_handler.h"
32 void DragDownloadItem(const content::DownloadItem
* download
,
34 gfx::NativeView view
) {
36 DCHECK_EQ(content::DownloadItem::COMPLETE
, download
->GetState());
38 // Set up our OLE machinery
39 ui::OSExchangeData data
;
42 drag_utils::CreateDragImageForFile(
43 download
->GetFileNameToReportUser(), icon
->ToImageSkia(), &data
);
46 base::FilePath full_path
= download
->GetTargetFilePath();
47 #if defined(OS_CHROMEOS)
48 // Overwrite |full_path| with drive cache file path when appropriate.
49 Profile
* profile
= Profile::FromBrowserContext(download
->GetBrowserContext());
50 drive::DownloadHandler
* drive_download_handler
=
51 drive::DownloadHandler::GetForProfile(profile
);
52 if (drive_download_handler
&&
53 drive_download_handler
->IsDriveDownload(download
))
54 full_path
= drive_download_handler
->GetCacheFilePath(download
);
56 std::vector
<ui::OSExchangeData::FileInfo
> file_infos
;
57 file_infos
.push_back(ui::OSExchangeData::FileInfo(
58 full_path
, download
->GetFileNameToReportUser()));
59 data
.SetFilenames(file_infos
);
61 // Add URL so that we can load supported files when dragged to WebContents.
62 data
.SetURL(net::FilePathToFileURL(full_path
),
63 download
->GetFileNameToReportUser().LossyDisplayName());
65 #if !defined(TOOLKIT_GTK)
67 aura::Window
* root_window
= view
->GetRootWindow();
68 if (!root_window
|| !aura::client::GetDragDropClient(root_window
))
71 gfx::Point location
= gfx::Screen::GetScreenFor(view
)->GetCursorScreenPoint();
72 // TODO(varunjain): Properly determine and send DRAG_EVENT_SOURCE below.
73 aura::client::GetDragDropClient(root_window
)->StartDragAndDrop(
78 ui::DragDropTypes::DRAG_COPY
| ui::DragDropTypes::DRAG_LINK
,
79 ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE
);
80 #else // We are on WIN without AURA
81 // We cannot use Widget::RunShellDrag on WIN since the |view| is backed by a
82 // WebContentsViewWin, not a NativeWidgetWin.
83 scoped_refptr
<ui::DragSourceWin
> drag_source(new ui::DragSourceWin
);
84 // Run the drag and drop loop
86 DoDragDrop(ui::OSExchangeDataProviderWin::GetIDataObject(data
),
88 DROPEFFECT_COPY
| DROPEFFECT_LINK
,
93 GtkWidget
* root
= gtk_widget_get_toplevel(view
);
97 views::NativeWidgetGtk
* widget
= static_cast<views::NativeWidgetGtk
*>(
98 views::Widget::GetWidgetForNativeView(root
)->native_widget());
103 ui::DragDropTypes::DRAG_COPY
| ui::DragDropTypes::DRAG_LINK
);
104 #endif // TOOLKIT_GTK