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

Side by Side Diff: content/browser/service_worker/service_worker_utils.cc

Issue 294593002: ServiceWorker: Support longest-prefix-match for registration scope (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 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 | Annotate | Revision Log
OLDNEW
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"
10 #include "url/url_canon.h" 12 #include "url/url_canon.h"
11 13
12 namespace content { 14 namespace content {
13 15
16 namespace {
17
18 bool ContainsWildcard(const GURL& scope) {
19 DCHECK(!scope.is_empty());
20 const std::string& scope_spec = scope.spec();
21 return scope_spec[scope_spec.size() - 1] == '*';
22 }
23
24 } // namespace
25
14 // static 26 // static
15 bool ServiceWorkerUtils::IsFeatureEnabled() { 27 bool ServiceWorkerUtils::IsFeatureEnabled() {
16 static bool enabled = CommandLine::ForCurrentProcess()->HasSwitch( 28 static bool enabled = CommandLine::ForCurrentProcess()->HasSwitch(
17 switches::kEnableServiceWorker); 29 switches::kEnableServiceWorker);
18 return enabled; 30 return enabled;
19 } 31 }
20 32
21 // static 33 // static
22 bool ServiceWorkerUtils::ScopeMatches(const GURL& scope, const GURL& url) { 34 ServiceWorkerUtils::MatchResult ServiceWorkerUtils::ScopeMatches(
35 const GURL& scope, const GURL& url) {
23 DCHECK(!scope.has_ref()); 36 DCHECK(!scope.has_ref());
24 DCHECK(!url.has_ref()); 37 DCHECK(!url.has_ref());
25 const std::string& scope_spec = scope.spec(); 38 const std::string& scope_spec = scope.spec();
26 const std::string& url_spec = url.spec(); 39 if (scope_spec == url.spec())
40 return SCOPE_EXACT_MATCH;
41 size_t len = scope_spec.size();
42 if (len > 0 && ContainsWildcard(scope) &&
43 scope_spec.compare(0, len - 1, url.spec(), 0, len - 1) == 0) {
44 return SCOPE_WILDCARD_MATCH;
45 }
46 return SCOPE_NO_MATCH;
47 }
27 48
28 size_t len = scope_spec.size(); 49 // static
29 if (len > 0 && scope_spec[len - 1] == '*') { 50 int ServiceWorkerUtils::CompareScopePriorities(
30 return scope_spec.compare(0, len - 1, url_spec, 0, len - 1) == 0; 51 const GURL& scope1, const GURL& scope2) {
31 } 52 DCHECK(!scope1.is_empty() && !scope2.is_empty());
32 return scope_spec == url_spec; 53 DCHECK_EQ(scope1.GetOrigin(), scope2.GetOrigin());
54 if (scope1 == scope2)
55 return 0;
56
57 // 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
58 if (!ContainsWildcard(scope1) && ContainsWildcard(scope2))
59 return 1;
60 if (ContainsWildcard(scope1) && !ContainsWildcard(scope2))
61 return -1;
62 DCHECK(ContainsWildcard(scope1) && ContainsWildcard(scope2));
63
64 // If |scope1| and |scope2| have the same length, both or one of them should
65 // be the exact match and handled above.
66 DCHECK_NE(scope1.spec().size(), scope2.spec().size());
67 return scope1.spec().size() - scope2.spec().size();
jsbell 2014/05/21 19:20:03 unsigned - unsigned = unsigned; this only works be
33 } 68 }
34 69
35 } // namespace content 70 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698