8000 feat: promisify session.getBlobData() by codebytere · Pull Request #17303 · electron/electron · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: promisify session.getBlobData() #17303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to 8000
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions atom/browser/api/atom_api_session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ const char kPersistPrefix[] = "persist:";
// Referenced session objects.
std::map<uint32_t, v8::Global<v8::Object>> g_sessions;

void ResolveOrRejectPromiseInUI(atom::util::Promise promise, int net_error) {
void ResolveOrRejectPromiseInUI(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));
Expand All @@ -221,7 +221,7 @@ void ResolveOrRejectPromiseInUI(atom::util::Promise promise, int net_error) {
// Callback of HttpCache::GetBackend.
void OnGetBackend(disk_cache::Backend** backend_ptr,
Session::CacheAction action,
const atom::util::CopyablePromise& promise,
const util::CopyablePromise& promise,
int result) {
if (result != net::OK) {
std::string err_msg =
Expand Down Expand Up @@ -254,7 +254,7 @@ void OnGetBackend(disk_cache::Backend** backend_ptr,
void DoCacheActionInIO(
const scoped_refptr<net::URLRequestContextGetter>& context_getter,
Session::CacheAction action,
atom::util::Promise promise) {
util::Promise promise) {
auto* request_context = context_getter->GetURLRequestContext();

auto* http_cache = request_context->http_transaction_factory()->GetCache();
Expand All @@ -270,7 +270,7 @@ void DoCacheActionInIO(
auto** backend_ptr = new BackendPtr(nullptr);
net::CompletionCallback on_get_backend =
base::Bind(&OnGetBackend, base::Owned(backend_ptr), action,
atom::util::CopyablePromise(promise));
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);
Expand Down Expand Up @@ -431,7 +431,7 @@ v8::Local<v8::Promise> Session::ResolveProxy(mate::Arguments* args) {
browser_context_->GetResolveProxyHelper()->ResolveProxy(
url,
base::Bind(util::CopyablePromise::ResolveCopyablePromise<std::string>,
atom::util::CopyablePromise(promise)));
util::CopyablePromise(promise)));

return handle;
}
Expand Down Expand Up @@ -659,16 +659,17 @@ std::string Session::GetUserAgent() {
return browser_context_->GetUserAgent();
}

void Session::GetBlobData(const std::string& uuid,
const AtomBlobReader::CompletionCallback& callback) {
if (callback.is_null())
return;
v8::Local<v8::Promise> Session::GetBlobData(v8::Isolate* isolate,
const std::string& uuid) {
util::Promise promise(isolate);
v8::Local<v8::Promise> handle = promise.GetHandle();

AtomBlobReader* blob_reader = browser_context()->GetBlobReader();
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::IO},
base::BindOnce(&AtomBlobReader::StartReading,
base::Unretained(blob_reader), uuid, callback));
base::Unretained(blob_reader), uuid, std::move(promise)));
return handle;
}

void Session::CreateInterruptedDownload(const mate::Dictionary& options) {
Expand Down
4 changes: 2 additions & 2 deletions atom/browser/api/atom_api_session.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ class Session : public mate::TrackableObject<Session>,
void AllowNTLMCredentialsForDomains(const std::string& domains);
void SetUserAgent(const std::string& user_agent, mate::Arguments* args);
std::string GetUserAgent();
void GetBlobData(const std::string& uuid,
const AtomBlobReader::CompletionCallback& callback);
v8::Local<v8::Promise> GetBlobData(v8::Isolate* isolate,
const std::string& uuid);
void CreateInterruptedDownload(const mate::Dictionary& options);
void SetPreloads(const std::vector<base::FilePath::StringType>& preloads);
std::vector<base::FilePath::StringType> GetPreloads() const;
Expand Down
33 changes: 16 additions & 17 deletions atom/browser/atom_blob_reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,20 @@ void FreeNodeBufferData(char* data, void* hint) {
delete[] data;
}

void RunCallbackInUI(const AtomBlobReader::CompletionCallback& callback,
char* blob_data,
int size) {
void RunPromiseInUI(util::Promise promise, char* blob_data, int size) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
v8::Isolate* isolate = promise.isolate();

v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate);
if (blob_data) {
v8::Local<v8::Value> buffer =
node::Buffer::New(isolate, blob_data, static_cast<size_t>(size),
&FreeNodeBufferData, nullptr)
.ToLocalChecked();
callback.Run(buffer);
promise.Resolve(buffer);
} else {
callback.Run(v8::Null(isolate));
promise.RejectWithErrorMessage("Could not get blob data");
}
}

