x11_os_exchange_data_provider.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright (c) 2020 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_X_X11_OS_EXCHANGE_DATA_PROVIDER_H_
  5. #define UI_BASE_X_X11_OS_EXCHANGE_DATA_PROVIDER_H_
  6. #include <stdint.h>
  7. #include <map>
  8. #include "base/component_export.h"
  9. #include "base/files/file_path.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/pickle.h"
  12. #include "ui/base/dragdrop/os_exchange_data_provider.h"
  13. #include "ui/base/x/selection_owner.h"
  14. #include "ui/base/x/selection_requestor.h"
  15. #include "ui/base/x/selection_utils.h"
  16. #include "ui/gfx/geometry/vector2d.h"
  17. #include "ui/gfx/image/image_skia.h"
  18. #include "url/gurl.h"
  19. namespace ui {
  20. class OSExchangeDataProviderX11Test;
  21. // Generic OSExchangeDataProvider implementation for X11. Lacks the event
  22. // handling; the subclass should listen for SelectionRequest X events and
  23. // route them to the |selection_owner_|.
  24. class COMPONENT_EXPORT(UI_BASE_X) XOSExchangeDataProvider
  25. : public OSExchangeDataProvider {
  26. public:
  27. // |x_window| is the window the cursor is over, |source_window| is the window
  28. // where the drag started, and |selection| is the set of data being offered.
  29. XOSExchangeDataProvider(x11::Window x_window,
  30. x11::Window source_window,
  31. const SelectionFormatMap& selection);
  32. // Creates a Provider for sending drag information. This creates its own,
  33. // hidden X11 window to own send data.
  34. XOSExchangeDataProvider();
  35. ~XOSExchangeDataProvider() override;
  36. XOSExchangeDataProvider(const XOSExchangeDataProvider&) = delete;
  37. XOSExchangeDataProvider& operator=(const XOSExchangeDataProvider&) = delete;
  38. // After all the Set* methods have built up the data we're offering, call
  39. // this to take ownership of the XdndSelection clipboard.
  40. void TakeOwnershipOfSelection() const;
  41. // Retrieves a list of types we're offering. Noop if we haven't taken the
  42. // selection.
  43. void RetrieveTargets(std::vector<x11::Atom>* targets) const;
  44. // Makes a copy of the format map currently being offered.
  45. SelectionFormatMap GetFormatMap() const;
  46. const base::FilePath& file_contents_name() const {
  47. return file_contents_name_;
  48. }
  49. // Overridden from OSExchangeDataProvider:
  50. std::unique_ptr<OSExchangeDataProvider> Clone() const override;
  51. void MarkOriginatedFromRenderer() override;
  52. bool DidOriginateFromRenderer() const override;
  53. void MarkAsFromPrivileged() override;
  54. bool IsFromPrivileged() const override;
  55. void SetString(const std::u16string& data) override;
  56. void SetURL(const GURL& url, const std::u16string& title) override;
  57. void SetFilename(const base::FilePath& path) override;
  58. void SetFilenames(const std::vector<FileInfo>& filenames) override;
  59. void SetPickledData(const ClipboardFormatType& format,
  60. const base::Pickle& pickle) override;
  61. bool GetString(std::u16string* data) const override;
  62. bool GetURLAndTitle(FilenameToURLPolicy policy,
  63. GURL* url,
  64. std::u16string* title) const override;
  65. bool GetFilename(base::FilePath* path) const override;
  66. bool GetFilenames(std::vector<FileInfo>* filenames) const override;
  67. bool GetPickledData(const ClipboardFormatType& format,
  68. base::Pickle* pickle) const override;
  69. bool HasString() const override;
  70. bool HasURL(FilenameToURLPolicy policy) const override;
  71. bool HasFile() const override;
  72. bool HasCustomFormat(const ClipboardFormatType& format) const override;
  73. void SetFileContents(const base::FilePath& filename,
  74. const std::string& file_contents) override;
  75. bool GetFileContents(base::FilePath* filename,
  76. std::string* file_contents) const override;
  77. bool HasFileContents() const override;
  78. void SetHtml(const std::u16string& html, const GURL& base_url) override;
  79. bool GetHtml(std::u16string* html, GURL* base_url) const override;
  80. bool HasHtml() const override;
  81. void SetDragImage(const gfx::ImageSkia& image,
  82. const gfx::Vector2d& cursor_offset) override;
  83. gfx::ImageSkia GetDragImage() const override;
  84. gfx::Vector2d GetDragImageOffset() const override;
  85. void SetSource(std::unique_ptr<DataTransferEndpoint> data_source) override;
  86. DataTransferEndpoint* GetSource() const override;
  87. protected:
  88. friend class OSExchangeDataProviderX11Test;
  89. using PickleData = std::map<ClipboardFormatType, base::Pickle>;
  90. bool own_window() const { return own_window_; }
  91. x11::Window x_window() const { return x_window_; }
  92. const SelectionFormatMap& format_map() const { return format_map_; }
  93. void set_format_map(const SelectionFormatMap& format_map) {
  94. format_map_ = format_map;
  95. }
  96. void set_file_contents_name(const base::FilePath& path) {
  97. file_contents_name_ = path;
  98. }
  99. SelectionOwner& selection_owner() const { return selection_owner_; }
  100. // Returns true if |formats_| contains a string format and the string can be
  101. // parsed as a URL.
  102. bool GetPlainTextURL(GURL* url) const;
  103. // Returns the targets in |format_map_|.
  104. std::vector<x11::Atom> GetTargets() const;
  105. // Inserts data into the format map.
  106. void InsertData(x11::Atom format,
  107. const scoped_refptr<base::RefCountedMemory>& data);
  108. private:
  109. // Drag image and offset data.
  110. gfx::ImageSkia drag_image_;
  111. gfx::Vector2d drag_image_offset_;
  112. // Our X11 state.
  113. raw_ptr<x11::Connection> connection_;
  114. x11::Window x_root_window_;
  115. // In X11, because the IPC parts of drag operations are implemented by
  116. // XSelection, we require an x11 window to receive drag messages on. The
  117. // OSExchangeDataProvider system is modeled on the Windows implementation,
  118. // which does not require a window. We only sometimes have a valid window
  119. // available (in the case of drag receiving). Other times, we need to create
  120. // our own xwindow just to receive events on it.
  121. const bool own_window_;
  122. x11::Window x_window_;
  123. x11::Window source_window_;
  124. // A representation of data. This is either passed to us from the other
  125. // process, or built up through a sequence of Set*() calls. It can be passed
  126. // to |selection_owner_| when we take the selection.
  127. SelectionFormatMap format_map_;
  128. // Auxiliary data for the X Direct Save protocol.
  129. base::FilePath file_contents_name_;
  130. // Takes a snapshot of |format_map_| and offers it to other windows.
  131. mutable SelectionOwner selection_owner_;
  132. bool is_from_privileged_ = false;
  133. };
  134. } // namespace ui
  135. #endif // UI_BASE_X_X11_OS_EXCHANGE_DATA_PROVIDER_H_