1// Copyright 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 "data_fetcher_shared_memory.h"
6
7#include "base/logging.h"
8#include "base/metrics/histogram.h"
9
10namespace {
11
12static 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;
20}
21
22static 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;
30}
31
32}
33
34namespace content {
35
36DataFetcherSharedMemory::DataFetcherSharedMemory()
37    : motion_buffer_(NULL), orientation_buffer_(NULL) {
38}
39
40DataFetcherSharedMemory::~DataFetcherSharedMemory() {
41}
42
43bool DataFetcherSharedMemory::Start(ConsumerType consumer_type, void* buffer) {
44  DCHECK(buffer);
45
46  switch (consumer_type) {
47    case CONSUMER_TYPE_MOTION:
48      motion_buffer_ = static_cast<DeviceMotionHardwareBuffer*>(buffer);
49      UMA_HISTOGRAM_BOOLEAN("InertialSensor.MotionDefaultAvailable", false);
50      return SetMotionBuffer(motion_buffer_, true);
51    case CONSUMER_TYPE_ORIENTATION:
52      orientation_buffer_ =
53          static_cast<DeviceOrientationHardwareBuffer*>(buffer);
54      UMA_HISTOGRAM_BOOLEAN("InertialSensor.OrientationDefaultAvailable",
55          false);
56      return SetOrientationBuffer(orientation_buffer_, true);
57    default:
58      NOTREACHED();
59  }
60  return false;
61}
62
63bool DataFetcherSharedMemory::Stop(ConsumerType consumer_type) {
64
65  switch (consumer_type) {
66    case CONSUMER_TYPE_MOTION:
67      return SetMotionBuffer(motion_buffer_, false);
68    case CONSUMER_TYPE_ORIENTATION:
69      return SetOrientationBuffer(orientation_buffer_, false);
70    default:
71      NOTREACHED();
72  }
73  return false;
74}
75
76}  // namespace content
77