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

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: Move API change to another patch and address comments Created 3 years, 5 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 "components/link_header_util/link_header_util.h" 10 #include "components/link_header_util/link_header_util.h"
(...skipping 15 matching lines...) Expand all
26 namespace content { 26 namespace content {
27 27
28 namespace { 28 namespace {
29 29
30 void RegisterServiceWorkerFinished(int64_t trace_id, bool result) { 30 void RegisterServiceWorkerFinished(int64_t trace_id, bool result) {
31 TRACE_EVENT_ASYNC_END1("ServiceWorker", 31 TRACE_EVENT_ASYNC_END1("ServiceWorker",
32 "LinkHeaderResourceThrottle::HandleServiceWorkerLink", 32 "LinkHeaderResourceThrottle::HandleServiceWorkerLink",
33 trace_id, "Success", result); 33 trace_id, "Success", result);
34 } 34 }
35 35
36 blink::WebServiceWorkerUpdateViaCache ParseUpdateViaCache(
37 const std::unordered_map<std::string, base::Optional<std::string>>&
38 params) {
39 // https://html.spec.whatwg.org/multipage/semantics.html#attr-link-updateviaca che
40 auto cache_param = params.find("updateviacache");
41 if (cache_param == params.end())
42 return blink::WebServiceWorkerUpdateViaCache::kImports;
43 const std::string& cache = cache_param->second.value_or("");
44 if (base::EqualsCaseInsensitiveASCII(cache, "imports"))
45 return blink::WebServiceWorkerUpdateViaCache::kImports;
46 if (base::EqualsCaseInsensitiveASCII(cache, "none"))
47 return blink::WebServiceWorkerUpdateViaCache::kNone;
48 if (base::EqualsCaseInsensitiveASCII(cache, "all"))
49 return blink::WebServiceWorkerUpdateViaCache::kAll;
50 return blink::WebServiceWorkerUpdateViaCache::kUnknown;
51 }
52
36 void HandleServiceWorkerLink( 53 void HandleServiceWorkerLink(
37 net::URLRequest* request, 54 net::URLRequest* request,
38 const std::string& url, 55 const std::string& url,
39 const std::unordered_map<std::string, base::Optional<std::string>>& params, 56 const std::unordered_map<std::string, base::Optional<std::string>>& params,
40 ServiceWorkerContextWrapper* service_worker_context_for_testing) { 57 ServiceWorkerContextWrapper* service_worker_context_for_testing) {
41 DCHECK_CURRENTLY_ON(BrowserThread::IO); 58 DCHECK_CURRENTLY_ON(BrowserThread::IO);
42 59
43 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( 60 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
44 switches::kEnableExperimentalWebPlatformFeatures) && 61 switches::kEnableExperimentalWebPlatformFeatures) &&
45 !TrialTokenValidator::RequestEnablesFeature(request, "ForeignFetch")) { 62 !TrialTokenValidator::RequestEnablesFeature(request, "ForeignFetch")) {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 // should share logic with ServiceWorkerRequestHandler and 105 // should share logic with ServiceWorkerRequestHandler and
89 // ForeignFetchRequestHandler to limit the requests for which serviceworker 106 // ForeignFetchRequestHandler to limit the requests for which serviceworker
90 // links are processed. 107 // links are processed.
91 108
92 GURL context_url = request->url(); 109 GURL context_url = request->url();
93 GURL script_url = context_url.Resolve(url); 110 GURL script_url = context_url.Resolve(url);
94 auto scope_param = params.find("scope"); 111 auto scope_param = params.find("scope");
95 GURL scope_url = scope_param == params.end() 112 GURL scope_url = scope_param == params.end()
96 ? script_url.Resolve("./") 113 ? script_url.Resolve("./")
97 : context_url.Resolve(scope_param->second.value_or("")); 114 : context_url.Resolve(scope_param->second.value_or(""));
115 blink::WebServiceWorkerUpdateViaCache update_via_cache =
116 ParseUpdateViaCache(params);
117 if (update_via_cache == blink::WebServiceWorkerUpdateViaCache::kUnknown) {
118 return;
119 }
98 120
99 if (!context_url.is_valid() || !script_url.is_valid() || 121 if (!context_url.is_valid() || !script_url.is_valid() ||
100 !scope_url.is_valid()) 122 !scope_url.is_valid())
101 return; 123 return;
102 if (!ServiceWorkerUtils::AllOriginsMatchAndCanAccessServiceWorkers( 124 if (!ServiceWorkerUtils::AllOriginsMatchAndCanAccessServiceWorkers(
103 {context_url, scope_url, script_url})) { 125 {context_url, scope_url, script_url})) {
104 return; 126 return;
105 } 127 }
106 std::string error; 128 std::string error;
107 if (ServiceWorkerUtils::ContainsDisallowedCharacter(scope_url, script_url, 129 if (ServiceWorkerUtils::ContainsDisallowedCharacter(scope_url, script_url,
108 &error)) 130 &error))
109 return; 131 return;
110 132
111 if (!GetContentClient()->browser()->AllowServiceWorker( 133 if (!GetContentClient()->browser()->AllowServiceWorker(
112 scope_url, request->first_party_for_cookies(), 134 scope_url, request->first_party_for_cookies(),
113 request_info->GetContext(), 135 request_info->GetContext(),
114 request_info->GetWebContentsGetterForRequest())) 136 request_info->GetWebContentsGetterForRequest()))
115 return; 137 return;
116 138
117 static int64_t trace_id = 0; 139 static int64_t trace_id = 0;
118 TRACE_EVENT_ASYNC_BEGIN2( 140 TRACE_EVENT_ASYNC_BEGIN2(
119 "ServiceWorker", "LinkHeaderResourceThrottle::HandleServiceWorkerLink", 141 "ServiceWorker", "LinkHeaderResourceThrottle::HandleServiceWorkerLink",
120 ++trace_id, "Pattern", scope_url.spec(), "Script URL", script_url.spec()); 142 ++trace_id, "Pattern", scope_url.spec(), "Script URL", script_url.spec());
121 service_worker_context->RegisterServiceWorker( 143 service_worker_context->RegisterServiceWorker(
122 scope_url, script_url, 144 scope_url, script_url, update_via_cache,
123 base::Bind(&RegisterServiceWorkerFinished, trace_id)); 145 base::Bind(&RegisterServiceWorkerFinished, trace_id));
124 } 146 }
125 147
126 void ProcessLinkHeaderValueForRequest( 148 void ProcessLinkHeaderValueForRequest(
127 net::URLRequest* request, 149 net::URLRequest* request,
128 std::string::const_iterator value_begin, 150 std::string::const_iterator value_begin,
129 std::string::const_iterator value_end, 151 std::string::const_iterator value_end,
130 ServiceWorkerContextWrapper* service_worker_context_for_testing) { 152 ServiceWorkerContextWrapper* service_worker_context_for_testing) {
131 DCHECK_CURRENTLY_ON(BrowserThread::IO); 153 DCHECK_CURRENTLY_ON(BrowserThread::IO);
132 154
(...skipping 30 matching lines...) Expand all
163 net::URLRequest* request, 185 net::URLRequest* request,
164 const std::string& link_header, 186 const std::string& link_header,
165 ServiceWorkerContextWrapper* service_worker_context_for_testing) { 187 ServiceWorkerContextWrapper* service_worker_context_for_testing) {
166 for (const auto& value : link_header_util::SplitLinkHeader(link_header)) { 188 for (const auto& value : link_header_util::SplitLinkHeader(link_header)) {
167 ProcessLinkHeaderValueForRequest(request, value.first, value.second, 189 ProcessLinkHeaderValueForRequest(request, value.first, value.second,
168 service_worker_context_for_testing); 190 service_worker_context_for_testing);
169 } 191 }
170 } 192 }
171 193
172 } // namespace content 194 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698