dom/camera/GonkCameraManager.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

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.

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

mercurial