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

@ -25,16 +25,12 @@ namespace mate {
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();
}
};
@ -77,8 +73,7 @@ class CustomProtocolRequestJob : public AdapterRequestJob {
// 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()) {
@ -208,81 +203,88 @@ Protocol::JsProtocolHandler Protocol::GetProtocolHandler(
}
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(
@ -291,7 +293,7 @@ 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),
@ -300,68 +302,54 @@ void Protocol::IsHandledProtocol(const std::string& 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) {
@ -369,28 +357,14 @@ 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

View file

@ -12,6 +12,7 @@
#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;
@ -27,9 +28,8 @@ namespace api {
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);
@ -50,12 +50,14 @@ class Protocol : public mate::EventEmitter {
// 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);
@ -64,18 +66,22 @@ class Protocol : public mate::EventEmitter {
// |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);
@ -83,9 +89,6 @@ class Protocol : public mate::EventEmitter {
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_;

View file

@ -6,6 +6,29 @@ EventEmitter = require('events').EventEmitter
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 =
class RequestStringJob
constructor: ({mimeType, charset, data}) ->

View file

@ -15,6 +15,9 @@ app.on('ready', function() {
protocol.registerProtocol('atom', function(request) {
var url = request.url.substr(7)
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
was emitted.
## protocol.registerProtocol(scheme, handler)
## protocol.registerProtocol(scheme, handler, callback)
* `scheme` String
* `handler` Function
* `callback` Function
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
response you would like to send.
## protocol.unregisterProtocol(scheme)
## protocol.unregisterProtocol(scheme, callback)
* `scheme` String
* `callback` Function
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.
## protocol.interceptProtocol(scheme, handler)
## protocol.interceptProtocol(scheme, handler, callback)
* `scheme` String
* `handler` Function
* `callback` Function
Intercepts an existing protocol with `scheme`, returning `null` or `undefined`
in `handler` would use the original protocol handler to handle the request.
## protocol.uninterceptProtocol(scheme)
## protocol.uninterceptProtocol(scheme, callback)
* `scheme` String
* `callback` Function
Unintercepts a protocol.

View file

@ -9,17 +9,18 @@ describe 'protocol module', ->
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()
@ -28,7 +29,7 @@ describe 'protocol module', ->
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) ->
@ -190,37 +191,39 @@ describe 'protocol module', ->
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()