os_exchange_data.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #ifndef UI_BASE_DRAGDROP_OS_EXCHANGE_DATA_H_
  5. #define UI_BASE_DRAGDROP_OS_EXCHANGE_DATA_H_
  6. #include <memory>
  7. #include <set>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  11. #include "build/build_config.h"
  12. #include "base/callback_forward.h"
  13. #include "base/component_export.h"
  14. #include "base/files/file_path.h"
  15. #include "ui/base/dragdrop/os_exchange_data_provider.h"
  16. class GURL;
  17. namespace base {
  18. class Pickle;
  19. }
  20. namespace ui {
  21. class ClipboardFormatType;
  22. class DataTransferEndpoint;
  23. struct FileInfo;
  24. ///////////////////////////////////////////////////////////////////////////////
  25. //
  26. // OSExchangeData
  27. // An object that holds interchange data to be sent out to OS services like
  28. // clipboard, drag and drop, etc. This object exposes an API that clients can
  29. // use to specify raw data and its high level type. This object takes care of
  30. // translating that into something the OS can understand.
  31. //
  32. ///////////////////////////////////////////////////////////////////////////////
  33. // NOTE: Support for html and file contents is required by TabContentViewWin.
  34. // TabContentsViewGtk uses a different class to handle drag support that does
  35. // not use OSExchangeData. As such, file contents and html support is only
  36. // compiled on windows.
  37. class COMPONENT_EXPORT(UI_BASE) OSExchangeData {
  38. public:
  39. // Enumeration of the known formats.
  40. enum Format {
  41. STRING = 1 << 0,
  42. URL = 1 << 1,
  43. FILE_NAME = 1 << 2,
  44. PICKLED_DATA = 1 << 3,
  45. FILE_CONTENTS = 1 << 4,
  46. #if defined(USE_AURA)
  47. HTML = 1 << 5,
  48. #endif
  49. #if BUILDFLAG(IS_CHROMEOS)
  50. DATA_TRANSFER_ENDPOINT = 1 << 6,
  51. #endif
  52. };
  53. OSExchangeData();
  54. // Creates an OSExchangeData with the specified provider. OSExchangeData
  55. // takes ownership of the supplied provider.
  56. explicit OSExchangeData(std::unique_ptr<OSExchangeDataProvider> provider);
  57. OSExchangeData(const OSExchangeData&) = delete;
  58. OSExchangeData& operator=(const OSExchangeData&) = delete;
  59. ~OSExchangeData();
  60. // Returns the Provider, which actually stores and manages the data.
  61. const OSExchangeDataProvider& provider() const { return *provider_; }
  62. OSExchangeDataProvider& provider() { return *provider_; }
  63. // Marks drag data as tainted if it originates from the renderer. This is used
  64. // to avoid granting privileges to a renderer when dragging in tainted data,
  65. // since it could allow potential escalation of privileges.
  66. void MarkOriginatedFromRenderer();
  67. bool DidOriginateFromRenderer() const;
  68. // Marks drag data as from privileged WebContents. This is used to
  69. // make sure non-privileged WebContents will not accept drop data from
  70. // privileged WebContents or vise versa.
  71. void MarkAsFromPrivileged();
  72. bool IsFromPrivileged() const;
  73. // These functions add data to the OSExchangeData object of various Chrome
  74. // types. The OSExchangeData object takes care of translating the data into
  75. // a format suitable for exchange with the OS.
  76. // NOTE WELL: Typically, a data object like this will contain only one of the
  77. // following types of data. In cases where more data is held, the
  78. // order in which these functions are called is _important_!
  79. // ---> The order types are added to an OSExchangeData object controls
  80. // the order of enumeration in our IEnumFORMATETC implementation!
  81. // This comes into play when selecting the best (most preferable)
  82. // data type for insertion into a DropTarget.
  83. void SetString(const std::u16string& data);
  84. // A URL can have an optional title in some exchange formats.
  85. void SetURL(const GURL& url, const std::u16string& title);
  86. // A full path to a file.
  87. void SetFilename(const base::FilePath& path);
  88. // Full path to one or more files. See also SetFilenames() in Provider.
  89. void SetFilenames(
  90. const std::vector<FileInfo>& file_names);
  91. // Adds pickled data of the specified format.
  92. void SetPickledData(const ClipboardFormatType& format,
  93. const base::Pickle& data);
  94. // These functions retrieve data of the specified type. If data exists, the
  95. // functions return and the result is in the out parameter. If the data does
  96. // not exist, the out parameter is not touched. The out parameter cannot be
  97. // NULL.
  98. // GetString() returns the plain text representation of the pasteboard
  99. // contents.
  100. bool GetString(std::u16string* data) const;
  101. bool GetURLAndTitle(FilenameToURLPolicy policy,
  102. GURL* url,
  103. std::u16string* title) const;
  104. // Return the path of a file, if available.
  105. bool GetFilename(base::FilePath* path) const;
  106. bool GetFilenames(std::vector<FileInfo>* file_names) const;
  107. bool GetPickledData(const ClipboardFormatType& format,
  108. base::Pickle* data) const;
  109. // Test whether or not data of certain types is present, without actually
  110. // returning anything.
  111. bool HasString() const;
  112. bool HasURL(FilenameToURLPolicy policy) const;
  113. bool HasFile() const;
  114. bool HasFileContents() const;
  115. bool HasCustomFormat(const ClipboardFormatType& format) const;
  116. // Returns true if this OSExchangeData has data in any of the formats in
  117. // |formats| or any custom format in |custom_formats|.
  118. bool HasAnyFormat(int formats,
  119. const std::set<ClipboardFormatType>& types) const;
  120. // Adds the bytes of a file (CFSTR_FILECONTENTS and CFSTR_FILEDESCRIPTOR on
  121. // Windows).
  122. void SetFileContents(const base::FilePath& filename,
  123. const std::string& file_contents);
  124. bool GetFileContents(base::FilePath* filename,
  125. std::string* file_contents) const;
  126. #if BUILDFLAG(IS_WIN)
  127. // Methods used to query and retrieve file data from a drag source
  128. // IDataObject implementation packaging the data with the
  129. // CFSTR_FILEDESCRIPTOR/CFSTR_FILECONTENTS clipboard formats instead of the
  130. // more common CF_HDROP. These formats are intended to represent "virtual
  131. // files," not files that live on the platform file system. For a drop target
  132. // to read the file contents, it must be streamed from the drag source
  133. // application.
  134. // Method that returns true if there are virtual files packaged in the data
  135. // store.
  136. bool HasVirtualFilenames() const;
  137. // Retrieves names of any "virtual files" in the data store packaged using the
  138. // CFSTR_FILEDESCRIPTOR/CFSTR_FILECONTENTS clipboard formats instead of the
  139. // more common CF_HDROP used for "real files." Real files are preferred over
  140. // virtual files here to avoid duplication, as the data store may package
  141. // the same file lists using different formats. GetVirtualFilenames just
  142. // retrieves the display names but not the temp file paths. The temp files
  143. // are only created upon drop via a call to the async method
  144. // GetVirtualFilesAsTempFiles.
  145. bool GetVirtualFilenames(std::vector<FileInfo>* file_names) const;
  146. // Retrieves "virtual file" contents via creation of intermediary temp files.
  147. // Method is called on dropping on the Chromium drop target. Since creating
  148. // the temp files involves file I/O, the method is asynchronous and the caller
  149. // must provide a callback function that receives a vector of pairs of temp
  150. // file paths and display names. Method immediately returns false if there are
  151. // no virtual files in the data object, in which case the callback will never
  152. // be invoked.
  153. // TODO(https://crbug.com/951574): Implement virtual file extraction to
  154. // dynamically stream data to the renderer when File's bytes are actually
  155. // requested
  156. bool GetVirtualFilesAsTempFiles(
  157. base::OnceCallback<void(const std::vector</*temp path*/ std::pair<
  158. base::FilePath,
  159. /*display name*/ base::FilePath>>&)> callback)
  160. const;
  161. #endif
  162. #if defined(USE_AURA)
  163. // Adds a snippet of HTML. |html| is just raw html but this sets both
  164. // text/html and CF_HTML.
  165. void SetHtml(const std::u16string& html, const GURL& base_url);
  166. bool GetHtml(std::u16string* html, GURL* base_url) const;
  167. bool HasHtml() const;
  168. #endif
  169. // Adds a DataTransferEndpoint to represent the source of the data.
  170. // TODO(crbug.com/1142406): Update all drag-and-drop references to set the
  171. // source of the data.
  172. void SetSource(std::unique_ptr<DataTransferEndpoint> data_source);
  173. DataTransferEndpoint* GetSource() const;
  174. private:
  175. // Provides the actual data.
  176. std::unique_ptr<OSExchangeDataProvider> provider_;
  177. };
  178. } // namespace ui
  179. #endif // UI_BASE_DRAGDROP_OS_EXCHANGE_DATA_H_