feat: promisify session.clearAuthCache() (#17259)

* feat: promisify session.clearAuthCache()

* remove unused callback runner helpers
This commit is contained in:
Shelley Vohr 2019-03-08 18:41:42 -08:00 committed by GitHub
parent 58a9a81895
commit 9ea6c01e02
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 80 additions and 25 deletions

View file

@ -210,17 +210,6 @@ const char kPersistPrefix[] = "persist:";
// Referenced session objects.
std::map<uint32_t, v8::Global<v8::Object>> g_sessions;
// Runs the callback in UI thread.
void RunCallbackInUI(const base::Callback<void()>& callback) {
base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI}, callback);
}
template <typename... T>
void RunCallbackInUI(const base::Callback<void(T...)>& callback, T... result) {
base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI},
base::BindOnce(callback, result...));
}
void ResolveOrRejectPromiseInUI(atom::util::Promise promise, int net_error) {
if (net_error != net::OK) {
std::string err_msg = net::ErrorToString(net_error);
@ -313,7 +302,7 @@ void ClearHostResolverCacheInIO(
void ClearAuthCacheInIO(
const scoped_refptr<net::URLRequestContextGetter>& context_getter,
const ClearAuthCacheOptions& options,
const base::Closure& callback) {
util::Promise promise) {
auto* request_context = context_getter->GetURLRequestContext();
auto* network_session =
request_context->http_transaction_factory()->GetSession();
@ -333,8 +322,9 @@ void ClearAuthCacheInIO(
}
network_session->CloseAllConnections();
}
if (!callback.is_null())
RunCallbackInUI(callback);
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::UI},
base::BindOnce(util::Promise::ResolveEmptyPromise, std::move(promise)));
}
void AllowNTLMCredentialsForDomainsInIO(
@ -625,20 +615,23 @@ v8::Local<v8::Promise> Session::ClearHostResolverCache(mate::Arguments* args) {
return handle;
}
void Session::ClearAuthCache(mate::Arguments* args) {
v8::Local<v8::Promise> Session::ClearAuthCache(mate::Arguments* args) {
v8::Isolate* isolate = args->isolate();
util::Promise promise(isolate);
v8::Local<v8::Promise> handle = promise.GetHandle();
ClearAuthCacheOptions options;
if (!args->GetNext(&options)) {
args->ThrowError("Must specify options object");
return;
promise.RejectWithErrorMessage("Must specify options object");
return handle;
}
base::Closure callback;
args->GetNext(&callback);
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(&ClearAuthCacheInIO,
WrapRefCounted(browser_context_->GetRequestContext()),
options, callback));
options, std::move(promise)));
return handle;
}
void Session::AllowNTLMCredentialsForDomains(const std::string& domains) {

View file

@ -78,7 +78,7 @@ class Session : public mate::TrackableObject<Session>,
void SetPermissionCheckHandler(v8::Local<v8::Value> val,
mate::Arguments* args);
v8::Local<v8::Promise> ClearHostResolverCache(mate::Arguments* args);
void ClearAuthCache(mate::Arguments* args);
v8::Local<v8::Promise> ClearAuthCache(mate::Arguments* args);
void AllowNTLMCredentialsForDomains(const std::string& domains);
void SetUserAgent(const std::string& user_agent, mate::Arguments* args);
std::string GetUserAgent();