os_exchange_data.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.h"
  5. #include <utility>
  6. #include <vector>
  7. #include "base/callback.h"
  8. #include "base/pickle.h"
  9. #include "build/build_config.h"
  10. #include "ui/base/clipboard/clipboard_format_type.h"
  11. #include "ui/base/data_transfer_policy/data_transfer_endpoint.h"
  12. #include "ui/base/dragdrop/os_exchange_data_provider_factory.h"
  13. #include "url/gurl.h"
  14. namespace ui {
  15. OSExchangeData::OSExchangeData()
  16. : provider_(OSExchangeDataProviderFactory::CreateProvider()) {
  17. }
  18. OSExchangeData::OSExchangeData(std::unique_ptr<OSExchangeDataProvider> provider)
  19. : provider_(std::move(provider)) {}
  20. OSExchangeData::~OSExchangeData() {
  21. }
  22. void OSExchangeData::MarkOriginatedFromRenderer() {
  23. provider_->MarkOriginatedFromRenderer();
  24. }
  25. bool OSExchangeData::DidOriginateFromRenderer() const {
  26. return provider_->DidOriginateFromRenderer();
  27. }
  28. void OSExchangeData::MarkAsFromPrivileged() {
  29. provider_->MarkAsFromPrivileged();
  30. }
  31. bool OSExchangeData::IsFromPrivileged() const {
  32. return provider_->IsFromPrivileged();
  33. }
  34. void OSExchangeData::SetString(const std::u16string& data) {
  35. provider_->SetString(data);
  36. }
  37. void OSExchangeData::SetURL(const GURL& url, const std::u16string& title) {
  38. provider_->SetURL(url, title);
  39. }
  40. void OSExchangeData::SetFilename(const base::FilePath& path) {
  41. provider_->SetFilename(path);
  42. }
  43. void OSExchangeData::SetFilenames(
  44. const std::vector<FileInfo>& filenames) {
  45. provider_->SetFilenames(filenames);
  46. }
  47. void OSExchangeData::SetPickledData(const ClipboardFormatType& format,
  48. const base::Pickle& data) {
  49. provider_->SetPickledData(format, data);
  50. }
  51. bool OSExchangeData::GetString(std::u16string* data) const {
  52. return provider_->GetString(data);
  53. }
  54. bool OSExchangeData::GetURLAndTitle(FilenameToURLPolicy policy,
  55. GURL* url,
  56. std::u16string* title) const {
  57. return provider_->GetURLAndTitle(policy, url, title);
  58. }
  59. bool OSExchangeData::GetFilename(base::FilePath* path) const {
  60. return provider_->GetFilename(path);
  61. }
  62. bool OSExchangeData::GetFilenames(std::vector<FileInfo>* filenames) const {
  63. return provider_->GetFilenames(filenames);
  64. }
  65. bool OSExchangeData::GetPickledData(const ClipboardFormatType& format,
  66. base::Pickle* data) const {
  67. return provider_->GetPickledData(format, data);
  68. }
  69. bool OSExchangeData::HasString() const {
  70. return provider_->HasString();
  71. }
  72. bool OSExchangeData::HasURL(FilenameToURLPolicy policy) const {
  73. return provider_->HasURL(policy);
  74. }
  75. bool OSExchangeData::HasFile() const {
  76. return provider_->HasFile();
  77. }
  78. bool OSExchangeData::HasFileContents() const {
  79. return provider_->HasFileContents();
  80. }
  81. bool OSExchangeData::HasCustomFormat(const ClipboardFormatType& format) const {
  82. return provider_->HasCustomFormat(format);
  83. }
  84. bool OSExchangeData::HasAnyFormat(
  85. int formats,
  86. const std::set<ClipboardFormatType>& format_types) const {
  87. if ((formats & STRING) != 0 && HasString())
  88. return true;
  89. if ((formats & URL) != 0 && HasURL(FilenameToURLPolicy::CONVERT_FILENAMES))
  90. return true;
  91. if ((formats & FILE_CONTENTS) != 0 && provider_->HasFileContents())
  92. return true;
  93. #if defined(USE_AURA)
  94. if ((formats & HTML) != 0 && provider_->HasHtml())
  95. return true;
  96. #endif
  97. if ((formats & FILE_NAME) != 0 && provider_->HasFile())
  98. return true;
  99. for (const auto& format : format_types) {
  100. if (HasCustomFormat(format))
  101. return true;
  102. }
  103. return false;
  104. }
  105. void OSExchangeData::SetFileContents(const base::FilePath& filename,
  106. const std::string& file_contents) {
  107. provider_->SetFileContents(filename, file_contents);
  108. }
  109. bool OSExchangeData::GetFileContents(base::FilePath* filename,
  110. std::string* file_contents) const {
  111. return provider_->GetFileContents(filename, file_contents);
  112. }
  113. #if BUILDFLAG(IS_WIN)
  114. bool OSExchangeData::HasVirtualFilenames() const {
  115. return provider_->HasVirtualFilenames();
  116. }
  117. bool OSExchangeData::GetVirtualFilenames(
  118. std::vector<FileInfo>* filenames) const {
  119. return provider_->GetVirtualFilenames(filenames);
  120. }
  121. bool OSExchangeData::GetVirtualFilesAsTempFiles(
  122. base::OnceCallback<
  123. void(const std::vector<std::pair<base::FilePath, base::FilePath>>&)>
  124. callback) const {
  125. return provider_->GetVirtualFilesAsTempFiles(std::move(callback));
  126. }
  127. #endif
  128. #if defined(USE_AURA)
  129. bool OSExchangeData::HasHtml() const {
  130. return provider_->HasHtml();
  131. }
  132. void OSExchangeData::SetHtml(const std::u16string& html, const GURL& base_url) {
  133. provider_->SetHtml(html, base_url);
  134. }
  135. bool OSExchangeData::GetHtml(std::u16string* html, GURL* base_url) const {
  136. return provider_->GetHtml(html, base_url);
  137. }
  138. #endif
  139. void OSExchangeData::SetSource(
  140. std::unique_ptr<DataTransferEndpoint> data_source) {
  141. provider_->SetSource(std::move(data_source));
  142. }
  143. DataTransferEndpoint* OSExchangeData::GetSource() const {
  144. return provider_->GetSource();
  145. }
  146. } // namespace ui