Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2020 The Chromium Authors |
[email protected] | 57118ea | 2020-07-17 03:17:14 | [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 | |
Georg Neis | 763539e | 2024-08-20 05:10:41 | [diff] [blame] | 5 | #include "chrome/browser/ui/ash/clipboard/clipboard_util.h" |
[email protected] | 57118ea | 2020-07-17 03:17:14 | [diff] [blame] | 6 | |
[email protected] | 026b7c4f | 2020-10-27 12:05:21 | [diff] [blame] | 7 | #include <stdint.h> |
Peter Kasting | a486324 | 2024-12-23 00:19:43 | [diff] [blame] | 8 | |
[email protected] | 026b7c4f | 2020-10-27 12:05:21 | [diff] [blame] | 9 | #include <memory> |
Austin Sullivan | 85ec877 | 2021-10-26 23:16:02 | [diff] [blame] | 10 | #include <vector> |
[email protected] | 026b7c4f | 2020-10-27 12:05:21 | [diff] [blame] | 11 | |
Alex Newcomer | 9fd61633 | 2021-01-15 23:04:58 | [diff] [blame] | 12 | #include "ash/public/cpp/clipboard_history_controller.h" |
[email protected] | 57118ea | 2020-07-17 03:17:14 | [diff] [blame] | 13 | #include "base/base64.h" |
| 14 | #include "base/files/file_util.h" |
Avi Drissman | 9269d4ed | 2023-01-07 01:38:06 | [diff] [blame] | 15 | #include "base/functional/callback.h" |
| 16 | #include "base/functional/callback_helpers.h" |
[email protected] | 57118ea | 2020-07-17 03:17:14 | [diff] [blame] | 17 | #include "base/memory/ref_counted_memory.h" |
[email protected] | 026b7c4f | 2020-10-27 12:05:21 | [diff] [blame] | 18 | #include "base/memory/scoped_refptr.h" |
[email protected] | 57118ea | 2020-07-17 03:17:14 | [diff] [blame] | 19 | #include "base/metrics/user_metrics.h" |
Lei Zhang | 2b7c9e0 | 2020-09-25 21:30:47 | [diff] [blame] | 20 | #include "base/strings/strcat.h" |
Joe Mason | a0ba58b05 | 2023-02-22 20:46:29 | [diff] [blame] | 21 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 57118ea | 2020-07-17 03:17:14 | [diff] [blame] | 22 | #include "base/threading/scoped_blocking_call.h" |
| 23 | #include "content/public/browser/browser_task_traits.h" |
| 24 | #include "content/public/browser/browser_thread.h" |
| 25 | #include "services/data_decoder/public/cpp/decode_image.h" |
| 26 | #include "ui/base/clipboard/clipboard.h" |
[email protected] | 026b7c4f | 2020-10-27 12:05:21 | [diff] [blame] | 27 | #include "ui/base/clipboard/clipboard_buffer.h" |
| 28 | #include "ui/base/clipboard/clipboard_data.h" |
| 29 | #include "ui/base/clipboard/clipboard_non_backed.h" |
[email protected] | 57118ea | 2020-07-17 03:17:14 | [diff] [blame] | 30 | #include "ui/base/clipboard/scoped_clipboard_writer.h" |
| 31 | |
| 32 | namespace clipboard_util { |
| 33 | namespace { |
| 34 | |
[email protected] | 026b7c4f | 2020-10-27 12:05:21 | [diff] [blame] | 35 | /* |
[email protected] | 026b7c4f | 2020-10-27 12:05:21 | [diff] [blame] | 36 | * `clipboard_sequence` is the versioning of the clipboard when we start our |
| 37 | * copy operation. |
Austin Sullivan | ec750b6 | 2021-10-27 19:07:22 | [diff] [blame] | 38 | * `decoded_image` and `html` are different formats of the same image which we |
| 39 | * are attempting to copy to the clipboard. */ |
| 40 | void CopyDecodedImageToClipboard( |
| 41 | ui::ClipboardSequenceNumberToken clipboard_sequence, |
Austin Sullivan | ec750b6 | 2021-10-27 19:07:22 | [diff] [blame] | 42 | std::string html, |
| 43 | const SkBitmap& decoded_image) { |
| 44 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 45 | |
| 46 | ui::ScopedClipboardWriter clipboard_writer(ui::ClipboardBuffer::kCopyPaste); |
Anupam Snigdha | 18ac05c | 2024-01-30 21:25:59 | [diff] [blame] | 47 | clipboard_writer.WriteHTML(base::UTF8ToUTF16(html), std::string()); |
Austin Sullivan | ec750b6 | 2021-10-27 19:07:22 | [diff] [blame] | 48 | clipboard_writer.WriteImage(decoded_image); |
[email protected] | 57118ea | 2020-07-17 03:17:14 | [diff] [blame] | 49 | } |
| 50 | |
[email protected] | 57118ea | 2020-07-17 03:17:14 | [diff] [blame] | 51 | } // namespace |
| 52 | |
| 53 | void ReadFileAndCopyToClipboardLocal(const base::FilePath& local_file) { |
| 54 | DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 55 | base::ScopedBlockingCall scoped_blocking_call(FROM_HERE, |
| 56 | base::BlockingType::WILL_BLOCK); |
François Doray | 73d3e00 | 2021-11-08 18:41:37 | [diff] [blame] | 57 | std::string png_data; |
| 58 | if (!base::ReadFileToString(local_file, &png_data)) { |
[email protected] | 57118ea | 2020-07-17 03:17:14 | [diff] [blame] | 59 | LOG(ERROR) << "Failed to read the screenshot file: " << local_file.value(); |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | content::GetUIThreadTaskRunner({})->PostTask( |
Austin Sullivan | bfe1af7 | 2021-07-20 23:14:14 | [diff] [blame] | 64 | FROM_HERE, |
François Doray | 73d3e00 | 2021-11-08 18:41:37 | [diff] [blame] | 65 | base::BindOnce(&DecodeImageFileAndCopyToClipboard, |
Austin Sullivan | bfe1af7 | 2021-07-20 23:14:14 | [diff] [blame] | 66 | /*clipboard_sequence=*/ui::ClipboardSequenceNumberToken(), |
François Doray | 73d3e00 | 2021-11-08 18:41:37 | [diff] [blame] | 67 | std::move(png_data))); |
[email protected] | 2ae4df18 | 2020-08-27 16:48:09 | [diff] [blame] | 68 | } |
| 69 | |
François Doray | 73d3e00 | 2021-11-08 18:41:37 | [diff] [blame] | 70 | void DecodeImageFileAndCopyToClipboard( |
Austin Sullivan | bfe1af7 | 2021-07-20 23:14:14 | [diff] [blame] | 71 | ui::ClipboardSequenceNumberToken clipboard_sequence, |
François Doray | 73d3e00 | 2021-11-08 18:41:37 | [diff] [blame] | 72 | std::string png_data) { |
[email protected] | 2ae4df18 | 2020-08-27 16:48:09 | [diff] [blame] | 73 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 74 | |
Austin Sullivan | ec750b6 | 2021-10-27 19:07:22 | [diff] [blame] | 75 | // Send both HTML and and Image formats to clipboard. HTML format is needed |
| 76 | // by ARC, while Image is needed by Hangout. |
| 77 | static const char kImageClipboardFormatPrefix[] = |
| 78 | "<img src='data:image/png;base64,"; |
| 79 | static const char kImageClipboardFormatSuffix[] = "'>"; |
| 80 | |
Peter Kasting | 650ea1c | 2024-12-01 05:51:55 | [diff] [blame] | 81 | std::string encoded = base::Base64Encode(base::as_byte_span(png_data)); |
Austin Sullivan | ec750b6 | 2021-10-27 19:07:22 | [diff] [blame] | 82 | std::string html = base::StrCat( |
| 83 | {kImageClipboardFormatPrefix, encoded, kImageClipboardFormatSuffix}); |
| 84 | |
François Doray | 73d3e00 | 2021-11-08 18:41:37 | [diff] [blame] | 85 | // Decode the image in sandboxed process because |png_data| comes from |
| 86 | // external storage. |
| 87 | data_decoder::DecodeImageIsolated( |
Peter Kasting | 650ea1c | 2024-12-01 05:51:55 | [diff] [blame] | 88 | base::as_byte_span(png_data), data_decoder::mojom::ImageCodec::kDefault, |
François Doray | 73d3e00 | 2021-11-08 18:41:37 | [diff] [blame] | 89 | /*shrink_to_fit=*/false, data_decoder::kDefaultMaxSizeInBytes, |
| 90 | gfx::Size(), |
| 91 | base::BindOnce(&CopyDecodedImageToClipboard, clipboard_sequence, |
| 92 | std::move(html))); |
[email protected] | 57118ea | 2020-07-17 03:17:14 | [diff] [blame] | 93 | } |
| 94 | } // namespace clipboard_util |