Expand All @@ -53,29 +51,29 @@ AtomBlobReader::AtomBlobReader(content::ChromeBlobStorageContext* blob_context)

AtomBlobReader::~AtomBlobReader() {}

void AtomBlobReader::StartReading(
const std::string& uuid,
const AtomBlobReader::CompletionCallback& completion_callback) {
void AtomBlobReader::StartReading(const std::string& uuid,
util::Promise promise) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);

auto blob_data_handle = blob_context_->context()->GetBlobDataFromUUID(uuid);
auto callback = base::Bind(&RunCallbackInUI, completion_callback);
if (!blob_data_handle) {
base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI},
base::BindOnce(callback, nullptr, 0));
util::Promise::RejectPromise(std::move(promise),
"Could not get blob data handle");
return;
}

auto blob_reader = blob_data_handle->CreateReader();
BlobReadHelper* blob_read_helper =
new BlobReadHelper(std::move(blob_reader), callback);
new BlobReadHelper(std::move(blob_reader),
base::BindOnce(&RunPromiseInUI, std::move(promise)));
blob_read_helper->Read();
}

AtomBlobReader::BlobReadHelper::BlobReadHelper(
std::unique_ptr<storage::BlobReader> blob_reader,
const BlobReadHelper::CompletionCallback& callback)
: blob_reader_(std::move(blob_reader)), completion_callback_(callback) {}
BlobReadHelper::CompletionCallback callback)
: blob_reader_(std::move(blob_reader)),
completion_callback_(std::move(callback)) {}

AtomBlobReader::BlobReadHelper::~BlobReadHelper() {}

Expand Down Expand Up @@ -117,8 +115,9 @@ void AtomBlobReader::BlobReadHelper::DidReadBlobData(

char* data = new char[size];
memcpy(data, blob_data->data(), size);
base::PostTaskWithTraits(FROM_HERE, {BrowserThread::UI},
base::BindOnce(completion_callback_, data, size));
base::PostTaskWithTraits(
FROM_HERE, {BrowserThread::UI},
base::BindOnce(std::move(completion_callback_), data, size));
delete this;
}

Expand Down
10 changes: 4 additions & 6 deletions atom/browser/atom_blob_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <memory>
#include <string>

#include "atom/common/promise_util.h"
#include "base/callback.h"

