chore: clean up promise resoution with helpers (#17268)

This commit is contained in:
Shelley Vohr 2019-03-13 14:30:21 -07:00 committed by GitHub
parent 3e5a98b5f4
commit d9234798d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 26 additions and 52 deletions

View file

@ -65,11 +65,6 @@ scoped_refptr<TracingController::TraceDataEndpoint> GetTraceDataEndpoint(
result_file_path, base::Bind(callback, result_file_path)); result_file_path, base::Bind(callback, result_file_path));
} }
void OnRecordingStopped(const atom::util::CopyablePromise& promise,
const base::FilePath& path) {
promise.GetPromise().Resolve(path);
}
v8::Local<v8::Promise> StopRecording(v8::Isolate* isolate, v8::Local<v8::Promise> StopRecording(v8::Isolate* isolate,
const base::FilePath& path) { const base::FilePath& path) {
atom::util::Promise promise(isolate); atom::util::Promise promise(isolate);
@ -78,28 +73,22 @@ v8::Local<v8::Promise> StopRecording(v8::Isolate* isolate,
// TODO(zcbenz): Remove the use of CopyablePromise when the // TODO(zcbenz): Remove the use of CopyablePromise when the
// CreateFileEndpoint API accepts OnceCallback. // CreateFileEndpoint API accepts OnceCallback.
TracingController::GetInstance()->StopTracing(GetTraceDataEndpoint( TracingController::GetInstance()->StopTracing(GetTraceDataEndpoint(
path, path, base::Bind(atom::util::CopyablePromise::ResolveCopyablePromise<
base::Bind(&OnRecordingStopped, atom::util::CopyablePromise(promise)))); const base::FilePath&>,
atom::util::CopyablePromise(promise))));
return handle; return handle;
} }
void OnCategoriesAvailable(atom::util::Promise promise,
const std::set<std::string>& categories) {
promise.Resolve(categories);
}
v8::Local<v8::Promise> GetCategories(v8::Isolate* isolate) { v8::Local<v8::Promise> GetCategories(v8::Isolate* isolate) {
atom::util::Promise promise(isolate); atom::util::Promise promise(isolate);
v8::Local<v8::Promise> handle = promise.GetHandle(); v8::Local<v8::Promise> handle = promise.GetHandle();
// Note: This method always succeeds. // Note: This method always succeeds.
TracingController::GetInstance()->GetCategories( TracingController::GetInstance()->GetCategories(base::BindOnce(
base::BindOnce(&OnCategoriesAvailable, std::move(promise))); atom::util::Promise::ResolvePromise<const std::set<std::string>&>,
return handle; std::move(promise)));
}
void OnTracingStarted(atom::util::Promise promise) { return handle;
promise.Resolve();
} }
v8::Local<v8::Promise> StartTracing( v8::Local<v8::Promise> StartTracing(
@ -110,7 +99,8 @@ v8::Local<v8::Promise> StartTracing(
// Note: This method always succeeds. // Note: This method always succeeds.
TracingController::GetInstance()->StartTracing( TracingController::GetInstance()->StartTracing(
trace_config, base::BindOnce(&OnTracingStarted, std::move(promise))); trace_config, base::BindOnce(atom::util::Promise::ResolveEmptyPromise,
std::move(promise)));
return handle; return handle;
} }

View file

@ -149,7 +149,7 @@ void FilterCookies(std::unique_ptr<base::DictionaryValue> filter,
base::PostTaskWithTraits( base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::UI}, FROM_HERE, {BrowserThread::UI},
base::BindOnce(util::Promise::ResolvePromise<net::CookieList>, base::BindOnce(util::Promise::ResolvePromise<const net::CookieList&>,
std::move(promise), std::move(result))); std::move(promise), std::move(result)));
} }

View file

