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

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

Issue 2481093003: Introduce ResourceRequesterInfo to abstract the requester of resource request (Closed)
Patch Set: make ResourceRequestInfoImpl::requester_info() non-const Created 4 years, 1 month 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 16 matching lines...) Expand all
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 void HandleServiceWorkerLink( 36 void HandleServiceWorkerLink(
37 const net::URLRequest* request, 37 net::URLRequest* request,
38 const std::string& url, 38 const std::string& url,
39 const std::unordered_map<std::string, base::Optional<std::string>>& params, 39 const std::unordered_map<std::string, base::Optional<std::string>>& params,
40 ServiceWorkerContextWrapper* service_worker_context_for_testing) { 40 ServiceWorkerContextWrapper* service_worker_context_for_testing) {
41 DCHECK_CURRENTLY_ON(BrowserThread::IO); 41 DCHECK_CURRENTLY_ON(BrowserThread::IO);
42 42
43 if (!base::CommandLine::ForCurrentProcess()->HasSwitch( 43 if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
44 switches::kEnableExperimentalWebPlatformFeatures) && 44 switches::kEnableExperimentalWebPlatformFeatures) &&
45 !TrialTokenValidator::RequestEnablesFeature(request, "ForeignFetch")) { 45 !TrialTokenValidator::RequestEnablesFeature(request, "ForeignFetch")) {
46 // TODO(mek): Log attempt to use without having correct token? 46 // TODO(mek): Log attempt to use without having correct token?
47 return; 47 return;
48 } 48 }
49 49
50 if (ContainsKey(params, "anchor")) 50 if (ContainsKey(params, "anchor"))
51 return; 51 return;
52 52
53 const ResourceRequestInfoImpl* request_info = 53 ResourceRequestInfoImpl* request_info =
54 ResourceRequestInfoImpl::ForRequest(request); 54 ResourceRequestInfoImpl::ForRequest(request);
55 ResourceMessageFilter* filter = request_info->filter();
56 ServiceWorkerContext* service_worker_context = 55 ServiceWorkerContext* service_worker_context =
57 filter ? filter->service_worker_context() 56 service_worker_context_for_testing
58 : service_worker_context_for_testing; 57 ? service_worker_context_for_testing
59 if (IsBrowserSideNavigationEnabled() && 58 : request_info->requester_info().service_worker_context();
60 ServiceWorkerUtils::IsMainResourceType(request_info->GetResourceType()) &&
61 !service_worker_context) {
62 service_worker_context = request_info->service_worker_context();
63 }
64 59
65 if (!service_worker_context) 60 if (!service_worker_context)
66 return; 61 return;
67 62
68 ServiceWorkerProviderHost* provider_host = 63 ServiceWorkerProviderHost* provider_host =
69 ServiceWorkerRequestHandler::GetProviderHost(request); 64 ServiceWorkerRequestHandler::GetProviderHost(request);
70 65
71 // If fetched from a service worker, make sure fetching service worker is 66 // If fetched from a service worker, make sure fetching service worker is
72 // controlling at least one client to prevent a service worker from spawning 67 // controlling at least one client to prevent a service worker from spawning
73 // new service workers in the background. 68 // new service workers in the background.
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 static int64_t trace_id = 0; 121 static int64_t trace_id = 0;
127 TRACE_EVENT_ASYNC_BEGIN2( 122 TRACE_EVENT_ASYNC_BEGIN2(
128 "ServiceWorker", "LinkHeaderResourceThrottle::HandleServiceWorkerLink", 123 "ServiceWorker", "LinkHeaderResourceThrottle::HandleServiceWorkerLink",
129 ++trace_id, "Pattern", scope_url.spec(), "Script URL", script_url.spec()); 124 ++trace_id, "Pattern", scope_url.spec(), "Script URL", script_url.spec());
130 service_worker_context->RegisterServiceWorker( 125 service_worker_context->RegisterServiceWorker(
131 scope_url, script_url, 126 scope_url, script_url,
132 base::Bind(&RegisterServiceWorkerFinished, trace_id)); 127 base::Bind(&RegisterServiceWorkerFinished, trace_id));
133 } 128 }
134 129
135 void ProcessLinkHeaderValueForRequest( 130 void ProcessLinkHeaderValueForRequest(
136 const net::URLRequest* request, 131 net::URLRequest* request,
137 std::string::const_iterator value_begin, 132 std::string::const_iterator value_begin,
138 std::string::const_iterator value_end, 133 std::string::const_iterator value_end,
139 ServiceWorkerContextWrapper* service_worker_context_for_testing) { 134 ServiceWorkerContextWrapper* service_worker_context_for_testing) {
140 DCHECK_CURRENTLY_ON(BrowserThread::IO); 135 DCHECK_CURRENTLY_ON(BrowserThread::IO);
141 136
142 std::string url; 137 std::string url;
143 std::unordered_map<std::string, base::Optional<std::string>> params; 138 std::unordered_map<std::string, base::Optional<std::string>> params;
144 if (!link_header_util::ParseLinkHeaderValue(value_begin, value_end, &url, 139 if (!link_header_util::ParseLinkHeaderValue(value_begin, value_end, &url,
145 &params)) 140 &params))
146 return; 141 return;
147 142
148 auto rel_param = params.find("rel"); 143 auto rel_param = params.find("rel");
149 if (rel_param == params.end() || !rel_param->second) 144 if (rel_param == params.end() || !rel_param->second)
150 return; 145 return;
151 for (const auto& rel : base::SplitStringPiece(rel_param->second.value(), 146 for (const auto& rel : base::SplitStringPiece(rel_param->second.value(),
152 HTTP_LWS, base::TRIM_WHITESPACE, 147 HTTP_LWS, base::TRIM_WHITESPACE,
153 base::SPLIT_WANT_NONEMPTY)) { 148 base::SPLIT_WANT_NONEMPTY)) {
154 if (base::EqualsCaseInsensitiveASCII(rel, "serviceworker")) 149 if (base::EqualsCaseInsensitiveASCII(rel, "serviceworker"))
155 HandleServiceWorkerLink(request, url, params, 150 HandleServiceWorkerLink(request, url, params,
156 service_worker_context_for_testing); 151 service_worker_context_for_testing);
157 } 152 }
158 } 153 }
159 154
160 } // namespace 155 } // namespace
161 156
162 void ProcessRequestForLinkHeaders(const net::URLRequest* request) { 157 void ProcessRequestForLinkHeaders(net::URLRequest* request) {
163 std::string link_header; 158 std::string link_header;
164 request->GetResponseHeaderByName("link", &link_header); 159 request->GetResponseHeaderByName("link", &link_header);
165 if (link_header.empty()) 160 if (link_header.empty())
166 return; 161 return;
167 162
168 ProcessLinkHeaderForRequest(request, link_header); 163 ProcessLinkHeaderForRequest(request, link_header);
169 } 164 }
170 165
171 void ProcessLinkHeaderForRequest( 166 void ProcessLinkHeaderForRequest(
172 const net::URLRequest* request, 167 net::URLRequest* request,
173 const std::string& link_header, 168 const std::string& link_header,
174 ServiceWorkerContextWrapper* service_worker_context_for_testing) { 169 ServiceWorkerContextWrapper* service_worker_context_for_testing) {
175 for (const auto& value : link_header_util::SplitLinkHeader(link_header)) { 170 for (const auto& value : link_header_util::SplitLinkHeader(link_header)) {
176 ProcessLinkHeaderValueForRequest(request, value.first, value.second, 171 ProcessLinkHeaderValueForRequest(request, value.first, value.second,
177 service_worker_context_for_testing); 172 service_worker_context_for_testing);
178 } 173 }
179 } 174 }
180 175
181 } // namespace content 176 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698