Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / dom / camera / CameraPreferences.cpp
blob509406e66c0d2cad5faac5fdf13282ce0f944b9d
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 #include "CameraPreferences.h"
7 #include "CameraCommon.h"
8 #include "mozilla/ArrayUtils.h"
9 #include "mozilla/Monitor.h"
10 #include "mozilla/StaticPtr.h"
11 #include "mozilla/Preferences.h"
13 using namespace mozilla;
15 /* statics */
16 static StaticAutoPtr<Monitor> sPrefMonitor;
18 StaticAutoPtr<nsCString> CameraPreferences::sPrefTestEnabled;
19 StaticAutoPtr<nsCString> CameraPreferences::sPrefHardwareTest;
20 StaticAutoPtr<nsCString> CameraPreferences::sPrefGonkParameters;
22 nsresult CameraPreferences::sPrefCameraControlMethodErrorOverride = NS_OK;
23 nsresult CameraPreferences::sPrefCameraControlAsyncErrorOverride = NS_OK;
25 uint32_t CameraPreferences::sPrefCameraControlLowMemoryThresholdMB = 0;
27 bool CameraPreferences::sPrefCameraParametersIsLowMemory = false;
29 #ifdef CAMERAPREFERENCES_HAVE_SEPARATE_UINT32_AND_NSRESULT
30 /* static */
31 nsresult
32 CameraPreferences::UpdatePref(const char* aPref, nsresult& aVal)
34 uint32_t val;
35 nsresult rv = Preferences::GetUint(aPref, &val);
36 if (NS_SUCCEEDED(rv)) {
37 aVal = static_cast<nsresult>(val);
39 return rv;
41 #endif
43 /* static */
44 nsresult
45 CameraPreferences::UpdatePref(const char* aPref, uint32_t& aVal)
47 uint32_t val;
48 nsresult rv = Preferences::GetUint(aPref, &val);
49 if (NS_SUCCEEDED(rv)) {
50 aVal = val;
52 return rv;
55 /* static */
56 nsresult
57 CameraPreferences::UpdatePref(const char* aPref, nsACString& aVal)
59 nsCString val;
60 nsresult rv = Preferences::GetCString(aPref, &val);
61 if (NS_SUCCEEDED(rv)) {
62 aVal = val;
64 return rv;
67 /* static */
68 nsresult
69 CameraPreferences::UpdatePref(const char* aPref, bool& aVal)
71 bool val;
72 nsresult rv = Preferences::GetBool(aPref, &val);
73 if (NS_SUCCEEDED(rv)) {
74 aVal = val;
76 return rv;
79 /* static */
80 CameraPreferences::Pref CameraPreferences::sPrefs[] = {
82 "camera.control.test.enabled",
83 kPrefValueIsCString,
84 { &sPrefTestEnabled }
87 "camera.control.test.hardware",
88 kPrefValueIsCString,
89 { &sPrefHardwareTest }
91 #ifdef MOZ_B2G
93 "camera.control.test.hardware.gonk.parameters",
94 kPrefValueIsCString,
95 { &sPrefGonkParameters }
97 #endif
99 "camera.control.test.method.error",
100 kPrefValueIsNsResult,
101 { &sPrefCameraControlMethodErrorOverride }
104 "camera.control.test.async.error",
105 kPrefValueIsNsResult,
106 { &sPrefCameraControlAsyncErrorOverride }
109 "camera.control.test.is_low_memory",
110 kPrefValueIsBoolean,
111 { &sPrefCameraParametersIsLowMemory }
114 "camera.control.low_memory_thresholdMB",
115 kPrefValueIsUint32,
116 { &sPrefCameraControlLowMemoryThresholdMB }
120 /* static */
121 uint32_t
122 CameraPreferences::PrefToIndex(const char* aPref)
124 for (uint32_t i = 0; i < ArrayLength(sPrefs); ++i) {
125 if (strcmp(aPref, sPrefs[i].mPref) == 0) {
126 return i;
129 return kPrefNotFound;
132 /* static */
133 void
134 CameraPreferences::PreferenceChanged(const char* aPref, void* aClosure)
136 MonitorAutoLock mon(*sPrefMonitor);
138 uint32_t i = PrefToIndex(aPref);
139 if (i == kPrefNotFound) {
140 DOM_CAMERA_LOGE("Preference '%s' is not tracked by CameraPreferences\n", aPref);
141 return;
144 Pref& p = sPrefs[i];
145 nsresult rv;
146 switch (p.mValueType) {
147 case kPrefValueIsNsResult:
148 #ifdef CAMERAPREFERENCES_HAVE_SEPARATE_UINT32_AND_NSRESULT
150 nsresult& v = *p.mValue.mAsNsResult;
151 rv = UpdatePref(aPref, v);
152 if (NS_SUCCEEDED(rv)) {
153 DOM_CAMERA_LOGI("Preference '%s' has changed, 0x%x\n", aPref, v);
156 break;
157 #endif
159 case kPrefValueIsUint32:
161 uint32_t& v = *p.mValue.mAsUint32;
162 rv = UpdatePref(aPref, v);
163 if (NS_SUCCEEDED(rv)) {
164 DOM_CAMERA_LOGI("Preference '%s' has changed, %u\n", aPref, v);
167 break;
169 case kPrefValueIsCString:
171 nsCString& v = **p.mValue.mAsCString;
172 rv = UpdatePref(aPref, v);
173 if (NS_SUCCEEDED(rv)) {
174 DOM_CAMERA_LOGI("Preference '%s' has changed, '%s'\n", aPref, v.get());
177 break;
179 case kPrefValueIsBoolean:
181 bool& v = *p.mValue.mAsBoolean;
182 rv = UpdatePref(aPref, v);
183 if (NS_SUCCEEDED(rv)) {
184 DOM_CAMERA_LOGI("Preference '%s' has changed, %s\n",
185 aPref, v ? "true" : "false");
188 break;
190 default:
191 MOZ_ASSERT_UNREACHABLE("Unhandled preference value type!");
192 return;
195 if (NS_FAILED(rv)) {
196 DOM_CAMERA_LOGE("Failed to get pref '%s' (0x%x)\n", aPref, rv);
200 /* static */
201 bool
202 CameraPreferences::Initialize()
204 DOM_CAMERA_LOGI("Initializing camera preference callbacks\n");
206 nsresult rv;
208 sPrefMonitor = new Monitor("CameraPreferences.sPrefMonitor");
210 sPrefTestEnabled = new nsCString();
211 sPrefHardwareTest = new nsCString();
212 sPrefGonkParameters = new nsCString();
214 for (uint32_t i = 0; i < ArrayLength(sPrefs); ++i) {
215 rv = Preferences::RegisterCallbackAndCall(CameraPreferences::PreferenceChanged,
216 sPrefs[i].mPref);
217 if (NS_WARN_IF(NS_FAILED(rv))) {
218 return false;
222 DOM_CAMERA_LOGI("Camera preferences initialized\n");
223 return true;
226 /* static */
227 void
228 CameraPreferences::Shutdown()
230 DOM_CAMERA_LOGI("Shutting down camera preference callbacks\n");
232 for (uint32_t i = 0; i < ArrayLength(sPrefs); ++i) {
233 Preferences::UnregisterCallback(CameraPreferences::PreferenceChanged,
234 sPrefs[i].mPref);
237 sPrefTestEnabled = nullptr;
238 sPrefHardwareTest = nullptr;
239 sPrefGonkParameters = nullptr;
240 sPrefMonitor = nullptr;
242 DOM_CAMERA_LOGI("Camera preferences shut down\n");
245 /* static */
246 bool
247 CameraPreferences::GetPref(const char* aPref, nsACString& aVal)
249 MOZ_ASSERT(sPrefMonitor, "sPrefMonitor missing in CameraPreferences::GetPref()");
250 MonitorAutoLock mon(*sPrefMonitor);
252 uint32_t i = PrefToIndex(aPref);
253 if (i == kPrefNotFound || i >= ArrayLength(sPrefs)) {
254 DOM_CAMERA_LOGW("Preference '%s' is not tracked by CameraPreferences\n", aPref);
255 return false;
257 if (sPrefs[i].mValueType != kPrefValueIsCString) {
258 DOM_CAMERA_LOGW("Preference '%s' is not a string type\n", aPref);
259 return false;
262 StaticAutoPtr<nsCString>* s = sPrefs[i].mValue.mAsCString;
263 if (!*s) {
264 DOM_CAMERA_LOGE("Preference '%s' cache is not initialized\n", aPref);
265 return false;
267 if ((*s)->IsEmpty()) {
268 DOM_CAMERA_LOGI("Preference '%s' is not set\n", aPref);
269 return false;
272 DOM_CAMERA_LOGI("Preference '%s', got '%s'\n", aPref, (*s)->get());
273 aVal = **s;
274 return true;
277 #ifdef CAMERAPREFERENCES_HAVE_SEPARATE_UINT32_AND_NSRESULT
278 /* static */
279 bool
280 CameraPreferences::GetPref(const char* aPref, nsresult& aVal)
282 MOZ_ASSERT(sPrefMonitor, "sPrefMonitor missing in CameraPreferences::GetPref()");
283 MonitorAutoLock mon(*sPrefMonitor);
285 uint32_t i = PrefToIndex(aPref);
286 if (i == kPrefNotFound || i >= ArrayLength(sPrefs)) {
287 DOM_CAMERA_LOGW("Preference '%s' is not tracked by CameraPreferences\n", aPref);
288 return false;
290 if (sPrefs[i].mValueType != kPrefValueIsNsResult) {
291 DOM_CAMERA_LOGW("Preference '%s' is not an nsresult type\n", aPref);
292 return false;
295 nsresult v = *sPrefs[i].mValue.mAsNsResult;
296 if (v == NS_OK) {
297 DOM_CAMERA_LOGW("Preference '%s' is not set\n", aPref);
298 return false;
301 DOM_CAMERA_LOGI("Preference '%s', got 0x%x\n", aPref, v);
302 aVal = v;
303 return true;
305 #endif
307 /* static */
308 bool
309 CameraPreferences::GetPref(const char* aPref, uint32_t& aVal)
311 MOZ_ASSERT(sPrefMonitor, "sPrefMonitor missing in CameraPreferences::GetPref()");
312 MonitorAutoLock mon(*sPrefMonitor);
314 uint32_t i = PrefToIndex(aPref);
315 if (i == kPrefNotFound || i >= ArrayLength(sPrefs)) {
316 DOM_CAMERA_LOGW("Preference '%s' is not tracked by CameraPreferences\n", aPref);
317 return false;
319 if (sPrefs[i].mValueType != kPrefValueIsUint32) {
320 DOM_CAMERA_LOGW("Preference '%s' is not a uint32_t type\n", aPref);
321 return false;
324 uint32_t v = *sPrefs[i].mValue.mAsUint32;
325 DOM_CAMERA_LOGI("Preference '%s', got %u\n", aPref, v);
326 aVal = v;
327 return true;
330 /* static */
331 bool
332 CameraPreferences::GetPref(const char* aPref, bool& aVal)
334 MOZ_ASSERT(sPrefMonitor, "sPrefMonitor missing in CameraPreferences::GetPref()");
335 MonitorAutoLock mon(*sPrefMonitor);
337 uint32_t i = PrefToIndex(aPref);
338 if (i == kPrefNotFound || i >= ArrayLength(sPrefs)) {
339 DOM_CAMERA_LOGW("Preference '%s' is not tracked by CameraPreferences\n", aPref);
340 return false;
342 if (sPrefs[i].mValueType != kPrefValueIsBoolean) {
343 DOM_CAMERA_LOGW("Preference '%s' is not a boolean type\n", aPref);
344 return false;
347 bool v = *sPrefs[i].mValue.mAsBoolean;
348 DOM_CAMERA_LOGI("Preference '%s', got %s\n", aPref, v ? "true" : "false");
349 aVal = v;
350 return true;