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
;
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
32 CameraPreferences::UpdatePref(const char* aPref
, nsresult
& aVal
)
35 nsresult rv
= Preferences::GetUint(aPref
, &val
);
36 if (NS_SUCCEEDED(rv
)) {
37 aVal
= static_cast<nsresult
>(val
);
45 CameraPreferences::UpdatePref(const char* aPref
, uint32_t& aVal
)
48 nsresult rv
= Preferences::GetUint(aPref
, &val
);
49 if (NS_SUCCEEDED(rv
)) {
57 CameraPreferences::UpdatePref(const char* aPref
, nsACString
& aVal
)
60 nsresult rv
= Preferences::GetCString(aPref
, &val
);
61 if (NS_SUCCEEDED(rv
)) {
69 CameraPreferences::UpdatePref(const char* aPref
, bool& aVal
)
72 nsresult rv
= Preferences::GetBool(aPref
, &val
);
73 if (NS_SUCCEEDED(rv
)) {
80 CameraPreferences::Pref
CameraPreferences::sPrefs
[] = {
82 "camera.control.test.enabled",
87 "camera.control.test.hardware",
89 { &sPrefHardwareTest
}
93 "camera.control.test.hardware.gonk.parameters",
95 { &sPrefGonkParameters
}
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",
111 { &sPrefCameraParametersIsLowMemory
}
114 "camera.control.low_memory_thresholdMB",
116 { &sPrefCameraControlLowMemoryThresholdMB
}
122 CameraPreferences::PrefToIndex(const char* aPref
)
124 for (uint32_t i
= 0; i
< ArrayLength(sPrefs
); ++i
) {
125 if (strcmp(aPref
, sPrefs
[i
].mPref
) == 0) {
129 return kPrefNotFound
;
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
);
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
);
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
);
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());
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");
191 MOZ_ASSERT_UNREACHABLE("Unhandled preference value type!");
196 DOM_CAMERA_LOGE("Failed to get pref '%s' (0x%x)\n", aPref
, rv
);
202 CameraPreferences::Initialize()
204 DOM_CAMERA_LOGI("Initializing camera preference callbacks\n");
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
,
217 if (NS_WARN_IF(NS_FAILED(rv
))) {
222 DOM_CAMERA_LOGI("Camera preferences initialized\n");
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
,
237 sPrefTestEnabled
= nullptr;
238 sPrefHardwareTest
= nullptr;
239 sPrefGonkParameters
= nullptr;
240 sPrefMonitor
= nullptr;
242 DOM_CAMERA_LOGI("Camera preferences shut down\n");
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
);
257 if (sPrefs
[i
].mValueType
!= kPrefValueIsCString
) {
258 DOM_CAMERA_LOGW("Preference '%s' is not a string type\n", aPref
);
262 StaticAutoPtr
<nsCString
>* s
= sPrefs
[i
].mValue
.mAsCString
;
264 DOM_CAMERA_LOGE("Preference '%s' cache is not initialized\n", aPref
);
267 if ((*s
)->IsEmpty()) {
268 DOM_CAMERA_LOGI("Preference '%s' is not set\n", aPref
);
272 DOM_CAMERA_LOGI("Preference '%s', got '%s'\n", aPref
, (*s
)->get());
277 #ifdef CAMERAPREFERENCES_HAVE_SEPARATE_UINT32_AND_NSRESULT
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
);
290 if (sPrefs
[i
].mValueType
!= kPrefValueIsNsResult
) {
291 DOM_CAMERA_LOGW("Preference '%s' is not an nsresult type\n", aPref
);
295 nsresult v
= *sPrefs
[i
].mValue
.mAsNsResult
;
297 DOM_CAMERA_LOGW("Preference '%s' is not set\n", aPref
);
301 DOM_CAMERA_LOGI("Preference '%s', got 0x%x\n", aPref
, v
);
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
);
319 if (sPrefs
[i
].mValueType
!= kPrefValueIsUint32
) {
320 DOM_CAMERA_LOGW("Preference '%s' is not a uint32_t type\n", aPref
);
324 uint32_t v
= *sPrefs
[i
].mValue
.mAsUint32
;
325 DOM_CAMERA_LOGI("Preference '%s', got %u\n", aPref
, v
);
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
);
342 if (sPrefs
[i
].mValueType
!= kPrefValueIsBoolean
) {
343 DOM_CAMERA_LOGW("Preference '%s' is not a boolean type\n", aPref
);
347 bool v
= *sPrefs
[i
].mValue
.mAsBoolean
;
348 DOM_CAMERA_LOGI("Preference '%s', got %s\n", aPref
, v
? "true" : "false");