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.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 3 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | #include "Hal.h" |
michael@0 | 5 | #include "nsITimer.h" |
michael@0 | 6 | #include "smslib.h" |
michael@0 | 7 | #include "nsComponentManagerUtils.h" |
michael@0 | 8 | |
michael@0 | 9 | #include <mach/mach.h> |
michael@0 | 10 | #include <cmath> |
michael@0 | 11 | #import <IOKit/IOKitLib.h> |
michael@0 | 12 | |
michael@0 | 13 | #define MEAN_GRAVITY 9.80665 |
michael@0 | 14 | #define DEFAULT_SENSOR_POLL 100 |
michael@0 | 15 | using namespace mozilla::hal; |
michael@0 | 16 | namespace mozilla { |
michael@0 | 17 | namespace hal_impl { |
michael@0 | 18 | static nsITimer* sUpdateTimer = nullptr; |
michael@0 | 19 | static bool sActiveSensors[NUM_SENSOR_TYPE]; |
michael@0 | 20 | static io_connect_t sDataPort = IO_OBJECT_NULL; |
michael@0 | 21 | static uint64_t sLastMean = -1; |
michael@0 | 22 | static float |
michael@0 | 23 | LMUvalueToLux(uint64_t aValue) |
michael@0 | 24 | { |
michael@0 | 25 | //Conversion formula from regression. See Bug 793728. |
michael@0 | 26 | // -3*(10^-27)*x^4 + 2.6*(10^-19)*x^3 + -3.4*(10^-12)*x^2 + 3.9*(10^-5)*x - 0.19 |
michael@0 | 27 | long double powerC4 = 1/pow((long double)10,27); |
michael@0 | 28 | long double powerC3 = 1/pow((long double)10,19); |
michael@0 | 29 | long double powerC2 = 1/pow((long double)10,12); |
michael@0 | 30 | long double powerC1 = 1/pow((long double)10,5); |
michael@0 | 31 | |
michael@0 | 32 | long double term4 = -3.0 * powerC4 * pow(aValue,4); |
michael@0 | 33 | long double term3 = 2.6 * powerC3 * pow(aValue,3); |
michael@0 | 34 | long double term2 = -3.4 * powerC2 * pow(aValue,2); |
michael@0 | 35 | long double term1 = 3.9 * powerC1 * aValue; |
michael@0 | 36 | |
michael@0 | 37 | float lux = ceil(static_cast<float>(term4 + term3 + term2 + term1 - 0.19)); |
michael@0 | 38 | return lux > 0 ? lux : 0; |
michael@0 | 39 | } |
michael@0 | 40 | void |
michael@0 | 41 | UpdateHandler(nsITimer *aTimer, void *aClosure) |
michael@0 | 42 | { |
michael@0 | 43 | for (int i = 0; i < NUM_SENSOR_TYPE; i++) { |
michael@0 | 44 | if (!sActiveSensors[i]) { |
michael@0 | 45 | continue; |
michael@0 | 46 | } |
michael@0 | 47 | SensorType sensor = static_cast<SensorType>(i); |
michael@0 | 48 | InfallibleTArray<float> values; |
michael@0 | 49 | if (sensor == SENSOR_ACCELERATION) { |
michael@0 | 50 | sms_acceleration accel; |
michael@0 | 51 | smsGetData(&accel); |
michael@0 | 52 | |
michael@0 | 53 | values.AppendElement(accel.x * MEAN_GRAVITY); |
michael@0 | 54 | values.AppendElement(accel.y * MEAN_GRAVITY); |
michael@0 | 55 | values.AppendElement(accel.z * MEAN_GRAVITY); |
michael@0 | 56 | } else if (sensor == SENSOR_LIGHT && sDataPort != IO_OBJECT_NULL) { |
michael@0 | 57 | kern_return_t kr; |
michael@0 | 58 | uint32_t outputs = 2; |
michael@0 | 59 | uint64_t lightLMU[outputs]; |
michael@0 | 60 | |
michael@0 | 61 | kr = IOConnectCallMethod(sDataPort, 0, nil, 0, nil, 0, lightLMU, &outputs, nil, 0); |
michael@0 | 62 | if (kr == KERN_SUCCESS) { |
michael@0 | 63 | uint64_t mean = (lightLMU[0] + lightLMU[1]) / 2; |
michael@0 | 64 | if (mean == sLastMean) { |
michael@0 | 65 | continue; |
michael@0 | 66 | } |
michael@0 | 67 | sLastMean = mean; |
michael@0 | 68 | values.AppendElement(LMUvalueToLux(mean)); |
michael@0 | 69 | } else if (kr == kIOReturnBusy) { |
michael@0 | 70 | continue; |
michael@0 | 71 | } |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | hal::SensorData sdata(sensor, |
michael@0 | 75 | PR_Now(), |
michael@0 | 76 | values, |
michael@0 | 77 | hal::SENSOR_ACCURACY_UNKNOWN); |
michael@0 | 78 | hal::NotifySensorChange(sdata); |
michael@0 | 79 | } |
michael@0 | 80 | } |
michael@0 | 81 | void |
michael@0 | 82 | EnableSensorNotifications(SensorType aSensor) |
michael@0 | 83 | { |
michael@0 | 84 | if (aSensor == SENSOR_ACCELERATION) { |
michael@0 | 85 | int result = smsStartup(nil, nil); |
michael@0 | 86 | |
michael@0 | 87 | if (result != SMS_SUCCESS) { |
michael@0 | 88 | return; |
michael@0 | 89 | } |
michael@0 | 90 | |
michael@0 | 91 | if (!smsLoadCalibration()) { |
michael@0 | 92 | return; |
michael@0 | 93 | } |
michael@0 | 94 | } else if (aSensor == SENSOR_LIGHT) { |
michael@0 | 95 | io_service_t serviceObject; |
michael@0 | 96 | serviceObject = IOServiceGetMatchingService(kIOMasterPortDefault, |
michael@0 | 97 | IOServiceMatching("AppleLMUController")); |
michael@0 | 98 | if (!serviceObject) { |
michael@0 | 99 | return; |
michael@0 | 100 | } |
michael@0 | 101 | kern_return_t kr; |
michael@0 | 102 | kr = IOServiceOpen(serviceObject, mach_task_self(), 0, &sDataPort); |
michael@0 | 103 | IOObjectRelease(serviceObject); |
michael@0 | 104 | if (kr != KERN_SUCCESS) { |
michael@0 | 105 | return; |
michael@0 | 106 | } |
michael@0 | 107 | } else { |
michael@0 | 108 | NS_WARNING("EnableSensorNotifications called on an unknown sensor type"); |
michael@0 | 109 | return; |
michael@0 | 110 | } |
michael@0 | 111 | sActiveSensors[aSensor] = true; |
michael@0 | 112 | |
michael@0 | 113 | if (!sUpdateTimer) { |
michael@0 | 114 | CallCreateInstance("@mozilla.org/timer;1", &sUpdateTimer); |
michael@0 | 115 | if (sUpdateTimer) { |
michael@0 | 116 | sUpdateTimer->InitWithFuncCallback(UpdateHandler, |
michael@0 | 117 | nullptr, |
michael@0 | 118 | DEFAULT_SENSOR_POLL, |
michael@0 | 119 | nsITimer::TYPE_REPEATING_SLACK); |
michael@0 | 120 | } |
michael@0 | 121 | } |
michael@0 | 122 | } |
michael@0 | 123 | void |
michael@0 | 124 | DisableSensorNotifications(SensorType aSensor) |
michael@0 | 125 | { |
michael@0 | 126 | if (!sActiveSensors[aSensor] || (aSensor != SENSOR_ACCELERATION && aSensor != SENSOR_LIGHT)) { |
michael@0 | 127 | return; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | sActiveSensors[aSensor] = false; |
michael@0 | 131 | |
michael@0 | 132 | if (aSensor == SENSOR_ACCELERATION) { |
michael@0 | 133 | smsShutdown(); |
michael@0 | 134 | } else if (aSensor == SENSOR_LIGHT) { |
michael@0 | 135 | IOServiceClose(sDataPort); |
michael@0 | 136 | } |
michael@0 | 137 | // If all sensors are disabled, cancel the update timer. |
michael@0 | 138 | if (sUpdateTimer) { |
michael@0 | 139 | for (int i = 0; i < NUM_SENSOR_TYPE; i++) { |
michael@0 | 140 | if (sActiveSensors[i]) { |
michael@0 | 141 | return; |
michael@0 | 142 | } |
michael@0 | 143 | } |
michael@0 | 144 | sUpdateTimer->Cancel(); |
michael@0 | 145 | NS_RELEASE(sUpdateTimer); |
michael@0 | 146 | } |
michael@0 | 147 | } |
michael@0 | 148 | } // hal_impl |
michael@0 | 149 | } // mozilla |