add compatibility will old api

This commit is contained in:
Robo 2015-07-07 20:13:13 +05:30
parent da00329d78
commit 2cd5fb5694
5 changed files with 139 additions and 129 deletions

View file

@ -26,15 +26,11 @@ template<>
struct Converter<const net::URLRequest*> { struct Converter<const net::URLRequest*> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const net::URLRequest* val) { const net::URLRequest* val) {
if (val) {
return mate::ObjectTemplateBuilder(isolate) return mate::ObjectTemplateBuilder(isolate)
.SetValue("method", val->method()) .SetValue("method", val->method())
.SetValue("url", val->url().spec()) .SetValue("url", val->url().spec())
.SetValue("referrer", val->referrer()) .SetValue("referrer", val->referrer())
.Build()->NewInstance(); .Build()->NewInstance();
} else {
return v8::Null(isolate);
}
} }
}; };
@ -77,8 +73,7 @@ class CustomProtocolRequestJob : public AdapterRequestJob {
// Call the JS handler. // Call the JS handler.
Protocol::JsProtocolHandler callback = Protocol::JsProtocolHandler callback =
registry_->GetProtocolHandler(request()->url().scheme()); registry_->GetProtocolHandler(request()->url().scheme());
v8::Local<v8::Value> result = v8::Local<v8::Value> result = callback.Run(request());
callback.Run(v8::Null(registry_->isolate()), request());
// Determine the type of the job we are going to create. // Determine the type of the job we are going to create.
if (result->IsString()) { if (result->IsString()) {
@ -208,81 +203,88 @@ Protocol::JsProtocolHandler Protocol::GetProtocolHandler(
} }
void Protocol::OnRegisterProtocol(const std::string& scheme, void Protocol::OnRegisterProtocol(const std::string& scheme,
const JsProtocolHandler& callback, const JsProtocolHandler& handler,
bool is_handled) { const JsCompletionCallback& callback,
int is_handled) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
v8::Locker locker(isolate()); v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate()); v8::HandleScope handle_scope(isolate());
if (is_handled || ContainsKey(protocol_handlers_, scheme)) { if (is_handled || ContainsKey(protocol_handlers_, scheme)) {
callback.Run(v8::Exception::Error( callback.Run(v8::Exception::Error(
mate::StringToV8(isolate(), "The Scheme is already registered")), mate::StringToV8(isolate(), "The Scheme is already registered")));
nullptr);
return; return;
} }
protocol_handlers_[scheme] = callback; protocol_handlers_[scheme] = handler;
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, BrowserThread::PostTaskAndReply(BrowserThread::IO, FROM_HERE,
base::Bind(&Protocol::RegisterProtocolInIO, base::Bind(&Protocol::RegisterProtocolInIO,
base::Unretained(this), scheme)); base::Unretained(this), scheme),
base::Bind(callback, v8::Null(isolate())));
} }
void Protocol::OnInterceptProtocol(const std::string& scheme, void Protocol::OnInterceptProtocol(const std::string& scheme,
const JsProtocolHandler& callback, const JsProtocolHandler& handler,
bool is_handled) { const JsCompletionCallback& callback,
int is_handled) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
v8::Locker locker(isolate()); v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate()); v8::HandleScope handle_scope(isolate());
if (!is_handled) { if (!is_handled) {
callback.Run(v8::Exception::Error( callback.Run(v8::Exception::Error(
mate::StringToV8(isolate(), "Scheme does not exist.")), nullptr); mate::StringToV8(isolate(), "Scheme does not exist.")));
return; return;
} }
if (ContainsKey(protocol_handlers_, scheme)) { if (ContainsKey(protocol_handlers_, scheme)) {
callback.Run(v8::Exception::Error( callback.Run(v8::Exception::Error(
mate::StringToV8(isolate(), "Cannot intercept custom protocols.")), mate::StringToV8(isolate(), "Cannot intercept custom protocols.")));
nullptr);
return; return;
} }
protocol_handlers_[scheme] = callback; protocol_handlers_[scheme] = handler;
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, BrowserThread::PostTaskAndReply(BrowserThread::IO, FROM_HERE,
base::Bind(&Protocol::InterceptProtocolInIO, base::Bind(&Protocol::InterceptProtocolInIO,
base::Unretained(this), scheme)); base::Unretained(this), scheme),
base::Bind(callback, v8::Null(isolate())));
} }
mate::ObjectTemplateBuilder Protocol::GetObjectTemplateBuilder( mate::ObjectTemplateBuilder Protocol::GetObjectTemplateBuilder(
v8::Isolate* isolate) { v8::Isolate* isolate) {
return mate::ObjectTemplateBuilder(isolate) return mate::ObjectTemplateBuilder(isolate)
.SetMethod("registerProtocol", &Protocol::RegisterProtocol) .SetMethod("_registerProtocol", &Protocol::RegisterProtocol)
.SetMethod("unregisterProtocol", &Protocol::UnregisterProtocol) .SetMethod("_unregisterProtocol", &Protocol::UnregisterProtocol)
.SetMethod("registerStandardSchemes", &Protocol::RegisterStandardSchemes) .SetMethod("registerStandardSchemes", &Protocol::RegisterStandardSchemes)
.SetMethod("isHandledProtocol", &Protocol::IsHandledProtocol) .SetMethod("isHandledProtocol", &Protocol::IsHandledProtocol)
.SetMethod("interceptProtocol", &Protocol::InterceptProtocol) .SetMethod("_interceptProtocol", &Protocol::InterceptProtocol)
.SetMethod("uninterceptProtocol", &Protocol::UninterceptProtocol); .SetMethod("_uninterceptProtocol", &Protocol::UninterceptProtocol);
} }
void Protocol::RegisterProtocol(v8::Isolate* isolate, void Protocol::RegisterProtocol(v8::Isolate* isolate,
const std::string& scheme, const std::string& scheme,
const JsProtocolHandler& callback) { const JsProtocolHandler& handler,
const JsCompletionCallback& callback) {
IsHandledProtocol(scheme, IsHandledProtocol(scheme,
base::Bind(&Protocol::OnRegisterProtocol, base::Bind(&Protocol::OnRegisterProtocol,
base::Unretained(this), base::Unretained(this),
scheme, callback)); scheme, handler, callback));
} }
void Protocol::UnregisterProtocol(v8::Isolate* isolate, void Protocol::UnregisterProtocol(v8::Isolate* isolate,
const std::string& scheme) { const std::string& scheme,
const JsCompletionCallback& callback) {
ProtocolHandlersMap::iterator it(protocol_handlers_.find(scheme)); ProtocolHandlersMap::iterator it(protocol_handlers_.find(scheme));
if (it == protocol_handlers_.end()) if (it == protocol_handlers_.end()) {
return node::ThrowError(isolate, "The scheme has not been registered"); callback.Run(v8::Exception::Error(
mate::StringToV8(isolate, "The Scheme has not been registered")));
return;
}
protocol_handlers_.erase(it); protocol_handlers_.erase(it);
BrowserThread::PostTask(BrowserThread::IO, BrowserThread::PostTaskAndReply(BrowserThread::IO, FROM_HERE,
FROM_HERE,
base::Bind(&Protocol::UnregisterProtocolInIO, base::Bind(&Protocol::UnregisterProtocolInIO,
base::Unretained(this), scheme)); base::Unretained(this), scheme),
base::Bind(callback, v8::Null(isolate)));
} }
void Protocol::RegisterStandardSchemes( void Protocol::RegisterStandardSchemes(
@ -291,7 +293,7 @@ void Protocol::RegisterStandardSchemes(
} }
void Protocol::IsHandledProtocol(const std::string& scheme, void Protocol::IsHandledProtocol(const std::string& scheme,
const CompletionCallback& callback) { const net::CompletionCallback& callback) {
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::IO, FROM_HERE, BrowserThread::PostTaskAndReplyWithResult(BrowserThread::IO, FROM_HERE,
base::Bind(&AtomURLRequestJobFactory::IsHandledProtocol, base::Bind(&AtomURLRequestJobFactory::IsHandledProtocol,
base::Unretained(job_factory_), scheme), base::Unretained(job_factory_), scheme),
@ -300,68 +302,54 @@ void Protocol::IsHandledProtocol(const std::string& scheme,
void Protocol::InterceptProtocol(v8::Isolate* isolate, void Protocol::InterceptProtocol(v8::Isolate* isolate,
const std::string& scheme, const std::string& scheme,
const JsProtocolHandler& callback) { const JsProtocolHandler& handler,
const JsCompletionCallback& callback) {
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::IO, FROM_HERE, BrowserThread::PostTaskAndReplyWithResult(BrowserThread::IO, FROM_HERE,
base::Bind(&AtomURLRequestJobFactory::HasProtocolHandler, base::Bind(&AtomURLRequestJobFactory::HasProtocolHandler,
base::Unretained(job_factory_), scheme), base::Unretained(job_factory_), scheme),
base::Bind(&Protocol::OnInterceptProtocol, base::Bind(&Protocol::OnInterceptProtocol,
base::Unretained(this), scheme, callback)); base::Unretained(this), scheme, handler, callback));
} }
void Protocol::UninterceptProtocol(v8::Isolate* isolate, void Protocol::UninterceptProtocol(v8::Isolate* isolate,
const std::string& scheme) { const std::string& scheme,
const JsCompletionCallback& callback) {
ProtocolHandlersMap::iterator it(protocol_handlers_.find(scheme)); ProtocolHandlersMap::iterator it(protocol_handlers_.find(scheme));
if (it == protocol_handlers_.end()) if (it == protocol_handlers_.end()) {
return node::ThrowError(isolate, "The scheme has not been registered"); callback.Run(v8::Exception::Error(
mate::StringToV8(isolate, "The Scheme has not been registered")));
return;
}
protocol_handlers_.erase(it); protocol_handlers_.erase(it);
BrowserThread::PostTask(BrowserThread::IO, BrowserThread::PostTaskAndReply(BrowserThread::IO, FROM_HERE,
FROM_HERE,
base::Bind(&Protocol::UninterceptProtocolInIO, base::Bind(&Protocol::UninterceptProtocolInIO,
base::Unretained(this), scheme)); base::Unretained(this), scheme),
base::Bind(callback, v8::Null(isolate)));
} }
void Protocol::RegisterProtocolInIO(const std::string& scheme) { void Protocol::RegisterProtocolInIO(const std::string& scheme) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
job_factory_->SetProtocolHandler(scheme, new CustomProtocolHandler(this)); job_factory_->SetProtocolHandler(scheme, new CustomProtocolHandler(this));
BrowserThread::PostTask(BrowserThread::UI,
FROM_HERE,
base::Bind(&Protocol::EmitEventInUI,
base::Unretained(this),
"registered", scheme));
} }
void Protocol::UnregisterProtocolInIO(const std::string& scheme) { void Protocol::UnregisterProtocolInIO(const std::string& scheme) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
job_factory_->SetProtocolHandler(scheme, NULL); job_factory_->SetProtocolHandler(scheme, NULL);
BrowserThread::PostTask(BrowserThread::UI,
FROM_HERE,
base::Bind(&Protocol::EmitEventInUI,
base::Unretained(this),
"unregistered", scheme));
} }
void Protocol::InterceptProtocolInIO(const std::string& scheme) { void Protocol::InterceptProtocolInIO(const std::string& scheme) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
ProtocolHandler* original_handler = job_factory_->GetProtocolHandler(scheme); ProtocolHandler* original_handler = job_factory_->GetProtocolHandler(scheme);
if (original_handler == NULL) { if (original_handler == nullptr) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( NOTREACHED();
&Protocol::EmitEventInUI,
base::Unretained(this),
"error", "There is no protocol handler to intercpet"));
return;
} }
job_factory_->ReplaceProtocol( job_factory_->ReplaceProtocol(
scheme, new CustomProtocolHandler(this, original_handler)); scheme, new CustomProtocolHandler(this, original_handler));
BrowserThread::PostTask(BrowserThread::UI,
FROM_HERE,
base::Bind(&Protocol::EmitEventInUI,
base::Unretained(this),
"intercepted", scheme));
} }
void Protocol::UninterceptProtocolInIO(const std::string& scheme) { void Protocol::UninterceptProtocolInIO(const std::string& scheme) {
@ -369,28 +357,14 @@ void Protocol::UninterceptProtocolInIO(const std::string& scheme) {
CustomProtocolHandler* handler = static_cast<CustomProtocolHandler*>( CustomProtocolHandler* handler = static_cast<CustomProtocolHandler*>(
job_factory_->GetProtocolHandler(scheme)); job_factory_->GetProtocolHandler(scheme));
if (handler->original_handler() == NULL) { if (handler->original_handler() == nullptr) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( NOTREACHED();
&Protocol::EmitEventInUI,
base::Unretained(this),
"error", "The protocol is not intercpeted"));
return;
} }
// Reset the protocol handler to the orignal one and delete current protocol // Reset the protocol handler to the orignal one and delete current protocol
// handler. // handler.
ProtocolHandler* original_handler = handler->ReleaseDefaultProtocolHandler(); ProtocolHandler* original_handler = handler->ReleaseDefaultProtocolHandler();
delete job_factory_->ReplaceProtocol(scheme, original_handler); delete job_factory_->ReplaceProtocol(scheme, original_handler);
BrowserThread::PostTask(BrowserThread::UI,
FROM_HERE,
base::Bind(&Protocol::EmitEventInUI,
base::Unretained(this),
"unintercepted", scheme));
}
void Protocol::EmitEventInUI(const std::string& event,
const std::string& parameter) {
Emit(event, parameter);
} }
// static // static

