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