Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CONTENT_BROWSER_RENDERER_HOST_CLIPBOARD_HOST_IMPL_H_ |
| 6 | #define CONTENT_BROWSER_RENDERER_HOST_CLIPBOARD_HOST_IMPL_H_ |
| 7 | |
| 8 | #include <map> |
| 9 | #include <memory> |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 10 | #include <optional> |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 14 | #include "base/functional/callback_forward.h" |
Daniel Cheng | d4c3eab | 2021-08-31 18:39:01 | [diff] [blame] | 15 | #include "base/gtest_prod_util.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 16 | #include "base/memory/weak_ptr.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 17 | #include "base/time/time.h" |
| 18 | #include "build/build_config.h" |
| 19 | #include "content/browser/renderer_host/render_frame_host_impl.h" |
| 20 | #include "content/common/content_export.h" |
Dominique Fauteux-Chapleau | 88798b9 | 2024-03-14 20:22:10 | [diff] [blame] | 21 | #include "content/public/browser/clipboard_types.h" |
Daniel Cheng | 9fb887ff | 2021-10-01 20:27:38 | [diff] [blame] | 22 | #include "content/public/browser/document_service.h" |
Austin Sullivan | fc87089 | 2021-04-29 18:40:11 | [diff] [blame] | 23 | #include "mojo/public/cpp/base/big_buffer.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 24 | #include "mojo/public/cpp/bindings/receiver.h" |
Rohan Raja | 183dfd9 | 2025-04-22 21:41:09 | [diff] [blame] | 25 | #include "mojo/public/cpp/bindings/remote.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 26 | #include "third_party/blink/public/mojom/clipboard/clipboard.mojom.h" |
| 27 | #include "ui/base/clipboard/clipboard.h" |
Rohan Raja | 183dfd9 | 2025-04-22 21:41:09 | [diff] [blame] | 28 | #include "ui/base/clipboard/clipboard_observer.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 29 | |
| 30 | class GURL; |
| 31 | |
| 32 | namespace ui { |
| 33 | class ScopedClipboardWriter; |
| 34 | } // namespace ui |
| 35 | |
| 36 | namespace content { |
| 37 | |
| 38 | class ClipboardHostImplTest; |
| 39 | |
Dominique Fauteux-Chapleau | c174eaa | 2024-02-12 19:29:24 | [diff] [blame] | 40 | // Returns a representation of the last source ClipboardEndpoint. This will |
Dominique Fauteux-Chapleau | 61a5359 | 2025-01-17 15:21:34 | [diff] [blame] | 41 | // either match the last clipboard write if there is an RFH token in the |
| 42 | // clipboard, or an endpoint built from `Clipboard::GetSource()` called with |
Dominique Fauteux-Chapleau | c174eaa | 2024-02-12 19:29:24 | [diff] [blame] | 43 | // `clipboard_buffer` otherwise. |
| 44 | // |
| 45 | // //content maintains additional metadata on top of what the //ui layer already |
| 46 | // tracks about clipboard data's source, e.g. the WebContents that provided the |
| 47 | // data. This function allows retrieving both the //ui metadata and the |
| 48 | // //content metadata in a single call. |
Dominique Fauteux-Chapleau | c174eaa | 2024-02-12 19:29:24 | [diff] [blame] | 49 | CONTENT_EXPORT ClipboardEndpoint |
Dominique Fauteux-Chapleau | 61a5359 | 2025-01-17 15:21:34 | [diff] [blame] | 50 | GetSourceClipboardEndpoint(const ui::DataTransferEndpoint* data_dst, |
Dominique Fauteux-Chapleau | c174eaa | 2024-02-12 19:29:24 | [diff] [blame] | 51 | ui::ClipboardBuffer clipboard_buffer); |
| 52 | |
Alexander Timin | e3383d0 | 2021-06-24 19:57:59 | [diff] [blame] | 53 | class CONTENT_EXPORT ClipboardHostImpl |
Rohan Raja | 183dfd9 | 2025-04-22 21:41:09 | [diff] [blame] | 54 | : public DocumentService<blink::mojom::ClipboardHost>, |
| 55 | public ui::ClipboardObserver { |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 56 | public: |
| 57 | ~ClipboardHostImpl() override; |
| 58 | |
Rohan Raja | 183dfd9 | 2025-04-22 21:41:09 | [diff] [blame] | 59 | // Override for ui::ClipboardObserver |
| 60 | void OnClipboardDataChanged() override; |
| 61 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 62 | static void Create( |
| 63 | RenderFrameHost* render_frame_host, |
| 64 | mojo::PendingReceiver<blink::mojom::ClipboardHost> receiver); |
| 65 | |
Nancy Xiao | 4f3eae5d6 | 2023-04-25 19:38:59 | [diff] [blame] | 66 | using ClipboardPasteData = content::ClipboardPasteData; |
| 67 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 68 | protected: |
| 69 | // These types and methods are protected for testing. |
| 70 | |
Dominique Fauteux-Chapleau | 4407a85d | 2024-01-05 13:18:58 | [diff] [blame] | 71 | using IsClipboardPasteAllowedCallback = |
| 72 | RenderFrameHostImpl::IsClipboardPasteAllowedCallback; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 73 | |
Alexander Timin | e3383d0 | 2021-06-24 19:57:59 | [diff] [blame] | 74 | explicit ClipboardHostImpl( |
danakj | c70aec1f | 2022-07-07 15:48:19 | [diff] [blame] | 75 | RenderFrameHost& render_frame_host, |
Alexander Timin | e3383d0 | 2021-06-24 19:57:59 | [diff] [blame] | 76 | mojo::PendingReceiver<blink::mojom::ClipboardHost> receiver); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 77 | |
Aya ElAttar | 84e6ef3 | 2021-03-02 16:29:19 | [diff] [blame] | 78 | // Performs a check to see if pasting `data` is allowed by data transfer |
Dominique Fauteux-Chapleau | 4407a85d | 2024-01-05 13:18:58 | [diff] [blame] | 79 | // policies and invokes FinishPasteIfAllowed upon completion. |
Aya ElAttar | 84e6ef3 | 2021-03-02 16:29:19 | [diff] [blame] | 80 | void PasteIfPolicyAllowed(ui::ClipboardBuffer clipboard_buffer, |
| 81 | const ui::ClipboardFormatType& data_type, |
Nancy Xiao | 4f3eae5d6 | 2023-04-25 19:38:59 | [diff] [blame] | 82 | ClipboardPasteData clipboard_paste_data, |
Dominique Fauteux-Chapleau | 4407a85d | 2024-01-05 13:18:58 | [diff] [blame] | 83 | IsClipboardPasteAllowedCallback callback); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 84 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 85 | private: |
| 86 | friend class ClipboardHostImplTest; |
Dominique Fauteux-Chapleau | 859e737d | 2024-04-22 22:00:49 | [diff] [blame] | 87 | friend class ClipboardHostImplWriteTest; |
Dominique Fauteux-Chapleau | 3685eb1 | 2024-05-03 17:46:18 | [diff] [blame] | 88 | friend class ClipboardHostImplAsyncWriteTest; |
Rohan Raja | 183dfd9 | 2025-04-22 21:41:09 | [diff] [blame] | 89 | friend class ClipboardHostImplChangeTest; |
Dominique Fauteux-Chapleau | 859e737d | 2024-04-22 22:00:49 | [diff] [blame] | 90 | |
| 91 | FRIEND_TEST_ALL_PREFIXES(ClipboardHostImplWriteTest, WriteText); |
| 92 | FRIEND_TEST_ALL_PREFIXES(ClipboardHostImplWriteTest, WriteText_Empty); |
| 93 | FRIEND_TEST_ALL_PREFIXES(ClipboardHostImplWriteTest, WriteHtml); |
| 94 | FRIEND_TEST_ALL_PREFIXES(ClipboardHostImplWriteTest, WriteHtml_Empty); |
| 95 | FRIEND_TEST_ALL_PREFIXES(ClipboardHostImplWriteTest, WriteSvg); |
| 96 | FRIEND_TEST_ALL_PREFIXES(ClipboardHostImplWriteTest, WriteSvg_Empty); |
| 97 | FRIEND_TEST_ALL_PREFIXES(ClipboardHostImplWriteTest, WriteBitmap); |
| 98 | FRIEND_TEST_ALL_PREFIXES(ClipboardHostImplWriteTest, WriteBitmap_Empty); |
Anupam Snigdha | 56968e7 | 2024-06-18 16:40:36 | [diff] [blame] | 99 | FRIEND_TEST_ALL_PREFIXES(ClipboardHostImplWriteTest, |
| 100 | WriteDataTransferCustomData); |
| 101 | FRIEND_TEST_ALL_PREFIXES(ClipboardHostImplWriteTest, |
| 102 | WriteDataTransferCustomData_Empty); |
Dominique Fauteux-Chapleau | 859e737d | 2024-04-22 22:00:49 | [diff] [blame] | 103 | FRIEND_TEST_ALL_PREFIXES(ClipboardHostImplWriteTest, |
Dominique Fauteux-Chapleau | 4407a85d | 2024-01-05 13:18:58 | [diff] [blame] | 104 | PerformPasteIfAllowed_EmptyData); |
Dominique Fauteux-Chapleau | 1f19c23 | 2025-03-13 16:59:10 | [diff] [blame] | 105 | FRIEND_TEST_ALL_PREFIXES(ClipboardHostImplWriteTest, |
| 106 | NoSourceWithoutDataWrite); |
Dominique Fauteux-Chapleau | 859e737d | 2024-04-22 22:00:49 | [diff] [blame] | 107 | FRIEND_TEST_ALL_PREFIXES(ClipboardHostImplWriteTest, MainFrameURL); |
| 108 | FRIEND_TEST_ALL_PREFIXES(ClipboardHostImplWriteTest, GetSourceEndpoint); |
Dominique Fauteux-Chapleau | 3685eb1 | 2024-05-03 17:46:18 | [diff] [blame] | 109 | FRIEND_TEST_ALL_PREFIXES(ClipboardHostImplAsyncWriteTest, WriteText); |
| 110 | FRIEND_TEST_ALL_PREFIXES(ClipboardHostImplAsyncWriteTest, WriteHtml); |
| 111 | FRIEND_TEST_ALL_PREFIXES(ClipboardHostImplAsyncWriteTest, WriteTextAndHtml); |
| 112 | FRIEND_TEST_ALL_PREFIXES(ClipboardHostImplAsyncWriteTest, ConcurrentWrites); |
Rohan Raja | 183dfd9 | 2025-04-22 21:41:09 | [diff] [blame] | 113 | FRIEND_TEST_ALL_PREFIXES(ClipboardHostImplChangeTest, AddClipboardListener); |
| 114 | FRIEND_TEST_ALL_PREFIXES(ClipboardHostImplChangeTest, |
| 115 | ClipboardListenerDisconnect); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 116 | |
| 117 | // mojom::ClipboardHost |
Rohan Raja | 183dfd9 | 2025-04-22 21:41:09 | [diff] [blame] | 118 | void RegisterClipboardListener( |
| 119 | mojo::PendingRemote<blink::mojom::ClipboardListener> listener) override; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 120 | void GetSequenceNumber(ui::ClipboardBuffer clipboard_buffer, |
| 121 | GetSequenceNumberCallback callback) override; |
| 122 | void IsFormatAvailable(blink::mojom::ClipboardFormat format, |
| 123 | ui::ClipboardBuffer clipboard_buffer, |
| 124 | IsFormatAvailableCallback callback) override; |
| 125 | void ReadAvailableTypes(ui::ClipboardBuffer clipboard_buffer, |
| 126 | ReadAvailableTypesCallback callback) override; |
| 127 | void ReadText(ui::ClipboardBuffer clipboard_buffer, |
| 128 | ReadTextCallback callback) override; |
| 129 | void ReadHtml(ui::ClipboardBuffer clipboard_buffer, |
| 130 | ReadHtmlCallback callback) override; |
| 131 | void ReadSvg(ui::ClipboardBuffer clipboard_buffer, |
| 132 | ReadSvgCallback callback) override; |
| 133 | void ReadRtf(ui::ClipboardBuffer clipboard_buffer, |
| 134 | ReadRtfCallback callback) override; |
Austin Sullivan | aa06098 | 2021-06-25 17:49:30 | [diff] [blame] | 135 | void ReadPng(ui::ClipboardBuffer clipboard_buffer, |
| 136 | ReadPngCallback callback) override; |
Joel Hockey | 4c0cb8c | 2021-02-22 02:59:59 | [diff] [blame] | 137 | void ReadFiles(ui::ClipboardBuffer clipboard_buffer, |
| 138 | ReadFilesCallback callback) override; |
Anupam Snigdha | 3c3f0844 | 2024-06-14 18:58:29 | [diff] [blame] | 139 | void ReadDataTransferCustomData( |
| 140 | ui::ClipboardBuffer clipboard_buffer, |
| 141 | const std::u16string& type, |
| 142 | ReadDataTransferCustomDataCallback callback) override; |
Anupam Snigdha | 74b68e4 | 2021-08-10 23:35:59 | [diff] [blame] | 143 | void ReadAvailableCustomAndStandardFormats( |
| 144 | ReadAvailableCustomAndStandardFormatsCallback callback) override; |
| 145 | void ReadUnsanitizedCustomFormat( |
| 146 | const std::u16string& format, |
| 147 | ReadUnsanitizedCustomFormatCallback callback) override; |
| 148 | void WriteUnsanitizedCustomFormat(const std::u16string& format, |
| 149 | mojo_base::BigBuffer data) override; |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 150 | void WriteText(const std::u16string& text) override; |
| 151 | void WriteHtml(const std::u16string& markup, const GURL& url) override; |
| 152 | void WriteSvg(const std::u16string& markup) override; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 153 | void WriteSmartPasteMarker() override; |
Anupam Snigdha | 56968e7 | 2024-06-18 16:40:36 | [diff] [blame] | 154 | void WriteDataTransferCustomData( |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 155 | const base::flat_map<std::u16string, std::u16string>& data) override; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 156 | void WriteBookmark(const std::string& url, |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 157 | const std::u16string& title) override; |
danakj | bdf1e0a | 2020-11-10 18:27:47 | [diff] [blame] | 158 | void WriteImage(const SkBitmap& unsafe_bitmap) override; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 159 | void CommitWrite() override; |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 160 | #if BUILDFLAG(IS_MAC) |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 161 | void WriteStringToFindPboard(const std::u16string& text) override; |
Rohan Raja | 4826d003 | 2025-07-01 14:09:37 | [diff] [blame] | 162 | void GetPlatformPermissionState( |
| 163 | GetPlatformPermissionStateCallback callback) override; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 164 | #endif |
| 165 | |
Roger Tawa | 34dd57f8 | 2022-12-15 23:24:20 | [diff] [blame] | 166 | // Checks if the renderer allows pasting. This check is skipped if called |
| 167 | // soon after a successful content allowed request. |
| 168 | bool IsRendererPasteAllowed(ui::ClipboardBuffer clipboard_buffer, |
| 169 | RenderFrameHost& render_frame_host); |
| 170 | |
Dominique Fauteux-Chapleau | 302580f | 2024-04-03 20:51:16 | [diff] [blame] | 171 | // Helper to be used when checking if data is allowed to be copied. |
| 172 | // |
Dominique Fauteux-Chapleau | f6e59f2 | 2024-02-05 19:57:53 | [diff] [blame] | 173 | // If `replacement_data` is null, `clipboard_writer_` will be used to write |
Dominique Fauteux-Chapleau | 302580f | 2024-04-03 20:51:16 | [diff] [blame] | 174 | // `data` to the clipboard. `data` should only have one of its fields set |
| 175 | // depending on which "Write" method lead to `OnCopyAllowedResult()` being |
| 176 | // called. That field should correspond to `data_type`. |
| 177 | // |
| 178 | // If `replacement_data` is not null, instead that replacement string is |
| 179 | // written to the clipboard as plaintext. |
| 180 | // |
| 181 | // This method can be called asynchronously. |
Dominique Fauteux-Chapleau | 3685eb1 | 2024-05-03 17:46:18 | [diff] [blame] | 182 | virtual void OnCopyAllowedResult( |
| 183 | const ui::ClipboardFormatType& data_type, |
| 184 | const ClipboardPasteData& data, |
| 185 | std::optional<std::u16string> replacement_data); |
Dominique Fauteux-Chapleau | 302580f | 2024-04-03 20:51:16 | [diff] [blame] | 186 | |
| 187 | // Does the same thing as the previous function with an extra `source_url` |
| 188 | // used to propagate the URL obtained in the `WriteHtml()` method call. |
| 189 | // |
| 190 | // This method can be called asynchronously. |
Dominique Fauteux-Chapleau | 3685eb1 | 2024-05-03 17:46:18 | [diff] [blame] | 191 | virtual void OnCopyHtmlAllowedResult( |
| 192 | const GURL& source_url, |
| 193 | const ui::ClipboardFormatType& data_type, |
| 194 | const ClipboardPasteData& data, |
| 195 | std::optional<std::u16string> replacement_data); |
Anthony Vallee-Dubois | ac84770 | 2021-12-06 21:00:48 | [diff] [blame] | 196 | |
Dominique Fauteux-Chapleau | f6e59f2 | 2024-02-05 19:57:53 | [diff] [blame] | 197 | using CopyAllowedCallback = base::OnceCallback<void()>; |
Dominique Fauteux-Chapleau | dbb4b709 | 2024-02-01 15:42:37 | [diff] [blame] | 198 | |
Austin Sullivan | fc87089 | 2021-04-29 18:40:11 | [diff] [blame] | 199 | void OnReadPng(ui::ClipboardBuffer clipboard_buffer, |
| 200 | ReadPngCallback callback, |
| 201 | const std::vector<uint8_t>& data); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 202 | |
Dominique Fauteux-Chapleau | 61a5359 | 2025-01-17 15:21:34 | [diff] [blame] | 203 | // Resets `clipboard_writer_` to write its data to the clipboard, and |
| 204 | // reinitialize it in preparation for the next write. |
| 205 | void ResetClipboardWriter(); |
| 206 | |
Dominique Fauteux-Chapleau | 1f19c23 | 2025-03-13 16:59:10 | [diff] [blame] | 207 | // Adds source-tracking metadata to `clipboard_writer_` so it can be written |
| 208 | // to the clipboard on the next `CommitWrite()` call. |
| 209 | void AddSourceDataToClipboardWriter(); |
| 210 | |
Dominique Fauteux-Chapleau | 3b7738c | 2024-01-16 21:08:55 | [diff] [blame] | 211 | // Creates a `ui::DataTransferEndpoint` representing the last committed URL. |
Dominique Fauteux-Chapleau | 2cdc6a9 | 2024-01-29 17:39:37 | [diff] [blame] | 212 | std::unique_ptr<ui::DataTransferEndpoint> CreateDataEndpoint(); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 213 | |
Dominique Fauteux-Chapleau | f6e59f2 | 2024-02-05 19:57:53 | [diff] [blame] | 214 | // Creates a `content::ClipboardEndpoint` representing the last committed URL. |
| 215 | ClipboardEndpoint CreateClipboardEndpoint(); |
| 216 | |
Rohan Raja | 183dfd9 | 2025-04-22 21:41:09 | [diff] [blame] | 217 | // Stops observing clipboard changes and resets the listener. |
| 218 | void StopObservingClipboard(); |
| 219 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 220 | std::unique_ptr<ui::ScopedClipboardWriter> clipboard_writer_; |
| 221 | |
Dominique Fauteux-Chapleau | 3685eb1 | 2024-05-03 17:46:18 | [diff] [blame] | 222 | // Counts the number of expected `Write*` calls to be made to the current |
| 223 | // `clipboard_writer_`. This should be used to handle asynchronous `Write*` |
| 224 | // calls made by `IsClipboardCopyAllowedByPolicy`. |
| 225 | int pending_writes_ = 0; |
| 226 | |
| 227 | // Indicates that the renderer called `CommitWrite()`, but that |
| 228 | // `pending_writes_` was not 0 at that time and that it should instead be |
| 229 | // called when the last pending `Write*` call is made. |
| 230 | bool pending_commit_write_ = false; |
| 231 | |
Rohan Raja | 183dfd9 | 2025-04-22 21:41:09 | [diff] [blame] | 232 | // Tracks whether this instance is currently observing clipboard changes. |
| 233 | bool listening_to_clipboard_ = false; |
| 234 | |
| 235 | // Single clipboard listener that will be notified on clipboard changes |
| 236 | mojo::Remote<blink::mojom::ClipboardListener> clipboard_listener_; |
| 237 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 238 | base::WeakPtrFactory<ClipboardHostImpl> weak_ptr_factory_{this}; |
| 239 | }; |
| 240 | |
| 241 | } // namespace content |
| 242 | |
| 243 | #endif // CONTENT_BROWSER_RENDERER_HOST_CLIPBOARD_HOST_IMPL_H_ |