Chromium Code Reviews| 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_STORAGE_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_STORAGE_H_ |
| 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_STORAGE_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_STORAGE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 13 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
| 14 #include "content/browser/service_worker/service_worker_registration_status.h" | 14 #include "content/browser/service_worker/service_worker_registration_status.h" |
| 15 #include "content/common/content_export.h" | 15 #include "content/common/content_export.h" |
| 16 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 17 | 17 |
| 18 namespace quota { | 18 namespace quota { |
| 19 class QuotaManagerProxy; | 19 class QuotaManagerProxy; |
| 20 } | 20 } |
| 21 | 21 |
| 22 namespace content { | 22 namespace content { |
| 23 | 23 |
| 24 class ServiceWorkerRegistration; | 24 class ServiceWorkerRegistration; |
| 25 class ServiceWorkerRegisterJob; | |
| 26 | 25 |
| 27 // This class provides an interface to load registration data and | 26 // This class provides an interface to load registration data and |
| 28 // instantiate ServiceWorkerRegistration objects. Any asynchronous | 27 // instantiate ServiceWorkerRegistration objects. |
| 29 // operations are run through instances of ServiceWorkerRegisterJob. | |
| 30 class CONTENT_EXPORT ServiceWorkerStorage { | 28 class CONTENT_EXPORT ServiceWorkerStorage { |
| 31 public: | 29 public: |
| 32 ServiceWorkerStorage(const base::FilePath& path, | 30 ServiceWorkerStorage(const base::FilePath& path, |
| 33 quota::QuotaManagerProxy* quota_manager_proxy); | 31 quota::QuotaManagerProxy* quota_manager_proxy); |
| 34 ~ServiceWorkerStorage(); | 32 ~ServiceWorkerStorage(); |
| 35 | 33 |
| 36 typedef base::Callback<void(ServiceWorkerRegistrationStatus status, | |
| 37 const scoped_refptr<ServiceWorkerRegistration>& | |
| 38 registration)> RegistrationCallback; | |
| 39 typedef base::Callback< | |
| 40 void(ServiceWorkerRegistrationStatus status)> UnregistrationCallback; | |
| 41 | |
| 42 // `found` is only valid if status == REGISTRATION_OK. | 34 // `found` is only valid if status == REGISTRATION_OK. |
| 43 typedef base::Callback<void(bool found, | 35 typedef base::Callback<void(bool found, |
| 44 ServiceWorkerRegistrationStatus status, | 36 ServiceWorkerRegistrationStatus status, |
| 45 const scoped_refptr<ServiceWorkerRegistration>& | 37 const scoped_refptr<ServiceWorkerRegistration>& |
| 46 registration)> FindRegistrationCallback; | 38 registration)> FindRegistrationCallback; |
| 47 | 39 |
| 48 void FindRegistrationForDocument(const GURL& document_url, | 40 void FindRegistrationForDocument(const GURL& document_url, |
| 49 const FindRegistrationCallback& callback); | 41 const FindRegistrationCallback& callback); |
| 50 void FindRegistrationForPattern(const GURL& pattern, | 42 void FindRegistrationForPattern(const GURL& pattern, |
| 51 const FindRegistrationCallback& callback); | 43 const FindRegistrationCallback& callback); |
| 52 | 44 |
| 53 void Register(const GURL& pattern, | |
| 54 const GURL& script_url, | |
| 55 const RegistrationCallback& callback); | |
| 56 | |
| 57 void Unregister(const GURL& pattern, const UnregistrationCallback& callback); | |
| 58 | |
| 59 private: | |
| 60 friend class ServiceWorkerRegisterJob; | |
| 61 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStorageTest, PatternMatches); | |
| 62 | |
| 63 typedef std::map<GURL, scoped_refptr<ServiceWorkerRegistration> > | |
| 64 PatternToRegistrationMap; | |
| 65 typedef ScopedVector<ServiceWorkerRegisterJob> RegistrationJobList; | |
| 66 | |
| 67 // TODO(alecflett): These are temporary internal methods providing | 45 // TODO(alecflett): These are temporary internal methods providing |
| 68 // synchronous in-memory registration. Eventually these will be | 46 // synchronous in-memory registration. Eventually these will be |
| 69 // replaced by asynchronous methods that persist registration to disk. | 47 // replaced by asynchronous methods that persist registration to disk. |
| 70 scoped_refptr<ServiceWorkerRegistration> RegisterInternal( | 48 scoped_refptr<ServiceWorkerRegistration> RegisterInternal( |
| 71 const GURL& pattern, | 49 const GURL& pattern, |
| 72 const GURL& script_url); | 50 const GURL& script_url); |
| 73 void UnregisterInternal(const GURL& pattern); | 51 void UnregisterInternal(const GURL& pattern); |
| 52 | |
| 53 base::WeakPtr<ServiceWorkerStorage> AsWeakPtr() { | |
| 54 return weak_factory_.GetWeakPtr(); | |
| 55 } | |
|
kinuko
2013/12/18 05:43:35
I don't think we should expose this as public AsWe
alecflett
2014/01/06 21:47:12
The real problem with SupportsWeakPtr is the odd o
kinuko
2014/01/08 06:19:10
I think I'm saying that I don't want to introduce
alecflett
2014/01/08 23:17:11
Ah I understand your preference now. the real reas
| |
| 56 | |
| 57 private: | |
| 58 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerStorageTest, PatternMatches); | |
| 59 | |
| 60 typedef std::map<GURL, scoped_refptr<ServiceWorkerRegistration> > | |
| 61 PatternToRegistrationMap; | |
| 62 | |
| 74 static bool PatternMatches(const GURL& pattern, const GURL& script_url); | 63 static bool PatternMatches(const GURL& pattern, const GURL& script_url); |
| 75 | 64 |
| 76 // Jobs are removed whenever they are finished or canceled. | |
| 77 void EraseJob(ServiceWorkerRegisterJob* job); | |
| 78 | |
| 79 // Called at ServiceWorkerRegisterJob completion. | |
| 80 void RegisterComplete(const RegistrationCallback& callback, | |
| 81 ServiceWorkerRegisterJob* job, | |
| 82 ServiceWorkerRegistrationStatus status, | |
| 83 ServiceWorkerRegistration* registration); | |
| 84 | |
| 85 // Called at ServiceWorkerRegisterJob completion. | |
| 86 void UnregisterComplete(const UnregistrationCallback& callback, | |
| 87 ServiceWorkerRegisterJob* job, | |
| 88 ServiceWorkerRegistrationStatus status, | |
| 89 ServiceWorkerRegistration* registration); | |
| 90 | |
| 91 // This is the in-memory registration. Eventually the registration will be | 65 // This is the in-memory registration. Eventually the registration will be |
| 92 // persisted to disk. | 66 // persisted to disk. |
| 93 // A list of currently running jobs. This is a temporary structure until we | 67 PatternToRegistrationMap registration_by_pattern_; |
|
kinuko
2013/12/18 05:43:35
I assume on-disk storage will have triplets like {
alecflett
2014/01/06 21:47:12
more or less - more likely something like a persis
| |
| 94 // start managing overlapping registrations explicitly. | |
| 95 RegistrationJobList registration_jobs_; | |
| 96 | 68 |
| 97 // in-memory map, to eventually be replaced with persistence | |
| 98 PatternToRegistrationMap registration_by_pattern_; | |
| 99 scoped_refptr<quota::QuotaManagerProxy> quota_manager_proxy_; | 69 scoped_refptr<quota::QuotaManagerProxy> quota_manager_proxy_; |
| 100 base::FilePath path_; | 70 base::FilePath path_; |
| 101 base::WeakPtrFactory<ServiceWorkerStorage> weak_factory_; | 71 base::WeakPtrFactory<ServiceWorkerStorage> weak_factory_; |
| 102 | 72 |
| 103 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerStorage); | 73 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerStorage); |
| 104 }; | 74 }; |
| 105 | 75 |
| 106 } // namespace content | 76 } // namespace content |
| 107 | 77 |
| 108 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_STORAGE_H_ | 78 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_STORAGE_H_ |
| OLD | NEW |