From 8cd7ccdc0d0703b69d13c6dd164432cdb8fb55d7 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Sat, 24 Aug 2013 20:18:12 +0800 Subject: [PATCH] Call the JS handler to get the type of job. --- browser/api/atom_api_protocol.cc | 38 ++++++++++++++++++++++++++------ browser/api/atom_api_protocol.h | 3 --- spec/api/protocol.coffee | 11 ++++++--- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/browser/api/atom_api_protocol.cc b/browser/api/atom_api_protocol.cc index 8d5f0d10..91d7f5cc 100644 --- a/browser/api/atom_api_protocol.cc +++ b/browser/api/atom_api_protocol.cc @@ -20,6 +20,9 @@ namespace api { namespace { +typedef std::map> HandlersMap; +static HandlersMap g_handlers; + net::URLRequestJobFactoryImpl* GetRequestJobFactory() { DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); // Get the job factory. @@ -96,6 +99,26 @@ class AdapterRequestJob : public net::URLRequestJob { void GetJobTypeInUI() { DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); + + // Call the JS handler. + v8::HandleScope scope; + v8::Handle argv[] = { + v8::String::New(request()->url().spec().c_str()), + v8::String::New(request()->referrer().c_str()), + }; + v8::Handle 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, @@ -113,6 +136,9 @@ class AdapterRequestJob : public net::URLRequestJob { scoped_refptr real_job_; JOB_TYPE type_; + int error_code_; + std::string string_; + base::FilePath file_path_; DISALLOW_COPY_AND_ASSIGN(AdapterRequestJob); }; @@ -137,18 +163,16 @@ class AdapterProtocolHandler } // namespace -Protocol::HandlersMap Protocol::handlers_; - // static v8::Handle 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::New( + g_handlers[scheme] = v8::Persistent::New( node::node_isolate, v8::Handle::Cast(args[1])); content::BrowserThread::PostTask(content::BrowserThread::IO, @@ -163,10 +187,10 @@ v8::Handle Protocol::UnregisterProtocol(const v8::Arguments& args) { 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, diff --git a/browser/api/atom_api_protocol.h b/browser/api/atom_api_protocol.h index 4f53c1d2..7e2f0bf3 100644 --- a/browser/api/atom_api_protocol.h +++ b/browser/api/atom_api_protocol.h @@ -26,9 +26,6 @@ class Protocol { static void RegisterProtocolInIO(const std::string& scheme); static void UnregisterProtocolInIO(const std::string& scheme); - typedef std::map> HandlersMap; - static HandlersMap handlers_; - DISALLOW_IMPLICIT_CONSTRUCTORS(Protocol); }; diff --git a/spec/api/protocol.coffee b/spec/api/protocol.coffee index 332afecf..5ca7ed1b 100644 --- a/spec/api/protocol.coffee +++ b/spec/api/protocol.coffee @@ -4,12 +4,17 @@ protocol = require('remote').require 'protocol' 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/ -- 2.43.0