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

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

Powered by Google App Engine
This is Rietveld 408576698