Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / location / location_manager.h
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   ~LocationManager() override;
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   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;
85
86   // BrowserContextKeyedAPI implementation.
87   static const char* service_name() { return "LocationManager"; }
88
89   content::BrowserContext* const browser_context_;
90
91   // A map of our pending location requests, per extension.
92   // Invariant: None of the LocationRequestLists are empty.
93   LocationRequestMap location_requests_;
94
95   ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
96       extension_registry_observer_;
97
98   DISALLOW_COPY_AND_ASSIGN(LocationManager);
99 };
100
101 }  // namespace extensions
102
103 #endif  // CHROME_BROWSER_EXTENSIONS_API_LOCATION_LOCATION_MANAGER_H_