• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
3 // found in the LICENSE file.
4 
5 #include "content/browser/service_worker/service_worker_utils.h"
6 
7 #include <string>
8 
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "content/public/common/content_switches.h"
12 #include "url/gurl.h"
13 
14 namespace content {
15 
16 // static
IsFeatureEnabled()17 bool ServiceWorkerUtils::IsFeatureEnabled() {
18   static bool enabled = CommandLine::ForCurrentProcess()->HasSwitch(
19       switches::kEnableServiceWorker);
20   return enabled;
21 }
22 
23 // static
ScopeMatches(const GURL & scope,const GURL & url)24 bool ServiceWorkerUtils::ScopeMatches(const GURL& scope, const GURL& url) {
25   DCHECK(!scope.has_ref());
26   DCHECK(!url.has_ref());
27   const std::string& scope_spec = scope.spec();
28   const std::string& url_spec = url.spec();
29 
30   size_t len = scope_spec.size();
31   if (len > 0 && scope_spec[len - 1] == '*')
32     return scope_spec.compare(0, len - 1, url_spec, 0, len - 1) == 0;
33   return scope_spec == url_spec;
34 }
35 
MatchLongest(const GURL & scope)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 
65 }  // namespace content
66