Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: extensions/browser/service_worker_manager.h

Issue 182253010: Register a Service Worker when an extension is enabled. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Roughly wire things up and add a test Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/browser_context_keyed_service/browser_context_keyed_service .h"
9 #include "components/browser_context_keyed_service/browser_context_keyed_service _factory.h"
10 #include "content/common/service_worker/service_worker_status_code.h"
11 #include "extensions/common/extension.h"
12
13 namespace content {
14 class ServiceWorkerContextWrapper;
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 BrowserContextKeyedService {
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 Extension* ext) const;
53 inline scoped_refptr<content::ServiceWorkerContextWrapper> GetSWContext(
54 const Extension* ext) const;
55 inline base::WeakPtr<ServiceWorkerManager> WeakThis();
56
57 void FinishRegistration(const ExtensionId& extension_id,
58 content::ServiceWorkerStatusCode result);
59 void FinishUnregistration(const ExtensionId& extension_id,
60 content::ServiceWorkerStatusCode result);
61
62 content::BrowserContext* const context_;
63
64 enum RegistrationState {
65 // Represented by not being in the map.
66 UNREGISTERED,
67 // Between a call to RegisterExtension and the response from
68 REGISTERING,
69 REGISTERED,
70 UNREGISTERING,
71 };
72 struct State {
73 RegistrationState registration;
74 int outstanding_state_changes;
75 // These two can be non-empty during REGISTERING.
76 std::vector<base::Closure> registration_succeeded;
77 std::vector<base::Closure> registration_failed;
78 // These two can be non-empty during UNREGISTERING.
79 std::vector<base::Closure> unregistration_succeeded;
80 std::vector<base::Closure> unregistration_failed;
81 State();
82 ~State();
83 };
84 base::hash_map<ExtensionId, State> states_;
85
86 base::WeakPtrFactory<ServiceWorkerManager> weak_this_factory_;
87 };
88
89 class ServiceWorkerManagerFactory : public BrowserContextKeyedServiceFactory {
90 public:
91 static ServiceWorkerManager* GetForBrowserContext(
92 content::BrowserContext* context);
93
94 static ServiceWorkerManagerFactory* GetInstance();
95
96 void SetInstanceForTesting(content::BrowserContext* context,
97 ServiceWorkerManager* prefs);
98
99 private:
100 friend struct DefaultSingletonTraits<ServiceWorkerManagerFactory>;
101
102 ServiceWorkerManagerFactory();
103 virtual ~ServiceWorkerManagerFactory();
104
105 virtual BrowserContextKeyedService* BuildServiceInstanceFor(
106 content::BrowserContext* context) const OVERRIDE;
107 virtual content::BrowserContext* GetBrowserContextToUse(
108 content::BrowserContext* context) const OVERRIDE;
109 };
110
111 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698