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