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> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
Austin Sullivan | fc87089 | 2021-04-29 18:40:11 | [diff] [blame] | 13 | #include "base/callback_forward.h" |
Daniel Cheng | d4c3eab | 2021-08-31 18:39:01 | [diff] [blame] | 14 | #include "base/gtest_prod_util.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 16 | #include "base/time/time.h" |
| 17 | #include "build/build_config.h" |
| 18 | #include "content/browser/renderer_host/render_frame_host_impl.h" |
| 19 | #include "content/common/content_export.h" |
Daniel Cheng | 9fb887ff | 2021-10-01 20:27:38 | [diff] [blame] | 20 | #include "content/public/browser/document_service.h" |
Austin Sullivan | fc87089 | 2021-04-29 18:40:11 | [diff] [blame] | 21 | #include "mojo/public/cpp/base/big_buffer.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 22 | #include "mojo/public/cpp/bindings/receiver.h" |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 23 | #include "third_party/abseil-cpp/absl/types/optional.h" |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 24 | #include "third_party/blink/public/mojom/clipboard/clipboard.mojom.h" |
| 25 | #include "ui/base/clipboard/clipboard.h" |
| 26 | |
| 27 | class GURL; |
| 28 | |
| 29 | namespace ui { |
| 30 | class ScopedClipboardWriter; |
| 31 | } // namespace ui |
| 32 | |
| 33 | namespace content { |
| 34 | |
| 35 | class ClipboardHostImplTest; |
| 36 | |
Alexander Timin | e3383d0 | 2021-06-24 19:57:59 | [diff] [blame] | 37 | class CONTENT_EXPORT ClipboardHostImpl |
Daniel Cheng | 9fb887ff | 2021-10-01 20:27:38 | [diff] [blame] | 38 | : public DocumentService<blink::mojom::ClipboardHost> { |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 39 | public: |
| 40 | ~ClipboardHostImpl() override; |
| 41 | |
| 42 | static void Create( |
| 43 | RenderFrameHost* render_frame_host, |
| 44 | mojo::PendingReceiver<blink::mojom::ClipboardHost> receiver); |
| 45 | |
| 46 | protected: |
| 47 | // These types and methods are protected for testing. |
| 48 | |
Darwin Huang | 6195d04 | 2021-02-12 22:36:00 | [diff] [blame] | 49 | using IsClipboardPasteContentAllowedCallback = |
| 50 | RenderFrameHostImpl::IsClipboardPasteContentAllowedCallback; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 51 | |
Roger Tawa | c617026 | 2022-10-28 23:48:52 | [diff] [blame] | 52 | // Represents the underlying type of the argument passed to |
| 53 | // IsClipboardPasteContentAllowedCallback without the const& part. |
| 54 | using IsClipboardPasteContentAllowedCallbackArgType = |
| 55 | absl::optional<std::string>; |
| 56 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 57 | // Keeps track of a request to see if some clipboard content, identified by |
Rakina Zata Amni | 39d8e7e | 2022-10-20 18:18:30 | [diff] [blame] | 58 | // its sequence number, is allowed to be pasted into the RenderFrameHost |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 59 | // that owns this clipboard host. |
| 60 | // |
| 61 | // A request starts in the state incomplete until Complete() is called with |
| 62 | // a value. Callbacks can be added to the request before or after it has |
| 63 | // completed. |
Darwin Huang | 6195d04 | 2021-02-12 22:36:00 | [diff] [blame] | 64 | class CONTENT_EXPORT IsPasteContentAllowedRequest { |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 65 | public: |
Darwin Huang | 6195d04 | 2021-02-12 22:36:00 | [diff] [blame] | 66 | IsPasteContentAllowedRequest(); |
| 67 | ~IsPasteContentAllowedRequest(); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 68 | |
| 69 | // Adds |callback| to be notified when the request completes. If the |
| 70 | // request is already completed |callback| is invoked immediately. Returns |
| 71 | // true if a request should be started after adding this callback. |
Darwin Huang | 6195d04 | 2021-02-12 22:36:00 | [diff] [blame] | 72 | bool AddCallback(IsClipboardPasteContentAllowedCallback callback); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 73 | |
| 74 | // Mark this request as completed with the specified result. |
| 75 | // Invoke all callbacks now. |
Roger Tawa | c617026 | 2022-10-28 23:48:52 | [diff] [blame] | 76 | void Complete(IsClipboardPasteContentAllowedCallbackArgType data); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 77 | |
Roger Tawa | 34dd57f8 | 2022-12-15 23:24:20 | [diff] [blame^] | 78 | // Returns true if the request has completed. |
| 79 | bool is_complete() const { return data_.has_value(); } |
| 80 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 81 | // Returns true if this request is obsolete. An obsolete request |
| 82 | // is one that is completed, all registered callbacks have been |
| 83 | // called, and is considered old. |
| 84 | // |
| 85 | // |now| represents the current time. It is an argument to ease testing. |
| 86 | bool IsObsolete(base::Time now); |
| 87 | |
Roger Tawa | 34dd57f8 | 2022-12-15 23:24:20 | [diff] [blame^] | 88 | // Returns the time at which this request was completed. If called |
| 89 | // before the request is completed the return value is undefined. |
| 90 | base::Time completed_time(); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 91 | |
| 92 | private: |
| 93 | // Calls all the callbacks in |callbacks_| with the current value of |
| 94 | // |allowed_|. |allowed_| must not be empty. |
| 95 | void InvokeCallbacks(); |
| 96 | |
Roger Tawa | 34dd57f8 | 2022-12-15 23:24:20 | [diff] [blame^] | 97 | // The time at which the request was completed. Before completion this |
| 98 | // value is undefined. |
| 99 | base::Time completed_time_; |
Roger Tawa | c617026 | 2022-10-28 23:48:52 | [diff] [blame] | 100 | |
| 101 | // The data argument to pass to the IsClipboardPasteContentAllowedCallback. |
| 102 | // This member is null until Complete() is called. |
| 103 | absl::optional<IsClipboardPasteContentAllowedCallbackArgType> data_; |
Darwin Huang | 6195d04 | 2021-02-12 22:36:00 | [diff] [blame] | 104 | std::vector<IsClipboardPasteContentAllowedCallback> callbacks_; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | // A paste allowed request is obsolete if it is older than this time. |
Darwin Huang | 6195d04 | 2021-02-12 22:36:00 | [diff] [blame] | 108 | static const base::TimeDelta kIsPasteContentAllowedRequestTooOld; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 109 | |
Alexander Timin | e3383d0 | 2021-06-24 19:57:59 | [diff] [blame] | 110 | explicit ClipboardHostImpl( |
danakj | c70aec1f | 2022-07-07 15:48:19 | [diff] [blame] | 111 | RenderFrameHost& render_frame_host, |
Alexander Timin | e3383d0 | 2021-06-24 19:57:59 | [diff] [blame] | 112 | mojo::PendingReceiver<blink::mojom::ClipboardHost> receiver); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 113 | |
Aya ElAttar | 84e6ef3 | 2021-03-02 16:29:19 | [diff] [blame] | 114 | // Performs a check to see if pasting `data` is allowed by data transfer |
| 115 | // policies and invokes PasteIfPolicyAllowedCallback upon completion. |
Ana SollanoKim | 2ae2589 | 2022-08-16 19:06:05 | [diff] [blame] | 116 | // PerformPasteIfContentAllowed may be invoked immediately if the policy |
Aya ElAttar | 84e6ef3 | 2021-03-02 16:29:19 | [diff] [blame] | 117 | // controller doesn't exist. |
| 118 | void PasteIfPolicyAllowed(ui::ClipboardBuffer clipboard_buffer, |
| 119 | const ui::ClipboardFormatType& data_type, |
| 120 | std::string data, |
| 121 | IsClipboardPasteContentAllowedCallback callback); |
| 122 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 123 | // Performs a check to see if pasting |data| is allowed and invokes |callback| |
Aya ElAttar | 84e6ef3 | 2021-03-02 16:29:19 | [diff] [blame] | 124 | // upon completion. |callback| may be invoked immediately if the data has |
| 125 | // already been checked. |data| and |seqno| should corresponds to the same |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 126 | // clipboard data. |
Darwin Huang | 6195d04 | 2021-02-12 22:36:00 | [diff] [blame] | 127 | void PerformPasteIfContentAllowed( |
Austin Sullivan | bfe1af7 | 2021-07-20 23:14:14 | [diff] [blame] | 128 | const ui::ClipboardSequenceNumberToken& seqno, |
Darwin Huang | 6195d04 | 2021-02-12 22:36:00 | [diff] [blame] | 129 | const ui::ClipboardFormatType& data_type, |
| 130 | std::string data, |
| 131 | IsClipboardPasteContentAllowedCallback callback); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 132 | |
| 133 | // Remove obsolete entries from the outstanding requests map. |
| 134 | // A request is obsolete if: |
| 135 | // - its sequence number is less than |seqno| |
| 136 | // - it has no callbacks |
| 137 | // - it is too old |
| 138 | void CleanupObsoleteRequests(); |
| 139 | |
Aya ElAttar | 84e6ef3 | 2021-03-02 16:29:19 | [diff] [blame] | 140 | // Completion callback of PerformPasteIfContentAllowed(). Sets the allowed |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 141 | // status for the clipboard data corresponding to sequence number |seqno|. |
Austin Sullivan | bfe1af7 | 2021-07-20 23:14:14 | [diff] [blame] | 142 | void FinishPasteIfContentAllowed( |
| 143 | const ui::ClipboardSequenceNumberToken& seqno, |
Roger Tawa | c617026 | 2022-10-28 23:48:52 | [diff] [blame] | 144 | const absl::optional<std::string>& data); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 145 | |
Austin Sullivan | bfe1af7 | 2021-07-20 23:14:14 | [diff] [blame] | 146 | const std::map<ui::ClipboardSequenceNumberToken, |
| 147 | IsPasteContentAllowedRequest>& |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 148 | is_paste_allowed_requests_for_testing() { |
| 149 | return is_allowed_requests_; |
| 150 | } |
| 151 | |
| 152 | private: |
| 153 | friend class ClipboardHostImplTest; |
| 154 | friend class ClipboardHostImplScanTest; |
| 155 | FRIEND_TEST_ALL_PREFIXES(ClipboardHostImplTest, |
Darwin Huang | 6195d04 | 2021-02-12 22:36:00 | [diff] [blame] | 156 | IsPasteContentAllowedRequest_AddCallback); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 157 | FRIEND_TEST_ALL_PREFIXES(ClipboardHostImplTest, |
Darwin Huang | 6195d04 | 2021-02-12 22:36:00 | [diff] [blame] | 158 | IsPasteContentAllowedRequest_Complete); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 159 | FRIEND_TEST_ALL_PREFIXES(ClipboardHostImplTest, |
Darwin Huang | 6195d04 | 2021-02-12 22:36:00 | [diff] [blame] | 160 | IsPasteContentAllowedRequest_IsObsolete); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 161 | FRIEND_TEST_ALL_PREFIXES(ClipboardHostImplScanTest, |
Darwin Huang | 6195d04 | 2021-02-12 22:36:00 | [diff] [blame] | 162 | PerformPasteIfContentAllowed_EmptyData); |
| 163 | FRIEND_TEST_ALL_PREFIXES(ClipboardHostImplScanTest, |
| 164 | PerformPasteIfContentAllowed); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 165 | |
| 166 | // mojom::ClipboardHost |
| 167 | void GetSequenceNumber(ui::ClipboardBuffer clipboard_buffer, |
| 168 | GetSequenceNumberCallback callback) override; |
| 169 | void IsFormatAvailable(blink::mojom::ClipboardFormat format, |
| 170 | ui::ClipboardBuffer clipboard_buffer, |
| 171 | IsFormatAvailableCallback callback) override; |
| 172 | void ReadAvailableTypes(ui::ClipboardBuffer clipboard_buffer, |
| 173 | ReadAvailableTypesCallback callback) override; |
| 174 | void ReadText(ui::ClipboardBuffer clipboard_buffer, |
| 175 | ReadTextCallback callback) override; |
| 176 | void ReadHtml(ui::ClipboardBuffer clipboard_buffer, |
| 177 | ReadHtmlCallback callback) override; |
| 178 | void ReadSvg(ui::ClipboardBuffer clipboard_buffer, |
| 179 | ReadSvgCallback callback) override; |
| 180 | void ReadRtf(ui::ClipboardBuffer clipboard_buffer, |
| 181 | ReadRtfCallback callback) override; |
Austin Sullivan | aa06098 | 2021-06-25 17:49:30 | [diff] [blame] | 182 | void ReadPng(ui::ClipboardBuffer clipboard_buffer, |
| 183 | ReadPngCallback callback) override; |
Joel Hockey | 4c0cb8c | 2021-02-22 02:59:59 | [diff] [blame] | 184 | void ReadFiles(ui::ClipboardBuffer clipboard_buffer, |
| 185 | ReadFilesCallback callback) override; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 186 | void ReadCustomData(ui::ClipboardBuffer clipboard_buffer, |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 187 | const std::u16string& type, |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 188 | ReadCustomDataCallback callback) override; |
Anupam Snigdha | 74b68e4 | 2021-08-10 23:35:59 | [diff] [blame] | 189 | void ReadAvailableCustomAndStandardFormats( |
| 190 | ReadAvailableCustomAndStandardFormatsCallback callback) override; |
| 191 | void ReadUnsanitizedCustomFormat( |
| 192 | const std::u16string& format, |
| 193 | ReadUnsanitizedCustomFormatCallback callback) override; |
| 194 | void WriteUnsanitizedCustomFormat(const std::u16string& format, |
| 195 | mojo_base::BigBuffer data) override; |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 196 | void WriteText(const std::u16string& text) override; |
| 197 | void WriteHtml(const std::u16string& markup, const GURL& url) override; |
| 198 | void WriteSvg(const std::u16string& markup) override; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 199 | void WriteSmartPasteMarker() override; |
| 200 | void WriteCustomData( |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 201 | const base::flat_map<std::u16string, std::u16string>& data) override; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 202 | void WriteBookmark(const std::string& url, |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 203 | const std::u16string& title) override; |
danakj | bdf1e0a | 2020-11-10 18:27:47 | [diff] [blame] | 204 | void WriteImage(const SkBitmap& unsafe_bitmap) override; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 205 | void CommitWrite() override; |
Xiaohan Wang | 7f8052e0 | 2022-01-14 18:44:28 | [diff] [blame] | 206 | #if BUILDFLAG(IS_MAC) |
Jan Wilken Dörrie | aace0cfef | 2021-03-11 22:01:58 | [diff] [blame] | 207 | void WriteStringToFindPboard(const std::u16string& text) override; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 208 | #endif |
| 209 | |
Roger Tawa | 34dd57f8 | 2022-12-15 23:24:20 | [diff] [blame^] | 210 | // Checks if the renderer allows pasting. This check is skipped if called |
| 211 | // soon after a successful content allowed request. |
| 212 | bool IsRendererPasteAllowed(ui::ClipboardBuffer clipboard_buffer, |
| 213 | RenderFrameHost& render_frame_host); |
| 214 | |
Anupam Snigdha | 74b68e4 | 2021-08-10 23:35:59 | [diff] [blame] | 215 | // Returns true if custom format is allowed to be read/written from/to the |
| 216 | // clipboard, else, fails. |
| 217 | bool IsUnsanitizedCustomFormatContentAllowed(); |
| 218 | |
Darwin Huang | 6195d04 | 2021-02-12 22:36:00 | [diff] [blame] | 219 | // Called by PerformPasteIfContentAllowed() when an is allowed request is |
| 220 | // needed. Virtual to be overridden in tests. |
| 221 | virtual void StartIsPasteContentAllowedRequest( |
Austin Sullivan | bfe1af7 | 2021-07-20 23:14:14 | [diff] [blame] | 222 | const ui::ClipboardSequenceNumberToken& seqno, |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 223 | const ui::ClipboardFormatType& data_type, |
| 224 | std::string data); |
| 225 | |
Aya ElAttar | 84e6ef3 | 2021-03-02 16:29:19 | [diff] [blame] | 226 | // Completion callback of PasteIfPolicyAllowed. If `is_allowed` is set to |
| 227 | // true, PerformPasteIfContentAllowed will be invoked. Otherwise `callback` |
| 228 | // will be invoked immediately to cancel the paste. |
| 229 | void PasteIfPolicyAllowedCallback( |
| 230 | ui::ClipboardBuffer clipboard_buffer, |
| 231 | const ui::ClipboardFormatType& data_type, |
| 232 | std::string data, |
| 233 | IsClipboardPasteContentAllowedCallback callback, |
| 234 | bool is_allowed); |
| 235 | |
Anthony Vallee-Dubois | ac84770 | 2021-12-06 21:00:48 | [diff] [blame] | 236 | using CopyAllowedCallback = base::OnceCallback<void()>; |
| 237 | void CopyIfAllowed(size_t data_size_in_bytes, CopyAllowedCallback callback); |
| 238 | |
Austin Sullivan | fc87089 | 2021-04-29 18:40:11 | [diff] [blame] | 239 | void OnReadPng(ui::ClipboardBuffer clipboard_buffer, |
| 240 | ReadPngCallback callback, |
| 241 | const std::vector<uint8_t>& data); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 242 | |
Aya ElAttar | 5e8cfb08 | 2020-10-28 14:33:18 | [diff] [blame] | 243 | std::unique_ptr<ui::DataTransferEndpoint> CreateDataEndpoint(); |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 244 | |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 245 | std::unique_ptr<ui::ScopedClipboardWriter> clipboard_writer_; |
| 246 | |
| 247 | // Outstanding is allowed requests per clipboard contents. Maps a clipboard |
| 248 | // sequence number to an outstanding request. |
Austin Sullivan | bfe1af7 | 2021-07-20 23:14:14 | [diff] [blame] | 249 | std::map<ui::ClipboardSequenceNumberToken, IsPasteContentAllowedRequest> |
| 250 | is_allowed_requests_; |
danakj | c492bf8 | 2020-09-09 20:02:44 | [diff] [blame] | 251 | |
| 252 | base::WeakPtrFactory<ClipboardHostImpl> weak_ptr_factory_{this}; |
| 253 | }; |
| 254 | |
| 255 | } // namespace content |
| 256 | |
| 257 | #endif // CONTENT_BROWSER_RENDERER_HOST_CLIPBOARD_HOST_IMPL_H_ |