X Tutup
Skip to content

Commit 1f2b02c

Browse files
authored
feat: promisify protocol.isProtocolHandled() (electron#16423)
* feat: promisify protocol * fix base::Bind and specs * update documentation * make callback-compatible * async awaitify tests
1 parent 32d9885 commit 1f2b02c

File tree

7 files changed

+62
-63
lines changed

7 files changed

+62
-63
lines changed

atom/browser/api/atom_api_protocol.cc

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,22 +103,29 @@ Protocol::ProtocolError Protocol::UnregisterProtocolInIO(
103103
return PROTOCOL_OK;
104104
}
105105

106-
void Protocol::IsProtocolHandled(const std::string& scheme,
107-
const BooleanCallback& callback) {
106+
bool IsProtocolHandledInIO(
107+
scoped_refptr<URLRequestContextGetter> request_context_getter,
108+
const std::string& scheme) {
109+
bool is_handled =
110+
request_context_getter->job_factory()->IsHandledProtocol(scheme);
111+
return is_handled;
112+
}
113+
114+
void PromiseCallback(scoped_refptr<util::Promise> promise, bool handled) {
115+
promise->Resolve(handled);
116+
}
117+
118+
v8::Local<v8::Promise> Protocol::IsProtocolHandled(const std::string& scheme) {
119+
scoped_refptr<util::Promise> promise = new util::Promise(isolate());
108120
auto* getter = static_cast<URLRequestContextGetter*>(
109121
browser_context_->GetRequestContext());
122+
110123
base::PostTaskWithTraitsAndReplyWithResult(
111124
FROM_HERE, {content::BrowserThread::IO},
112-
base::Bind(&Protocol::IsProtocolHandledInIO, base::RetainedRef(getter),
113-
scheme),
114-
callback);
115-
}
125+
base::Bind(&IsProtocolHandledInIO, base::RetainedRef(getter), scheme),
126+
base::Bind(&PromiseCallback, promise));
116127

117-
// static
118-
bool Protocol::IsProtocolHandledInIO(
119-
scoped_refptr<URLRequestContextGetter> request_context_getter,
120-
const std::string& scheme) {
121-
return request_context_getter->job_factory()->IsHandledProtocol(scheme);
128+
return promise->GetHandle();
122129
}
123130

124131
void Protocol::UninterceptProtocol(const std::string& scheme,

atom/browser/api/atom_api_protocol.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "atom/browser/api/trackable_object.h"
1515
#include "atom/browser/atom_browser_context.h"
1616
#include "atom/browser/net/atom_url_request_job_factory.h"
17+
#include "atom/common/promise_util.h"
1718
#include "base/callback.h"
1819
#include "base/memory/weak_ptr.h"
1920
#include "base/task/post_task.h"
@@ -41,7 +42,6 @@ class Protocol : public mate::TrackableObject<Protocol> {
4142
using Handler =
4243
base::Callback<void(const base::DictionaryValue&, v8::Local<v8::Value>)>;
4344
using CompletionCallback = base::Callback<void(v8::Local<v8::Value>)>;
44-
using BooleanCallback = base::Callback<void(bool)>;
4545

4646
static mate::Handle<Protocol> Create(v8::Isolate* isolate,
4747
AtomBrowserContext* browser_context);
@@ -136,11 +136,7 @@ class Protocol : public mate::TrackableObject<Protocol> {
136136
const std::string& scheme);
137137

138138
// Whether the protocol has handler registered.
139-
void IsProtocolHandled(const std::string& scheme,
140-
const BooleanCallback& callback);
141-
static bool IsProtocolHandledInIO(
142-
scoped_refptr<URLRequestContextGetter> request_context_getter,
143-
const std::string& scheme);
139+
v8::Local<v8::Promise> IsProtocolHandled(const std::string& scheme);
144140

145141
// Replace the protocol handler with a new one.
146142
template <typename RequestJob>

docs/api/promisification.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ When a majority of affected functions are migrated, this flag will be enabled by
3131
- [ ] [inAppPurchase.purchaseProduct(productID, quantity, callback)](https://github.com/electron/electron/blob/master/docs/api/in-app-purchase.md#purchaseProduct)
3232
- [ ] [inAppPurchase.getProducts(productIDs, callback)](https://github.com/electron/electron/blob/master/docs/api/in-app-purchase.md#getProducts)
3333
- [ ] [netLog.stopLogging([callback])](https://github.com/electron/electron/blob/master/docs/api/net-log.md#stopLogging)
34-
- [ ] [protocol.isProtocolHandled(scheme, callback)](https://github.com/electron/electron/blob/master/docs/api/protocol.md#isProtocolHandled)
3534
- [ ] [ses.getCacheSize(callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#getCacheSize)
3635
- [ ] [ses.clearCache(callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#clearCache)
3736
- [ ] [ses.clearStorageData([options, callback])](https://github.com/electron/electron/blob/master/docs/api/session.md#clearStorageData)
@@ -61,4 +60,5 @@ When a majority of affected functions are migrated, this flag will be enabled by
6160
- [ ] [webviewTag.capturePage([rect, ]callback)](https://github.com/electron/electron/blob/master/docs/api/webview-tag.md#capturePage)
6261
- [ ] [contents.capturePage([rect, ]callback)](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#capturePage)
6362
- [ ] [app.getFileIcon(path[, options], callback)](https://github.com/electron/electron/blob/master/docs/api/app.md#getFileIcon)
64-
- [ ] [shell.openExternal(url[, options, callback])](https://github.com/electron/electron/blob/master/docs/api/shell.md#openExternal)
63+
- [ ] [shell.openExternal(url[, options, callback])](https://github.com/electron/electron/blob/master/docs/api/shell.md#openExternal)
64+
- [ ] [protocol.isProtocolHandled(scheme, callback)](https://github.com/electron/electron/blob/master/docs/api/protocol.md#isProtocolHandled)

docs/api/protocol.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,15 @@ Unregisters the custom protocol of `scheme`.
275275
The `callback` will be called with a boolean that indicates whether there is
276276
already a handler for `scheme`.
277277

278+
**[Deprecated Soon](promisification.md)**
279+
280+
### `protocol.isProtocolHandled(scheme)`
281+
282+
* `scheme` String
283+
284+
Returns `Promise<Boolean>` - fulfilled with a boolean that indicates whether there is
285+
already a handler for `scheme`.
286+
278287
### `protocol.interceptFileProtocol(scheme, handler[, completion])`
279288

280289
* `scheme` String

lib/browser/api/session.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const { EventEmitter } = require('events')
4-
const { app } = require('electron')
4+
const { app, deprecate } = require('electron')
55
const { fromPartition, Session, Cookies } = process.atomBinding('session')
66

77
// Public API.
@@ -20,5 +20,6 @@ Object.setPrototypeOf(Session.prototype, EventEmitter.prototype)
2020
Object.setPrototypeOf(Cookies.prototype, EventEmitter.prototype)
2121

2222
Session.prototype._init = function () {
23+
this.protocol.isProtocolHandled = deprecate.promisify(this.protocol.isProtocolHandled, 1)
2324
app.emit('session-created', this)
2425
}

spec/api-protocol-spec.js

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -657,60 +657,48 @@ describe('protocol module', () => {
657657
})
658658

659659
describe('protocol.isProtocolHandled', () => {
660-
it('returns true for about:', (done) => {
661-
protocol.isProtocolHandled('about', (result) => {
662-
assert.strictEqual(result, true)
663-
done()
664-
})
660+
it('returns true for about:', async () => {
661+
const result = await protocol.isProtocolHandled('about')
662+
assert.strictEqual(result, true)
665663
})
666664

667-
it('returns true for file:', (done) => {
668-
protocol.isProtocolHandled('file', (result) => {
669-
assert.strictEqual(result, true)
670-
done()
671-
})
665+
it('returns true for file:', async () => {
666+
const result = await protocol.isProtocolHandled('file')
667+
assert.strictEqual(result, true)
672668
})
673669

674-
it('returns true for http:', (done) => {
675-
protocol.isProtocolHandled('http', (result) => {
676-
assert.strictEqual(result, true)
677-
done()
678-
})
670+
it('returns true for http:', async () => {
671+
const result = await protocol.isProtocolHandled('http')
672+
assert.strictEqual(result, true)
679673
})
680674

681-
it('returns true for https:', (done) => {
682-
protocol.isProtocolHandled('https', (result) => {
683-
assert.strictEqual(result, true)
684-
done()
685-
})
675+
it('returns true for https:', async () => {
676+
const result = await protocol.isProtocolHandled('https')
677+
assert.strictEqual(result, true)
686678
})
687679

688-
it('returns false when scheme is not registered', (done) => {
689-
protocol.isProtocolHandled('no-exist', (result) => {
690-
assert.strictEqual(result, false)
691-
done()
692-
})
680+
it('returns false when scheme is not registered', async () => {
681+
const result = await protocol.isProtocolHandled('no-exist')
682+
assert.strictEqual(result, false)
693683
})
694684

695685
it('returns true for custom protocol', (done) => {
696686
const emptyHandler = (request, callback) => callback()
697-
protocol.registerStringProtocol(protocolName, emptyHandler, (error) => {
687+
protocol.registerStringProtocol(protocolName, emptyHandler, async (error) => {
698688
assert.strictEqual(error, null)
699-
protocol.isProtocolHandled(protocolName, (result) => {
700-
assert.strictEqual(result, true)
701-
done()
702-
})
689+
const result = await protocol.isProtocolHandled(protocolName)
690+
assert.strictEqual(result, true)
691+
done()
703692
})
704693
})
705694

706695
it('returns true for intercepted protocol', (done) => {
707696
const emptyHandler = (request, callback) => callback()
708-
protocol.interceptStringProtocol('http', emptyHandler, (error) => {
697+
protocol.interceptStringProtocol('http', emptyHandler, async (error) => {
709698
assert.strictEqual(error, null)
710-
protocol.isProtocolHandled('http', (result) => {
711-
assert.strictEqual(result, true)
712-
done()
713-
})
699+
const result = await protocol.isProtocolHandled('http')
700+
assert.strictEqual(result, true)
701+
done()
714702
})
715703
})
716704
})

spec/api-session-spec.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -524,14 +524,12 @@ describe('session module', () => {
524524
partitionProtocol.unregisterProtocol(protocolName, () => done())
525525
})
526526

527-
it('does not affect defaultSession', (done) => {
528-
protocol.isProtocolHandled(protocolName, (result) => {
529-
assert.strictEqual(result, false)
530-
partitionProtocol.isProtocolHandled(protocolName, (result) => {
531-
assert.strictEqual(result, true)
532-
done()
533-
})
534-
})
527+
it('does not affect defaultSession', async () => {
528+
const result1 = await protocol.isProtocolHandled(protocolName)
529+
assert.strictEqual(result1, false)
530+
531+
const result2 = await partitionProtocol.isProtocolHandled(protocolName)
532+
assert.strictEqual(result2, true)
535533
})
536534

537535
it('handles requests from partition', (done) => {

0 commit comments

Comments
 (0)
X Tutup