Implement protocol.uninterceptProtocol
authorCheng Zhao <zcbenz@gmail.com>
Thu, 13 Aug 2015 12:19:02 +0000 (20:19 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 13 Aug 2015 12:19:02 +0000 (20:19 +0800)
atom/browser/api/atom_api_protocol.cc
atom/browser/api/atom_api_protocol.h

index f190bae..e2f5364 100644 (file)
@@ -65,7 +65,8 @@ mate::ObjectTemplateBuilder Protocol::GetObjectTemplateBuilder(
       .SetMethod("interceptFileProtocol",
                  &Protocol::InterceptProtocol<UrlRequestAsyncAsarJob>)
       .SetMethod("interceptHttpProtocol",
-                 &Protocol::InterceptProtocol<URLRequestFetchJob>);
+                 &Protocol::InterceptProtocol<URLRequestFetchJob>)
+      .SetMethod("uninterceptProtocol", &Protocol::UninterceptProtocol);
 }
 
 void Protocol::RegisterStandardSchemes(
@@ -106,6 +107,27 @@ bool Protocol::IsHandledProtocolInIO(const std::string& scheme) {
   return job_factory_->IsHandledProtocol(scheme);
 }
 
+void Protocol::UninterceptProtocol(
+    const std::string& scheme, mate::Arguments* args) {
+  CompletionCallback callback;
+  args->GetNext(&callback);
+  content::BrowserThread::PostTaskAndReplyWithResult(
+      content::BrowserThread::IO, FROM_HERE,
+      base::Bind(&Protocol::UninterceptProtocolInIO,
+                 base::Unretained(this), scheme),
+      base::Bind(&Protocol::OnIOCompleted,
+                 base::Unretained(this), callback));
+}
+
+Protocol::ProtocolError Protocol::UninterceptProtocolInIO(
+    const std::string& scheme) {
+  if (!original_protocols_.contains(scheme))
+    return PROTOCOL_NOT_INTERCEPTED;
+  job_factory_->ReplaceProtocol(scheme,
+                                original_protocols_.take_and_erase(scheme));
+  return PROTOCOL_OK;
+}
+
 void Protocol::OnIOCompleted(
     const CompletionCallback& callback, ProtocolError error) {
   // The completion callback is optional.
index ce8f087..2e706c4 100644 (file)
@@ -162,6 +162,10 @@ class Protocol : public mate::Wrappable {
     return PROTOCOL_OK;
   }
 
+  // Restore the |scheme| to its original protocol handler.
+  void UninterceptProtocol(const std::string& scheme, mate::Arguments* args);
+  ProtocolError UninterceptProtocolInIO(const std::string& scheme);
+
   // Convert error code to JS exception and call the callback.
   void OnIOCompleted(const CompletionCallback& callback, ProtocolError error);