Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 Loading... | |
| 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 GetHostnameMatchingScopeAgentsMap( |
|
pfeldman
2016/04/21 21:13:20
GetHostname seems to be a bad prefix - you don't r
horo
2016/04/21 23:11:21
Done.
| |
| 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) { |
| 149 continue; | 167 GURL best_scope; |
| 150 const bool scope_matched = | 168 bool best_scope_matched = false; |
| 151 ServiceWorkerUtils::ScopeMatches(host->scope(), url); | 169 int best_scope_length = 0; |
| 152 const int scope_length = host->scope().spec().length(); | 170 |
| 171 for (const auto& it : scope_agents_map) { | |
| 172 const bool scope_matched = ServiceWorkerUtils::ScopeMatches(it.first, url); | |
|
pfeldman
2016/04/21 21:13:20
Is there a reason to proceed with the hosts that d
horo
2016/04/21 23:11:21
Done.
| |
| 173 const int scope_length = it.first.spec().length(); | |
| 153 bool replace = false; | 174 bool replace = false; |
| 154 if (!best_host) | 175 if (!best_scope.is_empty()) |
| 155 replace = true; | 176 replace = true; |
| 156 else if (best_host_scope_matched) | 177 else if (best_scope_matched) |
| 157 replace = scope_matched && scope_length >= best_host_scope_length; | 178 replace = scope_matched && scope_length >= best_scope_length; |
| 158 else | 179 else |
| 159 replace = scope_matched || scope_length >= best_host_scope_length; | 180 replace = scope_matched || scope_length >= best_scope_length; |
| 160 | 181 |
| 161 if (replace) { | 182 if (replace) { |
| 162 best_host = host; | 183 best_scope = it.first; |
| 163 best_host_scope_matched = scope_matched; | 184 best_scope_matched = scope_matched; |
| 164 best_host_scope_length = scope_length; | 185 best_scope_length = scope_length; |
| 165 } | 186 } |
| 166 } | 187 } |
| 167 return best_host; | 188 return best_scope; |
| 189 } | |
| 190 | |
| 191 void AddEligibleHosts(const ServiceWorkerDevToolsAgentHost::List& list, | |
| 192 ServiceWorkerDevToolsAgentHost::Map* result) { | |
| 193 base::Time last_installed_time; | |
| 194 base::Time last_doomed_time; | |
| 195 for (const auto& host : list) { | |
| 196 if (host->version_installed_time() > last_installed_time) | |
| 197 last_installed_time = host->version_installed_time(); | |
| 198 if (host->version_doomed_time() > last_doomed_time) | |
| 199 last_doomed_time = host->version_doomed_time(); | |
| 200 } | |
| 201 for (const auto& host : list) { | |
| 202 // We don't attech old redundant Service Workers when there is newer | |
| 203 // installed Service Worker. | |
| 204 if (host->version_doomed_time().is_null() || | |
| 205 (last_installed_time < last_doomed_time && | |
| 206 last_doomed_time == host->version_doomed_time())) { | |
| 207 (*result)[host->GetId()] = host; | |
| 208 } | |
| 209 } | |
| 168 } | 210 } |
| 169 | 211 |
| 170 ServiceWorkerDevToolsAgentHost::Map GetMatchingServiceWorkers( | 212 ServiceWorkerDevToolsAgentHost::Map GetMatchingServiceWorkers( |
| 171 BrowserContext* browser_context, | 213 BrowserContext* browser_context, |
| 172 const std::set<GURL>& urls) { | 214 const std::set<GURL>& urls) { |
| 173 ServiceWorkerDevToolsAgentHost::Map result; | 215 ServiceWorkerDevToolsAgentHost::Map result; |
| 174 if (!browser_context) | 216 if (!browser_context) |
| 175 return result; | 217 return result; |
| 218 | |
| 176 ServiceWorkerDevToolsAgentHost::List agent_hosts; | 219 ServiceWorkerDevToolsAgentHost::List agent_hosts; |
| 177 ServiceWorkerDevToolsManager::GetInstance() | 220 ServiceWorkerDevToolsManager::GetInstance() |
| 178 ->AddAllAgentHostsForBrowserContext(browser_context, &agent_hosts); | 221 ->AddAllAgentHostsForBrowserContext(browser_context, &agent_hosts); |
| 179 for (const GURL& url : urls) { | 222 |
| 180 scoped_refptr<ServiceWorkerDevToolsAgentHost> host = | 223 ScopeAgentsMap scope_agents_map; |
| 181 GetMatchingServiceWorker(agent_hosts, url); | 224 GetHostnameMatchingScopeAgentsMap(agent_hosts, urls, &scope_agents_map); |
| 182 if (host) | 225 |
| 183 result[host->GetId()] = host; | 226 std::set<GURL> matching_scopes; |
| 227 for (const GURL& url : urls) | |
| 228 matching_scopes.insert(GetBestMatchingScope(scope_agents_map, url)); | |
| 229 | |
| 230 for (const auto& it : scope_agents_map) { | |
| 231 if (matching_scopes.find(it.first) == matching_scopes.end()) | |
| 232 continue; | |
| 233 AddEligibleHosts(*it.second.get(), &result); | |
| 184 } | 234 } |
| 185 return result; | 235 return result; |
| 186 } | 236 } |
| 187 | 237 |
| 188 void StopServiceWorkerOnIO(scoped_refptr<ServiceWorkerContextWrapper> context, | 238 void StopServiceWorkerOnIO(scoped_refptr<ServiceWorkerContextWrapper> context, |
| 189 int64_t version_id) { | 239 int64_t version_id) { |
| 190 if (content::ServiceWorkerVersion* version = | 240 if (content::ServiceWorkerVersion* version = |
| 191 context->GetLiveVersion(version_id)) { | 241 context->GetLiveVersion(version_id)) { |
| 192 version->StopWorker(base::Bind(&StatusNoOp)); | 242 version->StopWorker(base::Bind(&StatusNoOp)); |
| 193 } | 243 } |
| (...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 552 ServiceWorkerDevToolsAgentHost* host) { | 602 ServiceWorkerDevToolsAgentHost* host) { |
| 553 if (ServiceWorkerDevToolsManager::GetInstance() | 603 if (ServiceWorkerDevToolsManager::GetInstance() |
| 554 ->debug_service_worker_on_start()) { | 604 ->debug_service_worker_on_start()) { |
| 555 // When debug_service_worker_on_start is true, a new DevTools window will | 605 // When debug_service_worker_on_start is true, a new DevTools window will |
| 556 // be opend in ServiceWorkerDevToolsManager::WorkerReadyForInspection. | 606 // be opend in ServiceWorkerDevToolsManager::WorkerReadyForInspection. |
| 557 return; | 607 return; |
| 558 } | 608 } |
| 559 UpdateHosts(); | 609 UpdateHosts(); |
| 560 } | 610 } |
| 561 | 611 |
| 612 void ServiceWorkerHandler::WorkerVersionInstalled( | |
| 613 ServiceWorkerDevToolsAgentHost* host) { | |
| 614 UpdateHosts(); | |
| 615 } | |
| 616 void ServiceWorkerHandler::WorkerVersionDoomed( | |
| 617 ServiceWorkerDevToolsAgentHost* host) { | |
| 618 UpdateHosts(); | |
| 619 } | |
| 620 | |
| 562 void ServiceWorkerHandler::WorkerDestroyed( | 621 void ServiceWorkerHandler::WorkerDestroyed( |
| 563 ServiceWorkerDevToolsAgentHost* host) { | 622 ServiceWorkerDevToolsAgentHost* host) { |
| 564 UpdateHosts(); | 623 UpdateHosts(); |
| 565 } | 624 } |
| 566 | 625 |
| 567 void ServiceWorkerHandler::ReportWorkerCreated( | 626 void ServiceWorkerHandler::ReportWorkerCreated( |
| 568 ServiceWorkerDevToolsAgentHost* host) { | 627 ServiceWorkerDevToolsAgentHost* host) { |
| 569 if (host->IsAttached()) | 628 if (host->IsAttached()) |
| 570 return; | 629 return; |
| 571 attached_hosts_[host->GetId()] = host; | 630 attached_hosts_[host->GetId()] = host; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 589 } | 648 } |
| 590 | 649 |
| 591 void ServiceWorkerHandler::ClearForceUpdate() { | 650 void ServiceWorkerHandler::ClearForceUpdate() { |
| 592 if (context_) | 651 if (context_) |
| 593 context_->SetForceUpdateOnPageLoad(false); | 652 context_->SetForceUpdateOnPageLoad(false); |
| 594 } | 653 } |
| 595 | 654 |
| 596 } // namespace service_worker | 655 } // namespace service_worker |
| 597 } // namespace devtools | 656 } // namespace devtools |
| 598 } // namespace content | 657 } // namespace content |
| OLD | NEW |