Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(491)

Side by Side Diff: content/browser/geofencing/geofencing_manager.h

Issue 645763003: Refactor GeofencingManager to have one instance per StoragePartition. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <map> 8 #include <map>
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/callback_forward.h" 12 #include "base/callback_forward.h"
13 #include "base/macros.h" 13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
16 #include "content/browser/geofencing/geofencing_registration_delegate.h"
15 #include "content/common/content_export.h" 17 #include "content/common/content_export.h"
16 #include "content/common/geofencing_status.h" 18 #include "content/common/geofencing_status.h"
17 19
18 template <typename T> 20 template <typename T>
19 struct DefaultSingletonTraits; 21 struct DefaultSingletonTraits;
20 class GURL; 22 class GURL;
21 23
22 namespace blink { 24 namespace blink {
23 struct WebCircularGeofencingRegion; 25 struct WebCircularGeofencingRegion;
24 }; 26 };
25 27
26 namespace content { 28 namespace content {
27 29
28 class BrowserContext; 30 class GeofencingService;
29 class GeofencingProvider; 31 class ServiceWorkerContextWrapper;
30 32
31 // This is the main API to the geofencing subsystem. The application will hold 33 // This is the main API to the geofencing subsystem. There is one instance of
32 // a single instance of this class. 34 // this class per storage partition.
33 // This class is responsible for keeping track of which geofences are currently 35 // This class is responsible for keeping track of which geofences are currently
34 // registered by websites/workers, persisting this list of registrations and 36 // registered by websites/workers, persisting this list of registrations and
35 // registering a subset of these active registrations with the underlying 37 // registering them with the global |GeofencingService|.
36 // platform specific |GeofencingProvider| instance. 38 // This class is created on the UI thread, but all its methods should only be
37 // This class lives on the IO thread, and all public methods of it should only 39 // called from the IO thread.
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. 40 // TODO(mek): Implement some kind of persistence of registrations.
42 // TODO(mek): Limit the number of geofences that are registered with the 41 class CONTENT_EXPORT GeofencingManager
43 // underlying GeofencingProvider. 42 : NON_EXPORTED_BASE(public GeofencingRegistrationDelegate),
44 class CONTENT_EXPORT GeofencingManager { 43 public base::RefCountedThreadSafe<GeofencingManager> {
45 public: 44 public:
46 typedef base::Callback<void(GeofencingStatus)> StatusCallback; 45 typedef base::Callback<void(GeofencingStatus)> StatusCallback;
47 typedef base::Callback<void(
48 GeofencingStatus,
49 const std::map<std::string, blink::WebCircularGeofencingRegion>&)>
50 RegistrationsCallback;
51 46
52 // Gets a pointer to the singleton instance of the geofencing manager. This 47 explicit GeofencingManager(
53 // must only be called on the IO thread so that the GeofencingManager is 48 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context);
54 // always instantiated on the same thread. Ownership is NOT returned. 49
55 static GeofencingManager* GetInstance(); 50 // Init and Shutdown are for use on the UI thread when the storagepartition is
51 // being setup and torn down.
52 void Init();
53 void Shutdown();
56 54
57 // Initiates registration of a new geofence. StatusCallback is called when 55 // Initiates registration of a new geofence. StatusCallback is called when
58 // registration has completed or failed (which could possibly be before 56 // registration has completed or failed (which could possibly be before
59 // RegisterRegion returns. 57 // RegisterRegion returns.
60 // Attempting to register a region with the same ID as an already registered 58 // Attempting to register a region with the same ID as an already registered
61 // (or in progress of being registered) region will fail. 59 // (or in progress of being registered) region will fail.
62 // TODO(mek): Behavior when using an already used ID might need to be revised 60 // 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. 61 // depending on what the actual spec ends up saying about this.
64 void RegisterRegion(BrowserContext* browser_context, 62 void RegisterRegion(int64 service_worker_registration_id,
65 int64 service_worker_registration_id,
66 const GURL& service_worker_origin,
67 const std::string& region_id, 63 const std::string& region_id,
68 const blink::WebCircularGeofencingRegion& region, 64 const blink::WebCircularGeofencingRegion& region,
69 const StatusCallback& callback); 65 const StatusCallback& callback);
70 66
71 // Unregister a region that was previously registered by a call to 67 // Unregister a region that was previously registered by a call to
72 // RegisterRegion. Any attempt to unregister a region that has not been 68 // RegisterRegion. Any attempt to unregister a region that has not been
73 // registered, or for which the registration is still in progress 69 // registered, or for which the registration is still in progress
74 // (RegisterRegion hasn't called its callback yet) will fail. 70 // (RegisterRegion hasn't called its callback yet) will fail.
75 // TODO(mek): Maybe better behavior would be to allow unregistering still 71 // TODO(mek): Maybe better behavior would be to allow unregistering still
76 // in-progress registrations. 72 // in-progress registrations.
77 void UnregisterRegion(BrowserContext* browser_context, 73 void UnregisterRegion(int64 service_worker_registration_id,
78 int64 service_worker_registration_id,
79 const GURL& service_worker_origin,
80 const std::string& region_id, 74 const std::string& region_id,
81 const StatusCallback& callback); 75 const StatusCallback& callback);
82 76
83 // Returns all currently registered regions. In case of failure (no geofencing 77 // Returns all currently registered regions. In case of failure (no geofencing
84 // provider available for example) return an error status, while leaving 78 // provider available for example) return an error status, while leaving
85 // |regions| untouched. 79 // |regions| untouched.
86 // This only returns regions for which the callback passed to RegisterRegion 80 // This only returns regions for which the callback passed to RegisterRegion
87 // has been called already (so it doesn't include still in progress 81 // has been called already (so it doesn't include still in progress
88 // registrations). 82 // registrations).
89 GeofencingStatus GetRegisteredRegions( 83 GeofencingStatus GetRegisteredRegions(
90 BrowserContext* browser_context,
91 int64 service_worker_registration_id, 84 int64 service_worker_registration_id,
92 const GURL& service_worker_origin, 85 std::map<std::string, blink::WebCircularGeofencingRegion>* result);
93 std::map<std::string, blink::WebCircularGeofencingRegion>* regions);
94 86
95 void SetProviderForTests(scoped_ptr<GeofencingProvider> provider); 87 void SetServiceForTesting(GeofencingService* service) {
88 service_ = service;
89 }
96 90
97 protected: 91 protected:
98 friend struct DefaultSingletonTraits<GeofencingManager>; 92 friend class base::RefCountedThreadSafe<GeofencingManager>;
99 friend class GeofencingManagerTest; 93 ~GeofencingManager() override;
100 GeofencingManager();
101 virtual ~GeofencingManager();
102 94
103 private: 95 private:
104 // Internal bookkeeping associated with each registered geofence. 96 // Internal bookkeeping associated with each registered geofence.
105 struct RegistrationKey;
106 struct Registration; 97 struct Registration;
107 class RegistrationMatches;
108 98
109 // Called by GeofencingProvider when the platform specific provider completes 99 void InitOnIO();
110 // registration of a geofence. 100 void ShutdownOnIO();
111 void RegisterRegionCompleted(const StatusCallback& callback, 101
112 const RegistrationKey& key, 102 // GeofencingRegistrationDelegate implementation.
113 GeofencingStatus status, 103 void RegistrationFinished(int64 geofencing_registration_id,
114 int registration_id); 104 GeofencingStatus status) override;
115 105
116 // Looks up a particular geofence registration. Returns nullptr if no 106 // Looks up a particular geofence registration. Returns nullptr if no
117 // registration with the given IDs exists. 107 // registration with the given IDs exists.
118 Registration* FindRegistration(const RegistrationKey& key); 108 Registration* FindRegistration(int64 service_worker_registration_id,
109 const std::string& region_id);
110
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);
119 114
120 // Registers a new registration, returning a reference to the newly inserted 115 // Registers a new registration, returning a reference to the newly inserted
121 // object. Assumes no registration with the same IDs currently exists. 116 // object. Assumes no registration with the same IDs currently exists.
122 Registration& AddRegistration( 117 Registration& AddRegistration(
123 const RegistrationKey& key, 118 int64 service_worker_registration_id,
124 const blink::WebCircularGeofencingRegion& region); 119 const std::string& region_id,
120 const blink::WebCircularGeofencingRegion& region,
121 const StatusCallback& callback,
122 int64 geofencing_registration_id);
125 123
126 // Clears a registration. 124 // Clears a registration.
127 void ClearRegistration(const RegistrationKey& key); 125 void ClearRegistration(Registration* registration);
128 126
129 // List of all currently registered geofences. 127 // Map of all registered regions for a particular service worker registration.
130 // TODO(mek): Better way of storing these that allows more efficient lookup 128 typedef std::map<std::string, Registration> RegionIdRegistrationMap;
131 // and deletion. 129 // Map of service worker registration id to the regions registered by that
132 std::vector<Registration> registrations_; 130 // service worker.
131 typedef std::map<int64, RegionIdRegistrationMap>
132 ServiceWorkerRegistrationsMap;
133 ServiceWorkerRegistrationsMap registrations_;
133 134
134 scoped_ptr<GeofencingProvider> provider_; 135 // Map of all registered regions by geofencing_registration_id.
136 typedef std::map<int64, RegionIdRegistrationMap::iterator>
137 RegistrationIdRegistrationMap;
138 RegistrationIdRegistrationMap registrations_by_id_;
139
140 GeofencingService* service_;
141 scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_;
135 142
136 DISALLOW_COPY_AND_ASSIGN(GeofencingManager); 143 DISALLOW_COPY_AND_ASSIGN(GeofencingManager);
137 }; 144 };
138 145
139 } // namespace content 146 } // namespace content
140 147
141 #endif // CONTENT_BROWSER_GEOFENCING_GEOFENCING_MANAGER_H_ 148 #endif // CONTENT_BROWSER_GEOFENCING_GEOFENCING_MANAGER_H_
OLDNEW
« no previous file with comments | « content/browser/geofencing/geofencing_dispatcher_host.cc ('k') | content/browser/geofencing/geofencing_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698