| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_H_ |
| 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_H_ |
| 7 | 7 |
| 8 #include <map> |
| 9 #include <string> |
| 10 |
| 11 #include "base/bind.h" |
| 8 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/gtest_prod_util.h" |
| 9 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "content/browser/service_worker/service_worker_registration.h" |
| 10 #include "content/common/content_export.h" | 16 #include "content/common/content_export.h" |
| 17 #include "url/gurl.h" |
| 11 | 18 |
| 12 namespace base { | 19 namespace base { |
| 13 class FilePath; | 20 class FilePath; |
| 14 } | 21 } |
| 15 | 22 |
| 16 namespace quota { | 23 namespace quota { |
| 17 class QuotaManagerProxy; | 24 class QuotaManagerProxy; |
| 18 } | 25 } |
| 19 | 26 |
| 20 namespace content { | 27 namespace content { |
| 21 | 28 |
| 29 class ServiceWorkerRegistration; |
| 30 |
| 22 // This class manages metadata associated with all service workers, | 31 // This class manages metadata associated with all service workers, |
| 23 // including: | 32 // including: |
| 24 // - persistent storage of pattern -> service worker scripts | 33 // - persistent storage of pattern -> service worker scripts |
| 25 // - initialization and initial installation of service workers | 34 // - initialization and initial installation of service workers |
| 26 // - dispatching of non-fetch events to service workers | 35 // - dispatching of non-fetch events to service workers |
| 27 class CONTENT_EXPORT ServiceWorkerContext | 36 class CONTENT_EXPORT ServiceWorkerContext |
| 28 : public base::RefCountedThreadSafe<ServiceWorkerContext> { | 37 : NON_EXPORTED_BASE( |
| 38 public base::RefCountedThreadSafe<ServiceWorkerContext>) { |
| 29 public: | 39 public: |
| 40 typedef base::Callback<void(int64)> ResponseCallback; |
| 41 |
| 30 // This is owned by the StoragePartition, which will supply it with | 42 // This is owned by the StoragePartition, which will supply it with |
| 31 // the local path on disk. | 43 // the local path on disk. |
| 32 ServiceWorkerContext(const base::FilePath& path, | 44 ServiceWorkerContext(const base::FilePath& path, |
| 33 quota::QuotaManagerProxy* quota_manager_proxy); | 45 quota::QuotaManagerProxy* quota_manager_proxy); |
| 34 | 46 |
| 35 bool IsEnabled(); | 47 bool IsEnabled(); |
| 36 | 48 |
| 49 // The callback will be called on the IO thread. |
| 50 void RegisterServiceWorker(const GURL& pattern, |
| 51 const GURL& script_url, |
| 52 const ResponseCallback& callback); |
| 53 |
| 54 // The callback will be called on the IO thread. |
| 55 void UnregisterServiceWorker(const GURL& pattern, |
| 56 const base::Closure& callback); |
| 57 |
| 58 // TODO(alecflett): These exist primarily for testing. As this class |
| 59 // evolves to have persistent storage, they will need to become |
| 60 // asynchronous. |
| 61 scoped_refptr<ServiceWorkerRegistration> RegistrationForDocument( |
| 62 const GURL& document_url); |
| 63 scoped_refptr<ServiceWorkerRegistration> RegistrationForPattern( |
| 64 const GURL& pattern); |
| 65 |
| 37 private: | 66 private: |
| 38 friend class base::RefCountedThreadSafe<ServiceWorkerContext>; | 67 friend class base::RefCountedThreadSafe<ServiceWorkerContext>; |
| 39 ~ServiceWorkerContext(); | 68 ~ServiceWorkerContext(); |
| 40 | 69 |
| 70 // TODO(alecflett): These are temporary internal methods mainly for |
| 71 // exposure to tests. These should eventually call through to some |
| 72 // sort of ServiceWorkerStorage interface which will be |
| 73 // asynchronous. |
| 74 scoped_refptr<ServiceWorkerRegistration> Register(const GURL& pattern, |
| 75 const GURL& script_url); |
| 76 void Unregister(const GURL& pattern); |
| 77 static bool PatternMatches(const GURL& pattern, const GURL& script_url); |
| 78 |
| 79 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerContextTest, |
| 80 DifferentMatchDifferentRegistration); |
| 81 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerContextTest, |
| 82 SameDocumentSameRegistration); |
| 83 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerContextTest, SameMatchSameRegistration); |
| 84 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerContextTest, RegisterMatch); |
| 85 |
| 41 scoped_refptr<quota::QuotaManagerProxy> quota_manager_proxy_; | 86 scoped_refptr<quota::QuotaManagerProxy> quota_manager_proxy_; |
| 42 base::FilePath path_; | 87 base::FilePath path_; |
| 43 | 88 |
| 89 // Mapping of url pattern -> script instance |
| 90 // eventually the registration should be stored in leveldb, not in memory. |
| 91 typedef std::map<GURL, scoped_refptr<ServiceWorkerRegistration> > |
| 92 PatternToRegistrationMap; |
| 93 |
| 94 PatternToRegistrationMap registration_by_pattern_; |
| 95 |
| 44 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerContext); | 96 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerContext); |
| 45 }; | 97 }; |
| 46 | 98 |
| 47 } // namespace content | 99 } // namespace content |
| 48 | 100 |
| 49 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_H_ | 101 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_H_ |
| OLD | NEW |