template<>
struct Converter<const net::URLRequest*> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
- const net::URLRequest* val) {
- if (val) {
- return mate::ObjectTemplateBuilder(isolate)
- .SetValue("method", val->method())
- .SetValue("url", val->url().spec())
- .SetValue("referrer", val->referrer())
- .Build()->NewInstance();
- } else {
- return v8::Null(isolate);
- }
+ const net::URLRequest* val) {
+ return mate::ObjectTemplateBuilder(isolate)
+ .SetValue("method", val->method())
+ .SetValue("url", val->url().spec())
+ .SetValue("referrer", val->referrer())
+ .Build()->NewInstance();
}
};
// Call the JS handler.
Protocol::JsProtocolHandler callback =
registry_->GetProtocolHandler(request()->url().scheme());
- v8::Local<v8::Value> result =
- callback.Run(v8::Null(registry_->isolate()), request());
+ v8::Local<v8::Value> result = callback.Run(request());
// Determine the type of the job we are going to create.
if (result->IsString()) {
}
void Protocol::OnRegisterProtocol(const std::string& scheme,
- const JsProtocolHandler& callback,
- bool is_handled) {
+ const JsProtocolHandler& handler,
+ const JsCompletionCallback& callback,
+ int is_handled) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate());
if (is_handled || ContainsKey(protocol_handlers_, scheme)) {
callback.Run(v8::Exception::Error(
- mate::StringToV8(isolate(), "The Scheme is already registered")),
- nullptr);
+ mate::StringToV8(isolate(), "The Scheme is already registered")));
return;
}
- protocol_handlers_[scheme] = callback;
- BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
- base::Bind(&Protocol::RegisterProtocolInIO,
- base::Unretained(this), scheme));
+ protocol_handlers_[scheme] = handler;
+ BrowserThread::PostTaskAndReply(BrowserThread::IO, FROM_HERE,
+ base::Bind(&Protocol::RegisterProtocolInIO,
+ base::Unretained(this), scheme),
+ base::Bind(callback, v8::Null(isolate())));
}
void Protocol::OnInterceptProtocol(const std::string& scheme,
- const JsProtocolHandler& callback,
- bool is_handled) {
+ const JsProtocolHandler& handler,
+ const JsCompletionCallback& callback,
+ int is_handled) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate());
if (!is_handled) {
callback.Run(v8::Exception::Error(
- mate::StringToV8(isolate(), "Scheme does not exist.")), nullptr);
+ mate::StringToV8(isolate(), "Scheme does not exist.")));
return;
}
if (ContainsKey(protocol_handlers_, scheme)) {
callback.Run(v8::Exception::Error(
- mate::StringToV8(isolate(), "Cannot intercept custom protocols.")),
- nullptr);
+ mate::StringToV8(isolate(), "Cannot intercept custom protocols.")));
return;
}
- protocol_handlers_[scheme] = callback;
- BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
- base::Bind(&Protocol::InterceptProtocolInIO,
- base::Unretained(this), scheme));
+ protocol_handlers_[scheme] = handler;
+ BrowserThread::PostTaskAndReply(BrowserThread::IO, FROM_HERE,
+ base::Bind(&Protocol::InterceptProtocolInIO,
+ base::Unretained(this), scheme),
+ base::Bind(callback, v8::Null(isolate())));
}
mate::ObjectTemplateBuilder Protocol::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return mate::ObjectTemplateBuilder(isolate)
- .SetMethod("registerProtocol", &Protocol::RegisterProtocol)
- .SetMethod("unregisterProtocol", &Protocol::UnregisterProtocol)
+ .SetMethod("_registerProtocol", &Protocol::RegisterProtocol)
+ .SetMethod("_unregisterProtocol", &Protocol::UnregisterProtocol)
.SetMethod("registerStandardSchemes", &Protocol::RegisterStandardSchemes)
.SetMethod("isHandledProtocol", &Protocol::IsHandledProtocol)
- .SetMethod("interceptProtocol", &Protocol::InterceptProtocol)
- .SetMethod("uninterceptProtocol", &Protocol::UninterceptProtocol);
+ .SetMethod("_interceptProtocol", &Protocol::InterceptProtocol)
+ .SetMethod("_uninterceptProtocol", &Protocol::UninterceptProtocol);
}
void Protocol::RegisterProtocol(v8::Isolate* isolate,
const std::string& scheme,
- const JsProtocolHandler& callback) {
+ const JsProtocolHandler& handler,
+ const JsCompletionCallback& callback) {
IsHandledProtocol(scheme,
base::Bind(&Protocol::OnRegisterProtocol,
base::Unretained(this),
- scheme, callback));
+ scheme, handler, callback));
}
void Protocol::UnregisterProtocol(v8::Isolate* isolate,
- const std::string& scheme) {
+ const std::string& scheme,
+ const JsCompletionCallback& callback) {
ProtocolHandlersMap::iterator it(protocol_handlers_.find(scheme));
- if (it == protocol_handlers_.end())
- return node::ThrowError(isolate, "The scheme has not been registered");
+ if (it == protocol_handlers_.end()) {
+ callback.Run(v8::Exception::Error(
+ mate::StringToV8(isolate, "The Scheme has not been registered")));
+ return;
+ }
protocol_handlers_.erase(it);
- BrowserThread::PostTask(BrowserThread::IO,
- FROM_HERE,
- base::Bind(&Protocol::UnregisterProtocolInIO,
- base::Unretained(this), scheme));
+ BrowserThread::PostTaskAndReply(BrowserThread::IO, FROM_HERE,
+ base::Bind(&Protocol::UnregisterProtocolInIO,
+ base::Unretained(this), scheme),
+ base::Bind(callback, v8::Null(isolate)));
}
void Protocol::RegisterStandardSchemes(
}
void Protocol::IsHandledProtocol(const std::string& scheme,
- const CompletionCallback& callback) {
+ const net::CompletionCallback& callback) {
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::IO, FROM_HERE,
base::Bind(&AtomURLRequestJobFactory::IsHandledProtocol,
base::Unretained(job_factory_), scheme),
void Protocol::InterceptProtocol(v8::Isolate* isolate,
const std::string& scheme,
- const JsProtocolHandler& callback) {
+ const JsProtocolHandler& handler,
+ const JsCompletionCallback& callback) {
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::IO, FROM_HERE,
base::Bind(&AtomURLRequestJobFactory::HasProtocolHandler,
base::Unretained(job_factory_), scheme),
base::Bind(&Protocol::OnInterceptProtocol,
- base::Unretained(this), scheme, callback));
+ base::Unretained(this), scheme, handler, callback));
}
void Protocol::UninterceptProtocol(v8::Isolate* isolate,
- const std::string& scheme) {
+ const std::string& scheme,
+ const JsCompletionCallback& callback) {
ProtocolHandlersMap::iterator it(protocol_handlers_.find(scheme));
- if (it == protocol_handlers_.end())
- return node::ThrowError(isolate, "The scheme has not been registered");
+ if (it == protocol_handlers_.end()) {
+ callback.Run(v8::Exception::Error(
+ mate::StringToV8(isolate, "The Scheme has not been registered")));
+ return;
+ }
protocol_handlers_.erase(it);
- BrowserThread::PostTask(BrowserThread::IO,
- FROM_HERE,
- base::Bind(&Protocol::UninterceptProtocolInIO,
- base::Unretained(this), scheme));
+ BrowserThread::PostTaskAndReply(BrowserThread::IO, FROM_HERE,
+ base::Bind(&Protocol::UninterceptProtocolInIO,
+ base::Unretained(this), scheme),
+ base::Bind(callback, v8::Null(isolate)));
}
void Protocol::RegisterProtocolInIO(const std::string& scheme) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
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) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
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) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
ProtocolHandler* original_handler = job_factory_->GetProtocolHandler(scheme);
- if (original_handler == NULL) {
- BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
- &Protocol::EmitEventInUI,
- base::Unretained(this),
- "error", "There is no protocol handler to intercpet"));
- return;
+ if (original_handler == nullptr) {
+ NOTREACHED();
}
job_factory_->ReplaceProtocol(
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) {
CustomProtocolHandler* handler = static_cast<CustomProtocolHandler*>(
job_factory_->GetProtocolHandler(scheme));
- if (handler->original_handler() == NULL) {
- BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
- &Protocol::EmitEventInUI,
- base::Unretained(this),
- "error", "The protocol is not intercpeted"));
- return;
+ if (handler->original_handler() == nullptr) {
+ NOTREACHED();
}
// Reset the protocol handler to the orignal one and delete current protocol
// handler.
ProtocolHandler* original_handler = handler->ReleaseDefaultProtocolHandler();
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
#include "atom/browser/api/event_emitter.h"
#include "base/callback.h"
#include "native_mate/handle.h"
+#include "net/base/completion_callback.h"
namespace net {
class URLRequest;
class Protocol : public mate::EventEmitter {
public:
using JsProtocolHandler =
- base::Callback<v8::Local<v8::Value>(v8::Local<v8::Value>,
- const net::URLRequest*)>;
- using CompletionCallback = base::Callback<void(bool)>;
+ base::Callback<v8::Local<v8::Value>(const net::URLRequest*)>;
+ using JsCompletionCallback = base::Callback<void(v8::Local<v8::Value>)>;
static mate::Handle<Protocol> Create(
v8::Isolate* isolate, AtomBrowserContext* browser_context);
// Callback called if protocol can be registered.
void OnRegisterProtocol(const std::string& scheme,
- const JsProtocolHandler& callback,
- bool is_handled);
+ const JsProtocolHandler& handler,
+ const JsCompletionCallback& callback,
+ int is_handled);
// Callback called if protocol can be intercepted.
void OnInterceptProtocol(const std::string& scheme,
- const JsProtocolHandler& callback,
- bool is_handled);
+ const JsProtocolHandler& handler,
+ const JsCompletionCallback& callback,
+ int is_handled);
// Register schemes to standard scheme list.
void RegisterStandardSchemes(const std::vector<std::string>& schemes);
// |callback|.
void RegisterProtocol(v8::Isolate* isolate,
const std::string& scheme,
- const JsProtocolHandler& callback);
- void UnregisterProtocol(v8::Isolate* isolate, const std::string& scheme);
+ const JsProtocolHandler& handler,
+ const JsCompletionCallback& callback);
+ void UnregisterProtocol(v8::Isolate* isolate, const std::string& scheme,
+ const JsCompletionCallback& callback);
// Returns whether a scheme has been registered.
void IsHandledProtocol(const std::string& scheme,
- const CompletionCallback& callback);
+ const net::CompletionCallback& callback);
// Intercept/unintercept an existing protocol handler.
void InterceptProtocol(v8::Isolate* isolate,
const std::string& scheme,
- const JsProtocolHandler& callback);
- void UninterceptProtocol(v8::Isolate* isolate, const std::string& scheme);
+ const JsProtocolHandler& handler,
+ 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.
void RegisterProtocolInIO(const std::string& scheme);
void InterceptProtocolInIO(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_;
AtomURLRequestJobFactory* job_factory_;
ProtocolHandlersMap protocol_handlers_;
describe 'protocol.registerProtocol', ->
it 'error when scheme is already registered', (done) ->
register = ->
- protocol.registerProtocol 'test1', (error, request) ->
- assert error instanceof Error
- protocol.unregisterProtocol 'test1'
- done()
- protocol.once 'registered', (event, scheme) ->
- assert.equal scheme, 'test1'
- register()
+ protocol.registerProtocol 'test1', ((request) ->), (error, scheme) ->
+ if error?
+ protocol.unregisterProtocol 'test1', (error, scheme) ->
+ assert.equal scheme, 'test1'
+ done()
+ else
+ assert.equal scheme, 'test1'
+ register()
register()
it 'calls the callback when scheme is visited', (done) ->
- protocol.registerProtocol 'test2', (error, request) ->
+ protocol.registerProtocol 'test2', (request) ->
assert.equal request.url, 'test2://test2'
protocol.unregisterProtocol 'test2'
done()
describe 'protocol.unregisterProtocol', ->
it 'throws error when scheme does not exist', ->
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', ->
it 'returns string should send the string as request content', (done) ->
describe 'protocol.interceptProtocol', ->
it 'throws error when scheme is not a registered one', (done) ->
- protocol.interceptProtocol 'test-intercept', (error, request) ->
- assert error instanceof Error
- done()
+ protocol.interceptProtocol 'test-intercept', ( ->), (error, scheme) ->
+ if error?
+ assert.equal scheme, 'test-intercept'
+ done()
it 'throws error when scheme is a custom protocol', (done) ->
- protocol.once 'unregistered', (event, scheme) ->
+ protocol.once 'unregistered', (scheme) ->
assert.equal scheme, 'atom'
done()
- protocol.once 'registered', (event, scheme) ->
+ protocol.once 'registered', (scheme) ->
assert.equal scheme, 'atom'
- protocol.interceptProtocol 'test-intercept', (error, request) ->
- assert error instanceof Error
- protocol.unregisterProtocol scheme
+ protocol.interceptProtocol 'test-intercept', (->), (error, newScheme) ->
+ if error?
+ assert.equal newScheme, 'test-intercept'
+ protocol.unregisterProtocol scheme
protocol.registerProtocol('atom', ->)
it 'returns original job when callback returns nothing', (done) ->
targetScheme = 'file'
- protocol.once 'intercepted', (event, scheme) ->
+ protocol.once 'intercepted', (scheme) ->
assert.equal scheme, targetScheme
free = -> protocol.uninterceptProtocol targetScheme
$.ajax
url: "#{targetScheme}://#{__filename}",
success: ->
- protocol.once 'unintercepted', (event, scheme) ->
+ protocol.once 'unintercepted', (scheme) ->
assert.equal scheme, targetScheme
done()
free()
error: (xhr, errorType, error) ->
free()
assert false, 'Got error: ' + errorType + ' ' + error
- protocol.interceptProtocol targetScheme, (error, request) ->
+ protocol.interceptProtocol targetScheme, (request) ->
if process.platform is 'win32'
pathInUrl = path.normalize request.url.substr(8)
assert.equal pathInUrl.toLowerCase(), __filename.toLowerCase()