1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef nsGeolocationSettings_h
7 #define nsGeolocationSettings_h
9 #include "mozilla/Attributes.h"
10 #include "mozilla/StaticPtr.h"
12 #include "nsAutoPtr.h"
13 #include "nsClassHashtable.h"
15 #include "nsIObserver.h"
16 #include "nsJSUtils.h"
19 #if (defined(MOZ_GPS_DEBUG) && defined(ANDROID))
20 #include <android/log.h>
21 #define GPSLOG(fmt, ...) __android_log_print(ANDROID_LOG_WARN, "GPS", "%12s:%-5d " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
23 #define GPSLOG(...) {;}
24 #endif // MOZ_GPS_DEBUG && ANDROID
27 #define GEO_ENABLED "geolocation.enabled"
28 #define GEO_ALA_ENABLED "ala.settings.enabled"
29 #define GEO_ALA_TYPE "geolocation.type"
30 #define GEO_ALA_FIXED_COORDS "geolocation.fixed_coords"
31 #define GEO_ALA_APP_SETTINGS "geolocation.app_settings"
32 #define GEO_ALA_ALWAYS_PRECISE "geolocation.always_precise"
33 #ifdef MOZ_APPROX_LOCATION
34 #define GEO_ALA_APPROX_DISTANCE "geolocation.approx_distance"
37 enum GeolocationFuzzMethod
{
38 GEO_ALA_TYPE_PRECISE
, // default, GPS/AGPS location
39 GEO_ALA_TYPE_FIXED
, // user supplied lat/long
40 GEO_ALA_TYPE_NONE
, // no location given
41 #ifdef MOZ_APPROX_LOCATION
42 GEO_ALA_TYPE_APPROX
// approximate, grid-based location
46 #define GEO_ALA_TYPE_DEFAULT (GEO_ALA_TYPE_PRECISE)
47 #define GEO_ALA_TYPE_FIRST (GEO_ALA_TYPE_PRECISE)
48 #ifdef MOZ_APPROX_LOCATION
49 #define GEO_ALA_TYPE_LAST (GEO_ALA_TYPE_APPROX)
51 #define GEO_ALA_TYPE_LAST (GEO_ALA_TYPE_NONE)
55 * Simple class for holding the geolocation settings values.
58 class GeolocationSetting MOZ_FINAL
{
60 explicit GeolocationSetting(const nsString
& aOrigin
) :
61 mFuzzMethod(GEO_ALA_TYPE_DEFAULT
),
62 #ifdef MOZ_APPROX_LOCATION
69 GeolocationSetting(const GeolocationSetting
& rhs
) :
70 mFuzzMethod(rhs
.mFuzzMethod
),
71 #ifdef MOZ_APPROX_LOCATION
72 mDistance(rhs
.mDistance
),
74 mLatitude(rhs
.mLatitude
),
75 mLongitude(rhs
.mLongitude
),
76 mOrigin(rhs
.mOrigin
) {}
78 ~GeolocationSetting() {}
80 GeolocationSetting
& operator=(const GeolocationSetting
& rhs
) {
81 mFuzzMethod
= rhs
.mFuzzMethod
;
82 #ifdef MOZ_APPROX_LOCATION
83 mDistance
= rhs
.mDistance
;
85 mLatitude
= rhs
.mLatitude
;
86 mLongitude
= rhs
.mLongitude
;
87 mOrigin
= rhs
.mOrigin
;
91 void HandleTypeChange(const JS::Value
& aVal
);
92 void HandleApproxDistanceChange(const JS::Value
& aVal
);
93 void HandleFixedCoordsChange(const JS::Value
& aVal
);
95 inline GeolocationFuzzMethod
GetType() const { return mFuzzMethod
; }
96 #ifdef MOZ_APPROX_LOCATION
97 inline int32_t GetApproxDistance() const { return mDistance
; }
99 inline double GetFixedLatitude() const { return mLatitude
; }
100 inline double GetFixedLongitude() const { return mLongitude
; }
101 inline const nsString
& GetOrigin() const { return mOrigin
; }
104 GeolocationSetting() {} // can't default construct
105 GeolocationFuzzMethod mFuzzMethod
;
106 #ifdef MOZ_APPROX_LOCATION
115 * Singleton that holds the global and per-origin geolocation settings.
117 class nsGeolocationSettings MOZ_FINAL
: public nsIObserver
120 static already_AddRefed
<nsGeolocationSettings
> GetGeolocationSettings();
121 static mozilla::StaticRefPtr
<nsGeolocationSettings
> sSettings
;
123 NS_DECL_THREADSAFE_ISUPPORTS
126 nsGeolocationSettings() : mAlaEnabled(false), mGlobalSetting(NullString()) {}
129 void HandleGeolocationSettingsChange(const nsAString
& aKey
, const JS::Value
& aVal
);
130 void HandleGeolocationSettingsError(const nsAString
& aName
);
132 void PutWatchOrigin(int32_t aWatchID
, const nsCString
& aOrigin
);
133 void RemoveWatchOrigin(int32_t aWatchID
);
134 void GetWatchOrigin(int32_t aWatchID
, nsCString
& aOrigin
);
135 inline bool IsAlaEnabled() const { return mAlaEnabled
; }
137 // given a watch ID, retrieve the geolocation settings. the watch ID is
138 // mapped to the origin of the listener/request which is then used to
139 // retreive the geolocation settings for the origin.
140 // if the origin is in the always-precise list, the settings will always be
141 // 'precise'. if the origin has origin-specific settings, that will be returned
142 // otherwise the global geolocation settings will be returned.
143 // NOTE: this returns a copy of the settings to enforce read-only client access
144 GeolocationSetting
LookupGeolocationSetting(int32_t aWatchID
);
147 ~nsGeolocationSettings() {}
148 nsGeolocationSettings(const nsGeolocationSettings
&) :
149 mGlobalSetting(NullString()) {} // can't copy obj
151 void HandleMozsettingsChanged(nsISupports
* aSubject
);
152 void HandleGeolocationAlaEnabledChange(const JS::Value
& aVal
);
153 void HandleGeolocationPerOriginSettingsChange(const JS::Value
& aVal
);
154 void HandleGeolocationAlwaysPreciseChange(const JS::Value
& aVal
);
158 GeolocationSetting mGlobalSetting
;
159 nsClassHashtable
<nsStringHashKey
, GeolocationSetting
> mPerOriginSettings
;
160 nsTArray
<nsString
> mAlwaysPreciseApps
;
161 nsClassHashtable
<nsUint32HashKey
, nsCString
> mCurrentWatches
;
164 #endif /* nsGeolocationSettings_h */