hal/windows/WindowsSensor.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 /* 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
michael@0 5 #include "Hal.h"
michael@0 6
michael@0 7 #include <sensorsapi.h>
michael@0 8 #include <sensors.h>
michael@0 9 #include <portabledevicetypes.h>
michael@0 10
michael@0 11 #define MEAN_GRAVITY 9.80665
michael@0 12 #define DEFAULT_SENSOR_POLL 100
michael@0 13
michael@0 14 using namespace mozilla::hal;
michael@0 15
michael@0 16 namespace mozilla {
michael@0 17 namespace hal_impl {
michael@0 18
michael@0 19 static nsRefPtr<ISensor> sAccelerometer;
michael@0 20
michael@0 21 class SensorEvent MOZ_FINAL : public ISensorEvents {
michael@0 22 public:
michael@0 23 SensorEvent() : mCount(0) {
michael@0 24 }
michael@0 25
michael@0 26 // IUnknown interface
michael@0 27
michael@0 28 STDMETHODIMP_(ULONG) AddRef() {
michael@0 29 return InterlockedIncrement(&mCount);
michael@0 30 }
michael@0 31
michael@0 32 STDMETHODIMP_(ULONG) Release() {
michael@0 33 ULONG count = InterlockedDecrement(&mCount);
michael@0 34 if (!count) {
michael@0 35 delete this;
michael@0 36 return 0;
michael@0 37 }
michael@0 38 return count;
michael@0 39 }
michael@0 40
michael@0 41 STDMETHODIMP QueryInterface(REFIID iid, void** ppv) {
michael@0 42 if (iid == IID_IUnknown) {
michael@0 43 *ppv = static_cast<IUnknown*>(this);
michael@0 44 } else if (iid == IID_ISensorEvents) {
michael@0 45 *ppv = static_cast<ISensorEvents*>(this);
michael@0 46 } else {
michael@0 47 return E_NOINTERFACE;
michael@0 48 }
michael@0 49 AddRef();
michael@0 50 return S_OK;
michael@0 51 }
michael@0 52
michael@0 53 // ISensorEvents interface
michael@0 54
michael@0 55 STDMETHODIMP OnEvent(ISensor *aSensor, REFGUID aId, IPortableDeviceValues *aData) {
michael@0 56 return S_OK;
michael@0 57 }
michael@0 58
michael@0 59 STDMETHODIMP OnLeave(REFSENSOR_ID aId) {
michael@0 60 return S_OK;
michael@0 61 }
michael@0 62
michael@0 63 STDMETHODIMP OnStateChanged(ISensor *aSensor, SensorState state) {
michael@0 64 return S_OK;
michael@0 65 }
michael@0 66
michael@0 67 STDMETHODIMP OnDataUpdated(ISensor *aSensor, ISensorDataReport *aReport) {
michael@0 68 PROPVARIANT v;
michael@0 69 HRESULT hr;
michael@0 70 InfallibleTArray<float> values;
michael@0 71
michael@0 72 // X-axis acceleration in g's
michael@0 73 hr = aReport->GetSensorValue(SENSOR_DATA_TYPE_ACCELERATION_X_G, &v);
michael@0 74 if (FAILED(hr)) {
michael@0 75 return hr;
michael@0 76 }
michael@0 77 values.AppendElement(float(-v.dblVal * MEAN_GRAVITY));
michael@0 78
michael@0 79 // Y-axis acceleration in g's
michael@0 80 hr = aReport->GetSensorValue(SENSOR_DATA_TYPE_ACCELERATION_Y_G, &v);
michael@0 81 if (FAILED(hr)) {
michael@0 82 return hr;
michael@0 83 }
michael@0 84 values.AppendElement(float(-v.dblVal * MEAN_GRAVITY));
michael@0 85
michael@0 86 // Z-axis acceleration in g's
michael@0 87 hr = aReport->GetSensorValue(SENSOR_DATA_TYPE_ACCELERATION_Z_G, &v);
michael@0 88 if (FAILED(hr)) {
michael@0 89 return hr;
michael@0 90 }
michael@0 91 values.AppendElement(float(-v.dblVal * MEAN_GRAVITY));
michael@0 92
michael@0 93 hal::SensorData sdata(hal::SENSOR_ACCELERATION,
michael@0 94 PR_Now(),
michael@0 95 values,
michael@0 96 hal::SENSOR_ACCURACY_UNKNOWN);
michael@0 97 hal::NotifySensorChange(sdata);
michael@0 98
michael@0 99 return S_OK;
michael@0 100 }
michael@0 101
michael@0 102 private:
michael@0 103 ULONG mCount;
michael@0 104 };
michael@0 105
michael@0 106 void
michael@0 107 EnableSensorNotifications(SensorType aSensor)
michael@0 108 {
michael@0 109 if (aSensor != SENSOR_ACCELERATION) {
michael@0 110 return;
michael@0 111 }
michael@0 112
michael@0 113 if (sAccelerometer) {
michael@0 114 return;
michael@0 115 }
michael@0 116
michael@0 117 nsRefPtr<ISensorManager> manager;
michael@0 118 if (FAILED(CoCreateInstance(CLSID_SensorManager, nullptr,
michael@0 119 CLSCTX_INPROC_SERVER,
michael@0 120 IID_ISensorManager,
michael@0 121 getter_AddRefs(manager)))) {
michael@0 122 return;
michael@0 123 }
michael@0 124
michael@0 125 // accelerometer event
michael@0 126
michael@0 127 nsRefPtr<ISensorCollection> collection;
michael@0 128 if (FAILED(manager->GetSensorsByType(SENSOR_TYPE_ACCELEROMETER_3D,
michael@0 129 getter_AddRefs(collection)))) {
michael@0 130 return;
michael@0 131 }
michael@0 132
michael@0 133 ULONG count = 0;
michael@0 134 collection->GetCount(&count);
michael@0 135 if (!count) {
michael@0 136 return;
michael@0 137 }
michael@0 138
michael@0 139 nsRefPtr<ISensor> sensor;
michael@0 140 collection->GetAt(0, getter_AddRefs(sensor));
michael@0 141 if (!sensor) {
michael@0 142 return;
michael@0 143 }
michael@0 144
michael@0 145 // Set report interval to 100ms if possible.
michael@0 146 // Default value depends on drivers.
michael@0 147 nsRefPtr<IPortableDeviceValues> values;
michael@0 148 if (SUCCEEDED(CoCreateInstance(CLSID_PortableDeviceValues, nullptr,
michael@0 149 CLSCTX_INPROC_SERVER,
michael@0 150 IID_IPortableDeviceValues,
michael@0 151 getter_AddRefs(values)))) {
michael@0 152 if (SUCCEEDED(values->SetUnsignedIntegerValue(
michael@0 153 SENSOR_PROPERTY_CURRENT_REPORT_INTERVAL,
michael@0 154 DEFAULT_SENSOR_POLL))) {
michael@0 155 nsRefPtr<IPortableDeviceValues> returns;
michael@0 156 sensor->SetProperties(values, getter_AddRefs(returns));
michael@0 157 }
michael@0 158 }
michael@0 159
michael@0 160 nsRefPtr<SensorEvent> event = new SensorEvent();
michael@0 161 nsRefPtr<ISensorEvents> sensorEvents;
michael@0 162 if (FAILED(event->QueryInterface(IID_ISensorEvents,
michael@0 163 getter_AddRefs(sensorEvents)))) {
michael@0 164 return;
michael@0 165 }
michael@0 166
michael@0 167 if (FAILED(sensor->SetEventSink(sensorEvents))) {
michael@0 168 return;
michael@0 169 }
michael@0 170
michael@0 171 sAccelerometer = sensor;
michael@0 172 }
michael@0 173
michael@0 174 void
michael@0 175 DisableSensorNotifications(SensorType aSensor)
michael@0 176 {
michael@0 177 if (aSensor == SENSOR_ACCELERATION && sAccelerometer) {
michael@0 178 sAccelerometer->SetEventSink(nullptr);
michael@0 179 sAccelerometer = nullptr;
michael@0 180 }
michael@0 181 }
michael@0 182
michael@0 183 } // hal_impl
michael@0 184 } // mozilla

mercurial