Implement protocol.unregisterProtocol
authorCheng Zhao <zcbenz@gmail.com>
Thu, 13 Aug 2015 11:26:18 +0000 (19:26 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 13 Aug 2015 11:26:18 +0000 (19:26 +0800)
atom/browser/api/atom_api_protocol.cc
atom/browser/api/atom_api_protocol.h

index b7a4971..7571ee0 100644 (file)
@@ -49,13 +49,14 @@ mate::ObjectTemplateBuilder Protocol::GetObjectTemplateBuilder(
   return mate::ObjectTemplateBuilder(isolate)
       .SetMethod("registerStandardSchemes", &Protocol::RegisterStandardSchemes)
       .SetMethod("registerStringProtocol",
-                 &Protocol::JavaScriptRegisterProtocol<URLRequestStringJob>)
+                 &Protocol::RegisterProtocol<URLRequestStringJob>)
       .SetMethod("registerBufferProtocol",
-                 &Protocol::JavaScriptRegisterProtocol<URLRequestBufferJob>)
+                 &Protocol::RegisterProtocol<URLRequestBufferJob>)
       .SetMethod("registerFileProtocol",
-                 &Protocol::JavaScriptRegisterProtocol<UrlRequestAsyncAsarJob>)
+                 &Protocol::RegisterProtocol<UrlRequestAsyncAsarJob>)
       .SetMethod("registerHttpProtocol",
-                 &Protocol::JavaScriptRegisterProtocol<URLRequestFetchJob>);
+                 &Protocol::RegisterProtocol<URLRequestFetchJob>)
+      .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.
index 22d8989..c409d34 100644 (file)
@@ -92,10 +92,11 @@ class Protocol : public mate::Wrappable {
 
   // Register the protocol with certain request job.
   template<typename RequestJob>
-  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<RequestJob>,
@@ -117,24 +118,9 @@ class Protocol : public mate::Wrappable {
       return PROTOCOL_FAIL;
   }
 
-  // Parse optional parameters for registerProtocol.
-  template<typename RequestJob>
-  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<RequestJob>(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);