| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/service_worker_utils.h" | 5 #include "content/browser/service_worker/service_worker_utils.h" |
| 6 | 6 |
| 7 #include <string> |
| 8 |
| 7 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 8 #include "base/logging.h" | 10 #include "base/logging.h" |
| 9 #include "content/public/common/content_switches.h" | 11 #include "content/public/common/content_switches.h" |
| 12 #include "url/gurl.h" |
| 10 | 13 |
| 11 namespace content { | 14 namespace content { |
| 12 | 15 |
| 13 // static | 16 // static |
| 14 bool ServiceWorkerUtils::IsFeatureEnabled() { | 17 bool ServiceWorkerUtils::IsFeatureEnabled() { |
| 15 static bool enabled = CommandLine::ForCurrentProcess()->HasSwitch( | 18 static bool enabled = CommandLine::ForCurrentProcess()->HasSwitch( |
| 16 switches::kEnableServiceWorker); | 19 switches::kEnableServiceWorker); |
| 17 return enabled; | 20 return enabled; |
| 18 } | 21 } |
| 19 | 22 |
| 20 // static | 23 // static |
| 21 bool ServiceWorkerUtils::ScopeMatches(const GURL& scope, const GURL& url) { | 24 bool ServiceWorkerUtils::ScopeMatches(const GURL& scope, const GURL& url) { |
| 22 DCHECK(!scope.has_ref()); | 25 DCHECK(!scope.has_ref()); |
| 23 DCHECK(!url.has_ref()); | 26 DCHECK(!url.has_ref()); |
| 24 const std::string& scope_spec = scope.spec(); | 27 const std::string& scope_spec = scope.spec(); |
| 25 const std::string& url_spec = url.spec(); | 28 const std::string& url_spec = url.spec(); |
| 26 | 29 |
| 27 size_t len = scope_spec.size(); | 30 size_t len = scope_spec.size(); |
| 28 if (len > 0 && scope_spec[len - 1] == '*') | 31 if (len > 0 && scope_spec[len - 1] == '*') |
| 29 return scope_spec.compare(0, len - 1, url_spec, 0, len - 1) == 0; | 32 return scope_spec.compare(0, len - 1, url_spec, 0, len - 1) == 0; |
| 30 return scope_spec == url_spec; | 33 return scope_spec == url_spec; |
| 31 } | 34 } |
| 32 | 35 |
| 36 bool ServiceWorkerUtils::FindLongestScopeMatch( |
| 37 const std::vector<GURL>& scopes, const GURL& url, size_t* pos) { |
| 38 DCHECK(!url.has_ref()); |
| 39 DCHECK(pos); |
| 40 |
| 41 bool found = false; |
| 42 for (size_t i = 0; i < scopes.size(); ++i) { |
| 43 if (!ScopeMatches(scopes[i], url)) |
| 44 continue; |
| 45 if (!found || scopes.at(*pos) < scopes.at(i)) { |
| 46 *pos = i; |
| 47 found = true; |
| 48 } |
| 49 } |
| 50 return found; |
| 51 } |
| 52 |
| 33 } // namespace content | 53 } // namespace content |
| OLD | NEW |