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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_LOCATION_LOCATION_MANAGER_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_LOCATION_LOCATION_MANAGER_H_
10 #include "base/scoped_observer.h"
11 #include "extensions/browser/browser_context_keyed_api_factory.h"
12 #include "extensions/browser/extension_registry_observer.h"
17 } // namespace content
19 namespace extensions
{
20 class ExtensionRegistry
;
21 class LocationManager
;
22 class LocationRequest
;
29 } // namespace location
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
{
37 explicit LocationManager(content::BrowserContext
* context
);
38 ~LocationManager() override
;
40 // Adds location request for the given extension, and starts the location
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
);
48 // Cancels and removes the request with the given |name| for the given
50 void RemoveLocationRequest(const std::string
& extension_id
,
51 const std::string
& name
);
53 // BrowserContextKeyedAPI implementation.
54 static BrowserContextKeyedAPIFactory
<LocationManager
>* GetFactoryInstance();
56 // Convenience method to get the LocationManager for a context.
57 static LocationManager
* Get(content::BrowserContext
* context
);
60 friend class LocationRequest
;
61 friend class BrowserContextKeyedAPIFactory
<LocationManager
>;
63 typedef std::string ExtensionId
;
64 typedef scoped_refptr
<LocationRequest
> LocationRequestPointer
;
65 typedef std::multimap
<ExtensionId
, LocationRequestPointer
> LocationRequestMap
;
66 typedef LocationRequestMap::iterator LocationRequestIterator
;
68 // Converts |position| from GeolocationProvider to the location API
70 static void GeopositionToApiCoordinates(
71 const content::Geoposition
& position
,
72 api::location::Coordinates
* coordinates
);
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
);
79 // ExtensionRegistryObserver implementation.
80 void OnExtensionLoaded(content::BrowserContext
* browser_context
,
81 const Extension
* extension
) override
;
82 void OnExtensionUnloaded(content::BrowserContext
* browser_context
,
83 const Extension
* extension
,
84 UnloadedExtensionInfo::Reason reason
) override
;
86 // BrowserContextKeyedAPI implementation.
87 static const char* service_name() { return "LocationManager"; }
89 content::BrowserContext
* const browser_context_
;
91 // A map of our pending location requests, per extension.
92 // Invariant: None of the LocationRequestLists are empty.
93 LocationRequestMap location_requests_
;
95 ScopedObserver
<ExtensionRegistry
, ExtensionRegistryObserver
>
96 extension_registry_observer_
;
98 DISALLOW_COPY_AND_ASSIGN(LocationManager
);
101 } // namespace extensions
103 #endif // CHROME_BROWSER_EXTENSIONS_API_LOCATION_LOCATION_MANAGER_H_