data_source.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2017 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 COMPONENTS_EXO_DATA_SOURCE_H_
  5. #define COMPONENTS_EXO_DATA_SOURCE_H_
  6. #include <string>
  7. #include "base/callback.h"
  8. #include "base/containers/flat_set.h"
  9. #include "base/observer_list.h"
  10. #include "components/exo/surface.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. namespace exo {
  13. class DataSourceDelegate;
  14. class DataSourceObserver;
  15. enum class DndAction;
  16. // Object representing transferred data offered by a client.
  17. class DataSource {
  18. public:
  19. // The maximum number of different data types that will be read by
  20. // GetDataForPreferredMimeTypes (plain text, RTF, HTML, image, text/uri-list,
  21. // application/octet-stream, chromium/x-web-custom-data).
  22. static constexpr int kMaxDataTypes = 7;
  23. explicit DataSource(DataSourceDelegate* delegate);
  24. DataSource(const DataSource&) = delete;
  25. DataSource& operator=(const DataSource&) = delete;
  26. ~DataSource();
  27. void AddObserver(DataSourceObserver* observer);
  28. void RemoveObserver(DataSourceObserver* observer);
  29. // Notifies to DataSource that the client offers new mime type.
  30. void Offer(const std::string& mime_type);
  31. // Notifies the possible drag and drop actions selected by the data source to
  32. // DataSource.
  33. void SetActions(const base::flat_set<DndAction>& dnd_actions);
  34. const base::flat_set<DndAction>& GetActions() const { return dnd_actions_; }
  35. // Notifies the data source is cancelled. e.g. Replaced with another data
  36. // source.
  37. void Cancelled();
  38. // Notifies the client of the mime type that will be used by the
  39. // recipient. Only used during drag drop operations.
  40. void Target(const absl::optional<std::string>& mime_type);
  41. // Notifies the client of the dnd action that will be performed if the
  42. // currently running drag operation ends now. Only used during drag drop
  43. // operations.
  44. void Action(DndAction action);
  45. // Notifies the client that the user has released the current drag. At this
  46. // point the target and action are considered final, but it is still possible
  47. // for the recipient to reject the transfer.
  48. void DndDropPerformed();
  49. // Notifies the client that the drag was completed successfully. The data
  50. // source must not be used by the client after this point except to delete it.
  51. void DndFinished();
  52. // Search the set of offered MIME types for the most preferred of each of the
  53. // following categories: text/plain*, text/rtf, text/html*, image/*,
  54. // text/uri-list, chromium/x-web-custom-data. If any usable MIME types in a
  55. // given category are available, the corresponding
  56. // |*_reader| input callback will be called with the best one and the
  57. // corresponding data. For any category that has no available MIME types,
  58. // |failure_callback| is run. |failure_callback| may therefore be run as many
  59. // as seven times.
  60. using ReadDataCallback =
  61. base::OnceCallback<void(const std::string&, const std::vector<uint8_t>&)>;
  62. using ReadTextDataCallback =
  63. base::OnceCallback<void(const std::string&, std::u16string)>;
  64. using ReadFileContentsDataCallback =
  65. base::OnceCallback<void(const std::string&,
  66. const base::FilePath&,
  67. const std::vector<uint8_t>&)>;
  68. using ReadWebCustomDataCallback =
  69. base::OnceCallback<void(const std::string&, const std::vector<uint8_t>&)>;
  70. void ReadDataTransferEndpoint(ReadTextDataCallback dte_reader,
  71. base::RepeatingClosure failure_callback);
  72. void GetDataForPreferredMimeTypes(
  73. ReadTextDataCallback text_reader,
  74. ReadDataCallback rtf_reader,
  75. ReadTextDataCallback html_reader,
  76. ReadDataCallback image_reader,
  77. ReadDataCallback filenames_reader,
  78. ReadFileContentsDataCallback file_contents_reader,
  79. ReadDataCallback web_custom_data_reader,
  80. base::RepeatingClosure failure_callback);
  81. void ReadDataForTesting(const std::string& mime_type,
  82. ReadDataCallback callback);
  83. bool CanBeDataSourceForCopy(Surface* surface) const;
  84. private:
  85. // Reads data from the source. Then |callback| is invoked with read data. If
  86. // Cancelled() is invoked or DataSource is destroyed before completion,
  87. // |callback| is never called, and |failure_callback| is run instead.
  88. void ReadData(const std::string& mime_type,
  89. ReadDataCallback callback,
  90. base::OnceClosure failure_callback);
  91. void OnDataRead(ReadDataCallback callback,
  92. const std::string& mime_type,
  93. base::OnceClosure failure_callback,
  94. const absl::optional<std::vector<uint8_t>>& data);
  95. void OnTextRead(ReadTextDataCallback callback,
  96. const std::string& mime_type,
  97. const std::vector<uint8_t>& data);
  98. void OnFileContentsRead(ReadFileContentsDataCallback callback,
  99. const std::string& mime_type,
  100. const std::vector<uint8_t>& data);
  101. DataSourceDelegate* const delegate_;
  102. base::ObserverList<DataSourceObserver>::Unchecked observers_;
  103. // Mime types which has been offered.
  104. std::set<std::string> mime_types_;
  105. bool finished_;
  106. base::flat_set<DndAction> dnd_actions_;
  107. base::WeakPtrFactory<DataSource> read_data_weak_ptr_factory_{this};
  108. };
  109. class ScopedDataSource {
  110. public:
  111. ScopedDataSource(DataSource* data_source, DataSourceObserver* observer);
  112. ScopedDataSource(const ScopedDataSource&) = delete;
  113. ScopedDataSource& operator=(const ScopedDataSource&) = delete;
  114. ~ScopedDataSource();
  115. DataSource* get() { return data_source_; }
  116. private:
  117. DataSource* const data_source_;
  118. DataSourceObserver* const observer_;
  119. };
  120. } // namespace exo
  121. #endif // COMPONENTS_EXO_DATA_SOURCE_H_