OLD | NEW |
(Empty) | |
| 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 "base/callback_forward.h" |
| 6 #include "base/memory/singleton.h" |
| 7 #include "base/memory/weak_ptr.h" |
| 8 #include "components/keyed_service/content/browser_context_keyed_service_factory
.h" |
| 9 #include "components/keyed_service/core/keyed_service.h" |
| 10 #include "content/public/common/service_worker_status_code.h" |
| 11 #include "extensions/common/extension.h" |
| 12 |
| 13 namespace content { |
| 14 class ServiceWorkerContext; |
| 15 class ServiceWorkerRegistration; |
| 16 class StoragePartition; |
| 17 } |
| 18 |
| 19 namespace extensions { |
| 20 |
| 21 // This class registers and unregisters Service Workers for extensions that use |
| 22 // them and allows clients to look up the Service Worker for an extension. |
| 23 // |
| 24 // This class lives on the UI thread despite interacting with the |
| 25 // ServiceWorkerContextCore that lives on the IO thread. |
| 26 // |
| 27 // See |
| 28 // https://docs.google.com/document/d/1szeOHrr_qEJGSNbDtEqeKcGDkLmwvftqTV731kQw2
rM/edit |
| 29 // for more details. |
| 30 class ServiceWorkerManager : public KeyedService { |
| 31 public: |
| 32 // Convenience function to get the ServiceWorkerManager for a BrowserContext. |
| 33 static ServiceWorkerManager* Get(content::BrowserContext* context); |
| 34 |
| 35 // Makes sure a ServiceWorker is registered for |extension|. This immediately |
| 36 // cancels callbacks waiting for an unregistration. If multiple registrations |
| 37 // and unregistrations are in flight concurrently, only the last one takes |
| 38 // effect. |
| 39 void RegisterExtension(const Extension* extension); |
| 40 // Unregisters any ServiceWorker for |extension|. This immediately cancels |
| 41 // callbacks waiting for a registration, and has the same response to multiple |
| 42 // in-flight calls as RegisterExtension. |
| 43 void UnregisterExtension(const Extension* extension); |
| 44 |
| 45 private: |
| 46 friend class ServiceWorkerManagerFactory; |
| 47 |
| 48 ServiceWorkerManager(content::BrowserContext* context); |
| 49 virtual ~ServiceWorkerManager(); |
| 50 |
| 51 inline content::StoragePartition* GetStoragePartition( |
| 52 const ExtensionId& ext_id) const; |
| 53 inline content::ServiceWorkerContext* GetSWContext( |
| 54 const ExtensionId& ext_id) const; |
| 55 inline base::WeakPtr<ServiceWorkerManager> WeakThis(); |
| 56 |
| 57 void ContinueRegistrationWithExtensionHost(const ExtensionId& extension_id, |
| 58 const GURL& scope, |
| 59 const GURL& service_worker_script); |
| 60 void ContinueUnregistrationWithExtensionHost(const ExtensionId& extension_id, |
| 61 const GURL& scope); |
| 62 void FinishRegistration(const ExtensionId& extension_id, |
| 63 content::ServiceWorkerStatusCode result); |
| 64 void FinishUnregistration(const ExtensionId& extension_id, |
| 65 content::ServiceWorkerStatusCode result); |
| 66 |
| 67 content::BrowserContext* const context_; |
| 68 |
| 69 enum RegistrationState { |
| 70 // Represented by not being in the map. |
| 71 UNREGISTERED, |
| 72 // Between a call to RegisterExtension and the response from |
| 73 REGISTERING, |
| 74 REGISTERED, |
| 75 UNREGISTERING, |
| 76 }; |
| 77 struct State { |
| 78 RegistrationState registration; |
| 79 int outstanding_state_changes; |
| 80 // These two can be non-empty during REGISTERING. |
| 81 std::vector<base::Closure> registration_succeeded; |
| 82 std::vector<base::Closure> registration_failed; |
| 83 // These two can be non-empty during UNREGISTERING. |
| 84 std::vector<base::Closure> unregistration_succeeded; |
| 85 std::vector<base::Closure> unregistration_failed; |
| 86 State(); |
| 87 ~State(); |
| 88 }; |
| 89 base::hash_map<ExtensionId, State> states_; |
| 90 |
| 91 base::WeakPtrFactory<ServiceWorkerManager> weak_this_factory_; |
| 92 }; |
| 93 |
| 94 class ServiceWorkerManagerFactory : public BrowserContextKeyedServiceFactory { |
| 95 public: |
| 96 static ServiceWorkerManager* GetForBrowserContext( |
| 97 content::BrowserContext* context); |
| 98 |
| 99 static ServiceWorkerManagerFactory* GetInstance(); |
| 100 |
| 101 void SetInstanceForTesting(content::BrowserContext* context, |
| 102 ServiceWorkerManager* prefs); |
| 103 |
| 104 private: |
| 105 friend struct DefaultSingletonTraits<ServiceWorkerManagerFactory>; |
| 106 |
| 107 ServiceWorkerManagerFactory(); |
| 108 virtual ~ServiceWorkerManagerFactory(); |
| 109 |
| 110 virtual KeyedService* BuildServiceInstanceFor( |
| 111 content::BrowserContext* context) const OVERRIDE; |
| 112 virtual content::BrowserContext* GetBrowserContextToUse( |
| 113 content::BrowserContext* context) const OVERRIDE; |
| 114 }; |
| 115 |
| 116 } // namespace extensions |
OLD | NEW |