Home | History | Annotate | Download | only in download
      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/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"
     21 
     22 #if defined(OS_WIN) && !defined(USE_AURA)
     23 #include "ui/base/dragdrop/drag_source_win.h"
     24 #include "ui/base/dragdrop/os_exchange_data_provider_win.h"
     25 #endif
     26 
     27 #if defined(USE_AURA)
     28 #include "ui/aura/client/drag_drop_client.h"
     29 #include "ui/aura/root_window.h"
     30 #include "ui/aura/window.h"
     31 #endif
     32 
     33 #if defined(OS_CHROMEOS)
     34 #include "chrome/browser/chromeos/drive/download_handler.h"
     35 #endif
     36 
     37 void DragDownloadItem(const content::DownloadItem* download,
     38                       gfx::Image* icon,
     39                       gfx::NativeView view) {
     40   DCHECK(download);
     41   DCHECK_EQ(content::DownloadItem::COMPLETE, download->GetState());
     42 
     43   // Set up our OLE machinery
     44   ui::OSExchangeData data;
     45 
     46   if (icon) {
     47     drag_utils::CreateDragImageForFile(
     48         download->GetFileNameToReportUser(), icon->ToImageSkia(), &data);
     49   }
     50 
     51   base::FilePath full_path = download->GetTargetFilePath();
     52 #if defined(OS_CHROMEOS)
     53   // Overwrite |full_path| with drive cache file path when appropriate.
     54   Profile* profile = Profile::FromBrowserContext(download->GetBrowserContext());
     55   drive::DownloadHandler* drive_download_handler =
     56       drive::DownloadHandler::GetForProfile(profile);
     57   if (drive_download_handler &&
     58       drive_download_handler->IsDriveDownload(download))
     59     full_path = drive_download_handler->GetCacheFilePath(download);
     60 #endif
     61   std::vector<ui::OSExchangeData::FileInfo> file_infos;
     62   file_infos.push_back(ui::OSExchangeData::FileInfo(
     63       full_path, download->GetFileNameToReportUser()));
     64   data.SetFilenames(file_infos);
     65 
     66   // Add URL so that we can load supported files when dragged to WebContents.
     67   data.SetURL(net::FilePathToFileURL(full_path),
     68               download->GetFileNameToReportUser().LossyDisplayName());
     69 
     70 #if !defined(TOOLKIT_GTK)
     71 #if defined(USE_AURA)
     72   aura::Window* root_window = view->GetRootWindow();
     73   if (!root_window || !aura::client::GetDragDropClient(root_window))
     74     return;
     75 
     76   gfx::Point location = gfx::Screen::GetScreenFor(view)->GetCursorScreenPoint();
     77   // TODO(varunjain): Properly determine and send DRAG_EVENT_SOURCE below.
     78   aura::client::GetDragDropClient(root_window)->StartDragAndDrop(
     79       data,
     80       root_window,
     81       view,
     82       location,
     83       ui::DragDropTypes::DRAG_COPY | ui::DragDropTypes::DRAG_LINK,
     84       ui::DragDropTypes::DRAG_EVENT_SOURCE_MOUSE);
     85 #else  // We are on WIN without AURA
     86   // We cannot use Widget::RunShellDrag on WIN since the |view| is backed by a
     87   // WebContentsViewWin, not a NativeWidgetWin.
     88   scoped_refptr<ui::DragSourceWin> drag_source(new ui::DragSourceWin);
     89   // Run the drag and drop loop
     90   DWORD effects;
     91   DoDragDrop(ui::OSExchangeDataProviderWin::GetIDataObject(data),
     92              drag_source.get(),
     93              DROPEFFECT_COPY | DROPEFFECT_LINK,
     94              &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