[Mac] Implement Ambient Light API
[chromium-blink-merge.git] / content / browser / geofencing / geofencing_manager.h
blob6954d7bd39b48ebe841287dfc48a9730c74afee3
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_
8 #include <map>
9 #include <string>
10 #include <vector>
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_context_observer.h"
18 #include "content/browser/service_worker/service_worker_storage.h"
19 #include "content/common/content_export.h"
20 #include "content/common/geofencing_types.h"
21 #include "content/common/service_worker/service_worker_status_code.h"
23 template <typename T>
24 struct DefaultSingletonTraits;
25 class GURL;
27 namespace blink {
28 struct WebCircularGeofencingRegion;
31 namespace content {
33 class GeofencingService;
34 class MockGeofencingService;
35 class ServiceWorkerContextWrapper;
36 class ServiceWorkerRegistration;
38 // This is the main API to the geofencing subsystem. There is one instance of
39 // this class per storage partition.
40 // This class is responsible for keeping track of which geofences are currently
41 // registered by websites/workers, persisting this list of registrations and
42 // registering them with the global |GeofencingService|.
43 // This class is created on the UI thread, but all its methods should only be
44 // called from the IO thread.
45 // TODO(mek): Implement some kind of persistence of registrations.
46 class CONTENT_EXPORT GeofencingManager
47 : NON_EXPORTED_BASE(public GeofencingRegistrationDelegate),
48 NON_EXPORTED_BASE(public ServiceWorkerContextObserver),
49 public base::RefCountedThreadSafe<GeofencingManager> {
50 public:
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.
58 void Init();
59 void Shutdown();
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
88 // registrations).
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) {
102 service_ = service;
105 protected:
106 friend class base::RefCountedThreadSafe<GeofencingManager>;
107 ~GeofencingManager() override;
109 private:
110 // Internal bookkeeping associated with each registered geofence.
111 struct Registration;
113 void InitOnIO();
114 void ShutdownOnIO();
116 // ServiceWorkerContextObserver implementation.
117 void OnRegistrationDeleted(int64 service_worker_registration_id,
118 const GURL& pattern) override;
120 // GeofencingRegistrationDelegate implementation.
121 void RegistrationFinished(int64 geofencing_registration_id,
122 GeofencingStatus status) override;
123 void RegionEntered(int64 geofencing_registration_id) override;
124 void RegionExited(int64 geofencing_registration_id) override;
126 // Looks up a particular geofence registration. Returns nullptr if no
127 // registration with the given IDs exists.
128 Registration* FindRegistration(int64 service_worker_registration_id,
129 const std::string& region_id);
131 // Looks up a particular geofence registration. Returns nullptr if no
132 // registration with the given ID exists.
133 Registration* FindRegistrationById(int64 geofencing_registration_id);
135 // Registers a new registration, returning a reference to the newly inserted
136 // object. Assumes no registration with the same IDs currently exists.
137 Registration& AddRegistration(
138 int64 service_worker_registration_id,
139 const GURL& service_worker_origin,
140 const std::string& region_id,
141 const blink::WebCircularGeofencingRegion& region,
142 const StatusCallback& callback,
143 int64 geofencing_registration_id);
145 // Clears a registration.
146 void ClearRegistration(Registration* registration);
148 // Unregisters and clears all registrations associated with a specific
149 // service worker.
150 void CleanUpForServiceWorker(int64 service_worker_registration_id);
152 // Starts dispatching a particular geofencing |event_type| for the geofence
153 // registration with the given ID. This first looks up the Service Worker
154 // Registration the geofence is associated with, and then attempts to deliver
155 // the event to that service worker.
156 void DispatchGeofencingEvent(blink::WebGeofencingEventType event_type,
157 int64 geofencing_registration_id);
159 // Delivers an event to the specified service worker for the given geofence.
160 // If the geofence registration id is no longer valid, this method does
161 // nothing. This assumes the |service_worker_registration| is the service
162 // worker the geofence registration is associated with.
163 void DeliverGeofencingEvent(blink::WebGeofencingEventType event_type,
164 int64 geofencing_registration_id,
165 ServiceWorkerStatusCode service_worker_status,
166 const scoped_refptr<ServiceWorkerRegistration>&
167 service_worker_registration);
169 // Called when delivery of a geofence event to a service worker has finished
170 // (or failed to finish).
171 void DeliverGeofencingEventEnd(const scoped_refptr<ServiceWorkerRegistration>&
172 service_worker_registration,
173 ServiceWorkerStatusCode service_worker_status);
175 // Map of all registered regions for a particular service worker registration.
176 typedef std::map<std::string, Registration> RegionIdRegistrationMap;
177 // Map of service worker registration id to the regions registered by that
178 // service worker.
179 typedef std::map<int64, RegionIdRegistrationMap>
180 ServiceWorkerRegistrationsMap;
181 ServiceWorkerRegistrationsMap registrations_;
183 // Map of all registered regions by geofencing_registration_id.
184 typedef std::map<int64, RegionIdRegistrationMap::iterator>
185 RegistrationIdRegistrationMap;
186 RegistrationIdRegistrationMap registrations_by_id_;
188 GeofencingService* service_;
189 scoped_ptr<MockGeofencingService> mock_service_;
190 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_;
192 DISALLOW_COPY_AND_ASSIGN(GeofencingManager);
195 } // namespace content
197 #endif // CONTENT_BROWSER_GEOFENCING_GEOFENCING_MANAGER_H_