From 05fd81ebdc3d97eab89f4c99e38b5eecae5e112b Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 13 Aug 2015 19:26:18 +0800 Subject: [PATCH] Implement protocol.unregisterProtocol --- atom/browser/api/atom_api_protocol.cc | 29 +++++++++++++++++++++++---- atom/browser/api/atom_api_protocol.h | 28 +++++++------------------- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/atom/browser/api/atom_api_protocol.cc b/atom/browser/api/atom_api_protocol.cc index b7a4971a37dc..7571ee0e8377 100644 --- a/atom/browser/api/atom_api_protocol.cc +++ b/atom/browser/api/atom_api_protocol.cc @@ -49,13 +49,14 @@ mate::ObjectTemplateBuilder Protocol::GetObjectTemplateBuilder( return mate::ObjectTemplateBuilder(isolate) .SetMethod("registerStandardSchemes", &Protocol::RegisterStandardSchemes) .SetMethod("registerStringProtocol", - &Protocol::JavaScriptRegisterProtocol) + &Protocol::RegisterProtocol) .SetMethod("registerBufferProtocol", - &Protocol::JavaScriptRegisterProtocol) + &Protocol::RegisterProtocol) .SetMethod("registerFileProtocol", - &Protocol::JavaScriptRegisterProtocol) + &Protocol::RegisterProtocol) .SetMethod("registerHttpProtocol", - &Protocol::JavaScriptRegisterProtocol); + &Protocol::RegisterProtocol) + .SetMethod("unregisterProtocol", &Protocol::UnregisterProtocol); } void Protocol::RegisterStandardSchemes( @@ -63,6 +64,26 @@ void Protocol::RegisterStandardSchemes( atom::AtomBrowserClient::SetCustomSchemes(schemes); } +void Protocol::UnregisterProtocol( + const std::string& scheme, mate::Arguments* args) { + CompletionCallback callback; + args->GetNext(&callback); + content::BrowserThread::PostTaskAndReplyWithResult( + content::BrowserThread::IO, FROM_HERE, + base::Bind(&Protocol::UnregisterProtocolInIO, + base::Unretained(this), scheme), + base::Bind(&Protocol::OnIOCompleted, + base::Unretained(this), callback)); +} + +Protocol::ProtocolError Protocol::UnregisterProtocolInIO( + const std::string& scheme) { + if (!job_factory_->HasProtocolHandler(scheme)) + return PROTOCOL_NOT_REGISTERED; + job_factory_->SetProtocolHandler(scheme, nullptr); + return PROTOCOL_OK; +} + void Protocol::OnIOCompleted( const CompletionCallback& callback, ProtocolError error) { // The completion callback is optional. diff --git a/atom/browser/api/atom_api_protocol.h b/atom/browser/api/atom_api_protocol.h index 22d89896cfdf..c409d34c434d 100644 --- a/atom/browser/api/atom_api_protocol.h +++ b/atom/browser/api/atom_api_protocol.h @@ -92,10 +92,11 @@ class Protocol : public mate::Wrappable { // Register the protocol with certain request job. template - void RegisterProtocol(v8::Isolate* isolate, - const std::string& scheme, + void RegisterProtocol(const std::string& scheme, const Handler& handler, - const CompletionCallback& callback) { + mate::Arguments* args) { + CompletionCallback callback; + args->GetNext(&callback); content::BrowserThread::PostTaskAndReplyWithResult( content::BrowserThread::IO, FROM_HERE, base::Bind(&Protocol::RegisterProtocolInIO, @@ -117,24 +118,9 @@ class Protocol : public mate::Wrappable { return PROTOCOL_FAIL; } - // Parse optional parameters for registerProtocol. - template - void JavaScriptRegisterProtocol(v8::Isolate* isolate, - const std::string& scheme, - mate::Arguments* args) { - // protocol.registerProtocol(scheme[, options], handler[, callback]); - mate::Dictionary options = mate::Dictionary::CreateEmpty(isolate); - Handler handler; - CompletionCallback callback; - args->GetNext(&options); - if (!args->GetNext(&handler)) { - args->ThrowError(); - return; - } - args->GetNext(&callback); - - RegisterProtocol(isolate, scheme, handler, callback); - } + // Unregistered the protocol handler that handles |scheme|. + void UnregisterProtocol(const std::string& scheme, mate::Arguments* args); + ProtocolError UnregisterProtocolInIO(const std::string& scheme); // Convert error code to JS exception and call the callback. void OnIOCompleted(const CompletionCallback& callback, ProtocolError error);