| 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 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 std::unique_ptr<ServiceWorkerDevToolsAgentHost::List> new_list( | 155 std::unique_ptr<ServiceWorkerDevToolsAgentHost::List> new_list( |
| 156 new ServiceWorkerDevToolsAgentHost::List()); | 156 new ServiceWorkerDevToolsAgentHost::List()); |
| 157 new_list->push_back(host); | 157 new_list->push_back(host); |
| 158 (*scope_agents_map)[host->scope()] = std::move(new_list); | 158 (*scope_agents_map)[host->scope()] = std::move(new_list); |
| 159 } else { | 159 } else { |
| 160 it->second->push_back(host); | 160 it->second->push_back(host); |
| 161 } | 161 } |
| 162 } | 162 } |
| 163 } | 163 } |
| 164 | 164 |
| 165 GURL GetBestMatchingScope(const ScopeAgentsMap& scope_agents_map, | |
| 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()) | |
| 173 continue; | |
| 174 const bool scope_matched = ServiceWorkerUtils::ScopeMatches(it.first, url); | |
| 175 const int scope_length = it.first.spec().length(); | |
| 176 bool replace = false; | |
| 177 if (!best_scope.is_empty()) | |
| 178 replace = true; | |
| 179 else if (best_scope_matched) | |
| 180 replace = scope_matched && scope_length >= best_scope_length; | |
| 181 else | |
| 182 replace = scope_matched || scope_length >= best_scope_length; | |
| 183 | |
| 184 if (replace) { | |
| 185 best_scope = it.first; | |
| 186 best_scope_matched = scope_matched; | |
| 187 best_scope_length = scope_length; | |
| 188 } | |
| 189 } | |
| 190 return best_scope; | |
| 191 } | |
| 192 | |
| 193 void AddEligibleHosts(const ServiceWorkerDevToolsAgentHost::List& list, | 165 void AddEligibleHosts(const ServiceWorkerDevToolsAgentHost::List& list, |
| 194 ServiceWorkerDevToolsAgentHost::Map* result) { | 166 ServiceWorkerDevToolsAgentHost::Map* result) { |
| 195 base::Time last_installed_time; | 167 base::Time last_installed_time; |
| 196 base::Time last_doomed_time; | 168 base::Time last_doomed_time; |
| 197 for (const auto& host : list) { | 169 for (const auto& host : list) { |
| 198 if (host->version_installed_time() > last_installed_time) | 170 if (host->version_installed_time() > last_installed_time) |
| 199 last_installed_time = host->version_installed_time(); | 171 last_installed_time = host->version_installed_time(); |
| 200 if (host->version_doomed_time() > last_doomed_time) | 172 if (host->version_doomed_time() > last_doomed_time) |
| 201 last_doomed_time = host->version_doomed_time(); | 173 last_doomed_time = host->version_doomed_time(); |
| 202 } | 174 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 218 if (!browser_context) | 190 if (!browser_context) |
| 219 return result; | 191 return result; |
| 220 | 192 |
| 221 ServiceWorkerDevToolsAgentHost::List agent_hosts; | 193 ServiceWorkerDevToolsAgentHost::List agent_hosts; |
| 222 ServiceWorkerDevToolsManager::GetInstance() | 194 ServiceWorkerDevToolsManager::GetInstance() |
| 223 ->AddAllAgentHostsForBrowserContext(browser_context, &agent_hosts); | 195 ->AddAllAgentHostsForBrowserContext(browser_context, &agent_hosts); |
| 224 | 196 |
| 225 ScopeAgentsMap scope_agents_map; | 197 ScopeAgentsMap scope_agents_map; |
| 226 GetMatchingHostsByScopeMap(agent_hosts, urls, &scope_agents_map); | 198 GetMatchingHostsByScopeMap(agent_hosts, urls, &scope_agents_map); |
| 227 | 199 |
| 228 std::set<GURL> matching_scopes; | 200 for (const auto& it : scope_agents_map) |
| 229 for (const GURL& url : urls) | 201 AddEligibleHosts(*it.second.get(), &result); |
| 230 matching_scopes.insert(GetBestMatchingScope(scope_agents_map, url)); | |
| 231 | 202 |
| 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); | |
| 236 } | |
| 237 return result; | 203 return result; |
| 238 } | 204 } |
| 239 | 205 |
| 240 void StopServiceWorkerOnIO(scoped_refptr<ServiceWorkerContextWrapper> context, | 206 void StopServiceWorkerOnIO(scoped_refptr<ServiceWorkerContextWrapper> context, |
| 241 int64_t version_id) { | 207 int64_t version_id) { |
| 242 if (content::ServiceWorkerVersion* version = | 208 if (content::ServiceWorkerVersion* version = |
| 243 context->GetLiveVersion(version_id)) { | 209 context->GetLiveVersion(version_id)) { |
| 244 version->StopWorker(base::Bind(&StatusNoOp)); | 210 version->StopWorker(base::Bind(&StatusNoOp)); |
| 245 } | 211 } |
| 246 } | 212 } |
| (...skipping 412 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 659 } | 625 } |
| 660 | 626 |
| 661 void ServiceWorkerHandler::ClearForceUpdate() { | 627 void ServiceWorkerHandler::ClearForceUpdate() { |
| 662 if (context_) | 628 if (context_) |
| 663 context_->SetForceUpdateOnPageLoad(false); | 629 context_->SetForceUpdateOnPageLoad(false); |
| 664 } | 630 } |
| 665 | 631 |
| 666 } // namespace service_worker | 632 } // namespace service_worker |
| 667 } // namespace devtools | 633 } // namespace devtools |
| 668 } // namespace content | 634 } // namespace content |
| OLD | NEW |