| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_GEOFENCING_GEOFENCING_MANAGER_H_ | 5 #ifndef CONTENT_BROWSER_GEOFENCING_GEOFENCING_MANAGER_H_ |
| 6 #define CONTENT_BROWSER_GEOFENCING_GEOFENCING_MANAGER_H_ | 6 #define CONTENT_BROWSER_GEOFENCING_GEOFENCING_MANAGER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> |
| 9 |
| 8 #include <map> | 10 #include <map> |
| 9 #include <string> | 11 #include <string> |
| 10 #include <vector> | 12 #include <vector> |
| 11 | 13 |
| 12 #include "base/callback_forward.h" | 14 #include "base/callback_forward.h" |
| 13 #include "base/macros.h" | 15 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 16 #include "content/browser/geofencing/geofencing_registration_delegate.h" | 18 #include "content/browser/geofencing/geofencing_registration_delegate.h" |
| 17 #include "content/browser/service_worker/service_worker_context_observer.h" | 19 #include "content/browser/service_worker/service_worker_context_observer.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 void Init(); | 63 void Init(); |
| 62 void Shutdown(); | 64 void Shutdown(); |
| 63 | 65 |
| 64 // Initiates registration of a new geofence. StatusCallback is called when | 66 // Initiates registration of a new geofence. StatusCallback is called when |
| 65 // registration has completed or failed (which could possibly be before | 67 // registration has completed or failed (which could possibly be before |
| 66 // RegisterRegion returns. | 68 // RegisterRegion returns. |
| 67 // Attempting to register a region with the same ID as an already registered | 69 // Attempting to register a region with the same ID as an already registered |
| 68 // (or in progress of being registered) region will fail. | 70 // (or in progress of being registered) region will fail. |
| 69 // TODO(mek): Behavior when using an already used ID might need to be revised | 71 // TODO(mek): Behavior when using an already used ID might need to be revised |
| 70 // depending on what the actual spec ends up saying about this. | 72 // depending on what the actual spec ends up saying about this. |
| 71 void RegisterRegion(int64 service_worker_registration_id, | 73 void RegisterRegion(int64_t service_worker_registration_id, |
| 72 const std::string& region_id, | 74 const std::string& region_id, |
| 73 const blink::WebCircularGeofencingRegion& region, | 75 const blink::WebCircularGeofencingRegion& region, |
| 74 const StatusCallback& callback); | 76 const StatusCallback& callback); |
| 75 | 77 |
| 76 // Unregister a region that was previously registered by a call to | 78 // Unregister a region that was previously registered by a call to |
| 77 // RegisterRegion. Any attempt to unregister a region that has not been | 79 // RegisterRegion. Any attempt to unregister a region that has not been |
| 78 // registered, or for which the registration is still in progress | 80 // registered, or for which the registration is still in progress |
| 79 // (RegisterRegion hasn't called its callback yet) will fail. | 81 // (RegisterRegion hasn't called its callback yet) will fail. |
| 80 // TODO(mek): Maybe better behavior would be to allow unregistering still | 82 // TODO(mek): Maybe better behavior would be to allow unregistering still |
| 81 // in-progress registrations. | 83 // in-progress registrations. |
| 82 void UnregisterRegion(int64 service_worker_registration_id, | 84 void UnregisterRegion(int64_t service_worker_registration_id, |
| 83 const std::string& region_id, | 85 const std::string& region_id, |
| 84 const StatusCallback& callback); | 86 const StatusCallback& callback); |
| 85 | 87 |
| 86 // Returns all currently registered regions. In case of failure (no geofencing | 88 // Returns all currently registered regions. In case of failure (no geofencing |
| 87 // provider available for example) return an error status, while leaving | 89 // provider available for example) return an error status, while leaving |
| 88 // |regions| untouched. | 90 // |regions| untouched. |
| 89 // This only returns regions for which the callback passed to RegisterRegion | 91 // This only returns regions for which the callback passed to RegisterRegion |
| 90 // has been called already (so it doesn't include still in progress | 92 // has been called already (so it doesn't include still in progress |
| 91 // registrations). | 93 // registrations). |
| 92 GeofencingStatus GetRegisteredRegions( | 94 GeofencingStatus GetRegisteredRegions( |
| 93 int64 service_worker_registration_id, | 95 int64_t service_worker_registration_id, |
| 94 std::map<std::string, blink::WebCircularGeofencingRegion>* result); | 96 std::map<std::string, blink::WebCircularGeofencingRegion>* result); |
| 95 | 97 |
| 96 // Enables or disables mock geofencing service. | 98 // Enables or disables mock geofencing service. |
| 97 void SetMockProvider(GeofencingMockState mock_state); | 99 void SetMockProvider(GeofencingMockState mock_state); |
| 98 | 100 |
| 99 // Set the mock geofencing position. | 101 // Set the mock geofencing position. |
| 100 // TODO(mek): Unify this mock position with the devtools exposed geolocation | 102 // TODO(mek): Unify this mock position with the devtools exposed geolocation |
| 101 // mock position (http://crbug.com/440902). | 103 // mock position (http://crbug.com/440902). |
| 102 void SetMockPosition(double latitude, double longitude); | 104 void SetMockPosition(double latitude, double longitude); |
| 103 | 105 |
| 104 void SetServiceForTesting(GeofencingService* service) { | 106 void SetServiceForTesting(GeofencingService* service) { |
| 105 service_ = service; | 107 service_ = service; |
| 106 } | 108 } |
| 107 | 109 |
| 108 protected: | 110 protected: |
| 109 friend class base::RefCountedThreadSafe<GeofencingManager>; | 111 friend class base::RefCountedThreadSafe<GeofencingManager>; |
| 110 ~GeofencingManager() override; | 112 ~GeofencingManager() override; |
| 111 | 113 |
| 112 private: | 114 private: |
| 113 // Internal bookkeeping associated with each registered geofence. | 115 // Internal bookkeeping associated with each registered geofence. |
| 114 struct Registration; | 116 struct Registration; |
| 115 | 117 |
| 116 void InitOnIO(); | 118 void InitOnIO(); |
| 117 void ShutdownOnIO(); | 119 void ShutdownOnIO(); |
| 118 | 120 |
| 119 // ServiceWorkerContextObserver implementation. | 121 // ServiceWorkerContextObserver implementation. |
| 120 void OnRegistrationDeleted(int64 service_worker_registration_id, | 122 void OnRegistrationDeleted(int64_t service_worker_registration_id, |
| 121 const GURL& pattern) override; | 123 const GURL& pattern) override; |
| 122 | 124 |
| 123 // GeofencingRegistrationDelegate implementation. | 125 // GeofencingRegistrationDelegate implementation. |
| 124 void RegistrationFinished(int64 geofencing_registration_id, | 126 void RegistrationFinished(int64_t geofencing_registration_id, |
| 125 GeofencingStatus status) override; | 127 GeofencingStatus status) override; |
| 126 void RegionEntered(int64 geofencing_registration_id) override; | 128 void RegionEntered(int64_t geofencing_registration_id) override; |
| 127 void RegionExited(int64 geofencing_registration_id) override; | 129 void RegionExited(int64_t geofencing_registration_id) override; |
| 128 | 130 |
| 129 // Looks up a particular geofence registration. Returns nullptr if no | 131 // Looks up a particular geofence registration. Returns nullptr if no |
| 130 // registration with the given IDs exists. | 132 // registration with the given IDs exists. |
| 131 Registration* FindRegistration(int64 service_worker_registration_id, | 133 Registration* FindRegistration(int64_t service_worker_registration_id, |
| 132 const std::string& region_id); | 134 const std::string& region_id); |
| 133 | 135 |
| 134 // Looks up a particular geofence registration. Returns nullptr if no | 136 // Looks up a particular geofence registration. Returns nullptr if no |
| 135 // registration with the given ID exists. | 137 // registration with the given ID exists. |
| 136 Registration* FindRegistrationById(int64 geofencing_registration_id); | 138 Registration* FindRegistrationById(int64_t geofencing_registration_id); |
| 137 | 139 |
| 138 // Registers a new registration, returning a reference to the newly inserted | 140 // Registers a new registration, returning a reference to the newly inserted |
| 139 // object. Assumes no registration with the same IDs currently exists. | 141 // object. Assumes no registration with the same IDs currently exists. |
| 140 Registration& AddRegistration( | 142 Registration& AddRegistration( |
| 141 int64 service_worker_registration_id, | 143 int64_t service_worker_registration_id, |
| 142 const GURL& service_worker_origin, | 144 const GURL& service_worker_origin, |
| 143 const std::string& region_id, | 145 const std::string& region_id, |
| 144 const blink::WebCircularGeofencingRegion& region, | 146 const blink::WebCircularGeofencingRegion& region, |
| 145 const StatusCallback& callback, | 147 const StatusCallback& callback, |
| 146 int64 geofencing_registration_id); | 148 int64_t geofencing_registration_id); |
| 147 | 149 |
| 148 // Clears a registration. | 150 // Clears a registration. |
| 149 void ClearRegistration(Registration* registration); | 151 void ClearRegistration(Registration* registration); |
| 150 | 152 |
| 151 // Unregisters and clears all registrations associated with a specific | 153 // Unregisters and clears all registrations associated with a specific |
| 152 // service worker. | 154 // service worker. |
| 153 void CleanUpForServiceWorker(int64 service_worker_registration_id); | 155 void CleanUpForServiceWorker(int64_t service_worker_registration_id); |
| 154 | 156 |
| 155 // Starts dispatching a particular geofencing |event_type| for the geofence | 157 // Starts dispatching a particular geofencing |event_type| for the geofence |
| 156 // registration with the given ID. This first looks up the Service Worker | 158 // registration with the given ID. This first looks up the Service Worker |
| 157 // Registration the geofence is associated with, and then attempts to deliver | 159 // Registration the geofence is associated with, and then attempts to deliver |
| 158 // the event to that service worker. | 160 // the event to that service worker. |
| 159 void DispatchGeofencingEvent(blink::WebGeofencingEventType event_type, | 161 void DispatchGeofencingEvent(blink::WebGeofencingEventType event_type, |
| 160 int64 geofencing_registration_id); | 162 int64_t geofencing_registration_id); |
| 161 | 163 |
| 162 // Delivers an event to the specified service worker for the given geofence. | 164 // Delivers an event to the specified service worker for the given geofence. |
| 163 // If the geofence registration id is no longer valid, this method does | 165 // If the geofence registration id is no longer valid, this method does |
| 164 // nothing. This assumes the |service_worker_registration| is the service | 166 // nothing. This assumes the |service_worker_registration| is the service |
| 165 // worker the geofence registration is associated with. | 167 // worker the geofence registration is associated with. |
| 166 void DeliverGeofencingEvent(blink::WebGeofencingEventType event_type, | 168 void DeliverGeofencingEvent(blink::WebGeofencingEventType event_type, |
| 167 int64 geofencing_registration_id, | 169 int64_t geofencing_registration_id, |
| 168 ServiceWorkerStatusCode service_worker_status, | 170 ServiceWorkerStatusCode service_worker_status, |
| 169 const scoped_refptr<ServiceWorkerRegistration>& | 171 const scoped_refptr<ServiceWorkerRegistration>& |
| 170 service_worker_registration); | 172 service_worker_registration); |
| 171 | 173 |
| 172 // Called when delivery of a geofence event to a service worker has finished | 174 // Called when delivery of a geofence event to a service worker has finished |
| 173 // (or failed to finish). | 175 // (or failed to finish). |
| 174 void DeliverGeofencingEventEnd(const scoped_refptr<ServiceWorkerRegistration>& | 176 void DeliverGeofencingEventEnd(const scoped_refptr<ServiceWorkerRegistration>& |
| 175 service_worker_registration, | 177 service_worker_registration, |
| 176 ServiceWorkerStatusCode service_worker_status); | 178 ServiceWorkerStatusCode service_worker_status); |
| 177 | 179 |
| 178 // Map of all registered regions for a particular service worker registration. | 180 // Map of all registered regions for a particular service worker registration. |
| 179 typedef std::map<std::string, Registration> RegionIdRegistrationMap; | 181 typedef std::map<std::string, Registration> RegionIdRegistrationMap; |
| 180 // Map of service worker registration id to the regions registered by that | 182 // Map of service worker registration id to the regions registered by that |
| 181 // service worker. | 183 // service worker. |
| 182 typedef std::map<int64, RegionIdRegistrationMap> | 184 typedef std::map<int64_t, RegionIdRegistrationMap> |
| 183 ServiceWorkerRegistrationsMap; | 185 ServiceWorkerRegistrationsMap; |
| 184 ServiceWorkerRegistrationsMap registrations_; | 186 ServiceWorkerRegistrationsMap registrations_; |
| 185 | 187 |
| 186 // Map of all registered regions by geofencing_registration_id. | 188 // Map of all registered regions by geofencing_registration_id. |
| 187 typedef std::map<int64, RegionIdRegistrationMap::iterator> | 189 typedef std::map<int64_t, RegionIdRegistrationMap::iterator> |
| 188 RegistrationIdRegistrationMap; | 190 RegistrationIdRegistrationMap; |
| 189 RegistrationIdRegistrationMap registrations_by_id_; | 191 RegistrationIdRegistrationMap registrations_by_id_; |
| 190 | 192 |
| 191 GeofencingService* service_; | 193 GeofencingService* service_; |
| 192 scoped_ptr<MockGeofencingService> mock_service_; | 194 scoped_ptr<MockGeofencingService> mock_service_; |
| 193 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; | 195 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_; |
| 194 | 196 |
| 195 DISALLOW_COPY_AND_ASSIGN(GeofencingManager); | 197 DISALLOW_COPY_AND_ASSIGN(GeofencingManager); |
| 196 }; | 198 }; |
| 197 | 199 |
| 198 } // namespace content | 200 } // namespace content |
| 199 | 201 |
| 200 #endif // CONTENT_BROWSER_GEOFENCING_GEOFENCING_MANAGER_H_ | 202 #endif // CONTENT_BROWSER_GEOFENCING_GEOFENCING_MANAGER_H_ |
| OLD | NEW |