Browse Source

Merge pull request #7908 from deepak1556/about_scheme_patch

protocol: register about scheme handler
Kevin Sawicki 8 years ago
parent
commit
7b463297c6

+ 3 - 0
atom/browser/atom_browser_context.cc

@@ -10,6 +10,7 @@
 #include "atom/browser/atom_download_manager_delegate.h"
 #include "atom/browser/atom_permission_manager.h"
 #include "atom/browser/browser.h"
+#include "atom/browser/net/about_protocol_handler.h"
 #include "atom/browser/net/asar/asar_protocol_handler.h"
 #include "atom/browser/net/atom_cert_verifier.h"
 #include "atom/browser/net/atom_ct_delegate.h"
@@ -130,6 +131,8 @@ AtomBrowserContext::CreateURLRequestJobFactory(
   }
   protocol_handlers->clear();
 
+  job_factory->SetProtocolHandler(url::kAboutScheme,
+                                  base::WrapUnique(new AboutProtocolHandler));
   job_factory->SetProtocolHandler(
       url::kDataScheme, base::WrapUnique(new net::DataProtocolHandler));
   job_factory->SetProtocolHandler(

+ 25 - 0
atom/browser/net/about_protocol_handler.cc

@@ -0,0 +1,25 @@
+// Copyright (c) 2016 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/about_protocol_handler.h"
+
+#include "atom/browser/net/url_request_about_job.h"
+
+namespace atom {
+
+AboutProtocolHandler::AboutProtocolHandler() {}
+
+AboutProtocolHandler::~AboutProtocolHandler() {}
+
+net::URLRequestJob* AboutProtocolHandler::MaybeCreateJob(
+    net::URLRequest* request,
+    net::NetworkDelegate* network_delegate) const {
+  return new URLRequestAboutJob(request, network_delegate);
+}
+
+bool AboutProtocolHandler::IsSafeRedirectTarget(const GURL& location) const {
+  return false;
+}
+
+}  // namespace atom

+ 29 - 0
atom/browser/net/about_protocol_handler.h

@@ -0,0 +1,29 @@
+// Copyright (c) 2016 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_ABOUT_PROTOCOL_HANDLER_H_
+#define ATOM_BROWSER_NET_ABOUT_PROTOCOL_HANDLER_H_
+
+#include "net/url_request/url_request_job_factory.h"
+
+namespace atom {
+
+class AboutProtocolHandler : public net::URLRequestJobFactory::ProtocolHandler {
+ public:
+  AboutProtocolHandler();
+  ~AboutProtocolHandler() override;
+
+  // net::URLRequestJobFactory::ProtocolHandler:
+  net::URLRequestJob* MaybeCreateJob(
+      net::URLRequest* request,
+      net::NetworkDelegate* network_delegate) const override;
+  bool IsSafeRedirectTarget(const GURL& location) const override;
+
+ private:
+  DISALLOW_COPY_AND_ASSIGN(AboutProtocolHandler);
+};
+
+}  // namespace atom
+
+#endif  // ATOM_BROWSER_NET_ABOUT_PROTOCOL_HANDLER_H_

+ 37 - 0
atom/browser/net/url_request_about_job.cc

@@ -0,0 +1,37 @@
+// Copyright (c) 2016 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/url_request_about_job.h"
+
+#include "base/threading/thread_task_runner_handle.h"
+
+namespace atom {
+
+URLRequestAboutJob::URLRequestAboutJob(net::URLRequest* request,
+                                       net::NetworkDelegate* network_delegate)
+    : net::URLRequestJob(request, network_delegate), weak_ptr_factory_(this) {}
+
+void URLRequestAboutJob::Start() {
+  base::ThreadTaskRunnerHandle::Get()->PostTask(
+      FROM_HERE, base::Bind(&URLRequestAboutJob::StartAsync,
+                            weak_ptr_factory_.GetWeakPtr()));
+}
+
+void URLRequestAboutJob::Kill() {
+  weak_ptr_factory_.InvalidateWeakPtrs();
+  URLRequestJob::Kill();
+}
+
+bool URLRequestAboutJob::GetMimeType(std::string* mime_type) const {
+  *mime_type = "text/html";
+  return true;
+}
+
+URLRequestAboutJob::~URLRequestAboutJob() {}
+
+void URLRequestAboutJob::StartAsync() {
+  NotifyHeadersComplete();
+}
+
+}  // namespace atom

+ 35 - 0
atom/browser/net/url_request_about_job.h

@@ -0,0 +1,35 @@
+// Copyright (c) 2016 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_URL_REQUEST_ABOUT_JOB_H_
+#define ATOM_BROWSER_NET_URL_REQUEST_ABOUT_JOB_H_
+
+#include <string>
+
+#include "base/memory/weak_ptr.h"
+#include "net/url_request/url_request_job.h"
+
+namespace atom {
+
+class URLRequestAboutJob : public net::URLRequestJob {
+ public:
+  URLRequestAboutJob(net::URLRequest*, net::NetworkDelegate*);
+
+  // URLRequestJob:
+  void Start() override;
+  void Kill() override;
+  bool GetMimeType(std::string* mime_type) const override;
+
+ private:
+  ~URLRequestAboutJob() override;
+  void StartAsync();
+
+  base::WeakPtrFactory<URLRequestAboutJob> weak_ptr_factory_;
+
+  DISALLOW_COPY_AND_ASSIGN(URLRequestAboutJob);
+};
+
+}  // namespace atom
+
+#endif  // ATOM_BROWSER_NET_URL_REQUEST_ABOUT_JOB_H_

+ 4 - 0
filenames.gypi

@@ -228,6 +228,8 @@
       'atom/browser/osr/osr_render_widget_host_view.cc',
       'atom/browser/osr/osr_render_widget_host_view.h',
       'atom/browser/osr/osr_render_widget_host_view_mac.mm',
+      'atom/browser/net/about_protocol_handler.cc',
+      'atom/browser/net/about_protocol_handler.h',
       'atom/browser/net/asar/asar_protocol_handler.cc',
       'atom/browser/net/asar/asar_protocol_handler.h',
       'atom/browser/net/asar/url_request_asar_job.cc',
@@ -250,6 +252,8 @@
       'atom/browser/net/http_protocol_handler.h',
       'atom/browser/net/js_asker.cc',
       'atom/browser/net/js_asker.h',
+      'atom/browser/net/url_request_about_job.cc',
+      'atom/browser/net/url_request_about_job.h',
       'atom/browser/net/url_request_async_asar_job.cc',
       'atom/browser/net/url_request_async_asar_job.h',
       'atom/browser/net/url_request_string_job.cc',

+ 7 - 0
spec/api-protocol-spec.js

@@ -570,6 +570,13 @@ describe('protocol module', function () {
   })
 
   describe('protocol.isProtocolHandled', function () {
+    it('returns true for about:', function (done) {
+      protocol.isProtocolHandled('about', function (result) {
+        assert.equal(result, true)
+        done()
+      })
+    })
+
     it('returns true for file:', function (done) {
       protocol.isProtocolHandled('file', function (result) {
         assert.equal(result, true)