Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
1 /*
2 * Copyright (C) 2012-2014 Mozilla Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
17 #include "ICameraControl.h"
19 #include <camera/Camera.h>
21 #include "CameraCommon.h"
22 #include "GonkCameraControl.h"
24 using namespace mozilla;
26 // From ICameraControl, gonk-specific management functions
27 nsresult
28 ICameraControl::GetNumberOfCameras(int32_t& aDeviceCount)
29 {
30 aDeviceCount = android::Camera::getNumberOfCameras();
31 return NS_OK;
32 }
34 nsresult
35 ICameraControl::GetCameraName(uint32_t aDeviceNum, nsCString& aDeviceName)
36 {
37 int32_t count = android::Camera::getNumberOfCameras();
38 int32_t deviceNum = static_cast<int32_t>(aDeviceNum);
40 DOM_CAMERA_LOGI("GetCameraName : getNumberOfCameras() returned %d\n", count);
41 if (deviceNum < 0 || deviceNum > count) {
42 DOM_CAMERA_LOGE("GetCameraName : invalid device number (%u)\n", aDeviceNum);
43 return NS_ERROR_NOT_AVAILABLE;
44 }
46 android::CameraInfo info;
47 int rv = android::Camera::getCameraInfo(deviceNum, &info);
48 if (rv != 0) {
49 DOM_CAMERA_LOGE("GetCameraName : get_camera_info(%d) failed: %d\n", deviceNum, rv);
50 return NS_ERROR_NOT_AVAILABLE;
51 }
53 switch (info.facing) {
54 case CAMERA_FACING_BACK:
55 aDeviceName.Assign("back");
56 break;
58 case CAMERA_FACING_FRONT:
59 aDeviceName.Assign("front");
60 break;
62 default:
63 aDeviceName.Assign("extra-camera-");
64 aDeviceName.AppendInt(deviceNum);
65 break;
66 }
67 return NS_OK;
68 }
70 nsresult
71 ICameraControl::GetListOfCameras(nsTArray<nsString>& aList)
72 {
73 int32_t count = android::Camera::getNumberOfCameras();
74 DOM_CAMERA_LOGI("getListOfCameras : getNumberOfCameras() returned %d\n", count);
75 if (count <= 0) {
76 return NS_OK;
77 }
79 // Allocate 2 extra slots to reserve space for 'front' and 'back' cameras
80 // at the front of the array--we will collapse any empty slots below.
81 aList.SetLength(2);
82 uint32_t extraIdx = 2;
83 bool gotFront = false;
84 bool gotBack = false;
85 while (count--) {
86 nsCString cameraName;
87 nsresult result = GetCameraName(count, cameraName);
88 if (result != NS_OK) {
89 continue;
90 }
92 // The first camera we find named 'back' gets slot 0; and the first
93 // we find named 'front' gets slot 1. All others appear after these.
94 if (cameraName.EqualsLiteral("back")) {
95 CopyUTF8toUTF16(cameraName, aList[0]);
96 gotBack = true;
97 } else if (cameraName.EqualsLiteral("front")) {
98 CopyUTF8toUTF16(cameraName, aList[1]);
99 gotFront = true;
100 } else {
101 CopyUTF8toUTF16(cameraName, *aList.InsertElementAt(extraIdx));
102 extraIdx++;
103 }
104 }
106 if (!gotFront) {
107 aList.RemoveElementAt(1);
108 }
110 if (!gotBack) {
111 aList.RemoveElementAt(0);
112 }
114 return NS_OK;
115 }
117 // implementation-specific camera factory
118 already_AddRefed<ICameraControl>
119 ICameraControl::Create(uint32_t aCameraId)
120 {
121 nsRefPtr<nsGonkCameraControl> control = new nsGonkCameraControl(aCameraId);
122 return control.forget();
123 }