From 49bd74ff0e91a0f3c4030225c4813eb7244fc092 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 9 Sep 2019 00:10:18 +0900 Subject: [PATCH] chore: remove native_mate (Part 4) (#20146) * avoid patching gin::Dictionary by using our wrapper * remove SetHidden from mate::Dictionary --- native_mate/native_mate/dictionary.h | 26 --------- .../native_mate/object_template_builder.h | 6 +-- patches/chromium/.patches | 1 - .../gin_dictionary_default_constructor.patch | 37 ------------- shell/browser/api/atom_api_app.cc | 12 +++-- shell/browser/api/atom_api_dialog.cc | 15 +++--- shell/browser/api/atom_api_in_app_purchase.cc | 10 ++-- .../api/atom_api_system_preferences.cc | 3 +- .../browser/api/atom_api_top_level_window.cc | 2 +- .../browser/atom_download_manager_delegate.cc | 5 +- .../browser/atom_download_manager_delegate.h | 3 +- shell/browser/ui/cocoa/atom_bundle_mover.mm | 2 +- shell/browser/ui/file_dialog.h | 6 +-- shell/browser/ui/file_dialog_gtk.cc | 28 +++++----- shell/browser/ui/file_dialog_mac.mm | 32 ++++++----- shell/browser/ui/file_dialog_win.cc | 21 ++++---- shell/browser/web_dialog_helper.cc | 9 ++-- shell/common/api/electron_bindings.cc | 11 ++-- shell/common/gin_converters/std_converter.h | 17 ++++++ shell/common/gin_helper/dictionary.h | 53 +++++++++++++++++++ .../native_mate_converters/gfx_converter.cc | 28 +++++----- .../v8_value_converter.cc | 10 ++-- .../atom_sandboxed_renderer_client.cc | 3 +- shell/renderer/renderer_client_base.cc | 12 ++--- 24 files changed, 190 insertions(+), 162 deletions(-) delete mode 100644 patches/chromium/gin_dictionary_default_constructor.patch diff --git a/native_mate/native_mate/dictionary.h b/native_mate/native_mate/dictionary.h index bbd2efce0bd2..d6a430d9200b 100644 --- a/native_mate/native_mate/dictionary.h +++ b/native_mate/native_mate/dictionary.h @@ -55,19 +55,6 @@ class Dictionary { return ConvertFromV8(isolate_, val, out); } - template - bool GetHidden(base::StringPiece key, T* out) const { - v8::Local context = isolate_->GetCurrentContext(); - v8::Local privateKey = - v8::Private::ForApi(isolate_, StringToV8(isolate_, key)); - v8::Local value; - v8::Maybe result = GetHandle()->HasPrivate(context, privateKey); - if (internal::IsTrue(result) && - GetHandle()->GetPrivate(context, privateKey).ToLocal(&value)) - return ConvertFromV8(isolate_, value, out); - return false; - } - template bool Set(base::StringPiece key, const T& val) { v8::Local v8_value; @@ -78,19 +65,6 @@ class Dictionary { return !result.IsNothing() && result.FromJust(); } - template - bool SetHidden(base::StringPiece key, T val) { - v8::Local v8_value; - if (!TryConvertToV8(isolate_, val, &v8_value)) - return false; - v8::Local context = isolate_->GetCurrentContext(); - v8::Local privateKey = - v8::Private::ForApi(isolate_, StringToV8(isolate_, key)); - v8::Maybe result = - GetHandle()->SetPrivate(context, privateKey, v8_value); - return !result.IsNothing() && result.FromJust(); - } - template bool SetReadOnly(base::StringPiece key, T val) { v8::Local v8_value; diff --git a/native_mate/native_mate/object_template_builder.h b/native_mate/native_mate/object_template_builder.h index 5ed218c12d01..a576f4edfc8a 100644 --- a/native_mate/native_mate/object_template_builder.h +++ b/native_mate/native_mate/object_template_builder.h @@ -21,7 +21,7 @@ template struct CallbackTraits { static v8::Local CreateTemplate(v8::Isolate* isolate, T callback) { - return CreateFunctionTemplate(isolate, base::Bind(callback)); + return mate::CreateFunctionTemplate(isolate, base::Bind(callback)); } }; @@ -31,7 +31,7 @@ struct CallbackTraits> { static v8::Local CreateTemplate( v8::Isolate* isolate, const base::Callback& callback) { - return CreateFunctionTemplate(isolate, callback); + return mate::CreateFunctionTemplate(isolate, callback); } }; @@ -46,7 +46,7 @@ struct CallbackTraits< static v8::Local CreateTemplate(v8::Isolate* isolate, T callback) { int flags = HolderIsFirstArgument; - return CreateFunctionTemplate(isolate, base::Bind(callback), flags); + return mate::CreateFunctionTemplate(isolate, base::Bind(callback), flags); } }; diff --git a/patches/chromium/.patches b/patches/chromium/.patches index 554942093242..5ad65be39d9d 100644 --- a/patches/chromium/.patches +++ b/patches/chromium/.patches @@ -17,7 +17,6 @@ web_contents.patch webview_cross_drag.patch disable_user_gesture_requirement_for_beforeunload_dialogs.patch gin_enable_disable_v8_platform.patch -gin_dictionary_default_constructor.patch blink-worker-enable-csp-in-file-scheme.patch disable-redraw-lock.patch v8_context_snapshot_generator.patch diff --git a/patches/chromium/gin_dictionary_default_constructor.patch b/patches/chromium/gin_dictionary_default_constructor.patch deleted file mode 100644 index 3a2be76d07ea..000000000000 --- a/patches/chromium/gin_dictionary_default_constructor.patch +++ /dev/null @@ -1,37 +0,0 @@ -From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 -From: Cheng Zhao -Date: Thu, 4 Oct 2018 14:57:02 -0700 -Subject: gin_dictionary_default_constructor.patch - -Add default constructor for gin::Dictionary. - -This is required for automatically converting arguments for functions that -take gin::Dictionary as parameter. - -diff --git a/gin/dictionary.cc b/gin/dictionary.cc -index 95e00072700c..7643347890a5 100644 ---- a/gin/dictionary.cc -+++ b/gin/dictionary.cc -@@ -6,6 +6,10 @@ - - namespace gin { - -+Dictionary::Dictionary() -+ : isolate_(nullptr) { -+} -+ - Dictionary::Dictionary(v8::Isolate* isolate) - : isolate_(isolate) { - } -diff --git a/gin/dictionary.h b/gin/dictionary.h -index 2645d328b4c1..43b227dd7e48 100644 ---- a/gin/dictionary.h -+++ b/gin/dictionary.h -@@ -24,6 +24,7 @@ namespace gin { - // - class GIN_EXPORT Dictionary { - public: -+ Dictionary(); - explicit Dictionary(v8::Isolate* isolate); - Dictionary(v8::Isolate* isolate, v8::Local object); - Dictionary(const Dictionary& other); diff --git a/shell/browser/api/atom_api_app.cc b/shell/browser/api/atom_api_app.cc index 0d190782afa3..562db2a5ec2c 100644 --- a/shell/browser/api/atom_api_app.cc +++ b/shell/browser/api/atom_api_app.cc @@ -43,6 +43,7 @@ #include "shell/browser/relauncher.h" #include "shell/common/application_info.h" #include "shell/common/atom_command_line.h" +#include "shell/common/gin_helper/dictionary.h" #include "shell/common/native_mate_converters/callback_converter_deprecated.h" #include "shell/common/native_mate_converters/file_path_converter.h" #include "shell/common/native_mate_converters/gurl_converter.h" @@ -1207,8 +1208,11 @@ std::vector App::GetAppMetrics(v8::Isolate* isolate) { mate::Dictionary pid_dict = mate::Dictionary::CreateEmpty(isolate); mate::Dictionary cpu_dict = mate::Dictionary::CreateEmpty(isolate); - pid_dict.SetHidden("simple", true); - cpu_dict.SetHidden("simple", true); + // TODO(zcbenz): Just call SetHidden when this file is converted to gin. + gin_helper::Dictionary(isolate, pid_dict.GetHandle()) + .SetHidden("simple", true); + gin_helper::Dictionary(isolate, cpu_dict.GetHandle()) + .SetHidden("simple", true); cpu_dict.Set( "percentCPUUsage", @@ -1236,7 +1240,9 @@ std::vector App::GetAppMetrics(v8::Isolate* isolate) { auto memory_info = process_metric.second->GetMemoryInfo(); mate::Dictionary memory_dict = mate::Dictionary::CreateEmpty(isolate); - memory_dict.SetHidden("simple", true); + // TODO(zcbenz): Just call SetHidden when this file is converted to gin. + gin_helper::Dictionary(isolate, memory_dict.GetHandle()) + .SetHidden("simple", true); memory_dict.Set("workingSetSize", static_cast(memory_info.working_set_size >> 10)); memory_dict.Set( diff --git a/shell/browser/api/atom_api_dialog.cc b/shell/browser/api/atom_api_dialog.cc index 996b9ee8f271..003cd1a77479 100644 --- a/shell/browser/api/atom_api_dialog.cc +++ b/shell/browser/api/atom_api_dialog.cc @@ -25,11 +25,12 @@ int ShowMessageBoxSync(const electron::MessageBoxSettings& settings) { return electron::ShowMessageBoxSync(settings); } -void ResolvePromiseObject(electron::util::Promise promise, - int result, - bool checkbox_checked) { +void ResolvePromiseObject( + electron::util::Promise promise, + int result, + bool checkbox_checked) { v8::Isolate* isolate = promise.isolate(); - gin::Dictionary dict = gin::Dictionary::CreateEmpty(isolate); + gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate); dict.Set("response", result); dict.Set("checkboxChecked", checkbox_checked); @@ -41,7 +42,7 @@ v8::Local ShowMessageBox( const electron::MessageBoxSettings& settings, gin::Arguments* args) { v8::Isolate* isolate = args->isolate(); - electron::util::Promise promise(isolate); + electron::util::Promise promise(isolate); v8::Local handle = promise.GetHandle(); electron::ShowMessageBox( @@ -60,7 +61,7 @@ void ShowOpenDialogSync(const file_dialog::DialogSettings& settings, v8::Local ShowOpenDialog( const file_dialog::DialogSettings& settings, gin::Arguments* args) { - electron::util::Promise promise(args->isolate()); + electron::util::Promise promise(args->isolate()); v8::Local handle = promise.GetHandle(); file_dialog::ShowOpenDialog(settings, std::move(promise)); return handle; @@ -76,7 +77,7 @@ void ShowSaveDialogSync(const file_dialog::DialogSettings& settings, v8::Local ShowSaveDialog( const file_dialog::DialogSettings& settings, gin::Arguments* args) { - electron::util::Promise promise(args->isolate()); + electron::util::Promise promise(args->isolate()); v8::Local handle = promise.GetHandle(); file_dialog::ShowSaveDialog(settings, std::move(promise)); diff --git a/shell/browser/api/atom_api_in_app_purchase.cc b/shell/browser/api/atom_api_in_app_purchase.cc index e572dd486131..8924d99f03a5 100644 --- a/shell/browser/api/atom_api_in_app_purchase.cc +++ b/shell/browser/api/atom_api_in_app_purchase.cc @@ -9,6 +9,7 @@ #include #include "native_mate/dictionary.h" +#include "shell/common/gin_helper/dictionary.h" #include "shell/common/node_includes.h" namespace mate { @@ -18,7 +19,8 @@ struct Converter { static v8::Local ToV8(v8::Isolate* isolate, const in_app_purchase::Payment& payment) { mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate); - dict.SetHidden("simple", true); + // TODO(zcbenz): Just call SetHidden when this file is converted to gin. + gin_helper::Dictionary(isolate, dict.GetHandle()).SetHidden("simple", true); dict.Set("productIdentifier", payment.productIdentifier); dict.Set("quantity", payment.quantity); return dict.GetHandle(); @@ -30,7 +32,8 @@ struct Converter { static v8::Local ToV8(v8::Isolate* isolate, const in_app_purchase::Transaction& val) { mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate); - dict.SetHidden("simple", true); + // TODO(zcbenz): Just call SetHidden when this file is converted to gin. + gin_helper::Dictionary(isolate, dict.GetHandle()).SetHidden("simple", true); dict.Set("transactionIdentifier", val.transactionIdentifier); dict.Set("transactionDate", val.transactionDate); dict.Set("originalTransactionIdentifier", @@ -48,7 +51,8 @@ struct Converter { static v8::Local ToV8(v8::Isolate* isolate, const in_app_purchase::Product& val) { mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate); - dict.SetHidden("simple", true); + // TODO(zcbenz): Just call SetHidden when this file is converted to gin. + gin_helper::Dictionary(isolate, dict.GetHandle()).SetHidden("simple", true); dict.Set("productIdentifier", val.productIdentifier); dict.Set("localizedDescription", val.localizedDescription); dict.Set("localizedTitle", val.localizedTitle); diff --git a/shell/browser/api/atom_api_system_preferences.cc b/shell/browser/api/atom_api_system_preferences.cc index 504b11676fc7..1f98956c35ac 100644 --- a/shell/browser/api/atom_api_system_preferences.cc +++ b/shell/browser/api/atom_api_system_preferences.cc @@ -5,6 +5,7 @@ #include "shell/browser/api/atom_api_system_preferences.h" #include "native_mate/dictionary.h" +#include "shell/common/gin_helper/dictionary.h" #include "shell/common/native_mate_converters/callback_converter_deprecated.h" #include "shell/common/native_mate_converters/value_converter.h" #include "shell/common/node_includes.h" @@ -45,7 +46,7 @@ bool SystemPreferences::IsHighContrastColorScheme() { v8::Local SystemPreferences::GetAnimationSettings( v8::Isolate* isolate) { - mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate); + gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate); dict.SetHidden("simple", true); dict.Set("shouldRenderRichAnimation", gfx::Animation::ShouldRenderRichAnimation()); diff --git a/shell/browser/api/atom_api_top_level_window.cc b/shell/browser/api/atom_api_top_level_window.cc index 074791624c43..f2df07158cb9 100644 --- a/shell/browser/api/atom_api_top_level_window.cc +++ b/shell/browser/api/atom_api_top_level_window.cc @@ -47,7 +47,7 @@ struct Converter { static bool FromV8(v8::Isolate* isolate, v8::Handle val, electron::TaskbarHost::ThumbarButton* out) { - gin::Dictionary dict; + gin::Dictionary dict(isolate); if (!gin::ConvertFromV8(isolate, val, &dict)) return false; dict.Get("click", &(out->clicked_callback)); diff --git a/shell/browser/atom_download_manager_delegate.cc b/shell/browser/atom_download_manager_delegate.cc index f9534d6d12bc..9807dd0fa06a 100644 --- a/shell/browser/atom_download_manager_delegate.cc +++ b/shell/browser/atom_download_manager_delegate.cc @@ -17,7 +17,6 @@ #include "content/public/browser/browser_thread.h" #include "content/public/browser/download_item_utils.h" #include "content/public/browser/download_manager.h" -#include "gin/dictionary.h" #include "net/base/filename_util.h" #include "shell/browser/api/atom_api_download_item.h" #include "shell/browser/atom_browser_context.h" @@ -124,7 +123,7 @@ void AtomDownloadManagerDelegate::OnDownloadPathGenerated( settings.force_detached = offscreen; v8::Isolate* isolate = v8::Isolate::GetCurrent(); - electron::util::Promise dialog_promise(isolate); + electron::util::Promise dialog_promise(isolate); auto dialog_callback = base::BindOnce(&AtomDownloadManagerDelegate::OnDownloadSaveDialogDone, base::Unretained(this), download_id, callback); @@ -141,7 +140,7 @@ void AtomDownloadManagerDelegate::OnDownloadPathGenerated( void AtomDownloadManagerDelegate::OnDownloadSaveDialogDone( uint32_t download_id, const content::DownloadTargetCallback& download_callback, - gin::Dictionary result) { + gin_helper::Dictionary result) { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); auto* item = download_manager_->GetDownload(download_id); diff --git a/shell/browser/atom_download_manager_delegate.h b/shell/browser/atom_download_manager_delegate.h index cdbf6a7fed05..2cbf725ad907 100644 --- a/shell/browser/atom_download_manager_delegate.h +++ b/shell/browser/atom_download_manager_delegate.h @@ -10,6 +10,7 @@ #include "base/memory/weak_ptr.h" #include "content/public/browser/download_manager_delegate.h" #include "shell/browser/ui/file_dialog.h" +#include "shell/common/gin_helper/dictionary.h" namespace content { class DownloadManager; @@ -48,7 +49,7 @@ class AtomDownloadManagerDelegate : public content::DownloadManagerDelegate { void OnDownloadSaveDialogDone( uint32_t download_id, const content::DownloadTargetCallback& download_callback, - gin::Dictionary result); + gin_helper::Dictionary result); content::DownloadManager* download_manager_; base::WeakPtrFactory weak_ptr_factory_; diff --git a/shell/browser/ui/cocoa/atom_bundle_mover.mm b/shell/browser/ui/cocoa/atom_bundle_mover.mm index 1c51b21e94f9..99d7ab93d7ae 100644 --- a/shell/browser/ui/cocoa/atom_bundle_mover.mm +++ b/shell/browser/ui/cocoa/atom_bundle_mover.mm @@ -38,7 +38,7 @@ namespace electron { bool AtomBundleMover::ShouldContinueMove(gin_helper::ErrorThrower thrower, BundlerMoverConflictType type, gin::Arguments* args) { - gin::Dictionary options; + gin::Dictionary options(args->isolate()); bool hasOptions = args->GetNext(&options); base::OnceCallback(BundlerMoverConflictType)> conflict_cb; diff --git a/shell/browser/ui/file_dialog.h b/shell/browser/ui/file_dialog.h index 7430e1b9abfb..ab9c44e88005 100644 --- a/shell/browser/ui/file_dialog.h +++ b/shell/browser/ui/file_dialog.h @@ -10,7 +10,7 @@ #include #include "base/files/file_path.h" -#include "gin/dictionary.h" +#include "shell/common/gin_helper/dictionary.h" #include "shell/common/promise_util.h" namespace electron { @@ -65,12 +65,12 @@ bool ShowOpenDialogSync(const DialogSettings& settings, std::vector* paths); void ShowOpenDialog(const DialogSettings& settings, - electron::util::Promise promise); + electron::util::Promise promise); bool ShowSaveDialogSync(const DialogSettings& settings, base::FilePath* path); void ShowSaveDialog(const DialogSettings& settings, - electron::util::Promise promise); + electron::util::Promise promise); } // namespace file_dialog diff --git a/shell/browser/ui/file_dialog_gtk.cc b/shell/browser/ui/file_dialog_gtk.cc index fda14d94ecb1..b48846bd0c15 100644 --- a/shell/browser/ui/file_dialog_gtk.cc +++ b/shell/browser/ui/file_dialog_gtk.cc @@ -138,15 +138,17 @@ class FileChooserDialog { gtk_window_present_with_time(GTK_WINDOW(dialog_), time); } - void RunSaveAsynchronous(electron::util::Promise promise) { - save_promise_.reset( - new electron::util::Promise(std::move(promise))); + void RunSaveAsynchronous( + electron::util::Promise promise) { + save_promise_.reset(new electron::util::Promise( + std::move(promise))); RunAsynchronous(); } - void RunOpenAsynchronous(electron::util::Promise promise) { - open_promise_.reset( - new electron::util::Promise(std::move(promise))); + void RunOpenAsynchronous( + electron::util::Promise promise) { + open_promise_.reset(new electron::util::Promise( + std::move(promise))); RunAsynchronous(); } @@ -187,8 +189,10 @@ class FileChooserDialog { GtkWidget* preview_; Filters filters_; - std::unique_ptr> save_promise_; - std::unique_ptr> open_promise_; + std::unique_ptr> + save_promise_; + std::unique_ptr> + open_promise_; // Callback for when we update the preview for the selection. CHROMEG_CALLBACK_0(FileChooserDialog, void, OnUpdatePreview, GtkWidget*); @@ -199,7 +203,7 @@ class FileChooserDialog { void FileChooserDialog::OnFileDialogResponse(GtkWidget* widget, int response) { gtk_widget_hide(dialog_); if (save_promise_) { - gin::Dictionary dict = + gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(save_promise_->isolate()); if (response == GTK_RESPONSE_ACCEPT) { dict.Set("canceled", false); @@ -210,7 +214,7 @@ void FileChooserDialog::OnFileDialogResponse(GtkWidget* widget, int response) { } save_promise_->ResolveWithGin(dict); } else if (open_promise_) { - gin::Dictionary dict = + gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(open_promise_->isolate()); if (response == GTK_RESPONSE_ACCEPT) { dict.Set("canceled", false); @@ -295,7 +299,7 @@ bool ShowOpenDialogSync(const DialogSettings& settings, } void ShowOpenDialog(const DialogSettings& settings, - electron::util::Promise promise) { + electron::util::Promise promise) { GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN; if (settings.properties & OPEN_DIALOG_OPEN_DIRECTORY) action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER; @@ -318,7 +322,7 @@ bool ShowSaveDialogSync(const DialogSettings& settings, base::FilePath* path) { } void ShowSaveDialog(const DialogSettings& settings, - electron::util::Promise promise) { + electron::util::Promise promise) { FileChooserDialog* save_dialog = new FileChooserDialog(GTK_FILE_CHOOSER_ACTION_SAVE, settings); save_dialog->RunSaveAsynchronous(std::move(promise)); diff --git a/shell/browser/ui/file_dialog_mac.mm b/shell/browser/ui/file_dialog_mac.mm index eb3ccd5a5f02..f2ac88038bf1 100644 --- a/shell/browser/ui/file_dialog_mac.mm +++ b/shell/browser/ui/file_dialog_mac.mm @@ -298,11 +298,12 @@ bool ShowOpenDialogSync(const DialogSettings& settings, return true; } -void OpenDialogCompletion(int chosen, - NSOpenPanel* dialog, - bool security_scoped_bookmarks, - electron::util::Promise promise) { - gin::Dictionary dict = gin::Dictionary::CreateEmpty(promise.isolate()); +void OpenDialogCompletion( + int chosen, + NSOpenPanel* dialog, + bool security_scoped_bookmarks, + electron::util::Promise promise) { + gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(promise.isolate()); if (chosen == NSFileHandlingPanelCancelButton) { dict.Set("canceled", true); dict.Set("filePaths", std::vector()); @@ -330,7 +331,7 @@ void OpenDialogCompletion(int chosen, } void ShowOpenDialog(const DialogSettings& settings, - electron::util::Promise promise) { + electron::util::Promise promise) { NSOpenPanel* dialog = [NSOpenPanel openPanel]; SetupDialog(dialog, settings); @@ -340,7 +341,8 @@ void ShowOpenDialog(const DialogSettings& settings, // and pass it to the completion handler. bool security_scoped_bookmarks = settings.security_scoped_bookmarks; - __block electron::util::Promise p = std::move(promise); + __block electron::util::Promise p = + std::move(promise); if (!settings.parent_window || !settings.parent_window->GetNativeWindow() || settings.force_detached) { @@ -375,11 +377,12 @@ bool ShowSaveDialogSync(const DialogSettings& settings, base::FilePath* path) { return true; } -void SaveDialogCompletion(int chosen, - NSSavePanel* dialog, - bool security_scoped_bookmarks, - electron::util::Promise promise) { - gin::Dictionary dict = gin::Dictionary::CreateEmpty(promise.isolate()); +void SaveDialogCompletion( + int chosen, + NSSavePanel* dialog, + bool security_scoped_bookmarks, + electron::util::Promise promise) { + gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(promise.isolate()); if (chosen == NSFileHandlingPanelCancelButton) { dict.Set("canceled", true); dict.Set("filePath", base::FilePath()); @@ -402,7 +405,7 @@ void SaveDialogCompletion(int chosen, } void ShowSaveDialog(const DialogSettings& settings, - electron::util::Promise promise) { + electron::util::Promise promise) { NSSavePanel* dialog = [NSSavePanel savePanel]; SetupDialog(dialog, settings); @@ -413,7 +416,8 @@ void ShowSaveDialog(const DialogSettings& settings, // and pass it to the completion handler. bool security_scoped_bookmarks = settings.security_scoped_bookmarks; - __block electron::util::Promise p = std::move(promise); + __block electron::util::Promise p = + std::move(promise); if (!settings.parent_window || !settings.parent_window->GetNativeWindow() || settings.force_detached) { diff --git a/shell/browser/ui/file_dialog_win.cc b/shell/browser/ui/file_dialog_win.cc index b7d81312a93c..3710a82be15f 100644 --- a/shell/browser/ui/file_dialog_win.cc +++ b/shell/browser/ui/file_dialog_win.cc @@ -82,10 +82,10 @@ bool CreateDialogThread(RunState* run_state) { return true; } -void OnDialogOpened(electron::util::Promise promise, +void OnDialogOpened(electron::util::Promise promise, bool canceled, std::vector paths) { - gin::Dictionary dict = gin::Dictionary::CreateEmpty(promise.isolate()); + gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(promise.isolate()); dict.Set("canceled", canceled); dict.Set("filePaths", paths); promise.ResolveWithGin(dict); @@ -94,7 +94,7 @@ void OnDialogOpened(electron::util::Promise promise, void RunOpenDialogInNewThread( const RunState& run_state, const DialogSettings& settings, - electron::util::Promise promise) { + electron::util::Promise promise) { std::vector paths; bool result = ShowOpenDialogSync(settings, &paths); run_state.ui_task_runner->PostTask( @@ -103,10 +103,10 @@ void RunOpenDialogInNewThread( run_state.ui_task_runner->DeleteSoon(FROM_HERE, run_state.dialog_thread); } -void OnSaveDialogDone(electron::util::Promise promise, +void OnSaveDialogDone(electron::util::Promise promise, bool canceled, const base::FilePath path) { - gin::Dictionary dict = gin::Dictionary::CreateEmpty(promise.isolate()); + gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(promise.isolate()); dict.Set("canceled", canceled); dict.Set("filePath", path); promise.ResolveWithGin(dict); @@ -115,7 +115,7 @@ void OnSaveDialogDone(electron::util::Promise promise, void RunSaveDialogInNewThread( const RunState& run_state, const DialogSettings& settings, - electron::util::Promise promise) { + electron::util::Promise promise) { base::FilePath path; bool result = ShowSaveDialogSync(settings, &path); run_state.ui_task_runner->PostTask( @@ -277,8 +277,8 @@ bool ShowOpenDialogSync(const DialogSettings& settings, } void ShowOpenDialog(const DialogSettings& settings, - electron::util::Promise promise) { - gin::Dictionary dict = gin::Dictionary::CreateEmpty(promise.isolate()); + electron::util::Promise promise) { + gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(promise.isolate()); RunState run_state; if (!CreateDialogThread(&run_state)) { dict.Set("canceled", true); @@ -327,10 +327,11 @@ bool ShowSaveDialogSync(const DialogSettings& settings, base::FilePath* path) { } void ShowSaveDialog(const DialogSettings& settings, - electron::util::Promise promise) { + electron::util::Promise promise) { RunState run_state; if (!CreateDialogThread(&run_state)) { - gin::Dictionary dict = gin::Dictionary::CreateEmpty(promise.isolate()); + gin_helper::Dictionary dict = + gin::Dictionary::CreateEmpty(promise.isolate()); dict.Set("canceled", true); dict.Set("filePath", base::FilePath()); promise.ResolveWithGin(dict); diff --git a/shell/browser/web_dialog_helper.cc b/shell/browser/web_dialog_helper.cc index 0df6d0ed5dd2..a5510ac498f9 100644 --- a/shell/browser/web_dialog_helper.cc +++ b/shell/browser/web_dialog_helper.cc @@ -28,6 +28,7 @@ #include "shell/browser/ui/file_dialog.h" #include "shell/common/gin_converters/callback_converter.h" #include "shell/common/gin_converters/file_path_converter.h" +#include "shell/common/gin_helper/dictionary.h" #include "ui/shell_dialogs/selected_file_info.h" using blink::mojom::FileChooserFileInfo; @@ -55,7 +56,7 @@ class FileSelectHelper : public base::RefCounted, void ShowOpenDialog(const file_dialog::DialogSettings& settings) { v8::Isolate* isolate = v8::Isolate::GetCurrent(); - electron::util::Promise promise(isolate); + electron::util::Promise promise(isolate); auto callback = base::BindOnce(&FileSelectHelper::OnOpenDialogDone, this); ignore_result(promise.Then(std::move(callback))); @@ -65,7 +66,7 @@ class FileSelectHelper : public base::RefCounted, void ShowSaveDialog(const file_dialog::DialogSettings& settings) { v8::Isolate* isolate = v8::Isolate::GetCurrent(); - electron::util::Promise promise(isolate); + electron::util::Promise promise(isolate); auto callback = base::BindOnce(&FileSelectHelper::OnSaveDialogDone, this); ignore_result(promise.Then(std::move(callback))); @@ -116,7 +117,7 @@ class FileSelectHelper : public base::RefCounted, AddRef(); } - void OnOpenDialogDone(gin::Dictionary result) { + void OnOpenDialogDone(gin_helper::Dictionary result) { std::vector file_info; bool canceled = true; result.Get("canceled", &canceled); @@ -158,7 +159,7 @@ class FileSelectHelper : public base::RefCounted, } } - void OnSaveDialogDone(gin::Dictionary result) { + void OnSaveDialogDone(gin_helper::Dictionary result) { std::vector file_info; bool canceled = true; result.Get("canceled", &canceled); diff --git a/shell/common/api/electron_bindings.cc b/shell/common/api/electron_bindings.cc index 9a2a3cff6ab4..71e6c23cc016 100644 --- a/shell/common/api/electron_bindings.cc +++ b/shell/common/api/electron_bindings.cc @@ -24,6 +24,7 @@ #include "shell/browser/browser.h" #include "shell/common/api/locker.h" #include "shell/common/application_info.h" +#include "shell/common/gin_helper/dictionary.h" #include "shell/common/heap_snapshot.h" #include "shell/common/native_mate_converters/file_path_converter.h" #include "shell/common/native_mate_converters/string16_converter.h" @@ -162,7 +163,7 @@ v8::Local ElectronBindings::GetHeapStatistics(v8::Isolate* isolate) { v8::HeapStatistics v8_heap_stats; isolate->GetHeapStatistics(&v8_heap_stats); - mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate); + gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate); dict.SetHidden("simple", true); dict.Set("totalHeapSize", static_cast(v8_heap_stats.total_heap_size() >> 10)); @@ -207,7 +208,7 @@ v8::Local ElectronBindings::GetSystemMemoryInfo( return v8::Undefined(isolate); } - mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate); + gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate); dict.SetHidden("simple", true); dict.Set("total", mem_info.total); @@ -256,7 +257,7 @@ v8::Local ElectronBindings::GetBlinkMemoryInfo( auto allocated = blink::ProcessHeap::TotalAllocatedObjectSize(); auto total = blink::ProcessHeap::TotalAllocatedSpace(); - mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate); + gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate); dict.SetHidden("simple", true); dict.Set("allocated", static_cast(allocated >> 10)); dict.Set("total", static_cast(total >> 10)); @@ -308,7 +309,7 @@ void ElectronBindings::DidReceiveMemoryDump( v8::Local ElectronBindings::GetCPUUsage( base::ProcessMetrics* metrics, v8::Isolate* isolate) { - mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate); + gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate); dict.SetHidden("simple", true); int processor_count = base::SysInfo::NumberOfProcessors(); dict.Set("percentCPUUsage", @@ -329,7 +330,7 @@ v8::Local ElectronBindings::GetCPUUsage( v8::Local ElectronBindings::GetIOCounters(v8::Isolate* isolate) { auto metrics = base::ProcessMetrics::CreateCurrentProcessMetrics(); base::IoCounters io_counters; - mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate); + gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate); dict.SetHidden("simple", true); if (metrics->GetIOCounters(&io_counters)) { diff --git a/shell/common/gin_converters/std_converter.h b/shell/common/gin_converters/std_converter.h index eaa54f6cb380..50e6f8a9617c 100644 --- a/shell/common/gin_converters/std_converter.h +++ b/shell/common/gin_converters/std_converter.h @@ -30,6 +30,23 @@ struct Converter { // NOLINT(runtime/int) }; #endif +template <> +struct Converter { + static v8::Local ToV8(v8::Isolate* isolate, const char* val) { + return v8::String::NewFromUtf8(isolate, val, v8::NewStringType::kNormal) + .ToLocalChecked(); + } +}; + +template +struct Converter { + static v8::Local ToV8(v8::Isolate* isolate, const char* val) { + return v8::String::NewFromUtf8(isolate, val, v8::NewStringType::kNormal, + n - 1) + .ToLocalChecked(); + } +}; + template <> struct Converter> { static v8::Local ToV8(v8::Isolate* isolate, diff --git a/shell/common/gin_helper/dictionary.h b/shell/common/gin_helper/dictionary.h index ec637c5fbf3b..8a48a093fd76 100644 --- a/shell/common/gin_helper/dictionary.h +++ b/shell/common/gin_helper/dictionary.h @@ -56,9 +56,41 @@ struct CallbackTraits< // convert between 2 types, we must not add any member. class Dictionary : public gin::Dictionary { public: + Dictionary() : gin::Dictionary(nullptr) {} Dictionary(v8::Isolate* isolate, v8::Local object) : gin::Dictionary(isolate, object) {} + // Allow implicitly converting from gin::Dictionary, as it is absolutely + // safe in this case. + Dictionary(const gin::Dictionary& dict) // NOLINT(runtime/explicit) + : gin::Dictionary(dict) {} + + template + bool GetHidden(base::StringPiece key, T* out) const { + v8::Local context = isolate()->GetCurrentContext(); + v8::Local privateKey = + v8::Private::ForApi(isolate(), gin::StringToV8(isolate(), key)); + v8::Local value; + v8::Maybe result = GetHandle()->HasPrivate(context, privateKey); + if (result.IsJust() && result.FromJust() && + GetHandle()->GetPrivate(context, privateKey).ToLocal(&value)) + return gin::ConvertFromV8(isolate(), value, out); + return false; + } + + template + bool SetHidden(base::StringPiece key, T val) { + v8::Local v8_value; + if (!gin::TryConvertToV8(isolate(), val, &v8_value)) + return false; + v8::Local context = isolate()->GetCurrentContext(); + v8::Local privateKey = + v8::Private::ForApi(isolate(), gin::StringToV8(isolate(), key)); + v8::Maybe result = + GetHandle()->SetPrivate(context, privateKey, v8_value); + return !result.IsNothing() && result.FromJust(); + } + template bool SetMethod(base::StringPiece key, const T& callback) { auto context = isolate()->GetCurrentContext(); @@ -98,4 +130,25 @@ class Dictionary : public gin::Dictionary { } // namespace gin_helper +namespace gin { + +template <> +struct Converter { + static v8::Local ToV8(v8::Isolate* isolate, + gin_helper::Dictionary val) { + return val.GetHandle(); + } + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + gin_helper::Dictionary* out) { + gin::Dictionary gdict(isolate); + if (!ConvertFromV8(isolate, val, &gdict)) + return false; + *out = gin_helper::Dictionary(gdict); + return true; + } +}; + +} // namespace gin + #endif // SHELL_COMMON_GIN_HELPER_DICTIONARY_H_ diff --git a/shell/common/native_mate_converters/gfx_converter.cc b/shell/common/native_mate_converters/gfx_converter.cc index 18c691d5bea9..c1cf1aef074a 100644 --- a/shell/common/native_mate_converters/gfx_converter.cc +++ b/shell/common/native_mate_converters/gfx_converter.cc @@ -5,6 +5,7 @@ #include "shell/common/native_mate_converters/gfx_converter.h" #include "native_mate/dictionary.h" +#include "shell/common/gin_helper/dictionary.h" #include "ui/display/display.h" #include "ui/display/screen.h" #include "ui/gfx/geometry/point.h" @@ -15,7 +16,7 @@ namespace mate { v8::Local Converter::ToV8(v8::Isolate* isolate, const gfx::Point& val) { - mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate); + gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate); dict.SetHidden("simple", true); dict.Set("x", val.x()); dict.Set("y", val.y()); @@ -25,8 +26,8 @@ v8::Local Converter::ToV8(v8::Isolate* isolate, bool Converter::FromV8(v8::Isolate* isolate, v8::Local val, gfx::Point* out) { - mate::Dictionary dict; - if (!ConvertFromV8(isolate, val, &dict)) + gin::Dictionary dict(isolate); + if (!gin::ConvertFromV8(isolate, val, &dict)) return false; double x, y; if (!dict.Get("x", &x) || !dict.Get("y", &y)) @@ -38,7 +39,7 @@ bool Converter::FromV8(v8::Isolate* isolate, v8::Local Converter::ToV8(v8::Isolate* isolate, const gfx::PointF& val) { - mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate); + gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate); dict.SetHidden("simple", true); dict.Set("x", val.x()); dict.Set("y", val.y()); @@ -48,8 +49,8 @@ v8::Local Converter::ToV8(v8::Isolate* isolate, bool Converter::FromV8(v8::Isolate* isolate, v8::Local val, gfx::PointF* out) { - mate::Dictionary dict; - if (!ConvertFromV8(isolate, val, &dict)) + gin::Dictionary dict(isolate); + if (!gin::ConvertFromV8(isolate, val, &dict)) return false; float x, y; if (!dict.Get("x", &x) || !dict.Get("y", &y)) @@ -60,7 +61,7 @@ bool Converter::FromV8(v8::Isolate* isolate, v8::Local Converter::ToV8(v8::Isolate* isolate, const gfx::Size& val) { - mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate); + gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate); dict.SetHidden("simple", true); dict.Set("width", val.width()); dict.Set("height", val.height()); @@ -70,8 +71,8 @@ v8::Local Converter::ToV8(v8::Isolate* isolate, bool Converter::FromV8(v8::Isolate* isolate, v8::Local val, gfx::Size* out) { - mate::Dictionary dict; - if (!ConvertFromV8(isolate, val, &dict)) + gin::Dictionary dict(isolate); + if (!gin::ConvertFromV8(isolate, val, &dict)) return false; int width, height; if (!dict.Get("width", &width) || !dict.Get("height", &height)) @@ -82,7 +83,7 @@ bool Converter::FromV8(v8::Isolate* isolate, v8::Local Converter::ToV8(v8::Isolate* isolate, const gfx::Rect& val) { - mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate); + gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate); dict.SetHidden("simple", true); dict.Set("x", val.x()); dict.Set("y", val.y()); @@ -94,8 +95,8 @@ v8::Local Converter::ToV8(v8::Isolate* isolate, bool Converter::FromV8(v8::Isolate* isolate, v8::Local val, gfx::Rect* out) { - mate::Dictionary dict; - if (!ConvertFromV8(isolate, val, &dict)) + gin::Dictionary dict(isolate); + if (!gin::ConvertFromV8(isolate, val, &dict)) return false; int x, y, width, height; if (!dict.Get("x", &x) || !dict.Get("y", &y) || !dict.Get("width", &width) || @@ -140,7 +141,8 @@ v8::Local Converter::ToV8( v8::Isolate* isolate, const display::Display& val) { mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate); - dict.SetHidden("simple", true); + // TODO(zcbenz): Just call SetHidden when this file is converted to gin. + gin_helper::Dictionary(isolate, dict.GetHandle()).SetHidden("simple", true); dict.Set("id", val.id()); dict.Set("bounds", val.bounds()); dict.Set("workArea", val.work_area()); diff --git a/shell/common/native_mate_converters/v8_value_converter.cc b/shell/common/native_mate_converters/v8_value_converter.cc index d241c6fba2d9..da4386df44ac 100644 --- a/shell/common/native_mate_converters/v8_value_converter.cc +++ b/shell/common/native_mate_converters/v8_value_converter.cc @@ -10,10 +10,8 @@ #include #include -#include "base/logging.h" #include "base/values.h" -#include "native_mate/dictionary.h" - +#include "shell/common/gin_helper/dictionary.h" #include "shell/common/node_bindings.h" #include "shell/common/node_includes.h" @@ -224,7 +222,7 @@ v8::Local V8ValueConverter::ToV8Array( v8::Local V8ValueConverter::ToV8Object( v8::Isolate* isolate, const base::DictionaryValue* val) const { - mate::Dictionary result = mate::Dictionary::CreateEmpty(isolate); + gin_helper::Dictionary result = gin::Dictionary::CreateEmpty(isolate); result.SetHidden("simple", true); for (base::DictionaryValue::Iterator iter(*val); !iter.IsAtEnd(); @@ -262,7 +260,7 @@ v8::Local V8ValueConverter::ToArrayBuffer( // From this point, if something goes wrong(can't find Buffer class for // example) we'll simply return a Uint8Array based on the created ArrayBuffer. // This can happen if no preload script was specified to the renderer. - mate::Dictionary global(isolate, context->Global()); + gin_helper::Dictionary global(isolate, context->Global()); v8::Local buffer_value; // Get the Buffer class stored as a hidden value in the global object. We'll @@ -272,7 +270,7 @@ v8::Local V8ValueConverter::ToArrayBuffer( return v8::Uint8Array::New(array_buffer, 0, length); } - mate::Dictionary buffer_class( + gin::Dictionary buffer_class( isolate, buffer_value->ToObject(isolate->GetCurrentContext()).ToLocalChecked()); v8::Local from_value; diff --git a/shell/renderer/atom_sandboxed_renderer_client.cc b/shell/renderer/atom_sandboxed_renderer_client.cc index 2006c88dfb6c..85ef5950a10c 100644 --- a/shell/renderer/atom_sandboxed_renderer_client.cc +++ b/shell/renderer/atom_sandboxed_renderer_client.cc @@ -15,6 +15,7 @@ #include "native_mate/dictionary.h" #include "shell/common/api/electron_bindings.h" #include "shell/common/application_info.h" +#include "shell/common/gin_helper/dictionary.h" #include "shell/common/native_mate_converters/string16_converter.h" #include "shell/common/native_mate_converters/value_converter.h" #include "shell/common/node_bindings.h" @@ -45,7 +46,7 @@ bool IsDevToolsExtension(content::RenderFrame* render_frame) { v8::Local GetModuleCache(v8::Isolate* isolate) { auto context = isolate->GetCurrentContext(); - mate::Dictionary global(isolate, context->Global()); + gin_helper::Dictionary global(isolate, context->Global()); v8::Local cache; if (!global.GetHidden(kModuleCacheKey, &cache)) { diff --git a/shell/renderer/renderer_client_base.cc b/shell/renderer/renderer_client_base.cc index 714c151a3832..9454aacecbbd 100644 --- a/shell/renderer/renderer_client_base.cc +++ b/shell/renderer/renderer_client_base.cc @@ -19,9 +19,9 @@ #include "content/public/renderer/render_thread.h" #include "content/public/renderer/render_view.h" #include "electron/buildflags/buildflags.h" -#include "native_mate/dictionary.h" #include "printing/buildflags/buildflags.h" #include "shell/common/color_util.h" +#include "shell/common/gin_helper/dictionary.h" #include "shell/common/native_mate_converters/value_converter.h" #include "shell/common/options_switches.h" #include "shell/renderer/atom_autofill_agent.h" @@ -114,7 +114,7 @@ void RendererClientBase::DidCreateScriptContext( // global.setHidden("contextId", `${processHostId}-${++next_context_id_}`) auto context_id = base::StringPrintf( "%s-%" PRId64, renderer_client_id_.c_str(), ++next_context_id_); - mate::Dictionary global(context->GetIsolate(), context->Global()); + gin_helper::Dictionary global(context->GetIsolate(), context->Global()); global.SetHidden("contextId", context_id); auto* command_line = base::CommandLine::ForCurrentProcess(); @@ -125,9 +125,7 @@ void RendererClientBase::DidCreateScriptContext( void RendererClientBase::AddRenderBindings( v8::Isolate* isolate, - v8::Local binding_object) { - mate::Dictionary dict(isolate, binding_object); -} + v8::Local binding_object) {} void RendererClientBase::RenderThreadStarted() { auto* command_line = base::CommandLine::ForCurrentProcess(); @@ -380,14 +378,14 @@ bool RendererClientBase::IsWebViewFrame( if (render_frame->IsMainFrame()) return false; - mate::Dictionary window_dict( + gin::Dictionary window_dict( isolate, GetContext(render_frame->GetWebFrame(), isolate)->Global()); v8::Local frame_element; if (!window_dict.Get("frameElement", &frame_element)) return false; - mate::Dictionary frame_element_dict(isolate, frame_element); + gin_helper::Dictionary frame_element_dict(isolate, frame_element); v8::Local internal; if (!frame_element_dict.GetHidden("internal", &internal))