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 #ifndef CONTENT_BROWSER_GEOFENCING_GEOFENCING_MANAGER_H_ | |
6 #define CONTENT_BROWSER_GEOFENCING_GEOFENCING_MANAGER_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <map> | |
11 #include <memory> | |
12 #include <string> | |
13 #include <vector> | |
14 | |
15 #include "base/callback_forward.h" | |
16 #include "base/macros.h" | |
17 #include "base/memory/ref_counted.h" | |
18 #include "content/browser/geofencing/geofencing_registration_delegate.h" | |
19 #include "content/browser/service_worker/service_worker_context_observer.h" | |
20 #include "content/browser/service_worker/service_worker_storage.h" | |
21 #include "content/common/content_export.h" | |
22 #include "content/common/geofencing_types.h" | |
23 #include "content/common/service_worker/service_worker_status_code.h" | |
24 #include "third_party/WebKit/public/platform/WebGeofencingEventType.h" | |
25 | |
26 namespace base { | |
27 template <typename T> | |
28 struct DefaultSingletonTraits; | |
29 } // namespace base // namespace base | |
30 | |
31 class GURL; | |
32 | |
33 namespace blink { | |
34 struct WebCircularGeofencingRegion; | |
35 }; | |
36 | |
37 namespace content { | |
38 | |
39 class GeofencingService; | |
40 class MockGeofencingService; | |
41 class ServiceWorkerContextWrapper; | |
42 class ServiceWorkerRegistration; | |
43 | |
44 // This is the main API to the geofencing subsystem. There is one instance of | |
45 // this class per storage partition. | |
46 // This class is responsible for keeping track of which geofences are currently | |
47 // registered by websites/workers, persisting this list of registrations and | |
48 // registering them with the global |GeofencingService|. | |
49 // This class is created on the UI thread, but all its methods should only be | |
50 // called from the IO thread. | |
51 // TODO(mek): Implement some kind of persistence of registrations. | |
52 class CONTENT_EXPORT GeofencingManager | |
53 : NON_EXPORTED_BASE(public GeofencingRegistrationDelegate), | |
54 NON_EXPORTED_BASE(public ServiceWorkerContextObserver), | |
55 public base::RefCountedThreadSafe<GeofencingManager> { | |
56 public: | |
57 typedef base::Callback<void(GeofencingStatus)> StatusCallback; | |
58 | |
59 explicit GeofencingManager( | |
60 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context); | |
61 | |
62 // Init and Shutdown are for use on the UI thread when the storagepartition is | |
63 // being setup and torn down. | |
64 void Init(); | |
65 void Shutdown(); | |
66 | |
67 // Initiates registration of a new geofence. StatusCallback is called when | |
68 // registration has completed or failed (which could possibly be before | |
69 // RegisterRegion returns. | |
70 // Attempting to register a region with the same ID as an already registered | |
71 // (or in progress of being registered) region will fail. | |
72 // TODO(mek): Behavior when using an already used ID might need to be revised | |
73 // depending on what the actual spec ends up saying about this. | |
74 void RegisterRegion(int64_t service_worker_registration_id, | |
75 const std::string& region_id, | |
76 const blink::WebCircularGeofencingRegion& region, | |
77 const StatusCallback& callback); | |
78 | |
79 // Unregister a region that was previously registered by a call to | |
80 // RegisterRegion. Any attempt to unregister a region that has not been | |
81 // registered, or for which the registration is still in progress | |
82 // (RegisterRegion hasn't called its callback yet) will fail. | |
83 // TODO(mek): Maybe better behavior would be to allow unregistering still | |
84 // in-progress registrations. | |
85 void UnregisterRegion(int64_t service_worker_registration_id, | |
86 const std::string& region_id, | |
87 const StatusCallback& callback); | |
88 | |
89 // Returns all currently registered regions. In case of failure (no geofencing | |
90 // provider available for example) return an error status, while leaving | |
91 // |regions| untouched. | |
92 // This only returns regions for which the callback passed to RegisterRegion | |
93 // has been called already (so it doesn't include still in progress | |
94 // registrations). | |
95 GeofencingStatus GetRegisteredRegions( | |
96 int64_t service_worker_registration_id, | |
97 std::map<std::string, blink::WebCircularGeofencingRegion>* result); | |
98 | |
99 // Enables or disables mock geofencing service. | |
100 void SetMockProvider(GeofencingMockState mock_state); | |
101 | |
102 // Set the mock geofencing position. | |
103 // TODO(mek): Unify this mock position with the devtools exposed geolocation | |
104 // mock position (http://crbug.com/440902). | |
105 void SetMockPosition(double latitude, double longitude); | |
106 | |
107 void SetServiceForTesting(GeofencingService* service) { | |
108 service_ = service; | |
109 } | |
110 | |
111 protected: | |
112 friend class base::RefCountedThreadSafe<GeofencingManager>; | |
113 ~GeofencingManager() override; | |
114 | |
115 private: | |
116 // Internal bookkeeping associated with each registered geofence. | |
117 struct Registration; | |
118 | |
119 void InitOnIO(); | |
120 void ShutdownOnIO(); | |
121 | |
122 // ServiceWorkerContextObserver implementation. | |
123 void OnRegistrationDeleted(int64_t service_worker_registration_id, | |
124 const GURL& pattern) override; | |
125 | |
126 // GeofencingRegistrationDelegate implementation. | |
127 void RegistrationFinished(int64_t geofencing_registration_id, | |
128 GeofencingStatus status) override; | |
129 void RegionEntered(int64_t geofencing_registration_id) override; | |
130 void RegionExited(int64_t geofencing_registration_id) override; | |
131 | |
132 // Looks up a particular geofence registration. Returns nullptr if no | |
133 // registration with the given IDs exists. | |
134 Registration* FindRegistration(int64_t service_worker_registration_id, | |
135 const std::string& region_id); | |
136 | |
137 // Looks up a particular geofence registration. Returns nullptr if no | |
138 // registration with the given ID exists. | |
139 Registration* FindRegistrationById(int64_t geofencing_registration_id); | |
140 | |
141 // Registers a new registration, returning a reference to the newly inserted | |
142 // object. Assumes no registration with the same IDs currently exists. | |
143 Registration& AddRegistration( | |
144 int64_t service_worker_registration_id, | |
145 const GURL& service_worker_origin, | |
146 const std::string& region_id, | |
147 const blink::WebCircularGeofencingRegion& region, | |
148 const StatusCallback& callback, | |
149 int64_t geofencing_registration_id); | |
150 | |
151 // Clears a registration. | |
152 void ClearRegistration(Registration* registration); | |
153 | |
154 // Unregisters and clears all registrations associated with a specific | |
155 // service worker. | |
156 void CleanUpForServiceWorker(int64_t service_worker_registration_id); | |
157 | |
158 // Starts dispatching a particular geofencing |event_type| for the geofence | |
159 // registration with the given ID. This first looks up the Service Worker | |
160 // Registration the geofence is associated with, and then attempts to deliver | |
161 // the event to that service worker. | |
162 void DispatchGeofencingEvent(blink::WebGeofencingEventType event_type, | |
163 int64_t geofencing_registration_id); | |
164 | |
165 // Delivers an event to the specified service worker for the given geofence. | |
166 // If the geofence registration id is no longer valid, this method does | |
167 // nothing. This assumes the |service_worker_registration| is the service | |
168 // worker the geofence registration is associated with. | |
169 void DeliverGeofencingEvent(blink::WebGeofencingEventType event_type, | |
170 int64_t geofencing_registration_id, | |
171 ServiceWorkerStatusCode service_worker_status, | |
172 const scoped_refptr<ServiceWorkerRegistration>& | |
173 service_worker_registration); | |
174 | |
175 // Delivers an event to a specific service worker version. Called as callback | |
176 // after the worker has started. | |
177 void DeliverEventToRunningWorker( | |
178 const scoped_refptr<ServiceWorkerRegistration>& | |
179 service_worker_registration, | |
180 blink::WebGeofencingEventType event_type, | |
181 const std::string& region_id, | |
182 const blink::WebCircularGeofencingRegion& region, | |
183 const scoped_refptr<ServiceWorkerVersion>& worker); | |
184 | |
185 // Called for every response received for an event. There should only be one. | |
186 void OnEventResponse(const scoped_refptr<ServiceWorkerVersion>& worker, | |
187 const scoped_refptr<ServiceWorkerRegistration>& | |
188 service_worker_registration, | |
189 int request_id, | |
190 blink::WebServiceWorkerEventResult result); | |
191 | |
192 // Called when dispatching an event failed. | |
193 void OnEventError(ServiceWorkerStatusCode service_worker_status); | |
194 | |
195 // Map of all registered regions for a particular service worker registration. | |
196 typedef std::map<std::string, Registration> RegionIdRegistrationMap; | |
197 // Map of service worker registration id to the regions registered by that | |
198 // service worker. | |
199 typedef std::map<int64_t, RegionIdRegistrationMap> | |
200 ServiceWorkerRegistrationsMap; | |
201 ServiceWorkerRegistrationsMap registrations_; | |
202 | |
203 // Map of all registered regions by geofencing_registration_id. | |
204 typedef std::map<int64_t, RegionIdRegistrationMap::iterator> | |
205 RegistrationIdRegistrationMap; | |
206 RegistrationIdRegistrationMap registrations_by_id_; | |
207 | |
208 GeofencingService* service_; | |
209 std::unique_ptr<MockGeofencingService> mock_service_; | |
210 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; | |
211 | |
212 DISALLOW_COPY_AND_ASSIGN(GeofencingManager); | |
213 }; | |
214 | |
215 } // namespace content | |
216 | |
217 #endif // CONTENT_BROWSER_GEOFENCING_GEOFENCING_MANAGER_H_ | |
OLD | NEW |