Skip to content

Commit

Permalink
feat: promisify session.getBlobData()
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed Mar 11, 2019
1 parent a958eb9 commit 8f6f474
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 34 deletions.
11 changes: 6 additions & 5 deletions atom/browser/api/atom_api_session.cc
Expand Up @@ -660,16 +660,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(const std::string& uuid) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
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
3 changes: 1 addition & 2 deletions atom/browser/api/atom_api_session.h
Expand Up @@ -82,8 +82,7 @@ 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(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
27 changes: 13 additions & 14 deletions atom/browser/atom_blob_reader.cc
Expand Up @@ -28,22 +28,22 @@ void FreeNodeBufferData(char* data, void* hint) {
delete[] data;
}

void RunCallbackInUI(const AtomBlobReader::CompletionCallback& callback,
char* blob_data,
int size) {
void RunPromiseInUI(const atom::util::CopyablePromise& promise,
char* blob_data,
int size) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
v8::Isolate* isolate = promise.GetPromise().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.GetPromise().Resolve(buffer);
} else {
callback.Run(v8::Null(isolate));
promise.GetPromise().RejectWithErrorMessage("Could not get blob data");
}
}

Expand All @@ -54,22 +54,21 @@ 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,
atom::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));
atom::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);
BlobReadHelper* blob_read_helper = new BlobReadHelper(
std::move(blob_reader),
base::Bind(&RunPromiseInUI, atom::util::CopyablePromise(promise)));
blob_read_helper->Read();
}

Expand Down
6 changes: 2 additions & 4 deletions atom/browser/atom_blob_reader.h
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,13 +36,10 @@ 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.
Expand Down
2 changes: 1 addition & 1 deletion docs/api/promisification.md
Expand Up @@ -13,7 +13,6 @@ When a majority of affected functions are migrated, this flag will be enabled by
- [dialog.showCertificateTrustDialog([browserWindow, ]options, callback)](https://github.com/electron/electron/blob/master/docs/api/dialog.md#showCertificateTrustDialog)
- [inAppPurchase.purchaseProduct(productID, quantity, callback)](https://github.com/electron/electron/blob/master/docs/api/in-app-purchase.md#purchaseProduct)
- [inAppPurchase.getProducts(productIDs, callback)](https://github.com/electron/electron/blob/master/docs/api/in-app-purchase.md#getProducts)
- [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
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
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
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) => {
const result = session.defaultSession.getBlobData(uuid).then(result => {
assert.strictEqual(result.toString(), postData)
done()
})
Expand Down
22 changes: 16 additions & 6 deletions spec/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 8f6f474

Please sign in to comment.