]> khamidou — GitHub mirrors - electron.git/commitdiff
protocol: api to register schemes that can handle service worker
authorRobo <hop2deep@gmail.com>
Tue, 8 Dec 2015 19:21:46 +0000 (00:51 +0530)
committerRobo <hop2deep@gmail.com>
Tue, 8 Dec 2015 21:41:58 +0000 (03:11 +0530)
12 files changed:
atom/app/atom_content_client.cc
atom/app/atom_content_client.h
atom/browser/api/atom_api_protocol.cc
atom/browser/api/atom_api_protocol.h
atom/browser/atom_browser_client.cc
atom/browser/atom_browser_client.h
atom/browser/net/asar/url_request_asar_job.cc
atom/browser/net/asar/url_request_asar_job.h
atom/common/options_switches.cc
atom/common/options_switches.h
atom/renderer/atom_renderer_client.cc
docs/api/protocol.md

index 9f161ac569a60e071b91aaea8c9c9aa56312ea49..7a1241f2bcc4fbcd2326f7fb1345a7055d096dbc 100644 (file)
@@ -17,6 +17,7 @@
 #include "content/public/common/pepper_plugin_info.h"
 #include "content/public/common/user_agent.h"
 #include "ppapi/shared_impl/ppapi_permissions.h"
+#include "url/url_constants.h"
 
 namespace atom {
 
@@ -62,6 +63,17 @@ content::PepperPluginInfo CreatePepperFlashInfo(const base::FilePath& path,
   return plugin;
 }
 
+void ConvertStringWithSeparatorToVector(std::vector<std::string>* vec,
+                                        const char* separator,
+                                        const char* cmd_switch) {
+  auto command_line = base::CommandLine::ForCurrentProcess();
+  auto string_with_separator = command_line->GetSwitchValueASCII(cmd_switch);
+  if (!string_with_separator.empty())
+    *vec = base::SplitString(string_with_separator, separator,
+                             base::TRIM_WHITESPACE,
+                             base::SPLIT_WANT_NONEMPTY);
+}
+
 }  // namespace
 
 AtomContentClient::AtomContentClient() {
@@ -83,12 +95,10 @@ std::string AtomContentClient::GetUserAgent() const {
 void AtomContentClient::AddAdditionalSchemes(
     std::vector<url::SchemeWithType>* standard_schemes,
     std::vector<std::string>* savable_schemes) {
-  auto command_line = base::CommandLine::ForCurrentProcess();
-  auto custom_schemes = command_line->GetSwitchValueASCII(
-      switches::kRegisterStandardSchemes);
-  if (!custom_schemes.empty()) {
-    std::vector<std::string> schemes = base::SplitString(
-        custom_schemes, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
+  std::vector<std::string> schemes;
+  ConvertStringWithSeparatorToVector(&schemes, ",",
+                                     switches::kRegisterStandardSchemes);
+  if (!schemes.empty()) {
     for (const std::string& scheme : schemes)
       standard_schemes->push_back({scheme.c_str(), url::SCHEME_WITHOUT_PORT});
   }
@@ -110,4 +120,16 @@ void AtomContentClient::AddPepperPlugins(
       CreatePepperFlashInfo(flash_path, flash_version));
 }
 
+void AtomContentClient::AddServiceWorkerSchemes(
+    std::set<std::string>* service_worker_schemes) {
+  std::vector<std::string> schemes;
+  ConvertStringWithSeparatorToVector(&schemes, ",",
+                                     switches::kRegisterServiceWorkerSchemes);
+  if (!schemes.empty()) {
+    for (const std::string& scheme : schemes)
+      service_worker_schemes->insert(scheme);
+  }
+  service_worker_schemes->insert(url::kFileScheme);
+}
+
 }  // namespace atom
index 2716b1eea400b8a4a37456de58bff0c8247c3943..76ac37642a7162e2fa53a0761f6eae60c47305e1 100644 (file)
@@ -5,6 +5,7 @@
 #ifndef ATOM_APP_ATOM_CONTENT_CLIENT_H_
 #define ATOM_APP_ATOM_CONTENT_CLIENT_H_
 
+#include <set>
 #include <string>
 #include <vector>
 
@@ -26,6 +27,8 @@ class AtomContentClient : public brightray::ContentClient {
       std::vector<std::string>* savable_schemes) override;
   void AddPepperPlugins(
       std::vector<content::PepperPluginInfo>* plugins) override;
+  void AddServiceWorkerSchemes(
+      std::set<std::string>* service_worker_schemes) override;
 
  private:
   DISALLOW_COPY_AND_ASSIGN(AtomContentClient);
index b1ad7981377a2fe6d90fad678a36dbb26ae9680b..09da9c71cadbc204787cf20a4fec584d45696476 100644 (file)
@@ -32,6 +32,8 @@ mate::ObjectTemplateBuilder Protocol::GetObjectTemplateBuilder(
     v8::Isolate* isolate) {
   return mate::ObjectTemplateBuilder(isolate)
       .SetMethod("registerStandardSchemes", &Protocol::RegisterStandardSchemes)
+      .SetMethod("registerServiceWorkerSchemes",
+                 &Protocol::RegisterServiceWorkerSchemes)
       .SetMethod("registerStringProtocol",
                  &Protocol::RegisterProtocol<URLRequestStringJob>)
       .SetMethod("registerBufferProtocol",
@@ -58,6 +60,11 @@ void Protocol::RegisterStandardSchemes(
   atom::AtomBrowserClient::SetCustomSchemes(schemes);
 }
 
+void Protocol::RegisterServiceWorkerSchemes(
+    const std::vector<std::string>& schemes) {
+  atom::AtomBrowserClient::SetCustomServiceWorkerSchemes(schemes);
+}
+
 void Protocol::UnregisterProtocol(
     const std::string& scheme, mate::Arguments* args) {
   CompletionCallback callback;
index 9f98eb7673096581afb0eaa46a55b3c9eb373535..8aef406fbc38989429c9871a99830965979ef286 100644 (file)
@@ -92,6 +92,9 @@ class Protocol : public mate::Wrappable {
   // Register schemes to standard scheme list.
   void RegisterStandardSchemes(const std::vector<std::string>& schemes);
 
+  // Register schemes that can handle service worker.
+  void RegisterServiceWorkerSchemes(const std::vector<std::string>& schemes);
+
   // Register the protocol with certain request job.
   template<typename RequestJob>
   void RegisterProtocol(const std::string& scheme,
index b9b186d187d0919ce1304e4ef945777f92f99910..1303b91b04d2d83cef8ae4982ecd254448123a98 100644 (file)
@@ -54,6 +54,8 @@ bool g_suppress_renderer_process_restart = false;
 
 // Custom schemes to be registered to standard.
 std::string g_custom_schemes = "";
+// Custom schemes to be registered to handle service worker.
+std::string g_custom_service_worker_schemes = "";
 
 scoped_refptr<net::X509Certificate> ImportCertFromFile(
     const base::FilePath& path) {
@@ -87,6 +89,11 @@ void AtomBrowserClient::SetCustomSchemes(
   g_custom_schemes = base::JoinString(schemes, ",");
 }
 
+void AtomBrowserClient::SetCustomServiceWorkerSchemes(
+    const std::vector<std::string>& schemes) {
+  g_custom_service_worker_schemes = base::JoinString(schemes, ",");
+}
+
 AtomBrowserClient::AtomBrowserClient() : delegate_(nullptr) {
 }
 
@@ -172,6 +179,11 @@ void AtomBrowserClient::AppendExtraCommandLineSwitches(
     command_line->AppendSwitchASCII(switches::kRegisterStandardSchemes,
                                     g_custom_schemes);
 
+  // The registered service worker schemes.
+  if (!g_custom_service_worker_schemes.empty())
+    command_line->AppendSwitchASCII(switches::kRegisterServiceWorkerSchemes,
+                                    g_custom_service_worker_schemes);
+
 #if defined(OS_WIN)
   // Append --app-user-model-id.
   PWSTR current_app_id;
index 75e17494593b22dae02229995f4de51549ad7a67..3c54fab40bc1b5cf6efafa52c0fab7f4ad121d84 100644 (file)
@@ -38,6 +38,9 @@ class AtomBrowserClient : public brightray::BrowserClient,
   static void SuppressRendererProcessRestartForOnce();
   // Custom schemes to be registered to standard.
   static void SetCustomSchemes(const std::vector<std::string>& schemes);
+  // Custom schemes to be registered to handle service worker.
+  static void SetCustomServiceWorkerSchemes(
+      const std::vector<std::string>& schemes);
 
  protected:
   // content::ContentBrowserClient:
index 9b9a3c69d27fa41e7ebc09c6aaa9edc2074ae09c..d926d11117228b677cf10d61ca4a804364dba50b 100644 (file)
@@ -7,13 +7,14 @@
 #include <string>
 #include <vector>
 
+#include "atom/common/asar/archive.h"
+#include "atom/common/asar/asar_util.h"
+#include "atom/common/atom_constants.h"
 #include "base/bind.h"
 #include "base/files/file_util.h"
 #include "base/strings/string_util.h"
 #include "base/synchronization/lock.h"
 #include "base/task_runner.h"
-#include "atom/common/asar/archive.h"
-#include "atom/common/asar/asar_util.h"
 #include "net/base/file_stream.h"
 #include "net/base/filename_util.h"
 #include "net/base/io_buffer.h"
@@ -227,6 +228,19 @@ void URLRequestAsarJob::SetExtraRequestHeaders(
   }
 }
 
+int URLRequestAsarJob::GetResponseCode() const {
+  // Request Job gets created only if path exists.
+  return 200;
+}
+
+void URLRequestAsarJob::GetResponseInfo(net::HttpResponseInfo* info) {
+  std::string status("HTTP/1.1 200 OK");
+  net::HttpResponseHeaders* headers = new net::HttpResponseHeaders(status);
+
+  headers->AddHeader(atom::kCORSHeader);
+  info->headers = headers;
+}
+
 void URLRequestAsarJob::FetchMetaInfo(const base::FilePath& file_path,
                                       FileMetaInfo* meta_info) {
   base::File::Info file_info;
index 15a723d79e895a3cf25fb2604fa53a2423184e00..29d1afc521d6cf904f0eeb863047d042b0c026c6 100644 (file)
@@ -61,6 +61,8 @@ class URLRequestAsarJob : public net::URLRequestJob {
   net::Filter* SetupFilter() const override;
   bool GetMimeType(std::string* mime_type) const override;
   void SetExtraRequestHeaders(const net::HttpRequestHeaders& headers) override;
+  int GetResponseCode() const override;
+  void GetResponseInfo(net::HttpResponseInfo* info) override;
 
  private:
   // Meta information about the file. It's used as a member in the
index a0cb8384a3404f7346b4d8e2fdaef527b6c0944a..0303cf54693efc69313decb63f284362a3d7ca0b 100644 (file)
@@ -119,6 +119,9 @@ const char kDisableHttpCache[] = "disable-http-cache";
 // Register schemes to standard.
 const char kRegisterStandardSchemes[] = "register-standard-schemes";
 
+// Register schemes to handle service worker.
+const char kRegisterServiceWorkerSchemes[] = "register-service-worker-schemes";
+
 // The minimum SSL/TLS version ("tls1", "tls1.1", or "tls1.2") that
 // TLS fallback will accept.
 const char kSSLVersionFallbackMin[] = "ssl-version-fallback-min";
index 6960db83bc106f26d65dd23fca96398c543674c5..36c2be14314625cdabbe4841f88688c25d3d1667 100644 (file)
@@ -66,6 +66,7 @@ extern const char kPpapiFlashVersion[];
 extern const char kClientCertificate[];
 extern const char kDisableHttpCache[];
 extern const char kRegisterStandardSchemes[];
+extern const char kRegisterServiceWorkerSchemes[];
 extern const char kSSLVersionFallbackMin[];
 extern const char kCipherSuiteBlacklist[];
 extern const char kAppUserModelId[];
index 7c04c04249a76786797c0324e24b7f62aaf7f74a..e0d40189f20416b0d01fe426ab1093a6397aa815 100644 (file)
@@ -27,6 +27,7 @@
 #include "third_party/WebKit/public/web/WebLocalFrame.h"
 #include "third_party/WebKit/public/web/WebPluginParams.h"
 #include "third_party/WebKit/public/web/WebKit.h"
+#include "third_party/WebKit/public/web/WebSecurityPolicy.h"
 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
 #include "third_party/WebKit/public/web/WebView.h"
 
@@ -129,6 +130,9 @@ void AtomRendererClient::RenderFrameCreated(
     content::RenderFrame* render_frame) {
   new PepperHelper(render_frame);
   new AtomRenderFrameObserver(render_frame, this);
+
+  // Allow file scheme to handle service worker by default.
+  blink::WebSecurityPolicy::registerURLSchemeAsAllowingServiceWorkers("file");
 }
 
 void AtomRendererClient::RenderViewCreated(content::RenderView* render_view) {
index 5f34165fa84a48d485f54f4d0b7312614ccf2168..80597728e65000d3c4a9ec76f4eab118e171c1ad 100644 (file)
@@ -38,6 +38,10 @@ A standard `scheme` adheres to what RFC 3986 calls
 [generic URI syntax](https://tools.ietf.org/html/rfc3986#section-3). This
 includes `file:` and `filesystem:`.
 
+### `protocol.registerServiceWorkerSchemes(schemes)`
+
+* `schemes` Array - Custom schemes to be registered to handle service workers.
+
 ### `protocol.registerFileProtocol(scheme, handler[, completion])`
 
 * `scheme` String