From: Robo Date: Thu, 9 Jul 2015 09:18:45 +0000 (+0530) Subject: read/write protocol handler map in IO X-Git-Tag: v0.30.0~37^2 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=c56b3425a919254b4821dfbce1282bcb66d60551;p=electron.git read/write protocol handler map in IO --- diff --git a/atom/browser/api/atom_api_protocol.cc b/atom/browser/api/atom_api_protocol.cc index fa498a7f..65ba3039 100644 --- a/atom/browser/api/atom_api_protocol.cc +++ b/atom/browser/api/atom_api_protocol.cc @@ -36,7 +36,6 @@ struct Converter { } // namespace mate - namespace atom { namespace api { @@ -189,6 +188,26 @@ class CustomProtocolHandler : public ProtocolHandler { DISALLOW_COPY_AND_ASSIGN(CustomProtocolHandler); }; +std::string ConvertErrorCode(int error_code) { + switch (error_code) { + case Protocol::ERR_SCHEME_REGISTERED: + return "The Scheme is already registered"; + case Protocol::ERR_SCHEME_UNREGISTERED: + return "The Scheme has not been registered"; + case Protocol::ERR_SCHEME_INTERCEPTED: + return "There is no protocol handler to intercept"; + case Protocol::ERR_SCHEME_UNINTERCEPTED: + return "The protocol is not intercepted"; + case Protocol::ERR_NO_SCHEME: + return "The Scheme does not exist."; + case Protocol::ERR_SCHEME: + return "Cannot intercept custom protocols"; + default: + NOTREACHED(); + return std::string(); + } +} + } // namespace Protocol::Protocol(AtomBrowserContext* browser_context) @@ -202,91 +221,32 @@ Protocol::JsProtocolHandler Protocol::GetProtocolHandler( return protocol_handlers_[scheme]; } -void Protocol::OnRegisterProtocol(const std::string& scheme, - 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"))); - return; - } - - 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& handler, - const JsCompletionCallback& callback, - int is_handled) { +void Protocol::OnIOActionCompleted(const JsCompletionCallback& callback, + int error) { 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."))); - return; - } - if (ContainsKey(protocol_handlers_, scheme)) { + if (error) { callback.Run(v8::Exception::Error( - mate::StringToV8(isolate(), "Cannot intercept custom protocols."))); + mate::StringToV8(isolate(), ConvertErrorCode(error)))); return; } - protocol_handlers_[scheme] = handler; - BrowserThread::PostTaskAndReply(BrowserThread::IO, FROM_HERE, - base::Bind(&Protocol::InterceptProtocolInIO, - base::Unretained(this), scheme), - base::Bind(callback, v8::Null(isolate()))); + callback.Run(v8::Null(isolate())); } mate::ObjectTemplateBuilder Protocol::GetObjectTemplateBuilder( v8::Isolate* isolate) { return mate::ObjectTemplateBuilder(isolate) - .SetMethod("_registerProtocol", &Protocol::RegisterProtocol) - .SetMethod("_unregisterProtocol", &Protocol::UnregisterProtocol) .SetMethod("registerStandardSchemes", &Protocol::RegisterStandardSchemes) .SetMethod("isHandledProtocol", &Protocol::IsHandledProtocol) + .SetMethod("_registerProtocol", &Protocol::RegisterProtocol) + .SetMethod("_unregisterProtocol", &Protocol::UnregisterProtocol) .SetMethod("_interceptProtocol", &Protocol::InterceptProtocol) .SetMethod("_uninterceptProtocol", &Protocol::UninterceptProtocol); } -void Protocol::RegisterProtocol(v8::Isolate* isolate, - const std::string& scheme, - const JsProtocolHandler& handler, - const JsCompletionCallback& callback) { - IsHandledProtocol(scheme, - base::Bind(&Protocol::OnRegisterProtocol, - base::Unretained(this), - scheme, handler, callback)); -} - -void Protocol::UnregisterProtocol(v8::Isolate* isolate, - const std::string& scheme, - const JsCompletionCallback& callback) { - ProtocolHandlersMap::iterator it(protocol_handlers_.find(scheme)); - 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::PostTaskAndReply(BrowserThread::IO, FROM_HERE, - base::Bind(&Protocol::UnregisterProtocolInIO, - base::Unretained(this), scheme), - base::Bind(callback, v8::Null(isolate))); -} - void Protocol::RegisterStandardSchemes( const std::vector& schemes) { atom::AtomBrowserClient::SetCustomSchemes(schemes); @@ -300,71 +260,119 @@ void Protocol::IsHandledProtocol(const std::string& scheme, callback); } +void Protocol::RegisterProtocol(v8::Isolate* isolate, + const std::string& scheme, + const JsProtocolHandler& handler, + const JsCompletionCallback& callback) { + BrowserThread::PostTaskAndReplyWithResult(BrowserThread::IO, FROM_HERE, + base::Bind(&Protocol::RegisterProtocolInIO, + base::Unretained(this), scheme, handler), + base::Bind(&Protocol::OnIOActionCompleted, + base::Unretained(this), callback)); +} + +void Protocol::UnregisterProtocol(v8::Isolate* isolate, + const std::string& scheme, + const JsCompletionCallback& callback) { + BrowserThread::PostTaskAndReplyWithResult(BrowserThread::IO, FROM_HERE, + base::Bind(&Protocol::UnregisterProtocolInIO, + base::Unretained(this), scheme), + base::Bind(&Protocol::OnIOActionCompleted, + base::Unretained(this), callback)); +} + void Protocol::InterceptProtocol(v8::Isolate* isolate, const std::string& scheme, 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, handler, callback)); + base::Bind(&Protocol::InterceptProtocolInIO, + base::Unretained(this), scheme, handler), + base::Bind(&Protocol::OnIOActionCompleted, + base::Unretained(this), callback)); } void Protocol::UninterceptProtocol(v8::Isolate* isolate, const std::string& scheme, const JsCompletionCallback& callback) { - ProtocolHandlersMap::iterator it(protocol_handlers_.find(scheme)); - 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::PostTaskAndReply(BrowserThread::IO, FROM_HERE, + BrowserThread::PostTaskAndReplyWithResult(BrowserThread::IO, FROM_HERE, base::Bind(&Protocol::UninterceptProtocolInIO, base::Unretained(this), scheme), - base::Bind(callback, v8::Null(isolate))); + base::Bind(&Protocol::OnIOActionCompleted, + base::Unretained(this), callback)); } -void Protocol::RegisterProtocolInIO(const std::string& scheme) { +int Protocol::RegisterProtocolInIO(const std::string& scheme, + const JsProtocolHandler& handler) { DCHECK_CURRENTLY_ON(BrowserThread::IO); + if (ContainsKey(protocol_handlers_, scheme) || + job_factory_->IsHandledProtocol(scheme)) { + return ERR_SCHEME_REGISTERED; + } + + protocol_handlers_[scheme] = handler; job_factory_->SetProtocolHandler(scheme, new CustomProtocolHandler(this)); + + return OK; } -void Protocol::UnregisterProtocolInIO(const std::string& scheme) { +int Protocol::UnregisterProtocolInIO(const std::string& scheme) { DCHECK_CURRENTLY_ON(BrowserThread::IO); + ProtocolHandlersMap::iterator it(protocol_handlers_.find(scheme)); + if (it == protocol_handlers_.end()) { + return ERR_SCHEME_UNREGISTERED; + } + + protocol_handlers_.erase(it); job_factory_->SetProtocolHandler(scheme, NULL); + + return OK; } -void Protocol::InterceptProtocolInIO(const std::string& scheme) { +int Protocol::InterceptProtocolInIO(const std::string& scheme, + const JsProtocolHandler& handler) { DCHECK_CURRENTLY_ON(BrowserThread::IO); + if (!job_factory_->HasProtocolHandler(scheme)) + return ERR_NO_SCHEME; + + if (ContainsKey(protocol_handlers_, scheme)) + return ERR_SCHEME; + + protocol_handlers_[scheme] = handler; ProtocolHandler* original_handler = job_factory_->GetProtocolHandler(scheme); if (original_handler == nullptr) { - NOTREACHED(); + return ERR_SCHEME_INTERCEPTED; } job_factory_->ReplaceProtocol( scheme, new CustomProtocolHandler(this, original_handler)); + + return OK; } -void Protocol::UninterceptProtocolInIO(const std::string& scheme) { +int Protocol::UninterceptProtocolInIO(const std::string& scheme) { DCHECK_CURRENTLY_ON(BrowserThread::IO); + ProtocolHandlersMap::iterator it(protocol_handlers_.find(scheme)); + if (it == protocol_handlers_.end()) + return ERR_SCHEME_UNREGISTERED; + + protocol_handlers_.erase(it); CustomProtocolHandler* handler = static_cast( job_factory_->GetProtocolHandler(scheme)); if (handler->original_handler() == nullptr) { - NOTREACHED(); + return ERR_SCHEME_UNINTERCEPTED; } // 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); + + return OK; } // static diff --git a/atom/browser/api/atom_api_protocol.h b/atom/browser/api/atom_api_protocol.h index 148620a0..b4d56018 100644 --- a/atom/browser/api/atom_api_protocol.h +++ b/atom/browser/api/atom_api_protocol.h @@ -31,6 +31,16 @@ class Protocol : public mate::EventEmitter { base::Callback(const net::URLRequest*)>; using JsCompletionCallback = base::Callback)>; + enum { + OK = 0, + ERR_SCHEME_REGISTERED, + ERR_SCHEME_UNREGISTERED, + ERR_SCHEME_INTERCEPTED, + ERR_SCHEME_UNINTERCEPTED, + ERR_NO_SCHEME, + ERR_SCHEME + }; + static mate::Handle Create( v8::Isolate* isolate, AtomBrowserContext* browser_context); @@ -48,20 +58,17 @@ class Protocol : public mate::EventEmitter { private: typedef std::map ProtocolHandlersMap; - // Callback called if protocol can be registered. - void OnRegisterProtocol(const std::string& scheme, - const JsProtocolHandler& handler, - const JsCompletionCallback& callback, - int is_handled); - // Callback called if protocol can be intercepted. - void OnInterceptProtocol(const std::string& scheme, - const JsProtocolHandler& handler, - const JsCompletionCallback& callback, - int is_handled); + // Callback called after performing action on IO thread. + void OnIOActionCompleted(const JsCompletionCallback& callback, + int error); // Register schemes to standard scheme list. void RegisterStandardSchemes(const std::vector& schemes); + // Returns whether a scheme has been registered. + void IsHandledProtocol(const std::string& scheme, + const net::CompletionCallback& callback); + // Register/unregister an networking |scheme| which would be handled by // |callback|. void RegisterProtocol(v8::Isolate* isolate, @@ -71,10 +78,6 @@ class Protocol : public mate::EventEmitter { 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 net::CompletionCallback& callback); - // Intercept/unintercept an existing protocol handler. void InterceptProtocol(v8::Isolate* isolate, const std::string& scheme, @@ -84,10 +87,12 @@ class Protocol : public mate::EventEmitter { const JsCompletionCallback& callback); // The networking related operations have to be done in IO thread. - void RegisterProtocolInIO(const std::string& scheme); - void UnregisterProtocolInIO(const std::string& scheme); - void InterceptProtocolInIO(const std::string& scheme); - void UninterceptProtocolInIO(const std::string& scheme); + int RegisterProtocolInIO(const std::string& scheme, + const JsProtocolHandler& handler); + int UnregisterProtocolInIO(const std::string& scheme); + int InterceptProtocolInIO(const std::string& scheme, + const JsProtocolHandler& handler); + int UninterceptProtocolInIO(const std::string& scheme); AtomBrowserContext* browser_context_; AtomURLRequestJobFactory* job_factory_; diff --git a/spec/api-protocol-spec.coffee b/spec/api-protocol-spec.coffee index 3ddcdf62..b65002a6 100644 --- a/spec/api-protocol-spec.coffee +++ b/spec/api-protocol-spec.coffee @@ -28,8 +28,10 @@ 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/ + protocol.unregisterProtocol 'test3', (->), (error, scheme) -> + if (error) + assert.equal scheme, 'test3' + done() describe 'registered protocol callback', -> it 'returns string should send the string as request content', (done) ->