Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(813)

Side by Side Diff: content/browser/service_worker/link_header_support.cc

Issue 2771823002: Implement updateViaCache flag and no-cache by default for main service worker scripts
Patch Set: fix IPC Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/service_worker/link_header_support.h" 5 #include "content/browser/service_worker/link_header_support.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/strings/string_split.h" 8 #include "base/strings/string_split.h"
9 #include "base/strings/string_util.h" 9 #include "base/strings/string_util.h"
10 #include "base/time/time.h" 10 #include "base/time/time.h"
(...skipping 16 matching lines...) Expand all
27 namespace content { 27 namespace content {
28 28
29 namespace { 29 namespace {
30 30
31 void RegisterServiceWorkerFinished(int64_t trace_id, bool result) { 31 void RegisterServiceWorkerFinished(int64_t trace_id, bool result) {
32 TRACE_EVENT_ASYNC_END1("ServiceWorker", 32 TRACE_EVENT_ASYNC_END1("ServiceWorker",
33 "LinkHeaderResourceThrottle::HandleServiceWorkerLink", 33 "LinkHeaderResourceThrottle::HandleServiceWorkerLink",
34 trace_id, "Success", result); 34 trace_id, "Success", result);
35 } 35 }
36 36
37 blink::WebServiceWorkerUpdateViaCache ParseUpdateViaCache(
38 const std::unordered_map<std::string, base::Optional<std::string>>&
39 params) {
40 // https://html.spec.whatwg.org/multipage/semantics.html#attr-link-updateviaca che
41 auto cache_param = params.find("updateviacache");
42 if (cache_param == params.end())
43 return blink::WebServiceWorkerUpdateViaCache::kImports;
44 const std::string& cache = cache_param->second.value_or("");
45 if (base::EqualsCaseInsensitiveASCII(cache, "imports"))
46 return blink::WebServiceWorkerUpdateViaCache::kImports;
47 if (base::EqualsCaseInsensitiveASCII(cache, "all"))
48 return blink::WebServiceWorkerUpdateViaCache::kAll;
49 if (base::EqualsCaseInsensitiveASCII(cache, "none"))
50 return blink::WebServiceWorkerUpdateViaCache::kNone;
51 // Default value
52 return blink::WebServiceWorkerUpdateViaCache::kImports;
53 }
54
37 void HandleServiceWorkerLink( 55 void HandleServiceWorkerLink(
38 net::URLRequest* request, 56 net::URLRequest* request,
39 const std::string& url, 57 const std::string& url,
40 const std::unordered_map<std::string, base::Optional<std::string>>& params, 58 const std::unordered_map<std::string, base::Optional<std::string>>& params,
41 ServiceWorkerContextWrapper* service_worker_context_for_testing) { 59 ServiceWorkerContextWrapper* service_worker_context_for_testing) {
42 DCHECK_CURRENTLY_ON(BrowserThread::IO); 60 DCHECK_CURRENTLY_ON(BrowserThread::IO);
43 61
44 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( 62 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
45 switches::kEnableExperimentalWebPlatformFeatures) && 63 switches::kEnableExperimentalWebPlatformFeatures) &&
46 !TrialTokenValidator::RequestEnablesFeature(request, "ForeignFetch", 64 !TrialTokenValidator::RequestEnablesFeature(request, "ForeignFetch",
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 // should share logic with ServiceWorkerRequestHandler and 108 // should share logic with ServiceWorkerRequestHandler and
91 // ForeignFetchRequestHandler to limit the requests for which serviceworker 109 // ForeignFetchRequestHandler to limit the requests for which serviceworker
92 // links are processed. 110 // links are processed.
93 111
94 GURL context_url = request->url(); 112 GURL context_url = request->url();
95 GURL script_url = context_url.Resolve(url); 113 GURL script_url = context_url.Resolve(url);
96 auto scope_param = params.find("scope"); 114 auto scope_param = params.find("scope");
97 GURL scope_url = scope_param == params.end() 115 GURL scope_url = scope_param == params.end()
98 ? script_url.Resolve("./") 116 ? script_url.Resolve("./")
99 : context_url.Resolve(scope_param->second.value_or("")); 117 : context_url.Resolve(scope_param->second.value_or(""));
118 blink::WebServiceWorkerUpdateViaCache update_via_cache =
119 ParseUpdateViaCache(params);
120 if (update_via_cache == blink::WebServiceWorkerUpdateViaCache::kUnknown) {
121 return;
122 }
100 123
101 if (!context_url.is_valid() || !script_url.is_valid() || 124 if (!context_url.is_valid() || !script_url.is_valid() ||
102 !scope_url.is_valid()) 125 !scope_url.is_valid())
103 return; 126 return;
104 if (!ServiceWorkerUtils::AllOriginsMatchAndCanAccessServiceWorkers( 127 if (!ServiceWorkerUtils::AllOriginsMatchAndCanAccessServiceWorkers(
105 {context_url, scope_url, script_url})) { 128 {context_url, scope_url, script_url})) {
106 return; 129 return;
107 } 130 }
108 std::string error; 131 std::string error;
109 if (ServiceWorkerUtils::ContainsDisallowedCharacter(scope_url, script_url, 132 if (ServiceWorkerUtils::ContainsDisallowedCharacter(scope_url, script_url,
110 &error)) 133 &error))
111 return; 134 return;
112 135
113 if (!GetContentClient()->browser()->AllowServiceWorker( 136 if (!GetContentClient()->browser()->AllowServiceWorker(
114 scope_url, request->first_party_for_cookies(), 137 scope_url, request->first_party_for_cookies(),
115 request_info->GetContext(), 138 request_info->GetContext(),
116 request_info->GetWebContentsGetterForRequest())) 139 request_info->GetWebContentsGetterForRequest()))
117 return; 140 return;
118 141
119 static int64_t trace_id = 0; 142 static int64_t trace_id = 0;
120 TRACE_EVENT_ASYNC_BEGIN2( 143 TRACE_EVENT_ASYNC_BEGIN2(
121 "ServiceWorker", "LinkHeaderResourceThrottle::HandleServiceWorkerLink", 144 "ServiceWorker", "LinkHeaderResourceThrottle::HandleServiceWorkerLink",
122 ++trace_id, "Pattern", scope_url.spec(), "Script URL", script_url.spec()); 145 ++trace_id, "Pattern", scope_url.spec(), "Script URL", script_url.spec());
123 service_worker_context->RegisterServiceWorker( 146 service_worker_context->RegisterServiceWorker(
124 scope_url, script_url, 147 scope_url, script_url, update_via_cache,
125 base::Bind(&RegisterServiceWorkerFinished, trace_id)); 148 base::Bind(&RegisterServiceWorkerFinished, trace_id));
126 } 149 }
127 150
128 void ProcessLinkHeaderValueForRequest( 151 void ProcessLinkHeaderValueForRequest(
129 net::URLRequest* request, 152 net::URLRequest* request,
130 std::string::const_iterator value_begin, 153 std::string::const_iterator value_begin,
131 std::string::const_iterator value_end, 154 std::string::const_iterator value_end,
132 ServiceWorkerContextWrapper* service_worker_context_for_testing) { 155 ServiceWorkerContextWrapper* service_worker_context_for_testing) {
133 DCHECK_CURRENTLY_ON(BrowserThread::IO); 156 DCHECK_CURRENTLY_ON(BrowserThread::IO);
134 157
(...skipping 30 matching lines...) Expand all
165 net::URLRequest* request, 188 net::URLRequest* request,
166 const std::string& link_header, 189 const std::string& link_header,
167 ServiceWorkerContextWrapper* service_worker_context_for_testing) { 190 ServiceWorkerContextWrapper* service_worker_context_for_testing) {
168 for (const auto& value : link_header_util::SplitLinkHeader(link_header)) { 191 for (const auto& value : link_header_util::SplitLinkHeader(link_header)) {
169 ProcessLinkHeaderValueForRequest(request, value.first, value.second, 192 ProcessLinkHeaderValueForRequest(request, value.first, value.second,
170 service_worker_context_for_testing); 193 service_worker_context_for_testing);
171 } 194 }
172 } 195 }
173 196
174 } // namespace content 197 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698