Skip to content

Commit

Permalink
feat: promisify some session methods
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Mar 1, 2019
1 parent 6d55498 commit 1b9066b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 12 deletions.
50 changes: 39 additions & 11 deletions atom/browser/api/atom_api_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -220,48 +220,71 @@ void RunCallbackInUI(const base::Callback<void(T...)>& callback, T... result) {
base::BindOnce(callback, result...));
}

void RejectPromise(atom::util::Promise promise, int error) {
std::string errmsg =
"Failed to size failed with error number: " + std::to_string(error);
promise.RejectWithErrorMessage(errmsg);
}

void RejectPromiseInUI(atom::util::Promise promise, int error) {
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::UI},
base::BindOnce(RejectPromise, std::move(promise), error));
}

void ResolvePromise(atom::util::Promise promise, int cache_size) {
promise.Resolve(cache_size);
}

void ResolvePromiseInUI(atom::util::Promise promise, int cache_size) {
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::UI},
base::BindOnce(ResolvePromise, std::move(promise), cache_size));
}

// Callback of HttpCache::GetBackend.
void OnGetBackend(disk_cache::Backend** backend_ptr,
Session::CacheAction action,
const net::CompletionCallback& callback,
atom::util::Promise promise,
int result) {
if (result != net::OK) {
RunCallbackInUI(callback, result);
RejectPromiseInUI(std::move(promise), result);
} else if (backend_ptr && *backend_ptr) {
if (action == Session::CacheAction::CLEAR) {
(*backend_ptr)
->DoomAllEntries(base::Bind(&RunCallbackInUI<int>, callback));
->DoomAllEntries(base::Bind(&ResolvePromiseInUI, std::move(promise)));
} else if (action == Session::CacheAction::STATS) {
base::StringPairs stats;
(*backend_ptr)->GetStats(&stats);
for (const auto& stat : stats) {
if (stat.first == "Current size") {
int current_size;
base::StringToInt(stat.second, &current_size);
RunCallbackInUI(callback, current_size);
ResolvePromiseInUI(std::move(promise), current_size);
break;
}
}
}
} else {
RunCallbackInUI<int>(callback, net::ERR_FAILED);
RejectPromiseInUI(std::move(promise), 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);
RejectPromiseInUI(std::move(promise), net::ERR_FAILED);

// 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);
net::CompletionCallback on_get_backend = base::Bind(
&OnGetBackend, base::Owned(backend_ptr), action, std::move(promise));
int rv = http_cache->GetBackend(backend_ptr, on_get_backend);
if (rv != net::ERR_IO_PENDING)
on_get_backend.Run(net::OK);
Expand Down Expand Up @@ -421,12 +444,17 @@ void Session::ResolveProxy(
}

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);

base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(&DoCacheActionInIO,
WrapRefCounted(browser_context_->GetRequestContext()),
action, callback));
action, std::move(promise)));

return promise.GetHandle();
}

void Session::ClearStorageData(mate::Arguments* args) {
Expand Down
3 changes: 2 additions & 1 deletion atom/browser/api/atom_api_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "atom/browser/api/trackable_object.h"
#include "atom/browser/atom_blob_reader.h"
#include "atom/browser/net/resolve_proxy_helper.h"
#include "atom/common/promise_util.h"
#include "base/values.h"
#include "content/public/browser/download_manager.h"
#include "native_mate/handle.h"
Expand Down Expand Up @@ -65,7 +66,7 @@ class Session : public mate::TrackableObject<Session>,
void ResolveProxy(const GURL& url,
const ResolveProxyHelper::ResolveProxyCallback& callback);
template <CacheAction action>
void DoCacheAction(const net::CompletionCallback& callback);
v8::Local<v8::Promise> DoCacheAction();
void ClearStorageData(mate::Arguments* args);
void FlushStorageData();
void SetProxy(const mate::Dictionary& options, const base::Closure& callback);
Expand Down

0 comments on commit 1b9066b

Please sign in to comment.