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.
5 #ifndef CONTENT_BROWSER_GEOFENCING_GEOFENCING_MANAGER_H_
6 #define CONTENT_BROWSER_GEOFENCING_GEOFENCING_MANAGER_H_
12 #include "base/callback_forward.h"
13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "content/browser/geofencing/geofencing_registration_delegate.h"
17 #include "content/browser/service_worker/service_worker_storage.h"
18 #include "content/common/content_export.h"
19 #include "content/common/geofencing_types.h"
20 #include "content/common/service_worker/service_worker_status_code.h"
23 struct DefaultSingletonTraits
;
27 struct WebCircularGeofencingRegion
;
32 class GeofencingService
;
33 class MockGeofencingService
;
34 class ServiceWorkerContextWrapper
;
35 class ServiceWorkerRegistration
;
37 // This is the main API to the geofencing subsystem. There is one instance of
38 // this class per storage partition.
39 // This class is responsible for keeping track of which geofences are currently
40 // registered by websites/workers, persisting this list of registrations and
41 // registering them with the global |GeofencingService|.
42 // This class is created on the UI thread, but all its methods should only be
43 // called from the IO thread.
44 // TODO(mek): Implement some kind of persistence of registrations.
45 // TODO(mek): Unregister a geofence when the ServiceWorkerRegistration it
46 // belongs to goes away.
47 class CONTENT_EXPORT GeofencingManager
48 : NON_EXPORTED_BASE(public GeofencingRegistrationDelegate
),
49 public base::RefCountedThreadSafe
<GeofencingManager
> {
51 typedef base::Callback
<void(GeofencingStatus
)> StatusCallback
;
53 explicit GeofencingManager(
54 const scoped_refptr
<ServiceWorkerContextWrapper
>& service_worker_context
);
56 // Init and Shutdown are for use on the UI thread when the storagepartition is
57 // being setup and torn down.
61 // Initiates registration of a new geofence. StatusCallback is called when
62 // registration has completed or failed (which could possibly be before
63 // RegisterRegion returns.
64 // Attempting to register a region with the same ID as an already registered
65 // (or in progress of being registered) region will fail.
66 // TODO(mek): Behavior when using an already used ID might need to be revised
67 // depending on what the actual spec ends up saying about this.
68 void RegisterRegion(int64 service_worker_registration_id
,
69 const std::string
& region_id
,
70 const blink::WebCircularGeofencingRegion
& region
,
71 const StatusCallback
& callback
);
73 // Unregister a region that was previously registered by a call to
74 // RegisterRegion. Any attempt to unregister a region that has not been
75 // registered, or for which the registration is still in progress
76 // (RegisterRegion hasn't called its callback yet) will fail.
77 // TODO(mek): Maybe better behavior would be to allow unregistering still
78 // in-progress registrations.
79 void UnregisterRegion(int64 service_worker_registration_id
,
80 const std::string
& region_id
,
81 const StatusCallback
& callback
);
83 // Returns all currently registered regions. In case of failure (no geofencing
84 // provider available for example) return an error status, while leaving
85 // |regions| untouched.
86 // This only returns regions for which the callback passed to RegisterRegion
87 // has been called already (so it doesn't include still in progress
89 GeofencingStatus
GetRegisteredRegions(
90 int64 service_worker_registration_id
,
91 std::map
<std::string
, blink::WebCircularGeofencingRegion
>* result
);
93 // Enables or disables mock geofencing service.
94 void SetMockProvider(GeofencingMockState mock_state
);
96 // Set the mock geofencing position.
97 // TODO(mek): Unify this mock position with the devtools exposed geolocation
98 // mock position (http://crbug.com/440902).
99 void SetMockPosition(double latitude
, double longitude
);
101 void SetServiceForTesting(GeofencingService
* service
) {
106 friend class base::RefCountedThreadSafe
<GeofencingManager
>;
107 ~GeofencingManager() override
;
110 // Internal bookkeeping associated with each registered geofence.
116 // GeofencingRegistrationDelegate implementation.
117 void RegistrationFinished(int64 geofencing_registration_id
,
118 GeofencingStatus status
) override
;
119 void RegionEntered(int64 geofencing_registration_id
) override
;
120 void RegionExited(int64 geofencing_registration_id
) override
;
122 // Looks up a particular geofence registration. Returns nullptr if no
123 // registration with the given IDs exists.
124 Registration
* FindRegistration(int64 service_worker_registration_id
,
125 const std::string
& region_id
);
127 // Looks up a particular geofence registration. Returns nullptr if no
128 // registration with the given ID exists.
129 Registration
* FindRegistrationById(int64 geofencing_registration_id
);
131 // Registers a new registration, returning a reference to the newly inserted
132 // object. Assumes no registration with the same IDs currently exists.
133 Registration
& AddRegistration(
134 int64 service_worker_registration_id
,
135 const GURL
& service_worker_origin
,
136 const std::string
& region_id
,
137 const blink::WebCircularGeofencingRegion
& region
,
138 const StatusCallback
& callback
,
139 int64 geofencing_registration_id
);
141 // Clears a registration.
142 void ClearRegistration(Registration
* registration
);
144 // Starts dispatching a particular geofencing |event_type| for the geofence
145 // registration with the given ID. This first looks up the Service Worker
146 // Registration the geofence is associated with, and then attempts to deliver
147 // the event to that service worker.
148 void DispatchGeofencingEvent(blink::WebGeofencingEventType event_type
,
149 int64 geofencing_registration_id
);
151 // Delivers an event to the specified service worker for the given geofence.
152 // If the geofence registration id is no longer valid, this method does
153 // nothing. This assumes the |service_worker_registration| is the service
154 // worker the geofence registration is associated with.
155 void DeliverGeofencingEvent(blink::WebGeofencingEventType event_type
,
156 int64 geofencing_registration_id
,
157 ServiceWorkerStatusCode service_worker_status
,
158 const scoped_refptr
<ServiceWorkerRegistration
>&
159 service_worker_registration
);
161 // Called when delivery of a geofence event to a service worker has finished
162 // (or failed to finish).
163 void DeliverGeofencingEventEnd(const scoped_refptr
<ServiceWorkerRegistration
>&
164 service_worker_registration
,
165 ServiceWorkerStatusCode service_worker_status
);
167 // Map of all registered regions for a particular service worker registration.
168 typedef std::map
<std::string
, Registration
> RegionIdRegistrationMap
;
169 // Map of service worker registration id to the regions registered by that
171 typedef std::map
<int64
, RegionIdRegistrationMap
>
172 ServiceWorkerRegistrationsMap
;
173 ServiceWorkerRegistrationsMap registrations_
;
175 // Map of all registered regions by geofencing_registration_id.
176 typedef std::map
<int64
, RegionIdRegistrationMap::iterator
>
177 RegistrationIdRegistrationMap
;
178 RegistrationIdRegistrationMap registrations_by_id_
;
180 GeofencingService
* service_
;
181 scoped_ptr
<MockGeofencingService
> mock_service_
;
182 scoped_refptr
<ServiceWorkerContextWrapper
> service_worker_context_
;
184 DISALLOW_COPY_AND_ASSIGN(GeofencingManager
);
187 } // namespace content
189 #endif // CONTENT_BROWSER_GEOFENCING_GEOFENCING_MANAGER_H_