namespace {
+typedef std::map<std::string, v8::Persistent<v8::Function>> HandlersMap;
+static HandlersMap g_handlers;
+
net::URLRequestJobFactoryImpl* GetRequestJobFactory() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
// Get the job factory.
void GetJobTypeInUI() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
+
+ // Call the JS handler.
+ v8::HandleScope scope;
+ v8::Handle<v8::Value> argv[] = {
+ v8::String::New(request()->url().spec().c_str()),
+ v8::String::New(request()->referrer().c_str()),
+ };
+ v8::Handle<v8::Value> result = g_handlers[request()->url().scheme()]->Call(
+ v8::Context::GetCurrent()->Global(), arraysize(argv), argv);
+
+ // Determine the type of the job we are going to create.
+ if (result->IsString()) {
+ type_ = STRING_JOB;
+ string_ = *v8::String::Utf8Value(result);
+ } else {
+ type_ = ERROR_JOB;
+ error_code_ = ERR_NOT_IMPLEMENTED;
+ }
+
+ // Go back to the IO thread.
content::BrowserThread::PostTask(
content::BrowserThread::IO,
FROM_HERE,
scoped_refptr<net::URLRequestJob> real_job_;
JOB_TYPE type_;
+ int error_code_;
+ std::string string_;
+ base::FilePath file_path_;
DISALLOW_COPY_AND_ASSIGN(AdapterRequestJob);
};
} // namespace
-Protocol::HandlersMap Protocol::handlers_;
-
// static
v8::Handle<v8::Value> Protocol::RegisterProtocol(const v8::Arguments& args) {
std::string scheme(*v8::String::Utf8Value(args[0]));
- if (handlers_.find(scheme) != handlers_.end())
+ if (g_handlers.find(scheme) != g_handlers.end())
return node::ThrowError("The scheme is already registered");
// Store the handler in a map.
if (!args[1]->IsFunction())
return node::ThrowError("Handler must be a function");
- handlers_[scheme] = v8::Persistent<v8::Function>::New(
+ g_handlers[scheme] = v8::Persistent<v8::Function>::New(
node::node_isolate, v8::Handle<v8::Function>::Cast(args[1]));
content::BrowserThread::PostTask(content::BrowserThread::IO,
std::string scheme(*v8::String::Utf8Value(args[0]));
// Erase the handler from map.
- HandlersMap::iterator it(handlers_.find(scheme));
- if (it == handlers_.end())
+ HandlersMap::iterator it(g_handlers.find(scheme));
+ if (it == g_handlers.end())
return node::ThrowError("The scheme has not been registered");
- handlers_.erase(it);
+ g_handlers.erase(it);
content::BrowserThread::PostTask(content::BrowserThread::IO,
FROM_HERE,
describe 'protocol API', ->
describe 'protocol.registerProtocol', ->
it 'throws error when scheme is already registered', ->
- register = -> protocol.registerProtocol('test', ->)
+ register = -> protocol.registerProtocol('test1', ->)
register()
assert.throws register, /The scheme is already registered/
- protocol.unregisterProtocol 'test'
+ protocol.unregisterProtocol 'test1'
+
+ it 'calls the callback when scheme is visited', (done) ->
+ protocol.registerProtocol 'test2', -> done()
+ $.get 'test2://test2', ->
+ protocol.unregisterProtocol 'test2'
describe 'protocol.unregisterProtocol', ->
it 'throws error when scheme does not exist', ->
- unregister = -> protocol.unregisterProtocol 'test'
+ unregister = -> protocol.unregisterProtocol 'test3'
assert.throws unregister, /The scheme has not been registered/