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/common/content_export.h"
18 #include "content/common/geofencing_status.h"
21 struct DefaultSingletonTraits
;
25 struct WebCircularGeofencingRegion
;
30 class GeofencingService
;
31 class ServiceWorkerContextWrapper
;
33 // This is the main API to the geofencing subsystem. There is one instance of
34 // this class per storage partition.
35 // This class is responsible for keeping track of which geofences are currently
36 // registered by websites/workers, persisting this list of registrations and
37 // registering them with the global |GeofencingService|.
38 // This class is created on the UI thread, but all its methods should only be
39 // called from the IO thread.
40 // TODO(mek): Implement some kind of persistence of registrations.
41 class CONTENT_EXPORT GeofencingManager
42 : NON_EXPORTED_BASE(public GeofencingRegistrationDelegate
),
43 public base::RefCountedThreadSafe
<GeofencingManager
> {
45 typedef base::Callback
<void(GeofencingStatus
)> StatusCallback
;
47 explicit GeofencingManager(
48 const scoped_refptr
<ServiceWorkerContextWrapper
>& service_worker_context
);
50 // Init and Shutdown are for use on the UI thread when the storagepartition is
51 // being setup and torn down.
55 // Initiates registration of a new geofence. StatusCallback is called when
56 // registration has completed or failed (which could possibly be before
57 // RegisterRegion returns.
58 // Attempting to register a region with the same ID as an already registered
59 // (or in progress of being registered) region will fail.
60 // TODO(mek): Behavior when using an already used ID might need to be revised
61 // depending on what the actual spec ends up saying about this.
62 void RegisterRegion(int64 service_worker_registration_id
,
63 const std::string
& region_id
,
64 const blink::WebCircularGeofencingRegion
& region
,
65 const StatusCallback
& callback
);
67 // Unregister a region that was previously registered by a call to
68 // RegisterRegion. Any attempt to unregister a region that has not been
69 // registered, or for which the registration is still in progress
70 // (RegisterRegion hasn't called its callback yet) will fail.
71 // TODO(mek): Maybe better behavior would be to allow unregistering still
72 // in-progress registrations.
73 void UnregisterRegion(int64 service_worker_registration_id
,
74 const std::string
& region_id
,
75 const StatusCallback
& callback
);
77 // Returns all currently registered regions. In case of failure (no geofencing
78 // provider available for example) return an error status, while leaving
79 // |regions| untouched.
80 // This only returns regions for which the callback passed to RegisterRegion
81 // has been called already (so it doesn't include still in progress
83 GeofencingStatus
GetRegisteredRegions(
84 int64 service_worker_registration_id
,
85 std::map
<std::string
, blink::WebCircularGeofencingRegion
>* result
);
87 void SetServiceForTesting(GeofencingService
* service
) {
92 friend class base::RefCountedThreadSafe
<GeofencingManager
>;
93 ~GeofencingManager() override
;
96 // Internal bookkeeping associated with each registered geofence.
102 // GeofencingRegistrationDelegate implementation.
103 void RegistrationFinished(int64 geofencing_registration_id
,
104 GeofencingStatus status
) override
;
106 // Looks up a particular geofence registration. Returns nullptr if no
107 // registration with the given IDs exists.
108 Registration
* FindRegistration(int64 service_worker_registration_id
,
109 const std::string
& region_id
);
111 // Looks up a particular geofence registration. Returns nullptr if no
112 // registration with the given ID exists.
113 Registration
* FindRegistrationById(int64 geofencing_registration_id
);
115 // Registers a new registration, returning a reference to the newly inserted
116 // object. Assumes no registration with the same IDs currently exists.
117 Registration
& AddRegistration(
118 int64 service_worker_registration_id
,
119 const GURL
& service_worker_origin
,
120 const std::string
& region_id
,
121 const blink::WebCircularGeofencingRegion
& region
,
122 const StatusCallback
& callback
,
123 int64 geofencing_registration_id
);
125 // Clears a registration.
126 void ClearRegistration(Registration
* registration
);
128 // Map of all registered regions for a particular service worker registration.
129 typedef std::map
<std::string
, Registration
> RegionIdRegistrationMap
;
130 // Map of service worker registration id to the regions registered by that
132 typedef std::map
<int64
, RegionIdRegistrationMap
>
133 ServiceWorkerRegistrationsMap
;
134 ServiceWorkerRegistrationsMap registrations_
;
136 // Map of all registered regions by geofencing_registration_id.
137 typedef std::map
<int64
, RegionIdRegistrationMap::iterator
>
138 RegistrationIdRegistrationMap
;
139 RegistrationIdRegistrationMap registrations_by_id_
;
141 GeofencingService
* service_
;
142 scoped_refptr
<ServiceWorkerContextWrapper
> service_worker_context_
;
144 DISALLOW_COPY_AND_ASSIGN(GeofencingManager
);
147 } // namespace content
149 #endif // CONTENT_BROWSER_GEOFENCING_GEOFENCING_MANAGER_H_