refactor: migrates util::Promise to gin (#20871)

* refactor: use gin in Promise

* refactor: separate Promise impl that returns nothing

* refactor: use Promise<void> for promise that returns nothing

* fix: methods should be able to run on both browser and renderer process

* fix: should not pass base::StringPiece across threads

* refactor: no more need to use different ResolvePromise for empty Promise

* refactor: move Promise to gin_helper
This commit is contained in:
Cheng Zhao 2019-11-01 15:10:32 +09:00 committed by GitHub
parent bff113760a
commit eaf2c61bef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
48 changed files with 483 additions and 479 deletions

View file

@ -9,7 +9,7 @@
#include "base/memory/ref_counted.h"
#include "net/cert/x509_certificate.h"
#include "shell/common/promise_util.h"
#include "shell/common/gin_helper/promise.h"
namespace electron {
class NativeWindow;

View file

@ -19,7 +19,7 @@
@interface TrustDelegate : NSObject {
@private
std::unique_ptr<electron::util::Promise<void*>> promise_;
std::unique_ptr<gin_helper::Promise<void>> promise_;
SFCertificateTrustPanel* panel_;
scoped_refptr<net::X509Certificate> cert_;
SecTrustRef trust_;
@ -27,7 +27,7 @@
SecPolicyRef sec_policy_;
}
- (id)initWithPromise:(electron::util::Promise<void*>)promise
- (id)initWithPromise:(gin_helper::Promise<void>)promise
panel:(SFCertificateTrustPanel*)panel
cert:(const scoped_refptr<net::X509Certificate>&)cert
trust:(SecTrustRef)trust
@ -51,14 +51,14 @@
[super dealloc];
}
- (id)initWithPromise:(electron::util::Promise<void*>)promise
- (id)initWithPromise:(gin_helper::Promise<void>)promise
panel:(SFCertificateTrustPanel*)panel
cert:(const scoped_refptr<net::X509Certificate>&)cert
trust:(SecTrustRef)trust
certChain:(CFArrayRef)certChain
secPolicy:(SecPolicyRef)secPolicy {
if ((self = [super init])) {
promise_.reset(new electron::util::Promise<void*>(std::move(promise)));
promise_.reset(new gin_helper::Promise<void>(std::move(promise)));
panel_ = panel;
cert_ = cert;
trust_ = trust;
@ -90,7 +90,7 @@ v8::Local<v8::Promise> ShowCertificateTrust(
const scoped_refptr<net::X509Certificate>& cert,
const std::string& message) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
electron::util::Promise<void*> promise(isolate);
gin_helper::Promise<void> promise(isolate);
v8::Local<v8::Promise> handle = promise.GetHandle();
auto* sec_policy = SecPolicyCreateBasicX509();

View file

@ -61,7 +61,7 @@ v8::Local<v8::Promise> ShowCertificateTrust(
const scoped_refptr<net::X509Certificate>& cert,
const std::string& message) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
electron::util::Promise<void*> promise(isolate);
gin_helper::Promise<void> promise(isolate);
v8::Local<v8::Promise> handle = promise.GetHandle();
PCCERT_CHAIN_CONTEXT chain_context;

View file

@ -11,7 +11,7 @@
#include "base/files/file_path.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/promise_util.h"
#include "shell/common/gin_helper/promise.h"
namespace electron {
class NativeWindow;
@ -65,12 +65,12 @@ bool ShowOpenDialogSync(const DialogSettings& settings,
std::vector<base::FilePath>* paths);
void ShowOpenDialog(const DialogSettings& settings,
electron::util::Promise<gin_helper::Dictionary> promise);
gin_helper::Promise<gin_helper::Dictionary> promise);
bool ShowSaveDialogSync(const DialogSettings& settings, base::FilePath* path);
void ShowSaveDialog(const DialogSettings& settings,
electron::util::Promise<gin_helper::Dictionary> promise);
gin_helper::Promise<gin_helper::Dictionary> promise);
} // namespace file_dialog

View file

@ -141,17 +141,17 @@ class FileChooserDialog {
}
void RunSaveAsynchronous(
electron::util::Promise<gin_helper::Dictionary> promise) {
gin_helper::Promise<gin_helper::Dictionary> promise) {
save_promise_ =
std::make_unique<electron::util::Promise<gin_helper::Dictionary>>(
std::make_unique<gin_helper::Promise<gin_helper::Dictionary>>(
std::move(promise));
RunAsynchronous();
}
void RunOpenAsynchronous(
electron::util::Promise<gin_helper::Dictionary> promise) {
gin_helper::Promise<gin_helper::Dictionary> promise) {
open_promise_ =
std::make_unique<electron::util::Promise<gin_helper::Dictionary>>(
std::make_unique<gin_helper::Promise<gin_helper::Dictionary>>(
std::move(promise));
RunAsynchronous();
}
@ -193,10 +193,8 @@ class FileChooserDialog {
GtkWidget* preview_;
Filters filters_;
std::unique_ptr<electron::util::Promise<gin_helper::Dictionary>>
save_promise_;
std::unique_ptr<electron::util::Promise<gin_helper::Dictionary>>
open_promise_;
std::unique_ptr<gin_helper::Promise<gin_helper::Dictionary>> save_promise_;
std::unique_ptr<gin_helper::Promise<gin_helper::Dictionary>> open_promise_;
// Callback for when we update the preview for the selection.
CHROMEG_CALLBACK_0(FileChooserDialog, void, OnUpdatePreview, GtkWidget*);
@ -216,7 +214,7 @@ void FileChooserDialog::OnFileDialogResponse(GtkWidget* widget, int response) {
dict.Set("canceled", true);
dict.Set("filePath", base::FilePath());
}
save_promise_->ResolveWithGin(dict);
save_promise_->Resolve(dict);
} else if (open_promise_) {
gin_helper::Dictionary dict =
gin::Dictionary::CreateEmpty(open_promise_->isolate());
@ -227,7 +225,7 @@ void FileChooserDialog::OnFileDialogResponse(GtkWidget* widget, int response) {
dict.Set("canceled", true);
dict.Set("filePaths", std::vector<base::FilePath>());
}
open_promise_->ResolveWithGin(dict);
open_promise_->Resolve(dict);
}
delete this;
}
@ -301,7 +299,7 @@ bool ShowOpenDialogSync(const DialogSettings& settings,
}
void ShowOpenDialog(const DialogSettings& settings,
electron::util::Promise<gin_helper::Dictionary> promise) {
gin_helper::Promise<gin_helper::Dictionary> promise) {
GtkFileChooserAction action = GTK_FILE_CHOOSER_ACTION_OPEN;
if (settings.properties & OPEN_DIALOG_OPEN_DIRECTORY)
action = GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER;
@ -324,7 +322,7 @@ bool ShowSaveDialogSync(const DialogSettings& settings, base::FilePath* path) {
}
void ShowSaveDialog(const DialogSettings& settings,
electron::util::Promise<gin_helper::Dictionary> promise) {
gin_helper::Promise<gin_helper::Dictionary> promise) {
FileChooserDialog* save_dialog =
new FileChooserDialog(GTK_FILE_CHOOSER_ACTION_SAVE, settings);
save_dialog->RunSaveAsynchronous(std::move(promise));

View file

@ -298,11 +298,10 @@ bool ShowOpenDialogSync(const DialogSettings& settings,
return true;
}
void OpenDialogCompletion(
int chosen,
NSOpenPanel* dialog,
bool security_scoped_bookmarks,
electron::util::Promise<gin_helper::Dictionary> promise) {
void OpenDialogCompletion(int chosen,
NSOpenPanel* dialog,
bool security_scoped_bookmarks,
gin_helper::Promise<gin_helper::Dictionary> promise) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(promise.isolate());
if (chosen == NSFileHandlingPanelCancelButton) {
dict.Set("canceled", true);
@ -310,7 +309,7 @@ void OpenDialogCompletion(
#if defined(MAS_BUILD)
dict.Set("bookmarks", std::vector<std::string>());
#endif
promise.ResolveWithGin(dict);
promise.Resolve(dict);
} else {
std::vector<base::FilePath> paths;
dict.Set("canceled", false);
@ -326,12 +325,12 @@ void OpenDialogCompletion(
ReadDialogPaths(dialog, &paths);
dict.Set("filePaths", paths);
#endif
promise.ResolveWithGin(dict);
promise.Resolve(dict);
}
}
void ShowOpenDialog(const DialogSettings& settings,
electron::util::Promise<gin_helper::Dictionary> promise) {
gin_helper::Promise<gin_helper::Dictionary> promise) {
NSOpenPanel* dialog = [NSOpenPanel openPanel];
SetupDialog(dialog, settings);
@ -341,8 +340,7 @@ void ShowOpenDialog(const DialogSettings& settings,
// and pass it to the completion handler.
bool security_scoped_bookmarks = settings.security_scoped_bookmarks;
__block electron::util::Promise<gin_helper::Dictionary> p =
std::move(promise);
__block gin_helper::Promise<gin_helper::Dictionary> p = std::move(promise);
if (!settings.parent_window || !settings.parent_window->GetNativeWindow() ||
settings.force_detached) {
@ -377,11 +375,10 @@ bool ShowSaveDialogSync(const DialogSettings& settings, base::FilePath* path) {
return true;
}
void SaveDialogCompletion(
int chosen,
NSSavePanel* dialog,
bool security_scoped_bookmarks,
electron::util::Promise<gin_helper::Dictionary> promise) {
void SaveDialogCompletion(int chosen,
NSSavePanel* dialog,
bool security_scoped_bookmarks,
gin_helper::Promise<gin_helper::Dictionary> promise) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(promise.isolate());
if (chosen == NSFileHandlingPanelCancelButton) {
dict.Set("canceled", true);
@ -401,11 +398,11 @@ void SaveDialogCompletion(
}
#endif
}
promise.ResolveWithGin(dict);
promise.Resolve(dict);
}
void ShowSaveDialog(const DialogSettings& settings,
electron::util::Promise<gin_helper::Dictionary> promise) {
gin_helper::Promise<gin_helper::Dictionary> promise) {
NSSavePanel* dialog = [NSSavePanel savePanel];
SetupDialog(dialog, settings);
@ -416,8 +413,7 @@ void ShowSaveDialog(const DialogSettings& settings,
// and pass it to the completion handler.
bool security_scoped_bookmarks = settings.security_scoped_bookmarks;
__block electron::util::Promise<gin_helper::Dictionary> p =
std::move(promise);
__block gin_helper::Promise<gin_helper::Dictionary> p = std::move(promise);
if (!settings.parent_window || !settings.parent_window->GetNativeWindow() ||
settings.force_detached) {

View file

@ -80,19 +80,19 @@ bool CreateDialogThread(RunState* run_state) {
return true;
}
void OnDialogOpened(electron::util::Promise<gin_helper::Dictionary> promise,
void OnDialogOpened(gin_helper::Promise<gin_helper::Dictionary> promise,
bool canceled,
std::vector<base::FilePath> paths) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(promise.isolate());
dict.Set("canceled", canceled);
dict.Set("filePaths", paths);
promise.ResolveWithGin(dict);
promise.Resolve(dict);
}
void RunOpenDialogInNewThread(
const RunState& run_state,
const DialogSettings& settings,
electron::util::Promise<gin_helper::Dictionary> promise) {
gin_helper::Promise<gin_helper::Dictionary> promise) {
std::vector<base::FilePath> paths;
bool result = ShowOpenDialogSync(settings, &paths);
run_state.ui_task_runner->PostTask(
@ -101,19 +101,19 @@ void RunOpenDialogInNewThread(
run_state.ui_task_runner->DeleteSoon(FROM_HERE, run_state.dialog_thread);
}
void OnSaveDialogDone(electron::util::Promise<gin_helper::Dictionary> promise,
void OnSaveDialogDone(gin_helper::Promise<gin_helper::Dictionary> promise,
bool canceled,
const base::FilePath path) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(promise.isolate());
dict.Set("canceled", canceled);
dict.Set("filePath", path);
promise.ResolveWithGin(dict);
promise.Resolve(dict);
}
void RunSaveDialogInNewThread(
const RunState& run_state,
const DialogSettings& settings,
electron::util::Promise<gin_helper::Dictionary> promise) {
gin_helper::Promise<gin_helper::Dictionary> promise) {
base::FilePath path;
bool result = ShowSaveDialogSync(settings, &path);
run_state.ui_task_runner->PostTask(
@ -275,13 +275,13 @@ bool ShowOpenDialogSync(const DialogSettings& settings,
}
void ShowOpenDialog(const DialogSettings& settings,
electron::util::Promise<gin_helper::Dictionary> promise) {
gin_helper::Promise<gin_helper::Dictionary> promise) {
gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(promise.isolate());
RunState run_state;
if (!CreateDialogThread(&run_state)) {
dict.Set("canceled", true);
dict.Set("filePaths", std::vector<base::FilePath>());
promise.ResolveWithGin(dict);
promise.Resolve(dict);
} else {
run_state.dialog_thread->task_runner()->PostTask(
FROM_HERE, base::BindOnce(&RunOpenDialogInNewThread, run_state,
@ -325,14 +325,14 @@ bool ShowSaveDialogSync(const DialogSettings& settings, base::FilePath* path) {
}
void ShowSaveDialog(const DialogSettings& settings,
electron::util::Promise<gin_helper::Dictionary> promise) {
gin_helper::Promise<gin_helper::Dictionary> promise) {
RunState run_state;
if (!CreateDialogThread(&run_state)) {
gin_helper::Dictionary dict =
gin::Dictionary::CreateEmpty(promise.isolate());
dict.Set("canceled", true);
dict.Set("filePath", base::FilePath());
promise.ResolveWithGin(dict);
promise.Resolve(dict);
} else {
run_state.dialog_thread->task_runner()->PostTask(
FROM_HERE, base::BindOnce(&RunSaveDialogInNewThread, run_state,