| 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 LongestScopeMatcher::MatchLongest(const GURL& scope) { |
| 37 if (!ServiceWorkerUtils::ScopeMatches(scope, url_)) |
| 38 return false; |
| 39 if (match_.is_empty()) { |
| 40 match_ = scope; |
| 41 return true; |
| 42 } |
| 43 |
| 44 const std::string match_spec = match_.spec(); |
| 45 const std::string scope_spec = scope.spec(); |
| 46 if (match_spec.size() < scope_spec.size()) { |
| 47 match_ = scope; |
| 48 return true; |
| 49 } |
| 50 |
| 51 // If |scope| has the same length with |match_|, they are compared as strings. |
| 52 // For example: |
| 53 // 1) for a document "/foo", "/foo" is prioritized over "/fo*". |
| 54 // 2) for a document "/f(1)", "/f(1*" is prioritized over "/f(1)". |
| 55 // TODO(nhiroki): This isn't in the spec. |
| 56 // (https://github.com/slightlyoff/ServiceWorker/issues/287) |
| 57 if (match_spec.size() == scope_spec.size() && match_spec < scope_spec) { |
| 58 match_ = scope; |
| 59 return true; |
| 60 } |
| 61 |
| 62 return false; |
| 63 } |
| 64 |
| 33 } // namespace content | 65 } // namespace content |
| OLD | NEW |