Commit 2b7c9e05 authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Fix some nits in clipboard_util.cc.

- Use base::StrCat() to more efficiently concatenate strings.
- Use the base::span version of base::Base64Encode().
- Get rid of the weird scoping in CopyImageToClipboard().
- Use base::MakeRefCounted() instead of the new keyword.

Change-Id: Ia36fe209b48851000fa875f9e38366660081b8eb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2429973Reviewed-by: default avatarRicardo Quesada <ricardoq@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#810776}
parent 0213d77d
......@@ -8,6 +8,7 @@
#include "base/files/file_util.h"
#include "base/memory/ref_counted_memory.h"
#include "base/metrics/user_metrics.h"
#include "base/strings/strcat.h"
#include "base/threading/scoped_blocking_call.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
......@@ -18,26 +19,24 @@
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);
ui::ScopedClipboardWriter clipboard_writer(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);
}
// Send both HTML and and Image formats to clipboard. HTML format is needed
// by ARC, while Image is needed by Hangout.
static const char kImageClipboardFormatPrefix[] =
"<img src='data:image/png;base64,";
static const char kImageClipboardFormatSuffix[] = "'>";
std::string encoded =
base::Base64Encode(base::as_bytes(base::make_span(png_data->data())));
std::string html = base::StrCat(
{kImageClipboardFormatPrefix, encoded, kImageClipboardFormatSuffix});
clipboard_writer.WriteHTML(base::UTF8ToUTF16(html), std::string());
clipboard_writer.WriteImage(decoded_image);
}
} // namespace
......@@ -46,7 +45,7 @@ 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());
auto png_data = base::MakeRefCounted<base::RefCountedString>();
if (!base::ReadFileToString(local_file, &(png_data->data()))) {
LOG(ERROR) << "Failed to read the screenshot file: " << local_file.value();
return;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment