From 855d49100f88afe823fea20fda72e33e71fa95ba Mon Sep 17 00:00:00 2001 From: Robo Date: Wed, 9 Dec 2015 00:51:46 +0530 Subject: [PATCH] protocol: api to register schemes that can handle service worker --- atom/app/atom_content_client.cc | 34 +++++++++++++++---- atom/app/atom_content_client.h | 3 ++ atom/browser/api/atom_api_protocol.cc | 7 ++++ atom/browser/api/atom_api_protocol.h | 3 ++ atom/browser/atom_browser_client.cc | 12 +++++++ atom/browser/atom_browser_client.h | 3 ++ atom/browser/net/asar/url_request_asar_job.cc | 18 ++++++++-- atom/browser/net/asar/url_request_asar_job.h | 2 ++ atom/common/options_switches.cc | 3 ++ atom/common/options_switches.h | 1 + atom/renderer/atom_renderer_client.cc | 4 +++ docs/api/protocol.md | 4 +++ 12 files changed, 86 insertions(+), 8 deletions(-) diff --git a/atom/app/atom_content_client.cc b/atom/app/atom_content_client.cc index 9f161ac569a6..7a1241f2bcc4 100644 --- a/atom/app/atom_content_client.cc +++ b/atom/app/atom_content_client.cc @@ -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* 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* standard_schemes, std::vector* savable_schemes) { - auto command_line = base::CommandLine::ForCurrentProcess(); - auto custom_schemes = command_line->GetSwitchValueASCII( - switches::kRegisterStandardSchemes); - if (!custom_schemes.empty()) { - std::vector schemes = base::SplitString( - custom_schemes, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); + std::vector 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* service_worker_schemes) { + std::vector 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 diff --git a/atom/app/atom_content_client.h b/atom/app/atom_content_client.h index 2716b1eea400..76ac37642a71 100644 --- a/atom/app/atom_content_client.h +++ b/atom/app/atom_content_client.h @@ -5,6 +5,7 @@ #ifndef ATOM_APP_ATOM_CONTENT_CLIENT_H_ #define ATOM_APP_ATOM_CONTENT_CLIENT_H_ +#include #include #include @@ -26,6 +27,8 @@ class AtomContentClient : public brightray::ContentClient { std::vector* savable_schemes) override; void AddPepperPlugins( std::vector* plugins) override; + void AddServiceWorkerSchemes( + std::set* service_worker_schemes) override; private: DISALLOW_COPY_AND_ASSIGN(AtomContentClient); diff --git a/atom/browser/api/atom_api_protocol.cc b/atom/browser/api/atom_api_protocol.cc index b1ad7981377a..09da9c71cadb 100644 --- a/atom/browser/api/atom_api_protocol.cc +++ b/atom/browser/api/atom_api_protocol.cc @@ -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) .SetMethod("registerBufferProtocol", @@ -58,6 +60,11 @@ void Protocol::RegisterStandardSchemes( atom::AtomBrowserClient::SetCustomSchemes(schemes); } +void Protocol::RegisterServiceWorkerSchemes( + const std::vector& schemes) { + atom::AtomBrowserClient::SetCustomServiceWorkerSchemes(schemes); +} + void Protocol::UnregisterProtocol( const std::string& scheme, mate::Arguments* args) { CompletionCallback callback; diff --git a/atom/browser/api/atom_api_protocol.h b/atom/browser/api/atom_api_protocol.h index 9f98eb767309..8aef406fbc38 100644 --- a/atom/browser/api/atom_api_protocol.h +++ b/atom/browser/api/atom_api_protocol.h @@ -92,6 +92,9 @@ class Protocol : public mate::Wrappable { // Register schemes to standard scheme list. void RegisterStandardSchemes(const std::vector& schemes); + // Register schemes that can handle service worker. + void RegisterServiceWorkerSchemes(const std::vector& schemes); + // Register the protocol with certain request job. template void RegisterProtocol(const std::string& scheme, diff --git a/atom/browser/atom_browser_client.cc b/atom/browser/atom_browser_client.cc index b9b186d187d0..1303b91b04d2 100644 --- a/atom/browser/atom_browser_client.cc +++ b/atom/browser/atom_browser_client.cc @@ -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 ImportCertFromFile( const base::FilePath& path) { @@ -87,6 +89,11 @@ void AtomBrowserClient::SetCustomSchemes( g_custom_schemes = base::JoinString(schemes, ","); } +void AtomBrowserClient::SetCustomServiceWorkerSchemes( + const std::vector& 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; diff --git a/atom/browser/atom_browser_client.h b/atom/browser/atom_browser_client.h index 75e17494593b..3c54fab40bc1 100644 --- a/atom/browser/atom_browser_client.h +++ b/atom/browser/atom_browser_client.h @@ -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& schemes); + // Custom schemes to be registered to handle service worker. + static void SetCustomServiceWorkerSchemes( + const std::vector& schemes); protected: // content::ContentBrowserClient: diff --git a/atom/browser/net/asar/url_request_asar_job.cc b/atom/browser/net/asar/url_request_asar_job.cc index 9b9a3c69d27f..d926d1111722 100644 --- a/atom/browser/net/asar/url_request_asar_job.cc +++ b/atom/browser/net/asar/url_request_asar_job.cc @@ -7,13 +7,14 @@ #include #include +#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; diff --git a/atom/browser/net/asar/url_request_asar_job.h b/atom/browser/net/asar/url_request_asar_job.h index 15a723d79e89..29d1afc521d6 100644 --- a/atom/browser/net/asar/url_request_asar_job.h +++ b/atom/browser/net/asar/url_request_asar_job.h @@ -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 diff --git a/atom/common/options_switches.cc b/atom/common/options_switches.cc index a0cb8384a340..0303cf54693e 100644 --- a/atom/common/options_switches.cc +++ b/atom/common/options_switches.cc @@ -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"; diff --git a/atom/common/options_switches.h b/atom/common/options_switches.h index 6960db83bc10..36c2be143146 100644 --- a/atom/common/options_switches.h +++ b/atom/common/options_switches.h @@ -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[]; diff --git a/atom/renderer/atom_renderer_client.cc b/atom/renderer/atom_renderer_client.cc index 7c04c04249a7..e0d40189f204 100644 --- a/atom/renderer/atom_renderer_client.cc +++ b/atom/renderer/atom_renderer_client.cc @@ -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) { diff --git a/docs/api/protocol.md b/docs/api/protocol.md index 5f34165fa84a..80597728e650 100644 --- a/docs/api/protocol.md +++ b/docs/api/protocol.md @@ -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