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 "DOMCameraManager.h"
9 #include "mozilla/ArrayUtils.h"
10 #include "mozilla/Monitor.h"
11 #include "mozilla/StaticPtr.h"
12 #include "mozilla/Preferences.h"
13 #ifdef MOZ_WIDGET_GONK
14 #include "mozilla/Services.h"
15 #include "nsIObserverService.h"
18 using namespace mozilla
;
21 static StaticAutoPtr
<Monitor
> sPrefMonitor
;
23 StaticAutoPtr
<nsCString
> CameraPreferences::sPrefTestEnabled
;
24 StaticAutoPtr
<nsCString
> CameraPreferences::sPrefHardwareTest
;
25 StaticAutoPtr
<nsCString
> CameraPreferences::sPrefGonkParameters
;
27 nsresult
CameraPreferences::sPrefCameraControlMethodErrorOverride
= NS_OK
;
28 nsresult
CameraPreferences::sPrefCameraControlAsyncErrorOverride
= NS_OK
;
30 uint32_t CameraPreferences::sPrefCameraControlLowMemoryThresholdMB
= 0;
32 bool CameraPreferences::sPrefCameraParametersIsLowMemory
= false;
34 #ifdef MOZ_WIDGET_GONK
35 StaticRefPtr
<CameraPreferences
> CameraPreferences::sObserver
;
37 NS_IMPL_ISUPPORTS(CameraPreferences
, nsIObserver
);
40 #ifdef CAMERAPREFERENCES_HAVE_SEPARATE_UINT32_AND_NSRESULT
43 CameraPreferences::UpdatePref(const char* aPref
, nsresult
& aVal
)
46 nsresult rv
= Preferences::GetUint(aPref
, &val
);
47 if (NS_SUCCEEDED(rv
)) {
48 aVal
= static_cast<nsresult
>(val
);
56 CameraPreferences::UpdatePref(const char* aPref
, uint32_t& aVal
)
59 nsresult rv
= Preferences::GetUint(aPref
, &val
);
60 if (NS_SUCCEEDED(rv
)) {
68 CameraPreferences::UpdatePref(const char* aPref
, nsACString
& aVal
)
71 nsresult rv
= Preferences::GetCString(aPref
, &val
);
72 if (NS_SUCCEEDED(rv
)) {
80 CameraPreferences::UpdatePref(const char* aPref
, bool& aVal
)
83 nsresult rv
= Preferences::GetBool(aPref
, &val
);
84 if (NS_SUCCEEDED(rv
)) {
91 CameraPreferences::Pref
CameraPreferences::sPrefs
[] = {
93 "camera.control.test.enabled",
98 "camera.control.test.hardware",
100 { &sPrefHardwareTest
}
104 "camera.control.test.hardware.gonk.parameters",
106 { &sPrefGonkParameters
}
110 "camera.control.test.method.error",
111 kPrefValueIsNsResult
,
112 { &sPrefCameraControlMethodErrorOverride
}
115 "camera.control.test.async.error",
116 kPrefValueIsNsResult
,
117 { &sPrefCameraControlAsyncErrorOverride
}
120 "camera.control.test.is_low_memory",
122 { &sPrefCameraParametersIsLowMemory
}
125 "camera.control.low_memory_thresholdMB",
127 { &sPrefCameraControlLowMemoryThresholdMB
}
133 CameraPreferences::PrefToIndex(const char* aPref
)
135 for (uint32_t i
= 0; i
< ArrayLength(sPrefs
); ++i
) {
136 if (strcmp(aPref
, sPrefs
[i
].mPref
) == 0) {
140 return kPrefNotFound
;
145 CameraPreferences::PreferenceChanged(const char* aPref
, void* aClosure
)
147 MonitorAutoLock
mon(*sPrefMonitor
);
149 uint32_t i
= PrefToIndex(aPref
);
150 if (i
== kPrefNotFound
) {
151 DOM_CAMERA_LOGE("Preference '%s' is not tracked by CameraPreferences\n", aPref
);
157 switch (p
.mValueType
) {
158 case kPrefValueIsNsResult
:
159 #ifdef CAMERAPREFERENCES_HAVE_SEPARATE_UINT32_AND_NSRESULT
161 nsresult
& v
= *p
.mValue
.mAsNsResult
;
162 rv
= UpdatePref(aPref
, v
);
163 if (NS_SUCCEEDED(rv
)) {
164 DOM_CAMERA_LOGI("Preference '%s' has changed, 0x%x\n", aPref
, v
);
170 case kPrefValueIsUint32
:
172 uint32_t& v
= *p
.mValue
.mAsUint32
;
173 rv
= UpdatePref(aPref
, v
);
174 if (NS_SUCCEEDED(rv
)) {
175 DOM_CAMERA_LOGI("Preference '%s' has changed, %u\n", aPref
, v
);
180 case kPrefValueIsCString
:
182 nsCString
& v
= **p
.mValue
.mAsCString
;
183 rv
= UpdatePref(aPref
, v
);
184 if (NS_SUCCEEDED(rv
)) {
185 DOM_CAMERA_LOGI("Preference '%s' has changed, '%s'\n", aPref
, v
.get());
190 case kPrefValueIsBoolean
:
192 bool& v
= *p
.mValue
.mAsBoolean
;
193 rv
= UpdatePref(aPref
, v
);
194 if (NS_SUCCEEDED(rv
)) {
195 DOM_CAMERA_LOGI("Preference '%s' has changed, %s\n",
196 aPref
, v
? "true" : "false");
202 MOZ_ASSERT_UNREACHABLE("Unhandled preference value type!");
207 DOM_CAMERA_LOGE("Failed to get pref '%s' (0x%x)\n", aPref
, rv
);
213 CameraPreferences::Initialize()
215 DOM_CAMERA_LOGI("Initializing camera preference callbacks\n");
219 #ifdef MOZ_WIDGET_GONK
220 nsCOMPtr
<nsIObserverService
> obs
= services::GetObserverService();
222 sObserver
= new CameraPreferences();
223 rv
= obs
->AddObserver(sObserver
, "init-camera-hw", false);
224 if (NS_WARN_IF(NS_FAILED(rv
))) {
228 DOM_CAMERA_LOGE("Could not get observer service\n");
232 sPrefMonitor
= new Monitor("CameraPreferences.sPrefMonitor");
234 sPrefTestEnabled
= new nsCString();
235 sPrefHardwareTest
= new nsCString();
236 sPrefGonkParameters
= new nsCString();
238 for (uint32_t i
= 0; i
< ArrayLength(sPrefs
); ++i
) {
239 rv
= Preferences::RegisterCallbackAndCall(CameraPreferences::PreferenceChanged
,
241 if (NS_WARN_IF(NS_FAILED(rv
))) {
246 DOM_CAMERA_LOGI("Camera preferences initialized\n");
252 CameraPreferences::Shutdown()
254 DOM_CAMERA_LOGI("Shutting down camera preference callbacks\n");
256 for (uint32_t i
= 0; i
< ArrayLength(sPrefs
); ++i
) {
257 Preferences::UnregisterCallback(CameraPreferences::PreferenceChanged
,
261 sPrefTestEnabled
= nullptr;
262 sPrefHardwareTest
= nullptr;
263 sPrefGonkParameters
= nullptr;
264 sPrefMonitor
= nullptr;
266 #ifdef MOZ_WIDGET_GONK
267 nsCOMPtr
<nsIObserverService
> obs
= services::GetObserverService();
269 nsresult rv
= obs
->RemoveObserver(sObserver
, "init-camera-hw");
271 DOM_CAMERA_LOGE("Failed to remove CameraPreferences observer (0x%x)\n", rv
);
275 DOM_CAMERA_LOGE("Could not get observer service\n");
279 DOM_CAMERA_LOGI("Camera preferences shut down\n");
282 #ifdef MOZ_WIDGET_GONK
284 CameraPreferences::PreinitCameraHardware()
286 nsDOMCameraManager::PreinitCameraHardware();
291 CameraPreferences::Observe(nsISupports
* aSubject
, const char* aTopic
, const char16_t
* aData
)
293 if (strcmp(aTopic
, "init-camera-hw") == 0) {
294 return PreinitCameraHardware();
297 DOM_CAMERA_LOGE("Got unhandled topic '%s'\n", aTopic
);
304 CameraPreferences::GetPref(const char* aPref
, nsACString
& aVal
)
306 MOZ_ASSERT(sPrefMonitor
, "sPrefMonitor missing in CameraPreferences::GetPref()");
307 MonitorAutoLock
mon(*sPrefMonitor
);
309 uint32_t i
= PrefToIndex(aPref
);
310 if (i
== kPrefNotFound
|| i
>= ArrayLength(sPrefs
)) {
311 DOM_CAMERA_LOGW("Preference '%s' is not tracked by CameraPreferences\n", aPref
);
314 if (sPrefs
[i
].mValueType
!= kPrefValueIsCString
) {
315 DOM_CAMERA_LOGW("Preference '%s' is not a string type\n", aPref
);
319 StaticAutoPtr
<nsCString
>* s
= sPrefs
[i
].mValue
.mAsCString
;
321 DOM_CAMERA_LOGE("Preference '%s' cache is not initialized\n", aPref
);
324 if ((*s
)->IsEmpty()) {
325 DOM_CAMERA_LOGI("Preference '%s' is not set\n", aPref
);
329 DOM_CAMERA_LOGI("Preference '%s', got '%s'\n", aPref
, (*s
)->get());
334 #ifdef CAMERAPREFERENCES_HAVE_SEPARATE_UINT32_AND_NSRESULT
337 CameraPreferences::GetPref(const char* aPref
, nsresult
& aVal
)
339 MOZ_ASSERT(sPrefMonitor
, "sPrefMonitor missing in CameraPreferences::GetPref()");
340 MonitorAutoLock
mon(*sPrefMonitor
);
342 uint32_t i
= PrefToIndex(aPref
);
343 if (i
== kPrefNotFound
|| i
>= ArrayLength(sPrefs
)) {
344 DOM_CAMERA_LOGW("Preference '%s' is not tracked by CameraPreferences\n", aPref
);
347 if (sPrefs
[i
].mValueType
!= kPrefValueIsNsResult
) {
348 DOM_CAMERA_LOGW("Preference '%s' is not an nsresult type\n", aPref
);
352 nsresult v
= *sPrefs
[i
].mValue
.mAsNsResult
;
354 DOM_CAMERA_LOGW("Preference '%s' is not set\n", aPref
);
358 DOM_CAMERA_LOGI("Preference '%s', got 0x%x\n", aPref
, v
);
366 CameraPreferences::GetPref(const char* aPref
, uint32_t& aVal
)
368 MOZ_ASSERT(sPrefMonitor
, "sPrefMonitor missing in CameraPreferences::GetPref()");
369 MonitorAutoLock
mon(*sPrefMonitor
);
371 uint32_t i
= PrefToIndex(aPref
);
372 if (i
== kPrefNotFound
|| i
>= ArrayLength(sPrefs
)) {
373 DOM_CAMERA_LOGW("Preference '%s' is not tracked by CameraPreferences\n", aPref
);
376 if (sPrefs
[i
].mValueType
!= kPrefValueIsUint32
) {
377 DOM_CAMERA_LOGW("Preference '%s' is not a uint32_t type\n", aPref
);
381 uint32_t v
= *sPrefs
[i
].mValue
.mAsUint32
;
382 DOM_CAMERA_LOGI("Preference '%s', got %u\n", aPref
, v
);
389 CameraPreferences::GetPref(const char* aPref
, bool& aVal
)
391 MOZ_ASSERT(sPrefMonitor
, "sPrefMonitor missing in CameraPreferences::GetPref()");
392 MonitorAutoLock
mon(*sPrefMonitor
);
394 uint32_t i
= PrefToIndex(aPref
);
395 if (i
== kPrefNotFound
|| i
>= ArrayLength(sPrefs
)) {
396 DOM_CAMERA_LOGW("Preference '%s' is not tracked by CameraPreferences\n", aPref
);
399 if (sPrefs
[i
].mValueType
!= kPrefValueIsBoolean
) {
400 DOM_CAMERA_LOGW("Preference '%s' is not a boolean type\n", aPref
);
404 bool v
= *sPrefs
[i
].mValue
.mAsBoolean
;
405 DOM_CAMERA_LOGI("Preference '%s', got %s\n", aPref
, v
? "true" : "false");