Made the Read File and Copy to Clipboard public
The read file and copy to clipboard method was moved to a public method
so we can utilize this tool in other contexts. We want to implement this
method in the files app so we can copy images in the files app directly
to the system clipboard.
BUG=827333
Change-Id: I11d1429f48b3a087ad7f5251b8ca44dd81c0c75b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2273298
Commit-Queue: Alexander Nohe <[email protected]>
Reviewed-by: James Cook <[email protected]>
Cr-Commit-Position: refs/heads/master@{#789346}
diff --git a/chrome/browser/ui/ash/clipboard_util.cc b/chrome/browser/ui/ash/clipboard_util.cc
new file mode 100644
index 0000000..4380434
--- /dev/null
+++ b/chrome/browser/ui/ash/clipboard_util.cc
@@ -0,0 +1,71 @@
+// Copyright 2020 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/ui/ash/clipboard_util.h"
+
+#include "base/base64.h"
+#include "base/files/file_util.h"
+#include "base/memory/ref_counted_memory.h"
+#include "base/metrics/user_metrics.h"
+#include "base/threading/scoped_blocking_call.h"
+#include "content/public/browser/browser_task_traits.h"
+#include "content/public/browser/browser_thread.h"
+#include "services/data_decoder/public/cpp/decode_image.h"
+#include "ui/base/clipboard/clipboard.h"
+#include "ui/base/clipboard/scoped_clipboard_writer.h"
+
+namespace clipboard_util {
+namespace {
+
+const char kImageClipboardFormatPrefix[] = "<img src='data:image/png;base64,";
+const char kImageClipboardFormatSuffix[] = "'>";
+
+void CopyImageToClipboard(scoped_refptr<base::RefCountedString> png_data,
+ const SkBitmap& decoded_image) {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+
+ std::string encoded;
+ base::Base64Encode(png_data->data(), &encoded);
+ {
+ ui::ScopedClipboardWriter scw(ui::ClipboardBuffer::kCopyPaste);
+
+ // Send both HTML and and Image formats to clipboard. HTML format is needed
+ // by ARC, while Image is needed by Hangout.
+ std::string html(kImageClipboardFormatPrefix);
+ html += encoded;
+ html += kImageClipboardFormatSuffix;
+ scw.WriteHTML(base::UTF8ToUTF16(html), std::string());
+ scw.WriteImage(decoded_image);
+ }
+}
+
+void DecodeFileAndCopyToClipboard(
+ scoped_refptr<base::RefCountedString> png_data) {
+ DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
+
+ // Decode the image in sandboxed process because |png_data| comes from
+ // external storage.
+ data_decoder::DecodeImageIsolated(
+ std::vector<uint8_t>(png_data->data().begin(), png_data->data().end()),
+ data_decoder::mojom::ImageCodec::DEFAULT, false,
+ data_decoder::kDefaultMaxSizeInBytes, gfx::Size(),
+ base::BindOnce(&CopyImageToClipboard, png_data));
+}
+
+} // namespace
+
+void ReadFileAndCopyToClipboardLocal(const base::FilePath& local_file) {
+ DCHECK(!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
+ base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
+ base::BlockingType::WILL_BLOCK);
+ scoped_refptr<base::RefCountedString> png_data(new base::RefCountedString());
+ if (!base::ReadFileToString(local_file, &(png_data->data()))) {
+ LOG(ERROR) << "Failed to read the screenshot file: " << local_file.value();
+ return;
+ }
+
+ content::GetUIThreadTaskRunner({})->PostTask(
+ FROM_HERE, base::BindOnce(&DecodeFileAndCopyToClipboard, png_data));
+}
+} // namespace clipboard_util