blob: 0a227dc54a45d500d3225d54f7c40723c0377f57 [file] [log] [blame]
[email protected]57118ea2020-07-17 03:17:141// 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]026b7c4f2020-10-27 12:05:217#include <stdint.h>
8#include <memory>
9
[email protected]57118ea2020-07-17 03:17:1410#include "base/base64.h"
[email protected]026b7c4f2020-10-27 12:05:2111#include "base/callback.h"
[email protected]57118ea2020-07-17 03:17:1412#include "base/files/file_util.h"
13#include "base/memory/ref_counted_memory.h"
[email protected]026b7c4f2020-10-27 12:05:2114#include "base/memory/scoped_refptr.h"
[email protected]57118ea2020-07-17 03:17:1415#include "base/metrics/user_metrics.h"
Lei Zhang2b7c9e02020-09-25 21:30:4716#include "base/strings/strcat.h"
[email protected]57118ea2020-07-17 03:17:1417#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]026b7c4f2020-10-27 12:05:2122#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]57118ea2020-07-17 03:17:1425#include "ui/base/clipboard/scoped_clipboard_writer.h"
26
27namespace clipboard_util {
28namespace {
29
[email protected]026b7c4f2020-10-27 12:05:2130void 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]57118ea2020-07-17 03:17:1435 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
36
[email protected]026b7c4f2020-10-27 12:05:2137 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 */
54void 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]57118ea2020-07-17 03:17:1460
Lei Zhang2b7c9e02020-09-25 21:30:4761 // 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]026b7c4f2020-10-27 12:05:2171
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]57118ea2020-07-17 03:17:1495}
96
[email protected]57118ea2020-07-17 03:17:1497} // namespace
98
99void 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 Zhang2b7c9e02020-09-25 21:30:47103 auto png_data = base::MakeRefCounted<base::RefCountedString>();
[email protected]57118ea2020-07-17 03:17:14104 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]026b7c4f2020-10-27 12:05:21110 FROM_HERE, base::BindOnce(&DecodeImageFileAndCopyToClipboard,
111 /*clipboard_sequence=*/0,
112 /*maintain_clipboard=*/false, png_data,
113 base::DoNothing::Once<bool>()));
[email protected]2ae4df182020-08-27 16:48:09114}
115
116void DecodeImageFileAndCopyToClipboard(
[email protected]026b7c4f2020-10-27 12:05:21117 uint64_t clipboard_sequence,
118 bool maintain_clipboard,
119 scoped_refptr<base::RefCountedString> png_data,
120 base::OnceCallback<void(bool)> callback) {
[email protected]2ae4df182020-08-27 16:48:09121 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]026b7c4f2020-10-27 12:05:21129 base::BindOnce(&CopyImageToClipboard, maintain_clipboard,
130 clipboard_sequence, std::move(callback), png_data));
[email protected]57118ea2020-07-17 03:17:14131}
132} // namespace clipboard_util