Home | History | Annotate | Download | only in dragdrop
      1 // Copyright (c) 2012 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 #ifndef UI_BASE_DRAGDROP_OS_EXCHANGE_DATA_H_
      6 #define UI_BASE_DRAGDROP_OS_EXCHANGE_DATA_H_
      7 
      8 #include "build/build_config.h"
      9 
     10 #include <set>
     11 #include <string>
     12 
     13 #if defined(OS_WIN)
     14 #include <objidl.h>
     15 #elif defined(TOOLKIT_GTK)
     16 #include <gtk/gtk.h>
     17 #endif
     18 
     19 #include "base/basictypes.h"
     20 #include "base/files/file_path.h"
     21 #include "base/memory/scoped_ptr.h"
     22 #include "ui/base/clipboard/clipboard.h"
     23 #include "ui/base/dragdrop/download_file_interface.h"
     24 #include "ui/base/ui_export.h"
     25 
     26 class GURL;
     27 class Pickle;
     28 
     29 namespace gfx {
     30 class ImageSkia;
     31 class Vector2d;
     32 }
     33 
     34 namespace ui {
     35 
     36 ///////////////////////////////////////////////////////////////////////////////
     37 //
     38 // OSExchangeData
     39 //  An object that holds interchange data to be sent out to OS services like
     40 //  clipboard, drag and drop, etc. This object exposes an API that clients can
     41 //  use to specify raw data and its high level type. This object takes care of
     42 //  translating that into something the OS can understand.
     43 //
     44 ///////////////////////////////////////////////////////////////////////////////
     45 
     46 // NOTE: Support for html and file contents is required by TabContentViewWin.
     47 // TabContentsViewGtk uses a different class to handle drag support that does
     48 // not use OSExchangeData. As such, file contents and html support is only
     49 // compiled on windows.
     50 class UI_EXPORT OSExchangeData {
     51  public:
     52   // CustomFormats are used for non-standard data types. For example, bookmark
     53   // nodes are written using a CustomFormat.
     54   // TODO(dcheng): Remove this completely and just use Clipboard::FormatType.
     55   typedef Clipboard::FormatType CustomFormat;
     56 
     57   // Enumeration of the known formats.
     58   enum Format {
     59     STRING         = 1 << 0,
     60     URL            = 1 << 1,
     61     FILE_NAME      = 1 << 2,
     62     PICKLED_DATA   = 1 << 3,
     63 #if defined(OS_WIN)
     64     FILE_CONTENTS  = 1 << 4,
     65 #endif
     66 #if defined(OS_WIN) || defined(USE_AURA)
     67     HTML           = 1 << 5,
     68 #endif
     69   };
     70 
     71   // Controls whether or not filenames should be converted to file: URLs when
     72   // getting a URL.
     73   enum FilenameToURLPolicy { CONVERT_FILENAMES, DO_NOT_CONVERT_FILENAMES, };
     74 
     75   // Encapsulates the info about a file to be downloaded.
     76   struct UI_EXPORT DownloadFileInfo {
     77     DownloadFileInfo(const base::FilePath& filename,
     78                      DownloadFileProvider* downloader);
     79     ~DownloadFileInfo();
     80 
     81     base::FilePath filename;
     82     scoped_refptr<DownloadFileProvider> downloader;
     83   };
     84 
     85   // Encapsulates the info about a file.
     86   struct UI_EXPORT FileInfo {
     87     FileInfo(const base::FilePath& path, const base::FilePath& display_name);
     88     ~FileInfo();
     89 
     90     // The path of the file.
     91     base::FilePath path;
     92     // The display name of the file. This field is optional.
     93     base::FilePath display_name;
     94   };
     95 
     96   // Provider defines the platform specific part of OSExchangeData that
     97   // interacts with the native system.
     98   class UI_EXPORT Provider {
     99    public:
    100     Provider() {}
    101     virtual ~Provider() {}
    102 
    103     virtual Provider* Clone() const = 0;
    104 
    105     virtual void SetString(const base::string16& data) = 0;
    106     virtual void SetURL(const GURL& url, const base::string16& title) = 0;
    107     virtual void SetFilename(const base::FilePath& path) = 0;
    108     virtual void SetFilenames(
    109         const std::vector<FileInfo>& file_names) = 0;
    110     virtual void SetPickledData(const CustomFormat& format,
    111                                 const Pickle& data) = 0;
    112 
    113     virtual bool GetString(base::string16* data) const = 0;
    114     virtual bool GetURLAndTitle(FilenameToURLPolicy policy,
    115                                 GURL* url,
    116                                 base::string16* title) const = 0;
    117     virtual bool GetFilename(base::FilePath* path) const = 0;
    118     virtual bool GetFilenames(
    119         std::vector<FileInfo>* file_names) const = 0;
    120     virtual bool GetPickledData(const CustomFormat& format,
    121                                 Pickle* data) const = 0;
    122 
    123     virtual bool HasString() const = 0;
    124     virtual bool HasURL() const = 0;
    125     virtual bool HasFile() const = 0;
    126     virtual bool HasCustomFormat(const CustomFormat& format) const = 0;
    127 
    128 #if defined(OS_WIN)
    129     virtual void SetFileContents(const base::FilePath& filename,
    130                                  const std::string& file_contents) = 0;
    131     virtual bool GetFileContents(base::FilePath* filename,
    132                                  std::string* file_contents) const = 0;
    133     virtual bool HasFileContents() const = 0;
    134     virtual void SetDownloadFileInfo(const DownloadFileInfo& download) = 0;
    135     virtual void SetInDragLoop(bool in_drag_loop) = 0;
    136 #endif
    137 
    138 #if defined(OS_WIN) || defined(USE_AURA)
    139     virtual void SetHtml(const base::string16& html, const GURL& base_url) = 0;
    140     virtual bool GetHtml(base::string16* html, GURL* base_url) const = 0;
    141     virtual bool HasHtml() const = 0;
    142 #endif
    143 
    144 #if defined(USE_AURA)
    145     virtual void SetDragImage(const gfx::ImageSkia& image,
    146                               const gfx::Vector2d& cursor_offset) = 0;
    147     virtual const gfx::ImageSkia& GetDragImage() const = 0;
    148     virtual const gfx::Vector2d& GetDragImageOffset() const = 0;
    149 #endif
    150   };
    151 
    152   // Creates the platform specific Provider.
    153   static Provider* CreateProvider();
    154 
    155   OSExchangeData();
    156   // Creates an OSExchangeData with the specified provider. OSExchangeData
    157   // takes ownership of the supplied provider.
    158   explicit OSExchangeData(Provider* provider);
    159 
    160   ~OSExchangeData();
    161 
    162   // Returns the Provider, which actually stores and manages the data.
    163   const Provider& provider() const { return *provider_; }
    164   Provider& provider() { return *provider_; }
    165 
    166   // These functions add data to the OSExchangeData object of various Chrome
    167   // types. The OSExchangeData object takes care of translating the data into
    168   // a format suitable for exchange with the OS.
    169   // NOTE WELL: Typically, a data object like this will contain only one of the
    170   //            following types of data. In cases where more data is held, the
    171   //            order in which these functions are called is _important_!
    172   //       ---> The order types are added to an OSExchangeData object controls
    173   //            the order of enumeration in our IEnumFORMATETC implementation!
    174   //            This comes into play when selecting the best (most preferable)
    175   //            data type for insertion into a DropTarget.
    176   void SetString(const base::string16& data);
    177   // A URL can have an optional title in some exchange formats.
    178   void SetURL(const GURL& url, const base::string16& title);
    179   // A full path to a file.
    180   void SetFilename(const base::FilePath& path);
    181   // Full path to one or more files. See also SetFilenames() in Provider.
    182   void SetFilenames(
    183       const std::vector<FileInfo>& file_names);
    184   // Adds pickled data of the specified format.
    185   void SetPickledData(const CustomFormat& format, const Pickle& data);
    186 
    187   // These functions retrieve data of the specified type. If data exists, the
    188   // functions return and the result is in the out parameter. If the data does
    189   // not exist, the out parameter is not touched. The out parameter cannot be
    190   // NULL.
    191   bool GetString(base::string16* data) const;
    192   bool GetURLAndTitle(FilenameToURLPolicy policy,
    193                       GURL* url,
    194                       base::string16* title) const;
    195   // Return the path of a file, if available.
    196   bool GetFilename(base::FilePath* path) const;
    197   bool GetFilenames(
    198       std::vector<FileInfo>* file_names) const;
    199   bool GetPickledData(const CustomFormat& format, Pickle* data) const;
    200 
    201   // Test whether or not data of certain types is present, without actually
    202   // returning anything.
    203   bool HasString() const;
    204   bool HasURL() const;
    205   bool HasFile() const;
    206   bool HasCustomFormat(const CustomFormat& format) const;
    207 
    208   // Returns true if this OSExchangeData has data for ALL the formats in
    209   // |formats| and ALL the custom formats in |custom_formats|.
    210   bool HasAllFormats(int formats,
    211                      const std::set<CustomFormat>& custom_formats) const;
    212 
    213   // Returns true if this OSExchangeData has data in any of the formats in
    214   // |formats| or any custom format in |custom_formats|.
    215   bool HasAnyFormat(int formats,
    216                      const std::set<CustomFormat>& custom_formats) const;
    217 
    218 #if defined(OS_WIN)
    219   // Adds the bytes of a file (CFSTR_FILECONTENTS and CFSTR_FILEDESCRIPTOR).
    220   void SetFileContents(const base::FilePath& filename,
    221                        const std::string& file_contents);
    222   bool GetFileContents(base::FilePath* filename,
    223                        std::string* file_contents) const;
    224 
    225   // Adds a download file with full path (CF_HDROP).
    226   void SetDownloadFileInfo(const DownloadFileInfo& download);
    227 
    228   void SetInDragLoop(bool in_drag_loop);
    229 #endif
    230 
    231 #if defined(OS_WIN) || defined(USE_AURA)
    232   // Adds a snippet of HTML.  |html| is just raw html but this sets both
    233   // text/html and CF_HTML.
    234   void SetHtml(const base::string16& html, const GURL& base_url);
    235   bool GetHtml(base::string16* html, GURL* base_url) const;
    236 #endif
    237 
    238  private:
    239   // Provides the actual data.
    240   scoped_ptr<Provider> provider_;
    241 
    242   DISALLOW_COPY_AND_ASSIGN(OSExchangeData);
    243 };
    244 
    245 }  // namespace ui
    246 
    247 #endif  // UI_BASE_DRAGDROP_OS_EXCHANGE_DATA_H_
    248