From 19256a07dde3771b36cdec01ccf4b004a623d79b Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 22 Sep 2025 14:12:47 -0500 Subject: [PATCH] refactor: use gin::Arguments in api::Clipboard (#48354) * refactor: make api::Clipboard::GetClipboardBuffer() private * refactor: remove unused gin_helper::Arguments* arg from Clipboard::ReadBuffer() * refactor: remove unused gin_helper::Arguments* arg from Clipboard::ReadBookmark() * refactor: move GetClipboadBuffer() into anonymous namespace * refactor: use gin::Arguments in api::Clipboard --- shell/common/api/electron_api_clipboard.cc | 57 ++++++++++++---------- shell/common/api/electron_api_clipboard.h | 45 ++++++++--------- 2 files changed, 51 insertions(+), 51 deletions(-) diff --git a/shell/common/api/electron_api_clipboard.cc b/shell/common/api/electron_api_clipboard.cc index 17563f1e1d..2f810d4250 100644 --- a/shell/common/api/electron_api_clipboard.cc +++ b/shell/common/api/electron_api_clipboard.cc @@ -22,18 +22,21 @@ #include "ui/gfx/codec/png_codec.h" #include "ui/gfx/image/image.h" -namespace electron::api { +namespace { -ui::ClipboardBuffer Clipboard::GetClipboardBuffer(gin_helper::Arguments* args) { +[[nodiscard]] ui::ClipboardBuffer GetClipboardBuffer(gin::Arguments* args) { std::string type; - if (args->GetNext(&type) && type == "selection") - return ui::ClipboardBuffer::kSelection; - else - return ui::ClipboardBuffer::kCopyPaste; + return args->GetNext(&type) && type == "selection" + ? ui::ClipboardBuffer::kSelection + : ui::ClipboardBuffer::kCopyPaste; } +} // namespace + +namespace electron::api { + std::vector Clipboard::AvailableFormats( - gin_helper::Arguments* args) { + gin::Arguments* const args) { std::vector format_types; ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); clipboard->ReadAvailableTypes(GetClipboardBuffer(args), @@ -42,7 +45,7 @@ std::vector Clipboard::AvailableFormats( } bool Clipboard::Has(const std::string& format_string, - gin_helper::Arguments* args) { + gin::Arguments* const args) { ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); ui::ClipboardFormatType format = ui::ClipboardFormatType::CustomPlatformType(format_string); @@ -97,17 +100,17 @@ std::string Clipboard::Read(const std::string& format_string) { return data; } -v8::Local Clipboard::ReadBuffer(const std::string& format_string, - gin_helper::Arguments* args) { +v8::Local Clipboard::ReadBuffer(v8::Isolate* const isolate, + const std::string& format_string) { std::string data = Read(format_string); - return electron::Buffer::Copy(args->isolate(), data).ToLocalChecked(); + return electron::Buffer::Copy(isolate, data).ToLocalChecked(); } void Clipboard::WriteBuffer(const std::string& format, const v8::Local buffer, - gin_helper::Arguments* args) { + gin::Arguments* const args) { if (!node::Buffer::HasInstance(buffer)) { - args->ThrowError("buffer must be a node Buffer"); + args->ThrowTypeError("buffer must be a node Buffer"); return; } @@ -124,7 +127,7 @@ void Clipboard::WriteBuffer(const std::string& format, } void Clipboard::Write(const gin_helper::Dictionary& data, - gin_helper::Arguments* args) { + gin::Arguments* const args) { ui::ScopedClipboardWriter writer(GetClipboardBuffer(args)); std::u16string text, html, bookmark; gfx::Image image; @@ -148,7 +151,7 @@ void Clipboard::Write(const gin_helper::Dictionary& data, writer.WriteImage(image.AsBitmap()); } -std::u16string Clipboard::ReadText(gin_helper::Arguments* args) { +std::u16string Clipboard::ReadText(gin::Arguments* const args) { std::u16string data; ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); auto type = GetClipboardBuffer(args); @@ -170,24 +173,24 @@ std::u16string Clipboard::ReadText(gin_helper::Arguments* args) { } void Clipboard::WriteText(const std::u16string& text, - gin_helper::Arguments* args) { + gin::Arguments* const args) { ui::ScopedClipboardWriter writer(GetClipboardBuffer(args)); writer.WriteText(text); } -std::u16string Clipboard::ReadRTF(gin_helper::Arguments* args) { +std::u16string Clipboard::ReadRTF(gin::Arguments* const args) { std::string data; ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); clipboard->ReadRTF(GetClipboardBuffer(args), /* data_dst = */ nullptr, &data); return base::UTF8ToUTF16(data); } -void Clipboard::WriteRTF(const std::string& text, gin_helper::Arguments* args) { +void Clipboard::WriteRTF(const std::string& text, gin::Arguments* const args) { ui::ScopedClipboardWriter writer(GetClipboardBuffer(args)); writer.WriteRTF(text); } -std::u16string Clipboard::ReadHTML(gin_helper::Arguments* args) { +std::u16string Clipboard::ReadHTML(gin::Arguments* const args) { std::u16string data; std::u16string html; std::string url; @@ -201,15 +204,15 @@ std::u16string Clipboard::ReadHTML(gin_helper::Arguments* args) { } void Clipboard::WriteHTML(const std::u16string& html, - gin_helper::Arguments* args) { + gin::Arguments* const args) { ui::ScopedClipboardWriter writer(GetClipboardBuffer(args)); writer.WriteHTML(html, std::string()); } -v8::Local Clipboard::ReadBookmark(gin_helper::Arguments* args) { +v8::Local Clipboard::ReadBookmark(v8::Isolate* const isolate) { std::u16string title; std::string url; - auto dict = gin_helper::Dictionary::CreateEmpty(args->isolate()); + auto dict = gin_helper::Dictionary::CreateEmpty(isolate); ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); clipboard->ReadBookmark(/* data_dst = */ nullptr, &title, &url); dict.Set("title", title); @@ -219,15 +222,15 @@ v8::Local Clipboard::ReadBookmark(gin_helper::Arguments* args) { void Clipboard::WriteBookmark(const std::u16string& title, const std::string& url, - gin_helper::Arguments* args) { + gin::Arguments* const args) { ui::ScopedClipboardWriter writer(GetClipboardBuffer(args)); writer.WriteBookmark(title, url); } -gfx::Image Clipboard::ReadImage(gin_helper::Arguments* args) { +gfx::Image Clipboard::ReadImage(gin::Arguments* const args) { // The ReadPng uses thread pool which requires app ready. if (IsBrowserProcess() && !Browser::Get()->is_ready()) { - args->ThrowError( + gin_helper::ErrorThrower{args->isolate()}.ThrowError( "clipboard.readImage is available only after app ready in the main " "process"); return {}; @@ -256,7 +259,7 @@ gfx::Image Clipboard::ReadImage(gin_helper::Arguments* args) { } void Clipboard::WriteImage(const gfx::Image& image, - gin_helper::Arguments* args) { + gin::Arguments* const args) { ui::ScopedClipboardWriter writer(GetClipboardBuffer(args)); SkBitmap orig = image.AsBitmap(); SkBitmap bmp; @@ -274,7 +277,7 @@ std::u16string Clipboard::ReadFindText() { } #endif -void Clipboard::Clear(gin_helper::Arguments* args) { +void Clipboard::Clear(gin::Arguments* const args) { ui::Clipboard::GetForCurrentThread()->Clear(GetClipboardBuffer(args)); } diff --git a/shell/common/api/electron_api_clipboard.h b/shell/common/api/electron_api_clipboard.h index 74713c9a7b..d3c9c5a5ad 100644 --- a/shell/common/api/electron_api_clipboard.h +++ b/shell/common/api/electron_api_clipboard.h @@ -16,8 +16,11 @@ namespace gfx { class Image; } // namespace gfx -namespace gin_helper { +namespace gin { class Arguments; +} // namespace gin + +namespace gin_helper { class Dictionary; } // namespace gin_helper @@ -29,44 +32,38 @@ class Clipboard { Clipboard(const Clipboard&) = delete; Clipboard& operator=(const Clipboard&) = delete; - static ui::ClipboardBuffer GetClipboardBuffer(gin_helper::Arguments* args); - static std::vector AvailableFormats( - gin_helper::Arguments* args); - static bool Has(const std::string& format_string, - gin_helper::Arguments* args); - static void Clear(gin_helper::Arguments* args); + static std::vector AvailableFormats(gin::Arguments* args); + static bool Has(const std::string& format_string, gin::Arguments* args); + static void Clear(gin::Arguments* args); static std::string Read(const std::string& format_string); - static void Write(const gin_helper::Dictionary& data, - gin_helper::Arguments* args); + static void Write(const gin_helper::Dictionary& data, gin::Arguments* args); - static std::u16string ReadText(gin_helper::Arguments* args); - static void WriteText(const std::u16string& text, - gin_helper::Arguments* args); + static std::u16string ReadText(gin::Arguments* args); + static void WriteText(const std::u16string& text, gin::Arguments* args); - static std::u16string ReadRTF(gin_helper::Arguments* args); - static void WriteRTF(const std::string& text, gin_helper::Arguments* args); + static std::u16string ReadRTF(gin::Arguments* args); + static void WriteRTF(const std::string& text, gin::Arguments* args); - static std::u16string ReadHTML(gin_helper::Arguments* args); - static void WriteHTML(const std::u16string& html, - gin_helper::Arguments* args); + static std::u16string ReadHTML(gin::Arguments* args); + static void WriteHTML(const std::u16string& html, gin::Arguments* args); - static v8::Local ReadBookmark(gin_helper::Arguments* args); + static v8::Local ReadBookmark(v8::Isolate* isolate); static void WriteBookmark(const std::u16string& title, const std::string& url, - gin_helper::Arguments* args); + gin::Arguments* args); - static gfx::Image ReadImage(gin_helper::Arguments* args); - static void WriteImage(const gfx::Image& image, gin_helper::Arguments* args); + static gfx::Image ReadImage(gin::Arguments* args); + static void WriteImage(const gfx::Image& image, gin::Arguments* args); static std::u16string ReadFindText(); static void WriteFindText(const std::u16string& text); - static v8::Local ReadBuffer(const std::string& format_string, - gin_helper::Arguments* args); + static v8::Local ReadBuffer(v8::Isolate* isolate, + const std::string& format_string); static void WriteBuffer(const std::string& format_string, const v8::Local buffer, - gin_helper::Arguments* args); + gin::Arguments* args); static void WriteFilesForTesting(const std::vector& files); };