location_manager.h revision a93a17c8d99d686bd4a1511e5504e5e6cc9fcadf
1// Copyright (c) 2013 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 CHROME_BROWSER_EXTENSIONS_API_LOCATION_LOCATION_MANAGER_H_
6#define CHROME_BROWSER_EXTENSIONS_API_LOCATION_LOCATION_MANAGER_H_
7
8#include <map>
9#include <string>
10#include <vector>
11
12#include "base/memory/weak_ptr.h"
13#include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
14#include "content/public/browser/notification_observer.h"
15#include "content/public/browser/notification_registrar.h"
16
17class Profile;
18
19namespace content {
20struct Geoposition;
21}  // namespace content
22
23namespace extensions {
24class LocationManager;
25class LocationRequest;
26
27namespace api {
28namespace location {
29
30struct Coordinates;
31
32}  // namespace location
33}  // namespace api
34
35// Profile's manager of all location watch requests created by chrome.location
36// API. Lives in the UI thread.
37class LocationManager
38    : public ProfileKeyedAPI,
39      public content::NotificationObserver,
40      public base::SupportsWeakPtr<LocationManager> {
41 public:
42  explicit LocationManager(Profile* profile);
43  virtual ~LocationManager();
44
45  // Adds location request for the given extension, and starts the location
46  // tracking.
47  void AddLocationRequest(const std::string& extension_id,
48                          const std::string& request_name);
49
50  // Cancels and removes the request with the given |name| for the given
51  // extension.
52  void RemoveLocationRequest(const std::string& extension_id,
53                             const std::string& name);
54
55  // ProfileKeyedAPI implementation.
56  static ProfileKeyedAPIFactory<LocationManager>* GetFactoryInstance();
57
58  // Convenience method to get the LocationManager for a profile.
59  static LocationManager* Get(Profile* profile);
60
61 private:
62  friend class LocationRequest;
63  friend class ProfileKeyedAPIFactory<LocationManager>;
64
65  typedef scoped_refptr<LocationRequest> LocationRequestPointer;
66  typedef std::vector<LocationRequestPointer> LocationRequestList;
67  typedef std::string ExtensionId;
68
69  // TODO(vadimt): Consider converting to multimap.
70  typedef std::map<ExtensionId, LocationRequestList> LocationRequestMap;
71
72  // Iterator used to identify a particular request within the Map/List pair.
73  // "Not found" is represented by <location_requests_.end(), invalid_iterator>.
74  typedef std::pair<LocationRequestMap::iterator, LocationRequestList::iterator>
75      LocationRequestIterator;
76
77  // Helper to return the iterators within the LocationRequestMap and
78  // LocationRequestList for the  matching request, or an iterator to the end of
79  // the LocationRequestMap if none were found.
80  LocationRequestIterator GetLocationRequestIterator(
81      const std::string& extension_id,
82      const std::string& name);
83
84  // Converts |position| from GeolocationProvider to the location API
85  // |coordinates|.
86  static void GeopositionToApiCoordinates(
87      const content::Geoposition& position,
88      api::location::Coordinates* coordinates);
89
90  // Helper to cancel and remove the request at the given iterator. The iterator
91  // must be valid.
92  void RemoveLocationRequestIterator(const LocationRequestIterator& iter);
93
94  // Sends a location update to the extension.
95  void SendLocationUpdate(const std::string& extension_id,
96                          const std::string& request_name,
97                          const content::Geoposition& position);
98
99  // NotificationObserver:
100  virtual void Observe(int type,
101                       const content::NotificationSource& source,
102                       const content::NotificationDetails& details) OVERRIDE;
103
104  // ProfileKeyedAPI implementation.
105  static const char* service_name() { return "LocationManager"; }
106
107  // Profile for this location manager.
108  Profile* const profile_;
109
110  // A map of our pending location requests, per extension.
111  // Invariant: None of the LocationRequestLists are empty.
112  LocationRequestMap location_requests_;
113
114  // Used for tracking registrations to profile's extensions events.
115  content::NotificationRegistrar registrar_;
116
117  DISALLOW_COPY_AND_ASSIGN(LocationManager);
118};
119
120}  // namespace extensions
121
122#endif  // CHROME_BROWSER_EXTENSIONS_API_LOCATION_LOCATION_MANAGER_H_
123