• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #include "base/memory/weak_ptr.h"
12 #include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
13 #include "content/public/browser/notification_observer.h"
14 #include "content/public/browser/notification_registrar.h"
15 
16 class Profile;
17 
18 namespace content {
19 struct Geoposition;
20 }  // namespace content
21 
22 namespace extensions {
23 class LocationManager;
24 class LocationRequest;
25 
26 namespace api {
27 namespace location {
28 
29 struct Coordinates;
30 
31 }  // namespace location
32 }  // namespace api
33 
34 // Profile's manager of all location watch requests created by chrome.location
35 // API. Lives in the UI thread.
36 class LocationManager
37     : public ProfileKeyedAPI,
38       public content::NotificationObserver,
39       public base::SupportsWeakPtr<LocationManager> {
40  public:
41   explicit LocationManager(Profile* profile);
42   virtual ~LocationManager();
43 
44   // Adds location request for the given extension, and starts the location
45   // tracking.
46   void AddLocationRequest(
47       const std::string& extension_id,
48       const std::string& request_name,
49       const double* distance_update_threshold_meters,
50       const double* time_between_updates_ms);
51 
52   // Cancels and removes the request with the given |name| for the given
53   // extension.
54   void RemoveLocationRequest(const std::string& extension_id,
55                              const std::string& name);
56 
57   // ProfileKeyedAPI implementation.
58   static ProfileKeyedAPIFactory<LocationManager>* GetFactoryInstance();
59 
60   // Convenience method to get the LocationManager for a profile.
61   static LocationManager* Get(Profile* profile);
62 
63  private:
64   friend class LocationRequest;
65   friend class ProfileKeyedAPIFactory<LocationManager>;
66 
67   typedef std::string ExtensionId;
68   typedef scoped_refptr<LocationRequest> LocationRequestPointer;
69   typedef std::multimap<ExtensionId, LocationRequestPointer> LocationRequestMap;
70   typedef LocationRequestMap::iterator LocationRequestIterator;
71 
72   // Converts |position| from GeolocationProvider to the location API
73   // |coordinates|.
74   static void GeopositionToApiCoordinates(
75       const content::Geoposition& position,
76       api::location::Coordinates* coordinates);
77 
78   // Sends a location update to the extension.
79   void SendLocationUpdate(const std::string& extension_id,
80                           const std::string& request_name,
81                           const content::Geoposition& position);
82 
83   // NotificationObserver:
84   virtual void Observe(int type,
85                        const content::NotificationSource& source,
86                        const content::NotificationDetails& details) OVERRIDE;
87 
88   // ProfileKeyedAPI implementation.
service_name()89   static const char* service_name() { return "LocationManager"; }
90 
91   // Profile for this location manager.
92   Profile* const profile_;
93 
94   // A map of our pending location requests, per extension.
95   // Invariant: None of the LocationRequestLists are empty.
96   LocationRequestMap location_requests_;
97 
98   // Used for tracking registrations to profile's extensions events.
99   content::NotificationRegistrar registrar_;
100 
101   DISALLOW_COPY_AND_ASSIGN(LocationManager);
102 };
103 
104 }  // namespace extensions
105 
106 #endif  // CHROME_BROWSER_EXTENSIONS_API_LOCATION_LOCATION_MANAGER_H_
107