Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / device_sensors / data_fetcher_shared_memory_default.cc
blob9b3a60babe8f9e85dacda3fc898e131868434012
1 // Copyright 2014 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.
5 #include "content/browser/device_sensors/data_fetcher_shared_memory.h"
7 #include "base/logging.h"
8 #include "base/metrics/histogram.h"
10 namespace {
12 bool SetMotionBuffer(content::DeviceMotionHardwareBuffer* buffer,
13 bool enabled) {
14 if (!buffer)
15 return false;
16 buffer->seqlock.WriteBegin();
17 buffer->data.allAvailableSensorsAreActive = enabled;
18 buffer->seqlock.WriteEnd();
19 return true;
22 bool SetOrientationBuffer(
23 content::DeviceOrientationHardwareBuffer* buffer, bool enabled) {
24 if (!buffer)
25 return false;
26 buffer->seqlock.WriteBegin();
27 buffer->data.allAvailableSensorsAreActive = enabled;
28 buffer->seqlock.WriteEnd();
29 return true;
32 bool SetLightBuffer(content::DeviceLightHardwareBuffer* buffer,
33 double lux) {
34 if (!buffer)
35 return false;
36 buffer->seqlock.WriteBegin();
37 buffer->data.value = lux;
38 buffer->seqlock.WriteEnd();
39 return true;
42 } // namespace
44 namespace content {
46 DataFetcherSharedMemory::DataFetcherSharedMemory()
47 : motion_buffer_(nullptr),
48 orientation_buffer_(nullptr),
49 light_buffer_(nullptr) {
52 DataFetcherSharedMemory::~DataFetcherSharedMemory() {
55 bool DataFetcherSharedMemory::Start(ConsumerType consumer_type, void* buffer) {
56 DCHECK(buffer);
58 switch (consumer_type) {
59 case CONSUMER_TYPE_MOTION:
60 motion_buffer_ = static_cast<DeviceMotionHardwareBuffer*>(buffer);
61 UMA_HISTOGRAM_BOOLEAN("InertialSensor.MotionDefaultAvailable", false);
62 return SetMotionBuffer(motion_buffer_, true);
63 case CONSUMER_TYPE_ORIENTATION:
64 orientation_buffer_ =
65 static_cast<DeviceOrientationHardwareBuffer*>(buffer);
66 UMA_HISTOGRAM_BOOLEAN("InertialSensor.OrientationDefaultAvailable",
67 false);
68 return SetOrientationBuffer(orientation_buffer_, true);
69 case CONSUMER_TYPE_LIGHT:
70 light_buffer_ = static_cast<DeviceLightHardwareBuffer*>(buffer);
71 return SetLightBuffer(light_buffer_,
72 std::numeric_limits<double>::infinity());
73 default:
74 NOTREACHED();
76 return false;
79 bool DataFetcherSharedMemory::Stop(ConsumerType consumer_type) {
80 switch (consumer_type) {
81 case CONSUMER_TYPE_MOTION:
82 return SetMotionBuffer(motion_buffer_, false);
83 case CONSUMER_TYPE_ORIENTATION:
84 return SetOrientationBuffer(orientation_buffer_, false);
85 case CONSUMER_TYPE_LIGHT:
86 return SetLightBuffer(light_buffer_, -1);
87 default:
88 NOTREACHED();
90 return false;
93 } // namespace content