Chromium Code Reviews| 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 <map> | |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | |
| 12 #include "base/callback_forward.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "content/common/content_export.h" | |
| 16 #include "content/common/geofencing_status.h" | |
| 17 | |
| 18 template <typename T> | |
| 19 struct DefaultSingletonTraits; | |
| 20 class GURL; | |
| 21 | |
| 22 namespace blink { | |
| 23 struct WebCircularGeofencingRegion; | |
| 24 }; | |
| 25 | |
| 26 namespace content { | |
| 27 | |
| 28 class BrowserContext; | |
| 29 class GeofencingProvider; | |
| 30 | |
| 31 // This is the main API to the geofencing subsystem. The application will hold | |
| 32 // a single instance of this class. | |
| 33 // This class is responsible for keeping track of which geofences are currently | |
| 34 // registered by websites/workers, persisting this list of registrations and | |
| 35 // registering a subset of these active registrations with the underlying | |
| 36 // platform specific |GeofencingProvider| instance. | |
| 37 // This class lives on the IO thread, and all public methods of it should only | |
| 38 // ever be called from that same thread. | |
| 39 // FIXME: Does it make more sense for this to live on the UI thread instead of | |
| 40 // the IO thread? | |
| 41 // TODO(mek): Implement some kind of persistence of registrations. | |
| 42 // TODO(mek): Limit the number of geofences that are registered with the | |
| 43 // underlying GeofencingProvider. | |
| 44 class CONTENT_EXPORT GeofencingManager { | |
| 45 public: | |
| 46 typedef base::Callback<void(GeofencingStatus)> StatusCallback; | |
| 47 typedef base::Callback<void( | |
| 48 GeofencingStatus, | |
| 49 const std::map<std::string, blink::WebCircularGeofencingRegion>&)> | |
| 50 RegistrationsCallback; | |
| 51 | |
| 52 // Gets a pointer to the singleton instance of the geofencing manager. This | |
| 53 // must only be called on the IO thread so that the GeofencingManager is | |
| 54 // always instantiated on the same thread. Ownership is NOT returned. | |
| 55 static GeofencingManager* GetInstance(); | |
|
Michael van Ouwerkerk
2014/10/08 16:59:01
Why is this class a singleton though? It seems vag
Marijn Kruisselbrink
2014/10/08 17:17:09
I'm not entirely convinced that making this a sing
| |
| 56 | |
| 57 // Initiates registration of a new geofence. StatusCallback is called when | |
| 58 // registration has completed or failed (which could possibly be before | |
| 59 // RegisterRegion returns. | |
| 60 // Attempting to register a region with the same ID as an already registered | |
| 61 // (or in progress of being registered) region will fail. | |
| 62 // TODO(mek): Behavior when using an already used ID might need to be revised | |
| 63 // depending on what the actual spec ends up saying about this. | |
| 64 void RegisterRegion(BrowserContext* browser_context, | |
| 65 int64 service_worker_registration_id, | |
| 66 const GURL& service_worker_origin, | |
| 67 const std::string& region_id, | |
| 68 const blink::WebCircularGeofencingRegion& region, | |
| 69 const StatusCallback& callback); | |
| 70 | |
| 71 // Unregister a region that was previously registered by a call to | |
| 72 // RegisterRegion. Any attempt to unregister a region that has not been | |
| 73 // registered, or for which the registration is still in progress | |
| 74 // (RegisterRegion hasn't called its callback yet) will fail. | |
| 75 // TODO(mek): Maybe better behavior would be to allow unregistering still | |
| 76 // in-progress registrations. | |
| 77 void UnregisterRegion(BrowserContext* browser_context, | |
| 78 int64 service_worker_registration_id, | |
| 79 const GURL& service_worker_origin, | |
| 80 const std::string& region_id, | |
| 81 const StatusCallback& callback); | |
| 82 | |
| 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 BrowserContext* browser_context, | |
| 91 int64 service_worker_registration_id, | |
| 92 const GURL& service_worker_origin, | |
| 93 std::map<std::string, blink::WebCircularGeofencingRegion>* regions); | |
| 94 | |
| 95 void SetProviderForTests(scoped_ptr<GeofencingProvider> provider); | |
| 96 | |
| 97 protected: | |
| 98 friend struct DefaultSingletonTraits<GeofencingManager>; | |
| 99 friend class GeofencingManagerTest; | |
| 100 GeofencingManager(); | |
| 101 virtual ~GeofencingManager(); | |
| 102 | |
| 103 private: | |
| 104 // Internal bookkeeping associated with each registered geofence. | |
| 105 struct RegistrationKey; | |
| 106 struct Registration; | |
| 107 class RegistrationMatches; | |
| 108 | |
| 109 // Called by GeofencingProvider when the platform specific provider completes | |
| 110 // registration of a geofence. | |
| 111 void RegisterRegionCompleted(const StatusCallback& callback, | |
| 112 const RegistrationKey& key, | |
| 113 GeofencingStatus status, | |
| 114 int registration_id); | |
| 115 | |
| 116 // Looks up a particular geofence registration. Returns nullptr if no | |
| 117 // registration with the given IDs exists. | |
| 118 Registration* FindRegistration(const RegistrationKey& key); | |
| 119 | |
| 120 // Registers a new registration, returning a reference to the newly inserted | |
| 121 // object. Assumes no registration with the same IDs currently exists. | |
| 122 Registration& AddRegistration( | |
| 123 const RegistrationKey& key, | |
| 124 const blink::WebCircularGeofencingRegion& region); | |
| 125 | |
| 126 // Clears a registration. | |
| 127 void ClearRegistration(const RegistrationKey& key); | |
| 128 | |
| 129 // List of all currently registered geofences. | |
| 130 // TODO(mek): Better way of storing these that allows more efficient lookup | |
| 131 // and deletion. | |
| 132 std::vector<Registration> registrations_; | |
| 133 | |
| 134 scoped_ptr<GeofencingProvider> provider_; | |
| 135 | |
| 136 DISALLOW_COPY_AND_ASSIGN(GeofencingManager); | |
| 137 }; | |
| 138 | |
| 139 } // namespace content | |
| 140 | |
| 141 #endif // CONTENT_BROWSER_GEOFENCING_GEOFENCING_MANAGER_H_ | |
| OLD | NEW |