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

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

Powered by Google App Engine
This is Rietveld 408576698