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> | 7 #include <string> |
8 | 8 |
9 #include "base/command_line.h" | |
10 #include "base/logging.h" | 9 #include "base/logging.h" |
11 #include "content/public/common/content_switches.h" | 10 #include "base/strings/string_util.h" |
12 #include "url/gurl.h" | |
13 | 11 |
14 namespace content { | 12 namespace content { |
15 | 13 |
16 // static | 14 // static |
17 bool ServiceWorkerUtils::ScopeMatches(const GURL& scope, const GURL& url) { | 15 bool ServiceWorkerUtils::ScopeMatches(const GURL& scope, const GURL& url) { |
18 DCHECK(!scope.has_ref()); | 16 DCHECK(!scope.has_ref()); |
19 DCHECK(!url.has_ref()); | 17 DCHECK(!url.has_ref()); |
20 const std::string& scope_spec = scope.spec(); | 18 return StartsWithASCII(url.spec(), scope.spec(), true); |
21 const std::string& url_spec = url.spec(); | |
22 | |
23 size_t len = scope_spec.size(); | |
24 if (len > 0 && scope_spec[len - 1] == '*') | |
25 return scope_spec.compare(0, len - 1, url_spec, 0, len - 1) == 0; | |
26 return scope_spec == url_spec; | |
27 } | 19 } |
28 | 20 |
29 bool LongestScopeMatcher::MatchLongest(const GURL& scope) { | 21 bool LongestScopeMatcher::MatchLongest(const GURL& scope) { |
30 if (!ServiceWorkerUtils::ScopeMatches(scope, url_)) | 22 if (!ServiceWorkerUtils::ScopeMatches(scope, url_)) |
31 return false; | 23 return false; |
32 if (match_.is_empty()) { | 24 if (match_.is_empty() || match_.spec().size() < scope.spec().size()) { |
33 match_ = scope; | 25 match_ = scope; |
34 return true; | 26 return true; |
35 } | 27 } |
36 | |
37 const std::string match_spec = match_.spec(); | |
38 const std::string scope_spec = scope.spec(); | |
39 if (match_spec.size() < scope_spec.size()) { | |
40 match_ = scope; | |
41 return true; | |
42 } | |
43 | |
44 // If |scope| has the same length with |match_|, they are compared as strings. | |
45 // For example: | |
46 // 1) for a document "/foo", "/foo" is prioritized over "/fo*". | |
47 // 2) for a document "/f(1)", "/f(1*" is prioritized over "/f(1)". | |
48 // TODO(nhiroki): This isn't in the spec. | |
49 // (https://github.com/slightlyoff/ServiceWorker/issues/287) | |
50 if (match_spec.size() == scope_spec.size() && match_spec < scope_spec) { | |
51 match_ = scope; | |
52 return true; | |
53 } | |
54 | |
55 return false; | 28 return false; |
56 } | 29 } |
57 | 30 |
58 } // namespace content | 31 } // namespace content |
OLD | NEW |