[email protected] | 57118ea | 2020-07-17 03:17:14 | [diff] [blame] | 1 | // Copyright 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 | |
| 5 | #include "chrome/browser/ui/ash/clipboard_util.h" |
| 6 | |
[email protected] | 026b7c4f | 2020-10-27 12:05:21 | [diff] [blame^] | 7 | #include <stdint.h> |
| 8 | #include <memory> |
| 9 | |
[email protected] | 57118ea | 2020-07-17 03:17:14 | [diff] [blame] | 10 | #include "base/base64.h" |
[email protected] | 026b7c4f | 2020-10-27 12:05:21 | [diff] [blame^] | 11 | #include "base/callback.h" |
[email protected] | 57118ea | 2020-07-17 03:17:14 | [diff] [blame] | 12 | #include "base/files/file_util.h" |
| 13 | #include "base/memory/ref_counted_memory.h" |
[email protected] | 026b7c4f | 2020-10-27 12:05:21 | [diff] [blame^] | 14 | #include "base/memory/scoped_refptr.h" |
[email protected] | 57118ea | 2020-07-17 03:17:14 | [diff] [blame] | 15 | #include "base/metrics/user_metrics.h" |
Lei Zhang | 2b7c9e0 | 2020-09-25 21:30:47 | [diff] [blame] | 16 | #include "base/strings/strcat.h" |
[email protected] | 57118ea | 2020-07-17 03:17:14 | [diff] [blame] | 17 | #include "base/threading/scoped_blocking_call.h" |
| 18 | #include "content/public/browser/browser_task_traits.h" |
| 19 | #include "content/public/browser/browser_thread.h" |
| 20 | #include "services/data_decoder/public/cpp/decode_image.h" |
| 21 | #include "ui/base/clipboard/clipboard.h" |
[email protected] | 026b7c4f | 2020-10-27 12:05:21 | [diff] [blame^] | 22 | #include "ui/base/clipboard/clipboard_buffer.h" |
| 23 | #include "ui/base/clipboard/clipboard_data.h" |
| 24 | #include "ui/base/clipboard/clipboard_non_backed.h" |
[email protected] | 57118ea | 2020-07-17 03:17:14 | [diff] [blame] | 25 | #include "ui/base/clipboard/scoped_clipboard_writer.h" |
| 26 | |
| 27 | namespace clipboard_util { |
| 28 | namespace { |
| 29 | |
[email protected] | 026b7c4f | 2020-10-27 12:05:21 | [diff] [blame^] | 30 | void CopyAndMaintainClipboard( |
| 31 | std::unique_ptr<ui::ClipboardData> data_with_image, |
| 32 | const std::string& markup_content, |
| 33 | scoped_refptr<base::RefCountedString> png_data, |
| 34 | const SkBitmap& decoded_image) { |
[email protected] | 57118ea | 2020-07-17 03:17:14 | [diff] [blame] | 35 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 36 | |
[email protected] | 026b7c4f | 2020-10-27 12:05:21 | [diff] [blame^] | 37 | data_with_image->set_markup_data(markup_content); |
| 38 | data_with_image->SetBitmapData(decoded_image); |
| 39 | ui::ClipboardNonBacked::GetForCurrentThread()->WriteClipboardData( |
| 40 | std::move(data_with_image)); |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | * `maintain_clipboard` indicates whether the clipboard state should attempt to |
| 45 | * be maintained. |
| 46 | * `clipboard_sequence` is the versioning of the clipboard when we start our |
| 47 | * copy operation. |
| 48 | * `callback` alerts whether or not the image was copied to the clipboard while |
| 49 | * meeting the `maintain_clipboard` state. If the image is copied it will return |
| 50 | * true, otherwise if the image is not copied because the `clipboard_sequence` |
| 51 | * does not match, it will return false. |
| 52 | * `decoded_image` is the image we are attempting to copy to the clipboard. |
| 53 | */ |
| 54 | void CopyImageToClipboard(bool maintain_clipboard, |
| 55 | uint64_t clipboard_sequence, |
| 56 | base::OnceCallback<void(bool)> callback, |
| 57 | scoped_refptr<base::RefCountedString> png_data, |
| 58 | const SkBitmap& decoded_image) { |
| 59 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
[email protected] | 57118ea | 2020-07-17 03:17:14 | [diff] [blame] | 60 | |
Lei Zhang | 2b7c9e0 | 2020-09-25 21:30:47 | [diff] [blame] | 61 | // Send both HTML and and Image formats to clipboard. HTML format is needed |
| 62 | // by ARC, while Image is needed by Hangout. |
| 63 | static const char kImageClipboardFormatPrefix[] = |
| 64 | "<img src='data:image/png;base64,"; |
| 65 | static const char kImageClipboardFormatSuffix[] = "'>"; |
| 66 | |
| 67 | std::string encoded = |
| 68 | base::Base64Encode(base::as_bytes(base::make_span(png_data->data()))); |
| 69 | std::string html = base::StrCat( |
| 70 | {kImageClipboardFormatPrefix, encoded, kImageClipboardFormatSuffix}); |
[email protected] | 026b7c4f | 2020-10-27 12:05:21 | [diff] [blame^] | 71 | |
| 72 | if (!maintain_clipboard) { |
| 73 | ui::ScopedClipboardWriter clipboard_writer(ui::ClipboardBuffer::kCopyPaste); |
| 74 | clipboard_writer.WriteHTML(base::UTF8ToUTF16(html), std::string()); |
| 75 | clipboard_writer.WriteImage(decoded_image); |
| 76 | std::move(callback).Run(true); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | uint64_t current_sequence = |
| 81 | ui::ClipboardNonBacked::GetForCurrentThread()->GetSequenceNumber( |
| 82 | ui::ClipboardBuffer::kCopyPaste); |
| 83 | if (current_sequence != clipboard_sequence) { |
| 84 | // Clipboard data changed and this copy operation is no longer relevant. |
| 85 | std::move(callback).Run(false); |
| 86 | return; |
| 87 | } |
| 88 | std::unique_ptr<ui::ClipboardData> current_data = |
| 89 | std::make_unique<ui::ClipboardData>( |
| 90 | *ui::ClipboardNonBacked::GetForCurrentThread()->GetClipboardData( |
| 91 | nullptr)); |
| 92 | CopyAndMaintainClipboard(std::move(current_data), html, png_data, |
| 93 | decoded_image); |
| 94 | std::move(callback).Run(true); |
[email protected] | 57118ea | 2020-07-17 03:17:14 | [diff] [blame] | 95 | } |
| 96 | |
[email protected] | 57118ea | 2020-07-17 03:17:14 | [diff] [blame] | 97 | } // namespace |
| 98 | |
| 99 | void ReadFileAndCopyToClipboardLocal(const base::FilePath& local_file) { |
| 100 | DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 101 | base::ScopedBlockingCall scoped_blocking_call(FROM_HERE, |
| 102 | base::BlockingType::WILL_BLOCK); |
Lei Zhang | 2b7c9e0 | 2020-09-25 21:30:47 | [diff] [blame] | 103 | auto png_data = base::MakeRefCounted<base::RefCountedString>(); |
[email protected] | 57118ea | 2020-07-17 03:17:14 | [diff] [blame] | 104 | if (!base::ReadFileToString(local_file, &(png_data->data()))) { |
| 105 | LOG(ERROR) << "Failed to read the screenshot file: " << local_file.value(); |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | content::GetUIThreadTaskRunner({})->PostTask( |
[email protected] | 026b7c4f | 2020-10-27 12:05:21 | [diff] [blame^] | 110 | FROM_HERE, base::BindOnce(&DecodeImageFileAndCopyToClipboard, |
| 111 | /*clipboard_sequence=*/0, |
| 112 | /*maintain_clipboard=*/false, png_data, |
| 113 | base::DoNothing::Once<bool>())); |
[email protected] | 2ae4df18 | 2020-08-27 16:48:09 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | void DecodeImageFileAndCopyToClipboard( |
[email protected] | 026b7c4f | 2020-10-27 12:05:21 | [diff] [blame^] | 117 | uint64_t clipboard_sequence, |
| 118 | bool maintain_clipboard, |
| 119 | scoped_refptr<base::RefCountedString> png_data, |
| 120 | base::OnceCallback<void(bool)> callback) { |
[email protected] | 2ae4df18 | 2020-08-27 16:48:09 | [diff] [blame] | 121 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 122 | |
| 123 | // Decode the image in sandboxed process because |png_data| comes from |
| 124 | // external storage. |
| 125 | data_decoder::DecodeImageIsolated( |
| 126 | std::vector<uint8_t>(png_data->data().begin(), png_data->data().end()), |
| 127 | data_decoder::mojom::ImageCodec::DEFAULT, false, |
| 128 | data_decoder::kDefaultMaxSizeInBytes, gfx::Size(), |
[email protected] | 026b7c4f | 2020-10-27 12:05:21 | [diff] [blame^] | 129 | base::BindOnce(&CopyImageToClipboard, maintain_clipboard, |
| 130 | clipboard_sequence, std::move(callback), png_data)); |
[email protected] | 57118ea | 2020-07-17 03:17:14 | [diff] [blame] | 131 | } |
| 132 | } // namespace clipboard_util |