@ -19,15 +19,6 @@
#include "native_mate/handle.h" #include "native_mate/handle.h"
#include "net/url_request/url_request_context_getter.h" #include "net/url_request/url_request_context_getter.h"
namespace {
void OnGetFilePathToCompletedLog(const atom::util::CopyablePromise& promise,
const base::FilePath& file_path) {
promise.GetPromise().Resolve(file_path);
}
} // namespace
namespace atom { namespace atom {
namespace api { namespace api {
@ -120,7 +111,8 @@ void NetLog::OnNewState(const base::DictionaryValue& state) {
// TODO(zcbenz): Remove the use of CopyablePromise when the // TODO(zcbenz): Remove the use of CopyablePromise when the
// GetFilePathToCompletedLog API accepts OnceCallback. // GetFilePathToCompletedLog API accepts OnceCallback.
net_log_writer_->GetFilePathToCompletedLog(base::Bind( net_log_writer_->GetFilePathToCompletedLog(base::Bind(
&OnGetFilePathToCompletedLog, util::CopyablePromise(promise))); util::CopyablePromise::ResolveCopyablePromise<const base::FilePath&>,
util::CopyablePromise(promise)));
} }
stop_callback_queue_.clear(); stop_callback_queue_.clear();
} }

View file

@ -177,10 +177,6 @@ bool IsProtocolHandledInIO(
return is_handled; return is_handled;
} }
void PromiseCallback(util::Promise promise, bool handled) {
promise.Resolve(handled);
}
v8::Local<v8::Promise> Protocol::IsProtocolHandled(const std::string& scheme) { v8::Local<v8::Promise> Protocol::IsProtocolHandled(const std::string& scheme) {
util::Promise promise(isolate()); util::Promise promise(isolate());
v8::Local<v8::Promise> handle = promise.GetHandle(); v8::Local<v8::Promise> handle = promise.GetHandle();
@ -190,7 +186,7 @@ v8::Local<v8::Promise> Protocol::IsProtocolHandled(const std::string& scheme) {
base::PostTaskWithTraitsAndReplyWithResult( base::PostTaskWithTraitsAndReplyWithResult(
FROM_HERE, {content::BrowserThread::IO}, FROM_HERE, {content::BrowserThread::IO},
base::BindOnce(&IsProtocolHandledInIO, base::RetainedRef(getter), scheme), base::BindOnce(&IsProtocolHandledInIO, base::RetainedRef(getter), scheme),
base::BindOnce(&PromiseCallback, std::move(promise))); base::BindOnce(util::Promise::ResolvePromise<bool>, std::move(promise)));
return handle; return handle;
} }

View file

@ -451,14 +451,6 @@ bool SystemPreferences::CanPromptTouchID() {
return false; return false;
} }
void OnTouchIDCompleted(util::Promise promise) {
promise.Resolve();
}
void OnTouchIDFailed(util::Promise promise, const std::string& reason) {
promise.RejectWithErrorMessage(reason);
}
v8::Local<v8::Promise> SystemPreferences::PromptTouchID( v8::Local<v8::Promise> SystemPreferences::PromptTouchID(
v8::Isolate* isolate, v8::Isolate* isolate,
const std::string& reason) { const std::string& reason) {
@ -486,16 +478,19 @@ v8::Local<v8::Promise> SystemPreferences::PromptTouchID(
localizedReason:[NSString stringWithUTF8String:reason.c_str()] localizedReason:[NSString stringWithUTF8String:reason.c_str()]
reply:^(BOOL success, NSError* error) { reply:^(BOOL success, NSError* error) {
if (!success) { if (!success) {
std::string err_msg = std::string(
[error.localizedDescription UTF8String]);
runner->PostTask(
FROM_HERE,
base::BindOnce(util::Promise::RejectPromise,
std::move(p),
std::move(err_msg)));
} else {
runner->PostTask( runner->PostTask(
FROM_HERE, FROM_HERE,
base::BindOnce( base::BindOnce(
&OnTouchIDFailed, std::move(p), util::Promise::ResolveEmptyPromise,
std::string([error.localizedDescription std::move(p)));
UTF8String])));
} else {
runner->PostTask(FROM_HERE,
base::BindOnce(&OnTouchIDCompleted,
std::move(p)));
} }
}]; }];
} else { } else {

View file

@ -1300,11 +1300,12 @@ v8::Local<v8::Promise> WebContents::SavePage(
const base::FilePath& full_file_path, const base::FilePath& full_file_path,
const content::SavePageType& save_type) { const content::SavePageType& save_type) {
util::Promise promise(isolate()); util::Promise promise(isolate());
v8::Local<v8::Promise> ret = promise.GetHandle(); v8::Local<v8::Promise> handle = promise.GetHandle();
auto* handler = new SavePageHandler(web_contents(), std::move(promise)); auto* handler = new SavePageHandler(web_contents(), std::move(promise));
handler->Handle(full_file_path, save_type); handler->Handle(full_file_path, save_type);
return ret;
return handle;
} }
void WebContents::OpenDevTools(mate::Arguments* args) { void WebContents::OpenDevTools(mate::Arguments* args) {