Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/device_orientation/data_fetcher_impl_win.h" | |
| 6 | |
| 7 #include <InitGuid.h> | |
| 8 #include <PortableDeviceTypes.h> | |
| 9 #include <Sensors.h> | |
| 10 | |
| 11 #include "base/logging.h" | |
| 12 #include "content/browser/device_orientation/orientation.h" | |
| 13 | |
| 14 using base::win::ScopedComPtr; | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // This should match ProviderImpl::kDesiredSamplingIntervalMs. | |
| 19 const int kPeriodInMilliseconds = 100; | |
| 20 | |
| 21 } // namespace | |
| 22 | |
| 23 namespace content { | |
| 24 | |
| 25 class DataFetcherImplWin::SensorEventSink : public ISensorEvents { | |
| 26 public: | |
| 27 SensorEventSink(DataFetcherImplWin* fetcher) | |
|
bulach
2013/04/02 15:48:07
nit: use "explicit".
nhu
2013/04/03 03:06:52
Done.
| |
| 28 : fetcher_(fetcher), | |
| 29 ref_count_(0) {} | |
| 30 | |
| 31 virtual ~SensorEventSink() {} | |
| 32 | |
| 33 // IUnknown interface | |
| 34 STDMETHODIMP_(ULONG) AddRef() { | |
| 35 return InterlockedIncrement(&ref_count_); | |
| 36 } | |
|
bulach
2013/04/02 15:48:07
nit: add a \n here, 44 and 67 (i.e., a \n in betwe
nhu
2013/04/03 03:06:52
Done.
| |
| 37 STDMETHODIMP_(ULONG) Release() { | |
| 38 ULONG count = InterlockedDecrement(&ref_count_); | |
| 39 if (count == 0) { | |
| 40 delete this; | |
| 41 return 0; | |
| 42 } | |
| 43 return count; | |
| 44 } | |
| 45 STDMETHODIMP QueryInterface(REFIID iid, void** ppv) | |
| 46 { | |
|
bulach
2013/04/02 15:48:07
nit: move { up to the end of 45..
also, please ma
nhu
2013/04/03 03:06:52
Done.
| |
| 47 if (ppv == NULL) | |
| 48 return E_POINTER; | |
| 49 if (iid == __uuidof(IUnknown)) { | |
| 50 *ppv = static_cast<IUnknown*>(this); | |
| 51 } else if (iid == __uuidof(ISensorEvents)) { | |
| 52 *ppv = static_cast<ISensorEvents*>(this); | |
| 53 } else { | |
| 54 *ppv = NULL; | |
| 55 return E_NOINTERFACE; | |
| 56 } | |
| 57 AddRef(); | |
| 58 return S_OK; | |
| 59 } | |
| 60 | |
| 61 // ISensorEvents interface | |
| 62 STDMETHODIMP OnEvent(ISensor *pSensor, | |
|
bulach
2013/04/02 15:48:07
nit: * goes in the param type. also, as above, nam
nhu
2013/04/03 03:06:52
Done.
| |
| 63 REFGUID eventID, | |
| 64 IPortableDeviceValues *pEventData) { | |
| 65 return S_OK; | |
| 66 } | |
| 67 STDMETHODIMP OnDataUpdated(ISensor *pSensor, ISensorDataReport *pNewData) { | |
|
bulach
2013/04/02 15:48:07
nit:
ISensor* sensor, ISensorDataReport* new_data
nhu
2013/04/03 03:06:52
Done.
| |
| 68 if (NULL == pNewData || NULL == pSensor) | |
| 69 return E_INVALIDARG; | |
| 70 PROPVARIANT pv = {}; | |
| 71 bool can_provide_alpha, can_provide_beta, can_provide_gamma; | |
| 72 can_provide_alpha = can_provide_beta = can_provide_gamma = false; | |
|
bulach
2013/04/02 15:48:07
nit: we normally do one per line, and then you can
nhu
2013/04/03 03:06:52
Done.
| |
| 73 double alpha, beta, gamma; | |
| 74 if (SUCCEEDED(pNewData->GetSensorValue( | |
| 75 SENSOR_DATA_TYPE_TILT_X_DEGREES, &pv))) { | |
| 76 beta = pv.fltVal; | |
| 77 can_provide_beta = true; | |
| 78 } | |
| 79 PropVariantClear(&pv); | |
| 80 if (SUCCEEDED(pNewData->GetSensorValue( | |
| 81 SENSOR_DATA_TYPE_TILT_Y_DEGREES, &pv))) { | |
| 82 gamma = pv.fltVal; | |
| 83 can_provide_gamma = true; | |
| 84 } | |
| 85 PropVariantClear(&pv); | |
| 86 if (SUCCEEDED(pNewData->GetSensorValue( | |
| 87 SENSOR_DATA_TYPE_TILT_Z_DEGREES, &pv))) { | |
| 88 alpha = pv.fltVal; | |
| 89 can_provide_alpha = true; | |
| 90 } | |
| 91 PropVariantClear(&pv); | |
| 92 if (fetcher_) { | |
|
bulach
2013/04/02 15:48:07
nit: no need to test for fetcher_.. you could pote
nhu
2013/04/03 03:06:52
Done.
| |
| 93 fetcher_->OnOrientationData(can_provide_alpha, alpha, | |
| 94 can_provide_beta, beta, | |
| 95 can_provide_gamma, gamma); | |
| 96 } | |
| 97 | |
| 98 return S_OK; | |
| 99 } | |
| 100 STDMETHODIMP OnLeave(REFSENSOR_ID sensorID) { | |
| 101 return S_OK; | |
| 102 } | |
| 103 STDMETHODIMP OnStateChanged(ISensor* pSensor, SensorState state) { | |
| 104 return S_OK; | |
| 105 } | |
| 106 | |
| 107 private: | |
| 108 long ref_count_ ; | |
| 109 DataFetcherImplWin* fetcher_; | |
| 110 }; | |
| 111 | |
| 112 // Create a DataFetcherImplWin object and return NULL if no valid sensor found. | |
| 113 DataFetcher* DataFetcherImplWin::Create() { | |
| 114 scoped_ptr<DataFetcherImplWin> fetcher(new DataFetcherImplWin); | |
| 115 return fetcher->Init() ? fetcher.release() : NULL; | |
| 116 } | |
| 117 | |
| 118 DataFetcherImplWin::~DataFetcherImplWin() { | |
| 119 if (sensor_.get()) | |
| 120 sensor_->SetEventSink(NULL); | |
| 121 } | |
| 122 | |
| 123 DataFetcherImplWin::DataFetcherImplWin() { | |
| 124 } | |
| 125 | |
| 126 void DataFetcherImplWin::OnOrientationData(bool can_provide_alpha, | |
| 127 double alpha, | |
| 128 bool can_provide_beta, | |
| 129 double beta, | |
| 130 bool can_provide_gamma, | |
| 131 double gamma) { | |
| 132 base::AutoLock autolock(last_orientation_lock_); | |
| 133 Orientation* orientation = new Orientation(); | |
| 134 if (can_provide_alpha) | |
| 135 orientation->set_alpha(alpha); | |
| 136 if (can_provide_beta) | |
| 137 orientation->set_beta(beta); | |
| 138 if (can_provide_gamma) | |
| 139 orientation->set_gamma(gamma); | |
| 140 last_orientation_ = orientation; | |
| 141 } | |
| 142 | |
| 143 const DeviceData* DataFetcherImplWin::GetDeviceData(DeviceData::Type type) { | |
| 144 if (type != DeviceData::kTypeOrientation) | |
| 145 return NULL; | |
| 146 return GetOrientation(); | |
| 147 } | |
| 148 | |
| 149 const Orientation* DataFetcherImplWin::GetOrientation() { | |
| 150 base::AutoLock autolock(last_orientation_lock_); | |
| 151 scoped_refptr<Orientation> orientation(new Orientation()); | |
| 152 if (last_orientation_.get()) { | |
| 153 if (last_orientation_->can_provide_alpha()) | |
| 154 orientation->set_alpha(last_orientation_->alpha()); | |
| 155 if (last_orientation_->can_provide_beta()) | |
| 156 orientation->set_beta(last_orientation_->beta()); | |
| 157 if (last_orientation_->can_provide_gamma()) | |
| 158 orientation->set_gamma(last_orientation_->gamma()); | |
| 159 orientation->set_absolute(true); | |
| 160 } | |
| 161 orientation->AddRef(); | |
|
bulach
2013/04/02 15:48:07
can we use the same mechanism as android and hold
nhu
2013/04/03 03:06:52
Thanks for your suggestion. Actually, I used to tr
| |
| 162 return orientation.get(); | |
| 163 } | |
| 164 | |
| 165 bool DataFetcherImplWin::Init() { | |
| 166 ScopedComPtr<ISensorManager> sensor_manager; | |
| 167 if (FAILED(::CoCreateInstance(CLSID_SensorManager, | |
| 168 NULL, | |
| 169 CLSCTX_INPROC_SERVER, | |
| 170 __uuidof(ISensorManager), | |
| 171 sensor_manager.ReceiveVoid()))) | |
| 172 return false; | |
| 173 | |
| 174 ScopedComPtr<ISensorCollection> sensor_collection; | |
| 175 if (FAILED(sensor_manager->GetSensorsByType( | |
| 176 SENSOR_TYPE_INCLINOMETER_3D, sensor_collection.Receive()))) | |
|
bulach
2013/04/02 15:48:07
nit: continuation should be indented by 4 spaces
nhu
2013/04/03 03:06:52
Done.
| |
| 177 return false; | |
| 178 | |
| 179 if (FAILED(sensor_collection->GetAt(0, sensor_.Receive()))) | |
| 180 return false; | |
| 181 | |
| 182 ScopedComPtr<IPortableDeviceValues> device_values; | |
| 183 if (SUCCEEDED(CoCreateInstance(CLSID_PortableDeviceValues, | |
| 184 NULL, | |
| 185 CLSCTX_INPROC_SERVER, | |
| 186 __uuidof(IPortableDeviceValues), | |
| 187 device_values.ReceiveVoid()))) { | |
| 188 if (SUCCEEDED(device_values->SetUnsignedIntegerValue( | |
| 189 SENSOR_PROPERTY_CURRENT_REPORT_INTERVAL, | |
|
bulach
2013/04/02 15:48:07
nit: indent by 4
nhu
2013/04/03 03:06:52
Done.
| |
| 190 kPeriodInMilliseconds))) { | |
| 191 ScopedComPtr<IPortableDeviceValues> return_values; | |
| 192 sensor_->SetProperties(device_values.get(), return_values.Receive()); | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 SensorEventSink* sensor_event_class = new SensorEventSink(this); | |
| 197 ScopedComPtr<ISensorEvents> sensor_events; | |
| 198 if (FAILED(sensor_event_class->QueryInterface( | |
| 199 __uuidof(ISensorEvents), sensor_events.ReceiveVoid()))) | |
|
bulach
2013/04/02 15:48:07
should sensor_event_class be deleted here and 202?
nhu
2013/04/03 03:06:52
As my understanding and verification, the SensorEv
| |
| 200 return false; | |
| 201 if (FAILED(sensor_->SetEventSink(sensor_events.get()))) | |
| 202 return false; | |
| 203 | |
| 204 return sensor_.get() != NULL; | |
| 205 } | |
| 206 | |
| 207 } // namespace content | |
| 208 | |
|
bulach
2013/04/02 15:48:07
nit: remove extra \n
| |
| OLD | NEW |