View file

@ -12,6 +12,7 @@
#include "atom/browser/api/event_emitter.h" #include "atom/browser/api/event_emitter.h"
#include "base/callback.h" #include "base/callback.h"
#include "native_mate/handle.h" #include "native_mate/handle.h"
#include "net/base/completion_callback.h"
namespace net { namespace net {
class URLRequest; class URLRequest;
@ -27,9 +28,8 @@ namespace api {
class Protocol : public mate::EventEmitter { class Protocol : public mate::EventEmitter {
public: public:
using JsProtocolHandler = using JsProtocolHandler =
base::Callback<v8::Local<v8::Value>(v8::Local<v8::Value>, base::Callback<v8::Local<v8::Value>(const net::URLRequest*)>;
const net::URLRequest*)>; using JsCompletionCallback = base::Callback<void(v8::Local<v8::Value>)>;
using CompletionCallback = base::Callback<void(bool)>;
static mate::Handle<Protocol> Create( static mate::Handle<Protocol> Create(
v8::Isolate* isolate, AtomBrowserContext* browser_context); v8::Isolate* isolate, AtomBrowserContext* browser_context);
@ -50,12 +50,14 @@ class Protocol : public mate::EventEmitter {
// Callback called if protocol can be registered. // Callback called if protocol can be registered.
void OnRegisterProtocol(const std::string& scheme, void OnRegisterProtocol(const std::string& scheme,
const JsProtocolHandler& callback, const JsProtocolHandler& handler,
bool is_handled); const JsCompletionCallback& callback,
int is_handled);
// Callback called if protocol can be intercepted. // Callback called if protocol can be intercepted.
void OnInterceptProtocol(const std::string& scheme, void OnInterceptProtocol(const std::string& scheme,
const JsProtocolHandler& callback, const JsProtocolHandler& handler,
bool is_handled); const JsCompletionCallback& callback,
int is_handled);
// Register schemes to standard scheme list. // Register schemes to standard scheme list.
void RegisterStandardSchemes(const std::vector<std::string>& schemes); void RegisterStandardSchemes(const std::vector<std::string>& schemes);
@ -64,18 +66,22 @@ class Protocol : public mate::EventEmitter {
// |callback|. // |callback|.
void RegisterProtocol(v8::Isolate* isolate, void RegisterProtocol(v8::Isolate* isolate,
const std::string& scheme, const std::string& scheme,
const JsProtocolHandler& callback); const JsProtocolHandler& handler,
void UnregisterProtocol(v8::Isolate* isolate, const std::string& scheme); const JsCompletionCallback& callback);
void UnregisterProtocol(v8::Isolate* isolate, const std::string& scheme,
const JsCompletionCallback& callback);
// Returns whether a scheme has been registered. // Returns whether a scheme has been registered.
void IsHandledProtocol(const std::string& scheme, void IsHandledProtocol(const std::string& scheme,
const CompletionCallback& callback); const net::CompletionCallback& callback);
// Intercept/unintercept an existing protocol handler. // Intercept/unintercept an existing protocol handler.
void InterceptProtocol(v8::Isolate* isolate, void InterceptProtocol(v8::Isolate* isolate,
const std::string& scheme, const std::string& scheme,
const JsProtocolHandler& callback); const JsProtocolHandler& handler,
void UninterceptProtocol(v8::Isolate* isolate, const std::string& scheme); const JsCompletionCallback& callback);
void UninterceptProtocol(v8::Isolate* isolate, const std::string& scheme,
const JsCompletionCallback& callback);
// The networking related operations have to be done in IO thread. // The networking related operations have to be done in IO thread.
void RegisterProtocolInIO(const std::string& scheme); void RegisterProtocolInIO(const std::string& scheme);
@ -83,9 +89,6 @@ class Protocol : public mate::EventEmitter {
void InterceptProtocolInIO(const std::string& scheme); void InterceptProtocolInIO(const std::string& scheme);
void UninterceptProtocolInIO(const std::string& scheme); void UninterceptProtocolInIO(const std::string& scheme);
// Do protocol.emit(event, parameter) under UI thread.
void EmitEventInUI(const std::string& event, const std::string& parameter);
AtomBrowserContext* browser_context_; AtomBrowserContext* browser_context_;
AtomURLRequestJobFactory* job_factory_; AtomURLRequestJobFactory* job_factory_;
ProtocolHandlersMap protocol_handlers_; ProtocolHandlersMap protocol_handlers_;

View file

@ -6,6 +6,29 @@ EventEmitter = require('events').EventEmitter
protocol.__proto__ = EventEmitter.prototype protocol.__proto__ = EventEmitter.prototype
GetWrappedCallback = (scheme, callback, notification) ->
wrappedCallback = (error) ->
if not callback?
if error
throw error
else
protocol.emit notification, scheme
else
callback error, scheme
# Compatibility with old api.
protocol.registerProtocol = (scheme, handler, callback) ->
protocol._registerProtocol scheme, handler, GetWrappedCallback(scheme, callback, 'registered')
protocol.unregisterProtocol = (scheme, callback) ->
protocol._unregisterProtocol scheme, GetWrappedCallback(scheme, callback, 'unregistered')
protocol.interceptProtocol = (scheme, handler, callback) ->
protocol._interceptProtocol scheme, handler, GetWrappedCallback(scheme, callback, 'intercepted')
protocol.uninterceptProtocol = (scheme, callback) ->
protocol._uninterceptProtocol scheme, GetWrappedCallback(scheme, callback, 'unintercepted')
protocol.RequestStringJob = protocol.RequestStringJob =
class RequestStringJob class RequestStringJob
constructor: ({mimeType, charset, data}) -> constructor: ({mimeType, charset, data}) ->

View file

@ -15,6 +15,9 @@ app.on('ready', function() {
protocol.registerProtocol('atom', function(request) { protocol.registerProtocol('atom', function(request) {
var url = request.url.substr(7) var url = request.url.substr(7)
return new protocol.RequestFileJob(path.normalize(__dirname + '/' + url)); return new protocol.RequestFileJob(path.normalize(__dirname + '/' + url));
}, function (error, scheme) {
if (!error)
console.log(scheme, ' registered successfully')
}); });
}); });
``` ```
@ -22,20 +25,22 @@ app.on('ready', function() {
**Note:** This module can only be used after the `ready` event **Note:** This module can only be used after the `ready` event
was emitted. was emitted.
## protocol.registerProtocol(scheme, handler) ## protocol.registerProtocol(scheme, handler, callback)
* `scheme` String * `scheme` String
* `handler` Function * `handler` Function
* `callback` Function
Registers a custom protocol of `scheme`, the `handler` would be called with Registers a custom protocol of `scheme`, the `handler` would be called with
`handler(error, request)` when the a request with registered `scheme` is made. `handler(request)` when the a request with registered `scheme` is made.
You need to return a request job in the `handler` to specify which type of You need to return a request job in the `handler` to specify which type of
response you would like to send. response you would like to send.
## protocol.unregisterProtocol(scheme) ## protocol.unregisterProtocol(scheme, callback)
* `scheme` String * `scheme` String
* `callback` Function
Unregisters the custom protocol of `scheme`. Unregisters the custom protocol of `scheme`.
@ -52,17 +57,19 @@ Unregisters the custom protocol of `scheme`.
`callback` returns a boolean whether the `scheme` can be handled already. `callback` returns a boolean whether the `scheme` can be handled already.
## protocol.interceptProtocol(scheme, handler) ## protocol.interceptProtocol(scheme, handler, callback)
* `scheme` String * `scheme` String
* `handler` Function * `handler` Function
* `callback` Function
Intercepts an existing protocol with `scheme`, returning `null` or `undefined` Intercepts an existing protocol with `scheme`, returning `null` or `undefined`
in `handler` would use the original protocol handler to handle the request. in `handler` would use the original protocol handler to handle the request.
## protocol.uninterceptProtocol(scheme) ## protocol.uninterceptProtocol(scheme, callback)
* `scheme` String * `scheme` String
* `callback` Function
Unintercepts a protocol. Unintercepts a protocol.

View file

@ -9,17 +9,18 @@ describe 'protocol module', ->
describe 'protocol.registerProtocol', -> describe 'protocol.registerProtocol', ->
it 'error when scheme is already registered', (done) -> it 'error when scheme is already registered', (done) ->
register = -> register = ->
protocol.registerProtocol 'test1', (error, request) -> protocol.registerProtocol 'test1', ((request) ->), (error, scheme) ->
assert error instanceof Error if error?
protocol.unregisterProtocol 'test1' protocol.unregisterProtocol 'test1', (error, scheme) ->
assert.equal scheme, 'test1'
done() done()
protocol.once 'registered', (event, scheme) -> else
assert.equal scheme, 'test1' assert.equal scheme, 'test1'
register() register()
register() register()
it 'calls the callback when scheme is visited', (done) -> it 'calls the callback when scheme is visited', (done) ->
protocol.registerProtocol 'test2', (error, request) -> protocol.registerProtocol 'test2', (request) ->
assert.equal request.url, 'test2://test2' assert.equal request.url, 'test2://test2'
protocol.unregisterProtocol 'test2' protocol.unregisterProtocol 'test2'
done() done()
@ -28,7 +29,7 @@ describe 'protocol module', ->
describe 'protocol.unregisterProtocol', -> describe 'protocol.unregisterProtocol', ->
it 'throws error when scheme does not exist', -> it 'throws error when scheme does not exist', ->
unregister = -> protocol.unregisterProtocol 'test3' unregister = -> protocol.unregisterProtocol 'test3'
assert.throws unregister, /The scheme has not been registered/ assert.throws unregister, /The Scheme has not been registered/
describe 'registered protocol callback', -> describe 'registered protocol callback', ->
it 'returns string should send the string as request content', (done) -> it 'returns string should send the string as request content', (done) ->
@ -190,37 +191,39 @@ describe 'protocol module', ->
describe 'protocol.interceptProtocol', -> describe 'protocol.interceptProtocol', ->
it 'throws error when scheme is not a registered one', (done) -> it 'throws error when scheme is not a registered one', (done) ->
protocol.interceptProtocol 'test-intercept', (error, request) -> protocol.interceptProtocol 'test-intercept', ( ->), (error, scheme) ->
assert error instanceof Error if error?
assert.equal scheme, 'test-intercept'
done() done()
it 'throws error when scheme is a custom protocol', (done) -> it 'throws error when scheme is a custom protocol', (done) ->
protocol.once 'unregistered', (event, scheme) -> protocol.once 'unregistered', (scheme) ->
assert.equal scheme, 'atom' assert.equal scheme, 'atom'
done() done()
protocol.once 'registered', (event, scheme) -> protocol.once 'registered', (scheme) ->
assert.equal scheme, 'atom' assert.equal scheme, 'atom'
protocol.interceptProtocol 'test-intercept', (error, request) -> protocol.interceptProtocol 'test-intercept', (->), (error, newScheme) ->
assert error instanceof Error if error?
assert.equal newScheme, 'test-intercept'
protocol.unregisterProtocol scheme protocol.unregisterProtocol scheme
protocol.registerProtocol('atom', ->) protocol.registerProtocol('atom', ->)
it 'returns original job when callback returns nothing', (done) -> it 'returns original job when callback returns nothing', (done) ->
targetScheme = 'file' targetScheme = 'file'
protocol.once 'intercepted', (event, scheme) -> protocol.once 'intercepted', (scheme) ->
assert.equal scheme, targetScheme assert.equal scheme, targetScheme
free = -> protocol.uninterceptProtocol targetScheme free = -> protocol.uninterceptProtocol targetScheme
$.ajax $.ajax
url: "#{targetScheme}://#{__filename}", url: "#{targetScheme}://#{__filename}",
success: -> success: ->
protocol.once 'unintercepted', (event, scheme) -> protocol.once 'unintercepted', (scheme) ->
assert.equal scheme, targetScheme assert.equal scheme, targetScheme
done() done()
free() free()
error: (xhr, errorType, error) -> error: (xhr, errorType, error) ->
free() free()
assert false, 'Got error: ' + errorType + ' ' + error assert false, 'Got error: ' + errorType + ' ' + error
protocol.interceptProtocol targetScheme, (error, request) -> protocol.interceptProtocol targetScheme, (request) ->
if process.platform is 'win32' if process.platform is 'win32'
pathInUrl = path.normalize request.url.substr(8) pathInUrl = path.normalize request.url.substr(8)
assert.equal pathInUrl.toLowerCase(), __filename.toLowerCase() assert.equal pathInUrl.toLowerCase(), __filename.toLowerCase()