namespace content {
Expand Down Expand Up @@ -35,23 +36,20 @@ namespace atom {
// except Ctor are expected to be called on IO thread.
class AtomBlobReader {
public:
using CompletionCallback = base::Callback<void(v8::Local<v8::Value>)>;

explicit AtomBlobReader(content::ChromeBlobStorageContext* blob_context);
~AtomBlobReader();

void StartReading(const std::string& uuid,
const AtomBlobReader::CompletionCallback& callback);
void StartReading(const std::string& uuid, atom::util::Promise promise);

private:
// A self-destroyed helper class to read the blob data.
// Must be accessed on IO thread.
class BlobReadHelper {
public:
using CompletionCallback = base::Callback<void(char*, int)>;
using CompletionCallback = base::OnceCallback<void(char*, int)>;

BlobReadHelper(std::unique_ptr<storage::BlobReader> blob_reader,
const BlobReadHelper::CompletionCallback& callback);
BlobReadHelper::CompletionCallback callback);
~BlobReadHelper();

void Read();
Expand Down
2 changes: 1 addition & 1 deletion docs/api/promisification.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ When a majority of affected functions are migrated, this flag will be enabled by
- [app.importCertificate(options, callback)](https://github.com/electron/electron/blob/master/docs/api/app.md#importCertificate)
- [dialog.showMessageBox([browserWindow, ]options[, callback])](https://github.com/electron/electron/blob/master/docs/api/dialog.md#showMessageBox)
- [dialog.showCertificateTrustDialog([browserWindow, ]options, callback)](https://github.com/electron/electron/blob/master/docs/api/dialog.md#showCertificateTrustDialog)
- [ses.getBlobData(identifier, callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#getBlobData)
- [contents.executeJavaScript(code[, userGesture, callback])](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#executeJavaScript)
- [contents.print([options], [callback])](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#print)
- [webFrame.executeJavaScript(code[, userGesture, callback])](https://github.com/electron/electron/blob/master/docs/api/web-frame.md#executeJavaScript)
Expand Down Expand Up @@ -47,6 +46,7 @@ When a majority of affected functions are migrated, this flag will be enabled by
- [ses.getCacheSize(callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#getCacheSize)
- [ses.clearAuthCache(options[, callback])](https://github.com/electron/electron/blob/master/docs/api/session.md#clearAuthCache)
- [ses.clearCache(callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#clearCache)
- [ses.getBlobData(identifier, callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#getBlobData)
- [shell.openExternal(url[, options, callback])](https://github.com/electron/electron/blob/master/docs/api/shell.md#openExternal)
- [webviewTag.capturePage([rect, ]callback)](https://github.com/electron/electron/blob/master/docs/api/webview-tag.md#capturePage)
- [webviewTag.printToPDF(options, callback)](https://github.com/electron/electron/blob/master/docs/api/webview-tag.md#printToPDF)
Expand Down
8 changes: 8 additions & 0 deletions docs/api/session.md
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,14 @@ Returns `String` - The user agent for this session.
* `callback` Function
* `result` Buffer - Blob data.

**[Deprecated Soon](promisification.md)**

#### `ses.getBlobData(identifier)`

* `identifier` String - Valid UUID.

Returns `Promise<Buffer>` - resolves with blob data.

#### `ses.createInterruptedDownload(options)`

* `options` Object
Expand Down
1 change: 1 addition & 0 deletions lib/browser/api/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Session.prototype.setProxy = deprecate.promisify(Session.prototype.setProxy)
Session.prototype.getCacheSize = deprecate.promisify(Session.prototype.getCacheSize)
Session.prototype.clearCache = deprecate.promisify(Session.prototype.clearCache)
Session.prototype.clearAuthCache = deprecate.promisify(Session.prototype.clearAuthCache)
Session.prototype.getBlobData = deprecate.promisifyMultiArg(Session.prototype.getBlobData)

Cookies.prototype.flushStore = deprecate.promisify(Cookies.prototype.flushStore)
Cookies.prototype.get = deprecate.promisify(Cookies.prototype.get)
Expand Down
4 changes: 2 additions & 2 deletions spec/api-session-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ describe('session module', () => {
})
})

describe('ses.getBlobData(identifier, callback)', () => {
describe('ses.getBlobData()', () => {
it('returns blob data for uuid', (done) => {
const scheme = 'cors-blob'
const protocol = session.defaultSession.protocol
Expand Down Expand Up @@ -811,7 +811,7 @@ describe('session module', () => {
} else if (request.method === 'POST') {
const uuid = request.uploadData[1].blobUUID
assert(uuid)
session.defaultSession.getBlobData(uuid, (result) => {
session.defaultSession.getBlobData(uuid).then(result => {
assert.strictEqual(result.toString(), postData)
done()
})
Expand Down
0