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