Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / download / drag_download_item_views.cc
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.
4
5 #include "chrome/browser/download/drag_download_item.h"
6
7 #include <string>
8
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"
22 #include "url/gurl.h"
23
24 #if defined(USE_AURA)
25 #include "ui/aura/window.h"
26 #include "ui/aura/window_event_dispatcher.h"
27 #include "ui/wm/public/drag_drop_client.h"
28 #endif
29
30 #if defined(OS_CHROMEOS)
31 #include "chrome/browser/chromeos/drive/download_handler.h"
32 #endif
33
34 void DragDownloadItem(const content::DownloadItem* download,
35                       gfx::Image* icon,
36                       gfx::NativeView view) {
37   DCHECK(download);
38   DCHECK_EQ(content::DownloadItem::COMPLETE, download->GetState());
39
40   // Set up our OLE machinery
41   ui::OSExchangeData data;
42
43   drag_utils::CreateDragImageForFile(
44       download->GetFileNameToReportUser(),
45       icon ? icon->AsImageSkia() : gfx::ImageSkia(),
46       &data);
47
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);
57 #endif
58   std::vector<ui::FileInfo> file_infos;
59   file_infos.push_back(
60       ui::FileInfo(full_path, download->GetFileNameToReportUser()));
61   data.SetFilenames(file_infos);
62
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());
66
67 #if !defined(TOOLKIT_GTK)
68 #if defined(USE_AURA)
69   aura::Window* root_window = view->GetRootWindow();
70   if (!root_window || !aura::client::GetDragDropClient(root_window))
71     return;
72
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(
76       data,
77       root_window,
78       view,
79       location,
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
87   DWORD effects;
88   DoDragDrop(ui::OSExchangeDataProviderWin::GetIDataObject(data),
89              drag_source.get(),
90              DROPEFFECT_COPY | DROPEFFECT_LINK,
91              &effects);
92 #endif
93
94 #else
95   GtkWidget* root = gtk_widget_get_toplevel(view);
96   if (!root)
97     return;
98
99   views::NativeWidgetGtk* widget = static_cast<views::NativeWidgetGtk*>(
100       views::Widget::GetWidgetForNativeView(root)->native_widget());
101   if (!widget)
102     return;
103
104   widget->DoDrag(data,
105                  ui::DragDropTypes::DRAG_COPY | ui::DragDropTypes::DRAG_LINK);
106 #endif  // TOOLKIT_GTK
107 }