Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / download / drag_download_item_views.cc
blob72d6390e9ecab753748885c30bbbceceaf8a6315
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"
7 #include <string>
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"
20 #include "url/gurl.h"
22 #if defined(USE_AURA)
23 #include "ui/aura/client/drag_drop_client.h"
24 #include "ui/aura/root_window.h"
25 #include "ui/aura/window.h"
26 #endif
28 #if defined(OS_CHROMEOS)
29 #include "chrome/browser/chromeos/drive/download_handler.h"
30 #endif
32 void DragDownloadItem(const content::DownloadItem* download,
33 gfx::Image* icon,
34 gfx::NativeView view) {
35 DCHECK(download);
36 DCHECK_EQ(content::DownloadItem::COMPLETE, download->GetState());
38 // Set up our OLE machinery
39 ui::OSExchangeData data;
41 if (icon) {
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);
55 #endif
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)
66 #if defined(USE_AURA)
67 aura::Window* root_window = view->GetRootWindow();
68 if (!root_window || !aura::client::GetDragDropClient(root_window))
69 return;
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(
74 data,
75 root_window,
76 view,
77 location,
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
85 DWORD effects;
86 DoDragDrop(ui::OSExchangeDataProviderWin::GetIDataObject(data),
87 drag_source.get(),
88 DROPEFFECT_COPY | DROPEFFECT_LINK,
89 &effects);
90 #endif
92 #else
93 GtkWidget* root = gtk_widget_get_toplevel(view);
94 if (!root)
95 return;
97 views::NativeWidgetGtk* widget = static_cast<views::NativeWidgetGtk*>(
98 views::Widget::GetWidgetForNativeView(root)->native_widget());
99 if (!widget)
100 return;
102 widget->DoDrag(data,
103 ui::DragDropTypes::DRAG_COPY | ui::DragDropTypes::DRAG_LINK);
104 #endif // TOOLKIT_GTK