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/file_info.h"
16 #include "ui/base/dragdrop/os_exchange_data.h"
17 #include "ui/gfx/image/image.h"
18 #include "ui/gfx/image/image_skia.h"
19 #include "ui/gfx/point.h"
20 #include "ui/gfx/screen.h"
21 #include "ui/views/widget/widget.h"
25 #include "ui/aura/window.h"
26 #include "ui/aura/window_event_dispatcher.h"
27 #include "ui/wm/public/drag_drop_client.h"
30 #if defined(OS_CHROMEOS)
31 #include "chrome/browser/chromeos/drive/download_handler.h"
34 void DragDownloadItem(const content::DownloadItem* download,
36 gfx::NativeView view) {
38 DCHECK_EQ(content::DownloadItem::COMPLETE, download->GetState());
40 // Set up our OLE machinery
41 ui::OSExchangeData data;
43 drag_utils::CreateDragImageForFile(
44 download->GetFileNameToReportUser(),
45 icon ? icon->AsImageSkia() : gfx::ImageSkia(),
48 base::FilePath full_path = download->GetTargetFilePath();
49 #if defined(OS_CHROMEOS)
50 // Overwrite |full_path| with drive cache file path when appropriate.
51 Profile* profile = Profile::FromBrowserContext(download->GetBrowserContext());
52 drive::DownloadHandler* drive_download_handler =
53 drive::DownloadHandler::GetForProfile(profile);
54 if (drive_download_handler &&
55 drive_download_handler->IsDriveDownload(download))
56 full_path = drive_download_handler->GetCacheFilePath(download);
58 std::vector<ui::FileInfo> file_infos;
60 ui::FileInfo(full_path, download->GetFileNameToReportUser()));
61 data.SetFilenames(file_infos);
63 // Add URL so that we can load supported files when dragged to WebContents.
64 data.SetURL(net::FilePathToFileURL(full_path),
65 download->GetFileNameToReportUser().LossyDisplayName());
67 #if !defined(TOOLKIT_GTK)
69 aura::Window* root_window = view->GetRootWindow();
70 if (!root_window || !aura::client::GetDragDropClient(root_window))
73 gfx::Point location = gfx::Screen::GetScreenFor(view)->GetCursorScreenPoint();
74 // TODO(varunjain): Properly determine and send DRAG_EVENT_SOURCE below.
75 aura::client::GetDragDropClient(root_window)->StartDragAndDrop(
80 ui::DragDropTypes::DRAG_COPY | ui::DragDropTypes::DRAG_LINK,
81 ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE);
82 #else // We are on WIN without AURA
83 // We cannot use Widget::RunShellDrag on WIN since the |view| is backed by a
84 // WebContentsViewWin, not a NativeWidgetWin.
85 scoped_refptr<ui::DragSourceWin> drag_source(new ui::DragSourceWin);
86 // Run the drag and drop loop
88 DoDragDrop(ui::OSExchangeDataProviderWin::GetIDataObject(data),
90 DROPEFFECT_COPY | DROPEFFECT_LINK,
95 GtkWidget* root = gtk_widget_get_toplevel(view);
99 views::NativeWidgetGtk* widget = static_cast<views::NativeWidgetGtk*>(
100 views::Widget::GetWidgetForNativeView(root)->native_widget());
105 ui::DragDropTypes::DRAG_COPY | ui::DragDropTypes::DRAG_LINK);
106 #endif // TOOLKIT_GTK