Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(32)

Side by Side Diff: content/browser/device_orientation/data_fetcher_impl_win.cc

Issue 13433002: Implement DeviceOrientation API on Windows (Closed) Base URL: ssh://nhu@powerbuilder.sh.intel.com/home/www-data/git-repos/perc/chromium.git@sensor
Patch Set: Update according to sail's and peter's comments #8, #9 and #11 Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "base/win/iunknown_impl.h"
13 #include "base/win/windows_version.h"
14 #include "content/browser/device_orientation/orientation.h"
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 base::win::IUnknownImpl {
27 public:
28 explicit SensorEventSink(DataFetcherImplWin* const fetcher)
29 : fetcher_(fetcher) {}
30
31 virtual ~SensorEventSink() {}
32
33 // IUnknown interface
34 virtual ULONG STDMETHODCALLTYPE AddRef() OVERRIDE {
35 return IUnknownImpl::AddRef();
36 }
37
38 virtual ULONG STDMETHODCALLTYPE Release() OVERRIDE {
39 return IUnknownImpl::Release();
40 }
41
42 virtual STDMETHODIMP QueryInterface(REFIID riid, void** ppv) OVERRIDE {
43 if (riid == __uuidof(ISensorEvents)) {
44 *ppv = static_cast<ISensorEvents*>(this);
45 AddRef();
46 return S_OK;
47 }
48 return IUnknownImpl::QueryInterface(riid, ppv);
49 }
50
51 // ISensorEvents interface
52 STDMETHODIMP OnEvent(ISensor *sensor,
bulach 2013/04/10 07:59:46 nit: here and 54, 58 and 59, the style is: "type*
nhu 2013/04/10 09:46:42 Done.
53 REFGUID event_id,
54 IPortableDeviceValues *event_data) OVERRIDE {
55 return S_OK;
56 }
57
58 STDMETHODIMP OnDataUpdated(ISensor *sensor,
59 ISensorDataReport *new_data) OVERRIDE {
60 if (NULL == new_data || NULL == sensor)
Peter Beverloo 2013/04/10 08:59:51 nit: two spaces after ||
nhu 2013/04/10 09:46:42 Done.
61 return E_INVALIDARG;
62
63 PROPVARIANT value = {};
64 Orientation* orientation = new Orientation();
sail 2013/04/09 20:53:53 scoped_refptr
nhu 2013/04/10 09:46:42 Done.
65
66 if (SUCCEEDED(new_data->GetSensorValue(
67 SENSOR_DATA_TYPE_TILT_X_DEGREES, &value))) {
68 orientation->set_beta(value.fltVal);
69 }
70 PropVariantClear(&value);
71
72 if (SUCCEEDED(new_data->GetSensorValue(
73 SENSOR_DATA_TYPE_TILT_Y_DEGREES, &value))) {
74 orientation->set_gamma(value.fltVal);
75 }
76 PropVariantClear(&value);
77
78 if (SUCCEEDED(new_data->GetSensorValue(
79 SENSOR_DATA_TYPE_TILT_Z_DEGREES, &value))) {
80 orientation->set_alpha(value.fltVal);
81 }
82 PropVariantClear(&value);
83
84 orientation->set_absolute(true);
85 fetcher_->OnOrientationData(orientation);
86
87 return S_OK;
88 }
89
90 STDMETHODIMP OnLeave(REFSENSOR_ID sensor_id) OVERRIDE {
91 return S_OK;
92 }
93
94 STDMETHODIMP OnStateChanged(ISensor* sensor, SensorState state) OVERRIDE {
95 return S_OK;
96 }
97
98 private:
99 DataFetcherImplWin* const fetcher_;
100
101 DISALLOW_COPY_AND_ASSIGN(SensorEventSink);
102 };
103
104 // Create a DataFetcherImplWin object and return NULL if no valid sensor found.
105 // static
106 DataFetcher* DataFetcherImplWin::Create() {
107 scoped_ptr<DataFetcherImplWin> fetcher(new DataFetcherImplWin);
108 if (fetcher->Initialize())
109 return fetcher.release();
110
111 LOG(ERROR) << "DataFetcherImplWin::Initialize failed!";
112 return NULL;
113 }
114
115 DataFetcherImplWin::~DataFetcherImplWin() {
116 if (sensor_.get())
sail 2013/04/09 20:53:53 I don't think you need the .get() here.
nhu 2013/04/10 09:46:42 Done.
117 sensor_->SetEventSink(NULL);
118 }
119
120 DataFetcherImplWin::DataFetcherImplWin() {
121 }
122
123 void DataFetcherImplWin::OnOrientationData(Orientation* orientation) {
124 // This method is called on Windows sensor thread.
125 base::AutoLock autolock(next_orientation_lock_);
126
127 next_orientation_ = orientation;
128 }
129
130 const DeviceData* DataFetcherImplWin::GetDeviceData(DeviceData::Type type) {
131 if (type != DeviceData::kTypeOrientation)
132 return NULL;
133 return GetOrientation();
134 }
135
136 const Orientation* DataFetcherImplWin::GetOrientation() {
137 if (next_orientation_.get()) {
138 base::AutoLock autolock(next_orientation_lock_);
139 next_orientation_.swap(current_orientation_);
140 }
141 if (!current_orientation_.get())
142 return new Orientation();
143 return current_orientation_.get();
144 }
145
146 bool DataFetcherImplWin::Initialize() {
147 if (base::win::GetVersion() < base::win::VERSION_WIN7)
148 return false;
149
150 base::win::ScopedComPtr<ISensorManager> sensor_manager;
151 HRESULT hr = sensor_manager.CreateInstance(CLSID_SensorManager);
152 if (FAILED(hr) || !sensor_manager)
153 return false;
154
155 base::win::ScopedComPtr<ISensorCollection> sensor_collection;
156 hr = sensor_manager->GetSensorsByType(
157 SENSOR_TYPE_INCLINOMETER_3D, sensor_collection.Receive());
Peter Beverloo 2013/04/10 08:59:51 nit: four space indent?
nhu 2013/04/10 09:46:42 Done.
158
159 if (FAILED(hr) || !sensor_collection)
160 return false;
161
162 ULONG count = 0;
163 hr = sensor_collection->GetCount(&count);
164 if (FAILED(hr) || !count)
165 return false;
166
167 hr = sensor_collection->GetAt(0, sensor_.Receive());
168 if (FAILED(hr) || !sensor_)
169 return false;
170
171 base::win::ScopedComPtr<IPortableDeviceValues> device_values;
172 if (SUCCEEDED(device_values.CreateInstance(CLSID_PortableDeviceValues))) {
173 if (SUCCEEDED(device_values->SetUnsignedIntegerValue(
174 SENSOR_PROPERTY_CURRENT_REPORT_INTERVAL, kPeriodInMilliseconds))) {
175 base::win::ScopedComPtr<IPortableDeviceValues> return_values;
176 sensor_->SetProperties(device_values.get(), return_values.Receive());
177 }
178 }
179
180 scoped_refptr<SensorEventSink> sensor_event_impl = new SensorEventSink(this);
sail 2013/04/09 20:53:53 use constructor instead of assign? (sensor_event_i
nhu 2013/04/10 09:46:42 Done.
181 base::win::ScopedComPtr<ISensorEvents> sensor_events;
182 hr = sensor_event_impl->QueryInterface(
183 __uuidof(ISensorEvents), sensor_events.ReceiveVoid());
bulach 2013/04/10 07:59:46 nit: too much indentation, should be +4 rather tha
nhu 2013/04/10 09:46:42 Done.
184 if (FAILED(hr) || !sensor_events)
185 return false;
186
187 hr = sensor_->SetEventSink(sensor_events);
188 if (FAILED(hr))
189 return false;
190
191 return true;
192 }
193
194 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698