]> khamidou — GitHub mirrors - electron.git/commitdiff
protocol: wrapping httpjobfactory with a default protocol handler to intercept
authordeepak1556 <hop2deep@gmail.com>
Wed, 6 May 2015 20:15:56 +0000 (01:45 +0530)
committerdeepak1556 <hop2deep@gmail.com>
Mon, 11 May 2015 07:02:17 +0000 (12:32 +0530)
atom/browser/atom_browser_context.cc
atom/browser/net/http_protocol_handler.cc [new file with mode: 0644]
atom/browser/net/http_protocol_handler.h [new file with mode: 0644]
filenames.gypi
spec/api-protocol-spec.coffee

index f0a92af9a3f0d23068e691301408fabd26eb2510..835a5c50ae0a967e797e371baef53884bac2b42d 100644 (file)
@@ -7,6 +7,7 @@
 #include "atom/browser/atom_browser_main_parts.h"
 #include "atom/browser/net/atom_url_request_job_factory.h"
 #include "atom/browser/net/asar/asar_protocol_handler.h"
+#include "atom/browser/net/http_protocol_handler.h"
 #include "atom/browser/web_view_manager.h"
 #include "atom/common/options_switches.h"
 #include "base/command_line.h"
@@ -59,6 +60,8 @@ net::URLRequestJobFactory* AtomBrowserContext::CreateURLRequestJobFactory(
       url::kFileScheme, new asar::AsarProtocolHandler(
           BrowserThread::GetBlockingPool()->GetTaskRunnerWithShutdownBehavior(
               base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)));
+  job_factory->SetProtocolHandler(
+      url::kHttpScheme, new HttpProtocolHandler());
 
   // Set up interceptors in the reverse order.
   scoped_ptr<net::URLRequestJobFactory> top_job_factory = job_factory.Pass();
diff --git a/atom/browser/net/http_protocol_handler.cc b/atom/browser/net/http_protocol_handler.cc
new file mode 100644 (file)
index 0000000..9cc3d2d
--- /dev/null
@@ -0,0 +1,25 @@
+// Copyright (c) 2015 GitHub, Inc.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#include "atom/browser/net/http_protocol_handler.h"
+
+#include "net/url_request/url_request_http_job.h"
+
+namespace atom {
+
+HttpProtocolHandler::HttpProtocolHandler() {
+}
+
+HttpProtocolHandler::~HttpProtocolHandler() {
+}
+
+net::URLRequestJob* HttpProtocolHandler::MaybeCreateJob(
+    net::URLRequest* request,
+    net::NetworkDelegate* network_delegate) const {
+  return net::URLRequestHttpJob::Factory(request,
+                                         network_delegate,
+                                         "http");
+}
+
+}  // namespace atom
diff --git a/atom/browser/net/http_protocol_handler.h b/atom/browser/net/http_protocol_handler.h
new file mode 100644 (file)
index 0000000..ad66fa7
--- /dev/null
@@ -0,0 +1,25 @@
+// Copyright (c) 2015 GitHub, Inc.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#ifndef ATOM_BROWSER_NET_HTTP_PROTOCOL_HANDLER_H_
+#define ATOM_BROWSER_NET_HTTP_PROTOCOL_HANDLER_H_
+
+#include "net/url_request/url_request_job_factory.h"
+
+namespace atom {
+
+class HttpProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler {
+ public:
+  HttpProtocolHandler();
+  virtual ~HttpProtocolHandler();
+
+  // net::URLRequestJobFactory::ProtocolHandler:
+  net::URLRequestJob* MaybeCreateJob(
+      net::URLRequest* request,
+      net::NetworkDelegate* network_delegate) const override;
+};
+
+}  // namespace atom
+
+#endif  // ATOM_BROWSER_NET_HTTP_PROTOCOL_HANDLER_H_
index ebd1fb41aaf2b2603e4417b417b9207a40c86ebe..1aa038c0c33954c84f086032fccf9e8f610eb017 100644 (file)
       'atom/browser/net/asar/url_request_asar_job.h',
       'atom/browser/net/atom_url_request_job_factory.cc',
       'atom/browser/net/atom_url_request_job_factory.h',
+      'atom/browser/net/http_protocol_handler.cc',
+      'atom/browser/net/http_protocol_handler.h',            
       'atom/browser/net/url_request_string_job.cc',
       'atom/browser/net/url_request_string_job.h',
       'atom/browser/net/url_request_buffer_job.cc',
index 0b6c62c027548c130e2d9a038b61f609bd996725..060f035037d1882b0db5398628b7d2439440551e 100644 (file)
@@ -190,3 +190,10 @@ describe 'protocol module', ->
             assert false, 'Got error: ' + errorType + ' ' + error
             free()
       protocol.interceptProtocol 'file', handler
+
+    it 'can override http protocol handler', (done) ->
+      handler = remote.createFunctionWithReturnValue 'valar morghulis'
+      protocol.once 'intercepted', ->
+        protocol.uninterceptProtocol 'http'
+        done()
+      protocol.interceptProtocol 'http', handler