1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// found in the LICENSE file.
4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_UTILS_H_
6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_UTILS_H_
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
85f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "base/macros.h"
9a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "content/common/content_export.h"
10a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "content/common/service_worker/service_worker_status_code.h"
11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "content/public/common/resource_type.h"
125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "url/gurl.h"
1346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace content {
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class ServiceWorkerUtils {
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public:
185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  static bool IsMainResourceType(ResourceType type) {
195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return IsResourceTypeFrame(type) || type == RESOURCE_TYPE_SHARED_WORKER;
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
22a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // A helper for creating a do-nothing status callback.
23a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  static void NoOpStatusCallback(ServiceWorkerStatusCode status) {}
24a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
25a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  // Returns true if |scope| matches |url|.
26a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  CONTENT_EXPORT static bool ScopeMatches(const GURL& scope, const GURL& url);
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
2946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)class CONTENT_EXPORT LongestScopeMatcher {
3046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) public:
3146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  explicit LongestScopeMatcher(const GURL& url) : url_(url) {}
3246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  virtual ~LongestScopeMatcher() {}
3346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
3446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Returns true if |scope| matches |url_| longer than |match_|.
3546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  bool MatchLongest(const GURL& scope);
3646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
3746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles) private:
3846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  const GURL url_;
3946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  GURL match_;
4046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
4146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(LongestScopeMatcher);
4246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)};
4346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}  // namespace content
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_UTILS_H_
47