Chromium Code Reviews| Index: content/browser/service_worker/service_worker_utils.cc |
| diff --git a/content/browser/service_worker/service_worker_utils.cc b/content/browser/service_worker/service_worker_utils.cc |
| index 88f56438a1d452b26823f9183ad8a6276b66bc46..872c54f24c5ff9ed0a649a569fb38e61b1ab4edf 100644 |
| --- a/content/browser/service_worker/service_worker_utils.cc |
| +++ b/content/browser/service_worker/service_worker_utils.cc |
| @@ -4,6 +4,8 @@ |
| #include "content/browser/service_worker/service_worker_utils.h" |
| +#include <string> |
| + |
| #include "base/command_line.h" |
| #include "base/logging.h" |
| #include "content/public/common/content_switches.h" |
| @@ -11,6 +13,16 @@ |
| namespace content { |
| +namespace { |
| + |
| +bool ContainsWildcard(const GURL& scope) { |
| + DCHECK(!scope.is_empty()); |
| + const std::string& scope_spec = scope.spec(); |
| + return scope_spec[scope_spec.size() - 1] == '*'; |
| +} |
| + |
| +} // namespace |
| + |
| // static |
| bool ServiceWorkerUtils::IsFeatureEnabled() { |
| static bool enabled = CommandLine::ForCurrentProcess()->HasSwitch( |
| @@ -19,17 +31,40 @@ bool ServiceWorkerUtils::IsFeatureEnabled() { |
| } |
| // static |
| -bool ServiceWorkerUtils::ScopeMatches(const GURL& scope, const GURL& url) { |
| +ServiceWorkerUtils::MatchResult ServiceWorkerUtils::ScopeMatches( |
| + const GURL& scope, const GURL& url) { |
| DCHECK(!scope.has_ref()); |
| DCHECK(!url.has_ref()); |
| const std::string& scope_spec = scope.spec(); |
| - const std::string& url_spec = url.spec(); |
| - |
| + if (scope_spec == url.spec()) |
| + return SCOPE_EXACT_MATCH; |
| size_t len = scope_spec.size(); |
| - if (len > 0 && scope_spec[len - 1] == '*') { |
| - return scope_spec.compare(0, len - 1, url_spec, 0, len - 1) == 0; |
| + if (len > 0 && ContainsWildcard(scope) && |
| + scope_spec.compare(0, len - 1, url.spec(), 0, len - 1) == 0) { |
| + return SCOPE_WILDCARD_MATCH; |
| } |
| - return scope_spec == url_spec; |
| + return SCOPE_NO_MATCH; |
| +} |
| + |
| +// static |
| +int ServiceWorkerUtils::CompareScopePriorities( |
| + const GURL& scope1, const GURL& scope2) { |
| + DCHECK(!scope1.is_empty() && !scope2.is_empty()); |
| + DCHECK_EQ(scope1.GetOrigin(), scope2.GetOrigin()); |
| + if (scope1 == scope2) |
| + return 0; |
| + |
| + // Prioritize a scope which doesn't contain a wildcard (i.e. exact match). |
|
jsbell
2014/05/21 19:20:03
Again, not clear that prioritizing exact match ove
|
| + if (!ContainsWildcard(scope1) && ContainsWildcard(scope2)) |
| + return 1; |
| + if (ContainsWildcard(scope1) && !ContainsWildcard(scope2)) |
| + return -1; |
| + DCHECK(ContainsWildcard(scope1) && ContainsWildcard(scope2)); |
| + |
| + // If |scope1| and |scope2| have the same length, both or one of them should |
| + // be the exact match and handled above. |
| + DCHECK_NE(scope1.spec().size(), scope2.spec().size()); |
| + return scope1.spec().size() - scope2.spec().size(); |
|
jsbell
2014/05/21 19:20:03
unsigned - unsigned = unsigned; this only works be
|
| } |
| } // namespace content |