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