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

Side by Side Diff: content/browser/devtools/protocol/service_worker_handler.cc

Issue 1893313007: DevTools: Attach all Service Workers except for old redundant ones. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: incorporated pfeldman's comment Created 4 years, 8 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 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/devtools/protocol/service_worker_handler.h" 5 #include "content/browser/devtools/protocol/service_worker_handler.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/containers/scoped_ptr_hash_map.h" 8 #include "base/containers/scoped_ptr_hash_map.h"
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
(...skipping 23 matching lines...) Expand all
34 #endif 34 #endif
35 35
36 namespace content { 36 namespace content {
37 namespace devtools { 37 namespace devtools {
38 namespace service_worker { 38 namespace service_worker {
39 39
40 using Response = DevToolsProtocolClient::Response; 40 using Response = DevToolsProtocolClient::Response;
41 41
42 namespace { 42 namespace {
43 43
44 using ScopeAgentsMap =
45 std::map<GURL, std::unique_ptr<ServiceWorkerDevToolsAgentHost::List>>;
46
44 void ResultNoOp(bool success) { 47 void ResultNoOp(bool success) {
45 } 48 }
46 void StatusNoOp(ServiceWorkerStatusCode status) { 49 void StatusNoOp(ServiceWorkerStatusCode status) {
47 } 50 }
48 void PushDeliveryNoOp(PushDeliveryStatus status) { 51 void PushDeliveryNoOp(PushDeliveryStatus status) {
49 } 52 }
50 53
51 const std::string GetVersionRunningStatusString( 54 const std::string GetVersionRunningStatusString(
52 content::ServiceWorkerVersion::RunningStatus running_status) { 55 content::ServiceWorkerVersion::RunningStatus running_status) {
53 switch (running_status) { 56 switch (running_status) {
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 scoped_refptr<ServiceWorkerRegistration> registration( 133 scoped_refptr<ServiceWorkerRegistration> registration(
131 ServiceWorkerRegistration::Create() 134 ServiceWorkerRegistration::Create()
132 ->set_registration_id( 135 ->set_registration_id(
133 base::Int64ToString(registration_info.registration_id)) 136 base::Int64ToString(registration_info.registration_id))
134 ->set_scope_url(registration_info.pattern.spec()) 137 ->set_scope_url(registration_info.pattern.spec())
135 ->set_is_deleted(registration_info.delete_flag == 138 ->set_is_deleted(registration_info.delete_flag ==
136 ServiceWorkerRegistrationInfo::IS_DELETED)); 139 ServiceWorkerRegistrationInfo::IS_DELETED));
137 return registration; 140 return registration;
138 } 141 }
139 142
140 scoped_refptr<ServiceWorkerDevToolsAgentHost> GetMatchingServiceWorker( 143 void GetMatchingHostsByScopeMap(
141 const ServiceWorkerDevToolsAgentHost::List& agent_hosts, 144 const ServiceWorkerDevToolsAgentHost::List& agent_hosts,
142 const GURL& url) { 145 const std::set<GURL>& urls,
143 scoped_refptr<ServiceWorkerDevToolsAgentHost> best_host; 146 ScopeAgentsMap* scope_agents_map) {
144 bool best_host_scope_matched = false; 147 std::set<base::StringPiece> host_name_set;
145 int best_host_scope_length = 0; 148 for (const GURL& url : urls)
149 host_name_set.insert(url.host_piece());
150 for (const auto& host : agent_hosts) {
151 if (host_name_set.find(host->scope().host_piece()) == host_name_set.end())
152 continue;
153 const auto& it = scope_agents_map->find(host->scope());
154 if (it == scope_agents_map->end()) {
155 std::unique_ptr<ServiceWorkerDevToolsAgentHost::List> new_list(
156 new ServiceWorkerDevToolsAgentHost::List());
157 new_list->push_back(host);
158 (*scope_agents_map)[host->scope()] = std::move(new_list);
159 } else {
160 it->second->push_back(host);
161 }
162 }
163 }
146 164
147 for (auto host : agent_hosts) { 165 GURL GetBestMatchingScope(const ScopeAgentsMap& scope_agents_map,
148 if (host->GetURL().host_piece() != url.host_piece()) 166 const GURL& url) {
167 GURL best_scope;
168 bool best_scope_matched = false;
169 int best_scope_length = 0;
170
171 for (const auto& it : scope_agents_map) {
172 if (it.first.host_piece() != url.host_piece())
149 continue; 173 continue;
150 const bool scope_matched = 174 const bool scope_matched = ServiceWorkerUtils::ScopeMatches(it.first, url);
151 ServiceWorkerUtils::ScopeMatches(host->scope(), url); 175 const int scope_length = it.first.spec().length();
152 const int scope_length = host->scope().spec().length();
153 bool replace = false; 176 bool replace = false;
154 if (!best_host) 177 if (!best_scope.is_empty())
155 replace = true; 178 replace = true;
156 else if (best_host_scope_matched) 179 else if (best_scope_matched)
157 replace = scope_matched && scope_length >= best_host_scope_length; 180 replace = scope_matched && scope_length >= best_scope_length;
158 else 181 else
159 replace = scope_matched || scope_length >= best_host_scope_length; 182 replace = scope_matched || scope_length >= best_scope_length;
160 183
161 if (replace) { 184 if (replace) {
162 best_host = host; 185 best_scope = it.first;
163 best_host_scope_matched = scope_matched; 186 best_scope_matched = scope_matched;
164 best_host_scope_length = scope_length; 187 best_scope_length = scope_length;
165 } 188 }
166 } 189 }
167 return best_host; 190 return best_scope;
191 }
192
193 void AddEligibleHosts(const ServiceWorkerDevToolsAgentHost::List& list,
194 ServiceWorkerDevToolsAgentHost::Map* result) {
195 base::Time last_installed_time;
196 base::Time last_doomed_time;
197 for (const auto& host : list) {
198 if (host->version_installed_time() > last_installed_time)
199 last_installed_time = host->version_installed_time();
200 if (host->version_doomed_time() > last_doomed_time)
201 last_doomed_time = host->version_doomed_time();
202 }
203 for (const auto& host : list) {
204 // We don't attech old redundant Service Workers when there is newer
205 // installed Service Worker.
206 if (host->version_doomed_time().is_null() ||
207 (last_installed_time < last_doomed_time &&
208 last_doomed_time == host->version_doomed_time())) {
209 (*result)[host->GetId()] = host;
210 }
211 }
168 } 212 }
169 213
170 ServiceWorkerDevToolsAgentHost::Map GetMatchingServiceWorkers( 214 ServiceWorkerDevToolsAgentHost::Map GetMatchingServiceWorkers(
171 BrowserContext* browser_context, 215 BrowserContext* browser_context,
172 const std::set<GURL>& urls) { 216 const std::set<GURL>& urls) {
173 ServiceWorkerDevToolsAgentHost::Map result; 217 ServiceWorkerDevToolsAgentHost::Map result;
174 if (!browser_context) 218 if (!browser_context)
175 return result; 219 return result;
220
176 ServiceWorkerDevToolsAgentHost::List agent_hosts; 221 ServiceWorkerDevToolsAgentHost::List agent_hosts;
177 ServiceWorkerDevToolsManager::GetInstance() 222 ServiceWorkerDevToolsManager::GetInstance()
178 ->AddAllAgentHostsForBrowserContext(browser_context, &agent_hosts); 223 ->AddAllAgentHostsForBrowserContext(browser_context, &agent_hosts);
179 for (const GURL& url : urls) { 224
180 scoped_refptr<ServiceWorkerDevToolsAgentHost> host = 225 ScopeAgentsMap scope_agents_map;
181 GetMatchingServiceWorker(agent_hosts, url); 226 GetMatchingHostsByScopeMap(agent_hosts, urls, &scope_agents_map);
182 if (host) 227
183 result[host->GetId()] = host; 228 std::set<GURL> matching_scopes;
229 for (const GURL& url : urls)
230 matching_scopes.insert(GetBestMatchingScope(scope_agents_map, url));
231
232 for (const auto& it : scope_agents_map) {
233 if (matching_scopes.find(it.first) == matching_scopes.end())
234 continue;
235 AddEligibleHosts(*it.second.get(), &result);
184 } 236 }
185 return result; 237 return result;
186 } 238 }
187 239
188 void StopServiceWorkerOnIO(scoped_refptr<ServiceWorkerContextWrapper> context, 240 void StopServiceWorkerOnIO(scoped_refptr<ServiceWorkerContextWrapper> context,
189 int64_t version_id) { 241 int64_t version_id) {
190 if (content::ServiceWorkerVersion* version = 242 if (content::ServiceWorkerVersion* version =
191 context->GetLiveVersion(version_id)) { 243 context->GetLiveVersion(version_id)) {
192 version->StopWorker(base::Bind(&StatusNoOp)); 244 version->StopWorker(base::Bind(&StatusNoOp));
193 } 245 }
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after
552 ServiceWorkerDevToolsAgentHost* host) { 604 ServiceWorkerDevToolsAgentHost* host) {
553 if (ServiceWorkerDevToolsManager::GetInstance() 605 if (ServiceWorkerDevToolsManager::GetInstance()
554 ->debug_service_worker_on_start()) { 606 ->debug_service_worker_on_start()) {
555 // When debug_service_worker_on_start is true, a new DevTools window will 607 // When debug_service_worker_on_start is true, a new DevTools window will
556 // be opend in ServiceWorkerDevToolsManager::WorkerReadyForInspection. 608 // be opend in ServiceWorkerDevToolsManager::WorkerReadyForInspection.
557 return; 609 return;
558 } 610 }
559 UpdateHosts(); 611 UpdateHosts();
560 } 612 }
561 613
614 void ServiceWorkerHandler::WorkerVersionInstalled(
615 ServiceWorkerDevToolsAgentHost* host) {
616 UpdateHosts();
617 }
618 void ServiceWorkerHandler::WorkerVersionDoomed(
619 ServiceWorkerDevToolsAgentHost* host) {
620 UpdateHosts();
621 }
622
562 void ServiceWorkerHandler::WorkerDestroyed( 623 void ServiceWorkerHandler::WorkerDestroyed(
563 ServiceWorkerDevToolsAgentHost* host) { 624 ServiceWorkerDevToolsAgentHost* host) {
564 UpdateHosts(); 625 UpdateHosts();
565 } 626 }
566 627
567 void ServiceWorkerHandler::ReportWorkerCreated( 628 void ServiceWorkerHandler::ReportWorkerCreated(
568 ServiceWorkerDevToolsAgentHost* host) { 629 ServiceWorkerDevToolsAgentHost* host) {
569 if (host->IsAttached()) 630 if (host->IsAttached())
570 return; 631 return;
571 attached_hosts_[host->GetId()] = host; 632 attached_hosts_[host->GetId()] = host;
(...skipping 17 matching lines...) Expand all
589 } 650 }
590 651
591 void ServiceWorkerHandler::ClearForceUpdate() { 652 void ServiceWorkerHandler::ClearForceUpdate() {
592 if (context_) 653 if (context_)
593 context_->SetForceUpdateOnPageLoad(false); 654 context_->SetForceUpdateOnPageLoad(false);
594 } 655 }
595 656
596 } // namespace service_worker 657 } // namespace service_worker
597 } // namespace devtools 658 } // namespace devtools
598 } // namespace content 659 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698