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_SERVICE_H_ | |
6 #define CONTENT_BROWSER_GEOFENCING_GEOFENCING_SERVICE_H_ | |
7 | |
8 #include <stdint.h> | |
9 | |
10 #include <map> | |
11 #include <memory> | |
12 | |
13 #include "base/compiler_specific.h" | |
14 #include "base/macros.h" | |
15 #include "content/common/content_export.h" | |
16 #include "content/common/geofencing_types.h" | |
17 | |
18 namespace base { | |
19 template <typename T> | |
20 struct DefaultSingletonTraits; | |
21 } | |
22 | |
23 namespace blink { | |
24 struct WebCircularGeofencingRegion; | |
25 }; | |
26 | |
27 namespace content { | |
28 | |
29 class GeofencingProvider; | |
30 class GeofencingRegistrationDelegate; | |
31 | |
32 // This interface exists primarily to facilitate testing of classes that depend | |
33 // on GeofencingService. It defines the interface exposed by | |
34 // |GeofencingService|. | |
35 class GeofencingService { | |
36 public: | |
37 virtual ~GeofencingService() {} | |
38 | |
39 // Returns if a geofencing service is available. | |
40 virtual bool IsServiceAvailable() = 0; | |
41 | |
42 // Register a region. This returns a unique registration ID, and | |
43 // asynchronously calls the |delegate|s RegistrationFinished method to | |
44 // inform the delegate of the result of the attempt to register the region. | |
45 // This does not transfer ownership of the |delegate|. Callers have to ensure | |
46 // that the delegate remains valid as long as the region is registered. | |
47 virtual int64_t RegisterRegion( | |
48 const blink::WebCircularGeofencingRegion& region, | |
49 GeofencingRegistrationDelegate* delegate) = 0; | |
50 | |
51 // Unregister a region. This is assumed to always succeed. It is safe to call | |
52 // this even if a registration is still in progress. | |
53 virtual void UnregisterRegion(int64_t geofencing_registration_id) = 0; | |
54 }; | |
55 | |
56 // This class combines all the geofence registrations from the various per | |
57 // storage partition |GeofencingManager| instances, and registers a subset | |
58 // of those fences with an underlying platform specific |GeofencingProvider|. | |
59 // TODO(mek): Limit the number of geofences that are registered with the | |
60 // underlying GeofencingProvider. | |
61 class CONTENT_EXPORT GeofencingServiceImpl | |
62 : NON_EXPORTED_BASE(public GeofencingService) { | |
63 public: | |
64 // Gets a pointer to the singleton instance of the geofencing service. This | |
65 // must only be called on the IO thread so that the GeofencingService is | |
66 // always instantiated on the same thread. Ownership is NOT returned. | |
67 static GeofencingServiceImpl* GetInstance(); | |
68 | |
69 // GeofencingServiceInterface implementation. | |
70 bool IsServiceAvailable() override; | |
71 int64_t RegisterRegion(const blink::WebCircularGeofencingRegion& region, | |
72 GeofencingRegistrationDelegate* delegate) override; | |
73 void UnregisterRegion(int64_t geofencing_registration_id) override; | |
74 | |
75 protected: | |
76 friend class GeofencingServiceTest; | |
77 friend struct base::DefaultSingletonTraits<GeofencingServiceImpl>; | |
78 GeofencingServiceImpl(); | |
79 ~GeofencingServiceImpl() override; | |
80 | |
81 void SetProviderForTesting(std::unique_ptr<GeofencingProvider> provider); | |
82 int RegistrationCountForTesting(); | |
83 | |
84 private: | |
85 struct Registration; | |
86 typedef std::map<int64_t, Registration> RegistrationsMap; | |
87 | |
88 // This method checks if a |GeofencingProvider| exists, creates a new one if | |
89 // not, and finally returns false if it can't create a provider for the | |
90 // current platform. | |
91 bool EnsureProvider(); | |
92 | |
93 // Returns a new unique ID to use for the next geofence registration. | |
94 int64_t GetNextId(); | |
95 | |
96 // Notifies the correct delegate that registration has completed for a | |
97 // specific geofence registration. | |
98 void NotifyRegistrationFinished(int64_t geofencing_registration_id, | |
99 GeofencingStatus status); | |
100 | |
101 int64_t next_registration_id_; | |
102 RegistrationsMap registrations_; | |
103 std::unique_ptr<GeofencingProvider> provider_; | |
104 | |
105 DISALLOW_COPY_AND_ASSIGN(GeofencingServiceImpl); | |
106 }; | |
107 | |
108 } // namespace content | |
109 | |
110 #endif // CONTENT_BROWSER_GEOFENCING_GEOFENCING_SERVICE_H_ | |
OLD | NEW |