forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
nsGeolocationSettings.h
166 lines (141 loc) · 5.49 KB
/
nsGeolocationSettings.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef nsGeolocationSettings_h
#define nsGeolocationSettings_h
#include "mozilla/Attributes.h"
#include "mozilla/StaticPtr.h"
#include "nsCOMPtr.h"
#include "nsAutoPtr.h"
#include "nsClassHashtable.h"
#include "nsString.h"
#include "nsIObserver.h"
#include "nsJSUtils.h"
#include "nsTArray.h"
#if (defined(MOZ_GPS_DEBUG) && defined(ANDROID))
#include <android/log.h>
#define GPSLOG(fmt, ...) __android_log_print(ANDROID_LOG_WARN, "GPS", "%12s:%-5d " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
#else
#define GPSLOG(...) {;}
#endif // MOZ_GPS_DEBUG && ANDROID
// The settings key.
#define GEO_ENABLED "geolocation.enabled"
#define GEO_ALA_ENABLED "ala.settings.enabled"
#define GEO_ALA_TYPE "geolocation.type"
#define GEO_ALA_FIXED_COORDS "geolocation.fixed_coords"
#define GEO_ALA_APP_SETTINGS "geolocation.app_settings"
#define GEO_ALA_ALWAYS_PRECISE "geolocation.always_precise"
#ifdef MOZ_APPROX_LOCATION
#define GEO_ALA_APPROX_DISTANCE "geolocation.approx_distance"
#endif
enum GeolocationFuzzMethod {
GEO_ALA_TYPE_PRECISE, // default, GPS/AGPS location
GEO_ALA_TYPE_FIXED, // user supplied lat/long
GEO_ALA_TYPE_NONE, // no location given
#ifdef MOZ_APPROX_LOCATION
GEO_ALA_TYPE_APPROX // approximate, grid-based location
#endif
};
#define GEO_ALA_TYPE_DEFAULT (GEO_ALA_TYPE_PRECISE)
#define GEO_ALA_TYPE_FIRST (GEO_ALA_TYPE_PRECISE)
#ifdef MOZ_APPROX_LOCATION
#define GEO_ALA_TYPE_LAST (GEO_ALA_TYPE_APPROX)
#else
#define GEO_ALA_TYPE_LAST (GEO_ALA_TYPE_NONE)
#endif
/**
* Simple class for holding the geolocation settings values.
*/
class GeolocationSetting final {
public:
explicit GeolocationSetting(const nsString& aOrigin) :
mFuzzMethod(GEO_ALA_TYPE_DEFAULT),
#ifdef MOZ_APPROX_LOCATION
mDistance(0),
#endif
mLatitude(0.0),
mLongitude(0.0),
mOrigin(aOrigin) {}
GeolocationSetting(const GeolocationSetting& rhs) :
mFuzzMethod(rhs.mFuzzMethod),
#ifdef MOZ_APPROX_LOCATION
mDistance(rhs.mDistance),
#endif
mLatitude(rhs.mLatitude),
mLongitude(rhs.mLongitude),
mOrigin(rhs.mOrigin) {}
~GeolocationSetting() {}
GeolocationSetting& operator=(const GeolocationSetting& rhs) {
mFuzzMethod = rhs.mFuzzMethod;
#ifdef MOZ_APPROX_LOCATION
mDistance = rhs.mDistance;
#endif
mLatitude = rhs.mLatitude;
mLongitude = rhs.mLongitude;
mOrigin = rhs.mOrigin;
return *this;
}
void HandleTypeChange(const JS::Value& aVal);
void HandleApproxDistanceChange(const JS::Value& aVal);
void HandleFixedCoordsChange(const JS::Value& aVal);
inline GeolocationFuzzMethod GetType() const { return mFuzzMethod; }
#ifdef MOZ_APPROX_LOCATION
inline int32_t GetApproxDistance() const { return mDistance; }
#endif
inline double GetFixedLatitude() const { return mLatitude; }
inline double GetFixedLongitude() const { return mLongitude; }
inline const nsString& GetOrigin() const { return mOrigin; }
private:
GeolocationSetting() {} // can't default construct
GeolocationFuzzMethod mFuzzMethod;
#ifdef MOZ_APPROX_LOCATION
int32_t mDistance;
#endif
double mLatitude,
mLongitude;
nsString mOrigin;
};
/**
* Singleton that holds the global and per-origin geolocation settings.
*/
class nsGeolocationSettings final : public nsIObserver
{
public:
static already_AddRefed<nsGeolocationSettings> GetGeolocationSettings();
static mozilla::StaticRefPtr<nsGeolocationSettings> sSettings;
NS_DECL_THREADSAFE_ISUPPORTS
NS_DECL_NSIOBSERVER
nsGeolocationSettings() : mAlaEnabled(false), mGlobalSetting(NullString()) {}
nsresult Init();
void HandleGeolocationSettingsChange(const nsAString& aKey, const JS::Value& aVal);
void HandleGeolocationSettingsError(const nsAString& aName);
void PutWatchOrigin(int32_t aWatchID, const nsCString& aOrigin);
void RemoveWatchOrigin(int32_t aWatchID);
void GetWatchOrigin(int32_t aWatchID, nsCString& aOrigin);
inline bool IsAlaEnabled() const { return mAlaEnabled; }
// given a watch ID, retrieve the geolocation settings. the watch ID is
// mapped to the origin of the listener/request which is then used to
// retreive the geolocation settings for the origin.
// if the origin is in the always-precise list, the settings will always be
// 'precise'. if the origin has origin-specific settings, that will be returned
// otherwise the global geolocation settings will be returned.
// NOTE: this returns a copy of the settings to enforce read-only client access
GeolocationSetting LookupGeolocationSetting(int32_t aWatchID);
private:
~nsGeolocationSettings() {}
nsGeolocationSettings(const nsGeolocationSettings&) :
mGlobalSetting(NullString()) {} // can't copy obj
void HandleMozsettingsChanged(nsISupports* aSubject);
void HandleGeolocationAlaEnabledChange(const JS::Value& aVal);
void HandleGeolocationPerOriginSettingsChange(const JS::Value& aVal);
void HandleGeolocationAlwaysPreciseChange(const JS::Value& aVal);
private:
bool mAlaEnabled;
GeolocationSetting mGlobalSetting;
nsClassHashtable<nsStringHashKey, GeolocationSetting> mPerOriginSettings;
nsTArray<nsString> mAlwaysPreciseApps;
nsClassHashtable<nsUint32HashKey, nsCString> mCurrentWatches;
};
#endif /* nsGeolocationSettings_h */