diff options
author | horo <horo@chromium.org> | 2014-10-31 19:08:28 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-11-01 02:08:43 +0000 |
commit | e09b6c8616f4a8e45e91a171eedddefde9522ac2 (patch) | |
tree | a2bf42602693c66c8d5089e68aca52f7ae788d9a | |
parent | 2178ec8cf854f0996a2d66649f52841e1ed18795 (diff) | |
download | chromium_src-e09b6c8616f4a8e45e91a171eedddefde9522ac2.zip chromium_src-e09b6c8616f4a8e45e91a171eedddefde9522ac2.tar.gz chromium_src-e09b6c8616f4a8e45e91a171eedddefde9522ac2.tar.bz2 |
[ServiceWorker] Don't send the UMA related headers to the ServiceWorker.
Chrome add "X-Chrome-UMA-Enabled" (and sometimes "X-Client-Data") headers while accessing Google servers if UMA is enabled.
This header is added before sending the request to the ServiceWorker.
So the request which is passed to the ServiceWorker contains this header.
If event.request.mode is 'cors' and event.request.headers has "x-chrome-uma-enabled", fetch(event.request) send a CORS preflight request.
This preflight request contains "Access-Control-Request-Headers: x-chrome-uma-enabled" header.
But the response from the Google server doesn't contain "Access-Control-Allow-Headers: x-chrome-uma-enabled" header.
So this fetch fails.
To avoid this problem this patch removes these headers before sending the request to the ServiceWorker.
This patch introduces ServiceWorkerContext::AddExcludedHeadersForFetchEvent() method and changes DevToolsNetworkTransactionFactory to call this method.
BUG=425649
Review URL: https://codereview.chromium.org/666973003
Cr-Commit-Position: refs/heads/master@{#302379}
9 files changed, 84 insertions, 25 deletions
diff --git a/chrome/browser/devtools/devtools_network_transaction.cc b/chrome/browser/devtools/devtools_network_transaction.cc index 44111d7..6a0f1b5 100644 --- a/chrome/browser/devtools/devtools_network_transaction.cc +++ b/chrome/browser/devtools/devtools_network_transaction.cc @@ -11,16 +11,14 @@ #include "net/http/http_network_transaction.h" #include "net/http/http_request_info.h" -namespace { - // Keep in sync with kDevToolsRequestInitiator and // kDevToolsEmulateNetworkConditionsClientId defined in -// service_worker_url_request_job.cc and InspectorResourceAgent.cpp. -const char kDevToolsRequestInitiator[] = "X-DevTools-Request-Initiator"; -const char kDevToolsEmulateNetworkConditionsClientId[] = - "X-DevTools-Emulate-Network-Conditions-Client-Id"; - -} // namespace +// InspectorResourceAgent.cpp. +const char DevToolsNetworkTransaction::kDevToolsRequestInitiator[] = + "X-DevTools-Request-Initiator"; +const char + DevToolsNetworkTransaction::kDevToolsEmulateNetworkConditionsClientId[] = + "X-DevTools-Emulate-Network-Conditions-Client-Id"; DevToolsNetworkTransaction::DevToolsNetworkTransaction( DevToolsNetworkController* controller, diff --git a/chrome/browser/devtools/devtools_network_transaction.h b/chrome/browser/devtools/devtools_network_transaction.h index 7ae8269..bebf013 100644 --- a/chrome/browser/devtools/devtools_network_transaction.h +++ b/chrome/browser/devtools/devtools_network_transaction.h @@ -41,6 +41,9 @@ class DevToolsNetworkControllerHelper; // net::ERR_INTERNET_DISCONNECTED result value. class DevToolsNetworkTransaction : public net::HttpTransaction { public: + static const char kDevToolsRequestInitiator[]; + static const char kDevToolsEmulateNetworkConditionsClientId[]; + DevToolsNetworkTransaction( DevToolsNetworkController* controller, scoped_ptr<net::HttpTransaction> network_transaction); diff --git a/chrome/browser/devtools/devtools_network_transaction_factory.cc b/chrome/browser/devtools/devtools_network_transaction_factory.cc index 3fd8b84..09b8ddc 100644 --- a/chrome/browser/devtools/devtools_network_transaction_factory.cc +++ b/chrome/browser/devtools/devtools_network_transaction_factory.cc @@ -4,8 +4,12 @@ #include "chrome/browser/devtools/devtools_network_transaction_factory.h" +#include <set> +#include <string> + #include "chrome/browser/devtools/devtools_network_controller.h" #include "chrome/browser/devtools/devtools_network_transaction.h" +#include "content/public/browser/service_worker_context.h" #include "net/base/net_errors.h" #include "net/http/http_network_layer.h" #include "net/http/http_network_transaction.h" @@ -15,6 +19,11 @@ DevToolsNetworkTransactionFactory::DevToolsNetworkTransactionFactory( net::HttpNetworkSession* session) : controller_(controller), network_layer_(new net::HttpNetworkLayer(session)) { + std::set<std::string> headers; + headers.insert(DevToolsNetworkTransaction::kDevToolsRequestInitiator); + headers.insert( + DevToolsNetworkTransaction::kDevToolsEmulateNetworkConditionsClientId); + content::ServiceWorkerContext::AddExcludedHeadersForFetchEvent(headers); } DevToolsNetworkTransactionFactory::~DevToolsNetworkTransactionFactory() { diff --git a/chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.cc b/chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.cc index fd8b196..6188b5e 100644 --- a/chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.cc +++ b/chrome/browser/renderer_host/chrome_resource_dispatcher_host_delegate.cc @@ -42,6 +42,7 @@ #include "content/public/browser/resource_context.h" #include "content/public/browser/resource_dispatcher_host.h" #include "content/public/browser/resource_request_info.h" +#include "content/public/browser/service_worker_context.h" #include "content/public/browser/stream_info.h" #include "content/public/browser/web_contents.h" #include "content/public/common/resource_response.h" @@ -271,6 +272,12 @@ ChromeResourceDispatcherHostDelegate::ChromeResourceDispatcherHostDelegate( user_script_listener_(new extensions::UserScriptListener()), #endif prerender_tracker_(prerender_tracker) { + BrowserThread::PostTask( + BrowserThread::IO, + FROM_HERE, + base::Bind(content::ServiceWorkerContext::AddExcludedHeadersForFetchEvent, + variations::VariationsHttpHeaderProvider::GetInstance() + ->GetVariationHeaderNames())); } ChromeResourceDispatcherHostDelegate::~ChromeResourceDispatcherHostDelegate() { diff --git a/components/variations/net/variations_http_header_provider.cc b/components/variations/net/variations_http_header_provider.cc index fa630cf..79a935a 100644 --- a/components/variations/net/variations_http_header_provider.cc +++ b/components/variations/net/variations_http_header_provider.cc @@ -4,6 +4,8 @@ #include "components/variations/net/variations_http_header_provider.h" +#include <set> +#include <string> #include <vector> #include "base/base64.h" @@ -36,6 +38,9 @@ const char* kSuffixesToSetHeadersFor[] = { ".ytimg.com", }; +const char kChromeUMAEnabled[] = "X-Chrome-UMA-Enabled"; +const char kClientData[] = "X-Client-Data"; + } // namespace VariationsHttpHeaderProvider* VariationsHttpHeaderProvider::GetInstance() { @@ -61,7 +66,7 @@ void VariationsHttpHeaderProvider::AppendHeaders( return; if (uma_enabled) - headers->SetHeaderIfMissing("X-Chrome-UMA-Enabled", "1"); + headers->SetHeaderIfMissing(kChromeUMAEnabled, "1"); // Lazily initialize the header, if not already done, before attempting to // transmit it. @@ -75,8 +80,7 @@ void VariationsHttpHeaderProvider::AppendHeaders( if (!variation_ids_header_copy.empty()) { // Note that prior to M33 this header was named X-Chrome-Variations. - headers->SetHeaderIfMissing("X-Client-Data", - variation_ids_header_copy); + headers->SetHeaderIfMissing(kClientData, variation_ids_header_copy); } } @@ -267,4 +271,12 @@ bool VariationsHttpHeaderProvider::ShouldAppendHeaders(const GURL& url) { google_util::ALLOW_NON_STANDARD_PORTS); } +std::set<std::string> VariationsHttpHeaderProvider::GetVariationHeaderNames() + const { + std::set<std::string> headers; + headers.insert(kChromeUMAEnabled); + headers.insert(kClientData); + return headers; +} + } // namespace variations diff --git a/components/variations/net/variations_http_header_provider.h b/components/variations/net/variations_http_header_provider.h index 0a511cc..83fa546 100644 --- a/components/variations/net/variations_http_header_provider.h +++ b/components/variations/net/variations_http_header_provider.h @@ -54,6 +54,9 @@ class VariationsHttpHeaderProvider : public base::FieldTrialList::Observer, // with "t" it will be treated as a trigger experiment id. bool SetDefaultVariationIds(const std::string& variation_ids); + // Returns the HTTP header names which are added in this class. + std::set<std::string> GetVariationHeaderNames() const; + private: friend struct DefaultSingletonTraits<VariationsHttpHeaderProvider>; diff --git a/content/browser/service_worker/service_worker_context_wrapper.cc b/content/browser/service_worker/service_worker_context_wrapper.cc index 9170b24..298dc49 100644 --- a/content/browser/service_worker/service_worker_context_wrapper.cc +++ b/content/browser/service_worker/service_worker_context_wrapper.cc @@ -5,9 +5,14 @@ #include "content/browser/service_worker/service_worker_context_wrapper.h" #include <map> +#include <set> +#include <string> +#include <vector> #include "base/barrier_closure.h" +#include "base/bind.h" #include "base/files/file_path.h" +#include "base/lazy_instance.h" #include "base/logging.h" #include "base/threading/sequenced_worker_pool.h" #include "content/browser/fileapi/chrome_blob_storage_context.h" @@ -17,6 +22,7 @@ #include "content/browser/service_worker/service_worker_quota_client.h" #include "content/public/browser/browser_context.h" #include "content/public/browser/browser_thread.h" +#include "content/public/browser/service_worker_context.h" #include "net/url_request/url_request_context_getter.h" #include "storage/browser/blob/blob_storage_context.h" #include "storage/browser/quota/quota_manager_proxy.h" @@ -24,6 +30,27 @@ namespace content { +namespace { + +typedef std::set<std::string> HeaderNameSet; +base::LazyInstance<HeaderNameSet> g_excluded_header_name_set = + LAZY_INSTANCE_INITIALIZER; +} + +void ServiceWorkerContext::AddExcludedHeadersForFetchEvent( + const std::set<std::string>& header_names) { + DCHECK_CURRENTLY_ON(BrowserThread::IO); + g_excluded_header_name_set.Get().insert(header_names.begin(), + header_names.end()); +} + +bool ServiceWorkerContext::IsExcludedHeaderNameForFetchEvent( + const std::string& header_name) { + DCHECK_CURRENTLY_ON(BrowserThread::IO); + return g_excluded_header_name_set.Get().find(header_name) != + g_excluded_header_name_set.Get().end(); +} + ServiceWorkerContextWrapper::ServiceWorkerContextWrapper( BrowserContext* browser_context) : observer_list_( diff --git a/content/browser/service_worker/service_worker_url_request_job.cc b/content/browser/service_worker/service_worker_url_request_job.cc index a041426..0e6ef2b 100644 --- a/content/browser/service_worker/service_worker_url_request_job.cc +++ b/content/browser/service_worker/service_worker_url_request_job.cc @@ -18,6 +18,7 @@ #include "content/common/service_worker/service_worker_types.h" #include "content/public/browser/blob_handle.h" #include "content/public/browser/resource_request_info.h" +#include "content/public/browser/service_worker_context.h" #include "net/base/net_errors.h" #include "net/http/http_request_headers.h" #include "net/http/http_response_headers.h" @@ -30,17 +31,6 @@ namespace content { -namespace { - -// Keep in sync with kDevToolsRequestInitiator and -// kDevToolsEmulateNetworkConditionsClientId defined in -// devtools_network_transaction.cc and InspectorResourceAgent.cpp. -const char kDevToolsRequestInitiator[] = "X-DevTools-Request-Initiator"; -const char kDevToolsEmulateNetworkConditionsClientId[] = - "X-DevTools-Emulate-Network-Conditions-Client-Id"; - -} // namespace - ServiceWorkerURLRequestJob::ServiceWorkerURLRequestJob( net::URLRequest* request, net::NetworkDelegate* network_delegate, @@ -299,10 +289,8 @@ ServiceWorkerURLRequestJob::CreateFetchRequest() { request->method = request_->method(); const net::HttpRequestHeaders& headers = request_->extra_request_headers(); for (net::HttpRequestHeaders::Iterator it(headers); it.GetNext();) { - if (it.name() == kDevToolsRequestInitiator || - it.name() == kDevToolsEmulateNetworkConditionsClientId) { + if (ServiceWorkerContext::IsExcludedHeaderNameForFetchEvent(it.name())) continue; - } request->headers[it.name()] = it.value(); } request->blob_uuid = blob_uuid; diff --git a/content/public/browser/service_worker_context.h b/content/public/browser/service_worker_context.h index 594408a..94ab73f 100644 --- a/content/public/browser/service_worker_context.h +++ b/content/public/browser/service_worker_context.h @@ -5,6 +5,9 @@ #ifndef CONTENT_PUBLIC_BROWSER_SERVICE_WORKER_CONTEXT_H_ #define CONTENT_PUBLIC_BROWSER_SERVICE_WORKER_CONTEXT_H_ +#include <set> +#include <string> + #include "base/basictypes.h" #include "base/callback_forward.h" #include "content/public/browser/service_worker_usage_info.h" @@ -24,6 +27,15 @@ class ServiceWorkerContext { typedef base::Callback<void(const std::vector<ServiceWorkerUsageInfo>& usage_info)> GetUsageInfoCallback; + // Registers the header name which should not be passed to the ServiceWorker. + // Must be called from the IO thread. + CONTENT_EXPORT static void AddExcludedHeadersForFetchEvent( + const std::set<std::string>& header_names); + + // Returns true if the header name should not be passed to the ServiceWorker. + // Must be called from the IO thread. + static bool IsExcludedHeaderNameForFetchEvent(const std::string& header_name); + // Equivalent to calling navigator.serviceWorker.register(script_url, {scope: // pattern}) from a renderer, except that |pattern| is an absolute URL instead // of relative to some current origin. |callback| is passed true when the JS |