feat: promisify session cache methods (#17185)
This commit is contained in:
parent
652e232813
commit
fc10620082
6 changed files with 62 additions and 17 deletions
|
@ -221,17 +221,32 @@ void RunCallbackInUI(const base::Callback<void(T...)>& callback, T... result) {
|
|||
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);
|
||||
util::Promise::RejectPromise(std::move(promise), std::move(err_msg));
|
||||
} else {
|
||||
util::Promise::ResolveEmptyPromise(std::move(promise));
|
||||
}
|
||||
}
|
||||
|
||||
// Callback of HttpCache::GetBackend.
|
||||
void OnGetBackend(disk_cache::Backend** backend_ptr,
|
||||
Session::CacheAction action,
|
||||
const net::CompletionCallback& callback,
|
||||
const atom::util::CopyablePromise& promise,
|
||||
int result) {
|
||||
if (result != net::OK) {
|
||||
RunCallbackInUI(callback, result);
|
||||
std::string err_msg =
|
||||
"Failed to retrieve cache backend: " + net::ErrorToString(result);
|
||||
util::Promise::RejectPromise(promise.GetPromise(), std::move(err_msg));
|
||||
} else if (backend_ptr && *backend_ptr) {
|
||||
if (action == Session::CacheAction::CLEAR) {
|
||||
(*backend_ptr)
|
||||
->DoomAllEntries(base::Bind(&RunCallbackInUI<int>, callback));
|
||||
auto success =
|
||||
(*backend_ptr)
|
||||
->DoomAllEntries(base::BindOnce(&ResolveOrRejectPromiseInUI,
|
||||
promise.GetPromise()));
|
||||
if (success != net::ERR_IO_PENDING)
|
||||
ResolveOrRejectPromiseInUI(promise.GetPromise(), success);
|
||||
} else if (action == Session::CacheAction::STATS) {
|
||||
base::StringPairs stats;
|
||||
(*backend_ptr)->GetStats(&stats);
|
||||
|
@ -239,30 +254,35 @@ void OnGetBackend(disk_cache::Backend** backend_ptr,
|
|||
if (stat.first == "Current size") {
|
||||
int current_size;
|
||||
base::StringToInt(stat.second, ¤t_size);
|
||||
RunCallbackInUI(callback, current_size);
|
||||
util::Promise::ResolvePromise<int>(promise.GetPromise(),
|
||||
current_size);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
RunCallbackInUI<int>(callback, net::ERR_FAILED);
|
||||
}
|
||||
}
|
||||
|
||||
void DoCacheActionInIO(
|
||||
const scoped_refptr<net::URLRequestContextGetter>& context_getter,
|
||||
Session::CacheAction action,
|
||||
const net::CompletionCallback& callback) {
|
||||
atom::util::Promise promise) {
|
||||
auto* request_context = context_getter->GetURLRequestContext();
|
||||
|
||||
auto* http_cache = request_context->http_transaction_factory()->GetCache();
|
||||
if (!http_cache)
|
||||
RunCallbackInUI<int>(callback, net::ERR_FAILED);
|
||||
if (!http_cache) {
|
||||
std::string err_msg =
|
||||
"Failed to retrieve cache: " + net::ErrorToString(net::ERR_FAILED);
|
||||
util::Promise::RejectPromise(std::move(promise), std::move(err_msg));
|
||||
return;
|
||||
}
|
||||
|
||||
// Call GetBackend and make the backend's ptr accessable in OnGetBackend.
|
||||
using BackendPtr = disk_cache::Backend*;
|
||||
auto** backend_ptr = new BackendPtr(nullptr);
|
||||
net::CompletionCallback on_get_backend =
|
||||
base::Bind(&OnGetBackend, base::Owned(backend_ptr), action, callback);
|
||||
base::Bind(&OnGetBackend, base::Owned(backend_ptr), action,
|
||||
atom::util::CopyablePromise(promise));
|
||||
int rv = http_cache->GetBackend(backend_ptr, on_get_backend);
|
||||
if (rv != net::ERR_IO_PENDING)
|
||||
on_get_backend.Run(net::OK);
|
||||
|
@ -428,12 +448,18 @@ v8::Local<v8::Promise> Session::ResolveProxy(mate::Arguments* args) {
|
|||
}
|
||||
|
||||
template <Session::CacheAction action>
|
||||
void Session::DoCacheAction(const net::CompletionCallback& callback) {
|
||||
v8::Local<v8::Promise> Session::DoCacheAction() {
|
||||
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
||||
util::Promise promise(isolate);
|
||||
v8::Local<v8::Promise> handle = promise.GetHandle();
|
||||
|
||||
base::PostTaskWithTraits(
|
||||
FROM_HERE, {BrowserThread::IO},
|
||||
base::BindOnce(&DoCacheActionInIO,
|
||||
WrapRefCounted(browser_context_->GetRequestContext()),
|
||||
action, callback));
|
||||
action, std::move(promise)));
|
||||
|
||||
return handle;
|
||||
}
|
||||
|
||||
v8::Local<v8::Promise> Session::ClearStorageData(mate::Arguments* args) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue