os_exchange_data_provider_non_backed.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. #include "ui/base/dragdrop/os_exchange_data_provider_non_backed.h"
  5. #include <memory>
  6. #include <string>
  7. #include "base/check.h"
  8. #include "base/containers/contains.h"
  9. #include "base/files/file_path.h"
  10. #include "base/strings/utf_string_conversions.h"
  11. #include "build/build_config.h"
  12. #include "build/chromeos_buildflags.h"
  13. #include "net/base/filename_util.h"
  14. #include "ui/base/clipboard/clipboard_format_type.h"
  15. #include "ui/base/clipboard/file_info.h"
  16. #include "ui/base/data_transfer_policy/data_transfer_endpoint.h"
  17. #include "ui/base/dragdrop/os_exchange_data.h"
  18. #include "url/gurl.h"
  19. namespace ui {
  20. OSExchangeDataProviderNonBacked::OSExchangeDataProviderNonBacked() = default;
  21. OSExchangeDataProviderNonBacked::~OSExchangeDataProviderNonBacked() = default;
  22. std::unique_ptr<OSExchangeDataProvider> OSExchangeDataProviderNonBacked::Clone()
  23. const {
  24. auto clone = std::make_unique<OSExchangeDataProviderNonBacked>();
  25. CopyData(clone.get());
  26. return clone;
  27. }
  28. void OSExchangeDataProviderNonBacked::MarkOriginatedFromRenderer() {
  29. // TODO(dcheng): Currently unneeded because ChromeOS Aura correctly separates
  30. // URL and filename metadata, and does not implement the DownloadURL protocol.
  31. #if !BUILDFLAG(IS_CHROMEOS_ASH)
  32. originated_from_renderer_ = true;
  33. #endif
  34. }
  35. bool OSExchangeDataProviderNonBacked::DidOriginateFromRenderer() const {
  36. #if BUILDFLAG(IS_CHROMEOS_ASH)
  37. return false;
  38. #else
  39. return originated_from_renderer_;
  40. #endif
  41. }
  42. void OSExchangeDataProviderNonBacked::MarkAsFromPrivileged() {
  43. is_from_privileged_ = true;
  44. }
  45. bool OSExchangeDataProviderNonBacked::IsFromPrivileged() const {
  46. return is_from_privileged_;
  47. }
  48. void OSExchangeDataProviderNonBacked::SetString(const std::u16string& data) {
  49. if (HasString())
  50. return;
  51. string_ = data;
  52. formats_ |= OSExchangeData::STRING;
  53. }
  54. void OSExchangeDataProviderNonBacked::SetURL(const GURL& url,
  55. const std::u16string& title) {
  56. url_ = url;
  57. title_ = title;
  58. formats_ |= OSExchangeData::URL;
  59. SetString(base::UTF8ToUTF16(url.spec()));
  60. }
  61. void OSExchangeDataProviderNonBacked::SetFilename(const base::FilePath& path) {
  62. filenames_.clear();
  63. filenames_.push_back(FileInfo(path, base::FilePath()));
  64. formats_ |= OSExchangeData::FILE_NAME;
  65. }
  66. void OSExchangeDataProviderNonBacked::SetFilenames(
  67. const std::vector<FileInfo>& filenames) {
  68. filenames_ = filenames;
  69. formats_ |= OSExchangeData::FILE_NAME;
  70. }
  71. void OSExchangeDataProviderNonBacked::SetPickledData(
  72. const ClipboardFormatType& format,
  73. const base::Pickle& data) {
  74. pickle_data_[format] = data;
  75. formats_ |= OSExchangeData::PICKLED_DATA;
  76. }
  77. bool OSExchangeDataProviderNonBacked::GetString(std::u16string* data) const {
  78. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  79. if (HasFile()) {
  80. // Various Linux file managers both pass a list of file:// URIs and set the
  81. // string representation to the URI. We explicitly don't want to return use
  82. // this representation.
  83. return false;
  84. }
  85. #endif // BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
  86. if ((formats_ & OSExchangeData::STRING) == 0)
  87. return false;
  88. *data = string_;
  89. return true;
  90. }
  91. bool OSExchangeDataProviderNonBacked::GetURLAndTitle(
  92. FilenameToURLPolicy policy,
  93. GURL* url,
  94. std::u16string* title) const {
  95. if ((formats_ & OSExchangeData::URL) == 0) {
  96. title->clear();
  97. return GetPlainTextURL(url) ||
  98. (policy == FilenameToURLPolicy::CONVERT_FILENAMES &&
  99. GetFileURL(url));
  100. }
  101. if (!url_.is_valid())
  102. return false;
  103. *url = url_;
  104. *title = title_;
  105. return true;
  106. }
  107. bool OSExchangeDataProviderNonBacked::GetFilename(base::FilePath* path) const {
  108. if ((formats_ & OSExchangeData::FILE_NAME) == 0)
  109. return false;
  110. DCHECK(!filenames_.empty());
  111. *path = filenames_[0].path;
  112. return true;
  113. }
  114. bool OSExchangeDataProviderNonBacked::GetFilenames(
  115. std::vector<FileInfo>* filenames) const {
  116. if ((formats_ & OSExchangeData::FILE_NAME) == 0)
  117. return false;
  118. *filenames = filenames_;
  119. return true;
  120. }
  121. bool OSExchangeDataProviderNonBacked::GetPickledData(
  122. const ClipboardFormatType& format,
  123. base::Pickle* data) const {
  124. const auto i = pickle_data_.find(format);
  125. if (i == pickle_data_.end())
  126. return false;
  127. *data = i->second;
  128. return true;
  129. }
  130. bool OSExchangeDataProviderNonBacked::HasString() const {
  131. return (formats_ & OSExchangeData::STRING) != 0;
  132. }
  133. bool OSExchangeDataProviderNonBacked::HasURL(FilenameToURLPolicy policy) const {
  134. if ((formats_ & OSExchangeData::URL) != 0) {
  135. return true;
  136. }
  137. // No URL, see if we have plain text that can be parsed as a URL.
  138. return GetPlainTextURL(nullptr) ||
  139. (policy == FilenameToURLPolicy::CONVERT_FILENAMES &&
  140. GetFileURL(nullptr));
  141. }
  142. bool OSExchangeDataProviderNonBacked::HasFile() const {
  143. return (formats_ & OSExchangeData::FILE_NAME) != 0;
  144. }
  145. bool OSExchangeDataProviderNonBacked::HasCustomFormat(
  146. const ClipboardFormatType& format) const {
  147. return base::Contains(pickle_data_, format);
  148. }
  149. void OSExchangeDataProviderNonBacked::SetFileContents(
  150. const base::FilePath& filename,
  151. const std::string& file_contents) {
  152. file_contents_filename_ = filename;
  153. file_contents_ = file_contents;
  154. }
  155. bool OSExchangeDataProviderNonBacked::GetFileContents(
  156. base::FilePath* filename,
  157. std::string* file_contents) const {
  158. if (file_contents_filename_.empty()) {
  159. return false;
  160. }
  161. *filename = file_contents_filename_;
  162. *file_contents = file_contents_;
  163. return true;
  164. }
  165. bool OSExchangeDataProviderNonBacked::HasFileContents() const {
  166. return !file_contents_filename_.empty();
  167. }
  168. void OSExchangeDataProviderNonBacked::SetHtml(const std::u16string& html,
  169. const GURL& base_url) {
  170. formats_ |= OSExchangeData::HTML;
  171. html_ = html;
  172. base_url_ = base_url;
  173. }
  174. bool OSExchangeDataProviderNonBacked::GetHtml(std::u16string* html,
  175. GURL* base_url) const {
  176. if ((formats_ & OSExchangeData::HTML) == 0)
  177. return false;
  178. *html = html_;
  179. *base_url = base_url_;
  180. return true;
  181. }
  182. bool OSExchangeDataProviderNonBacked::HasHtml() const {
  183. return ((formats_ & OSExchangeData::HTML) != 0);
  184. }
  185. void OSExchangeDataProviderNonBacked::SetDragImage(
  186. const gfx::ImageSkia& image,
  187. const gfx::Vector2d& cursor_offset) {
  188. drag_image_ = image;
  189. drag_image_offset_ = cursor_offset;
  190. }
  191. gfx::ImageSkia OSExchangeDataProviderNonBacked::GetDragImage() const {
  192. return drag_image_;
  193. }
  194. gfx::Vector2d OSExchangeDataProviderNonBacked::GetDragImageOffset() const {
  195. return drag_image_offset_;
  196. }
  197. bool OSExchangeDataProviderNonBacked::GetFileURL(GURL* url) const {
  198. base::FilePath file_path;
  199. if (!GetFilename(&file_path))
  200. return false;
  201. GURL test_url = net::FilePathToFileURL(file_path);
  202. if (!test_url.is_valid())
  203. return false;
  204. if (url)
  205. *url = test_url;
  206. return true;
  207. }
  208. bool OSExchangeDataProviderNonBacked::GetPlainTextURL(GURL* url) const {
  209. if ((formats_ & OSExchangeData::STRING) == 0)
  210. return false;
  211. GURL test_url(string_);
  212. if (!test_url.is_valid())
  213. return false;
  214. if (url)
  215. *url = test_url;
  216. return true;
  217. }
  218. void OSExchangeDataProviderNonBacked::SetSource(
  219. std::unique_ptr<DataTransferEndpoint> data_source) {
  220. source_ = std::move(data_source);
  221. }
  222. DataTransferEndpoint* OSExchangeDataProviderNonBacked::GetSource() const {
  223. return source_.get();
  224. }
  225. void OSExchangeDataProviderNonBacked::CopyData(
  226. OSExchangeDataProviderNonBacked* provider) const {
  227. DCHECK(provider);
  228. provider->formats_ = formats_;
  229. provider->string_ = string_;
  230. provider->url_ = url_;
  231. provider->title_ = title_;
  232. provider->filenames_ = filenames_;
  233. provider->pickle_data_ = pickle_data_;
  234. provider->file_contents_filename_ = file_contents_filename_;
  235. provider->file_contents_ = file_contents_;
  236. provider->html_ = html_;
  237. provider->base_url_ = base_url_;
  238. provider->source_ =
  239. source_ ? std::make_unique<DataTransferEndpoint>(*source_.get())
  240. : nullptr;
  241. provider->is_from_privileged_ = is_from_privileged_;
  242. #if !BUILDFLAG(IS_CHROMEOS_ASH)
  243. provider->originated_from_renderer_ = originated_from_renderer_;
  244. #endif
  245. }
  246. } // namespace ui