Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / content / browser / geofencing / geofencing_manager.h
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/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "content/browser/geofencing/geofencing_registration_delegate.h"
17 #include "content/browser/service_worker/service_worker_storage.h"
18 #include "content/common/content_export.h"
19 #include "content/common/geofencing_status.h"
20 #include "content/common/service_worker/service_worker_status_code.h"
21
22 template <typename T>
23 struct DefaultSingletonTraits;
24 class GURL;
25
26 namespace blink {
27 struct WebCircularGeofencingRegion;
28 };
29
30 namespace content {
31
32 class GeofencingService;
33 class ServiceWorkerContextWrapper;
34 class ServiceWorkerRegistration;
35
36 // This is the main API to the geofencing subsystem. There is one instance of
37 // this class per storage partition.
38 // This class is responsible for keeping track of which geofences are currently
39 // registered by websites/workers, persisting this list of registrations and
40 // registering them with the global |GeofencingService|.
41 // This class is created on the UI thread, but all its methods should only be
42 // called from the IO thread.
43 // TODO(mek): Implement some kind of persistence of registrations.
44 // TODO(mek): Unregister a geofence when the ServiceWorkerRegistration it
45 //    belongs to goes away.
46 class CONTENT_EXPORT GeofencingManager
47     : NON_EXPORTED_BASE(public GeofencingRegistrationDelegate),
48       public base::RefCountedThreadSafe<GeofencingManager> {
49  public:
50   typedef base::Callback<void(GeofencingStatus)> StatusCallback;
51
52   explicit GeofencingManager(
53       const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context);
54
55   // Init and Shutdown are for use on the UI thread when the storagepartition is
56   // being setup and torn down.
57   void Init();
58   void Shutdown();
59
60   // Initiates registration of a new geofence. StatusCallback is called when
61   // registration has completed or failed (which could possibly be before
62   // RegisterRegion returns.
63   // Attempting to register a region with the same ID as an already registered
64   // (or in progress of being registered) region will fail.
65   // TODO(mek): Behavior when using an already used ID might need to be revised
66   //     depending on what the actual spec ends up saying about this.
67   void RegisterRegion(int64 service_worker_registration_id,
68                       const std::string& region_id,
69                       const blink::WebCircularGeofencingRegion& region,
70                       const StatusCallback& callback);
71
72   // Unregister a region that was previously registered by a call to
73   // RegisterRegion. Any attempt to unregister a region that has not been
74   // registered, or for which the registration is still in progress
75   // (RegisterRegion hasn't called its callback yet) will fail.
76   // TODO(mek): Maybe better behavior would be to allow unregistering still
77   //     in-progress registrations.
78   void UnregisterRegion(int64 service_worker_registration_id,
79                         const std::string& region_id,
80                         const StatusCallback& callback);
81
82   // Returns all currently registered regions. In case of failure (no geofencing
83   // provider available for example) return an error status, while leaving
84   // |regions| untouched.
85   // This only returns regions for which the callback passed to RegisterRegion
86   // has been called already (so it doesn't include still in progress
87   // registrations).
88   GeofencingStatus GetRegisteredRegions(
89       int64 service_worker_registration_id,
90       std::map<std::string, blink::WebCircularGeofencingRegion>* result);
91
92   void SetServiceForTesting(GeofencingService* service) {
93     service_ = service;
94   }
95
96  protected:
97   friend class base::RefCountedThreadSafe<GeofencingManager>;
98   ~GeofencingManager() override;
99
100  private:
101   // Internal bookkeeping associated with each registered geofence.
102   struct Registration;
103
104   void InitOnIO();
105   void ShutdownOnIO();
106
107   // GeofencingRegistrationDelegate implementation.
108   void RegistrationFinished(int64 geofencing_registration_id,
109                             GeofencingStatus status) override;
110   void RegionEntered(int64 geofencing_registration_id) override;
111   void RegionExited(int64 geofencing_registration_id) override;
112
113   // Looks up a particular geofence registration. Returns nullptr if no
114   // registration with the given IDs exists.
115   Registration* FindRegistration(int64 service_worker_registration_id,
116                                  const std::string& region_id);
117
118   // Looks up a particular geofence registration. Returns nullptr if no
119   // registration with the given ID exists.
120   Registration* FindRegistrationById(int64 geofencing_registration_id);
121
122   // Registers a new registration, returning a reference to the newly inserted
123   // object. Assumes no registration with the same IDs currently exists.
124   Registration& AddRegistration(
125       int64 service_worker_registration_id,
126       const GURL& service_worker_origin,
127       const std::string& region_id,
128       const blink::WebCircularGeofencingRegion& region,
129       const StatusCallback& callback,
130       int64 geofencing_registration_id);
131
132   // Clears a registration.
133   void ClearRegistration(Registration* registration);
134
135   // Starts dispatching a particular geofencing |event_type| for the geofence
136   // registration with the given ID. This first looks up the Service Worker
137   // Registration the geofence is associated with, and then attempts to deliver
138   // the event to that service worker.
139   void DispatchGeofencingEvent(blink::WebGeofencingEventType event_type,
140                                int64 geofencing_registration_id);
141
142   // Delivers an event to the specified service worker for the given geofence.
143   // If the geofence registration id is no longer valid, this method does
144   // nothing. This assumes the |service_worker_registration| is the service
145   // worker the geofence registration is associated with.
146   void DeliverGeofencingEvent(blink::WebGeofencingEventType event_type,
147                               int64 geofencing_registration_id,
148                               ServiceWorkerStatusCode service_worker_status,
149                               const scoped_refptr<ServiceWorkerRegistration>&
150                                   service_worker_registration);
151
152   // Called when delivery of a geofence event to a service worker has finished
153   // (or failed to finish).
154   void DeliverGeofencingEventEnd(const scoped_refptr<ServiceWorkerRegistration>&
155                                      service_worker_registration,
156                                  ServiceWorkerStatusCode service_worker_status);
157
158   // Map of all registered regions for a particular service worker registration.
159   typedef std::map<std::string, Registration> RegionIdRegistrationMap;
160   // Map of service worker registration id to the regions registered by that
161   // service worker.
162   typedef std::map<int64, RegionIdRegistrationMap>
163       ServiceWorkerRegistrationsMap;
164   ServiceWorkerRegistrationsMap registrations_;
165
166   // Map of all registered regions by geofencing_registration_id.
167   typedef std::map<int64, RegionIdRegistrationMap::iterator>
168       RegistrationIdRegistrationMap;
169   RegistrationIdRegistrationMap registrations_by_id_;
170
171   GeofencingService* service_;
172   scoped_refptr<ServiceWorkerContextWrapper> service_worker_context_;
173
174   DISALLOW_COPY_AND_ASSIGN(GeofencingManager);
175 };
176
177 }  // namespace content
178
179 #endif  // CONTENT_BROWSER_GEOFENCING_GEOFENCING_MANAGER_H_