Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / device_sensors / device_inertial_sensor_service.cc
blob8970cd68e3f2c688398be719495e0482eab5e31f
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/device_inertial_sensor_service.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/memory/singleton.h"
10 #include "content/browser/device_sensors/data_fetcher_shared_memory.h"
12 namespace content {
14 DeviceInertialSensorService::DeviceInertialSensorService()
15 : num_light_readers_(0),
16 num_motion_readers_(0),
17 num_orientation_readers_(0),
18 is_shutdown_(false) {
21 DeviceInertialSensorService::~DeviceInertialSensorService() {
24 DeviceInertialSensorService* DeviceInertialSensorService::GetInstance() {
25 return base::Singleton<
26 DeviceInertialSensorService,
27 base::LeakySingletonTraits<DeviceInertialSensorService>>::get();
30 void DeviceInertialSensorService::AddConsumer(ConsumerType consumer_type) {
31 if (!ChangeNumberConsumers(consumer_type, 1))
32 return;
34 DCHECK(GetNumberConsumers(consumer_type));
36 if (!data_fetcher_)
37 data_fetcher_.reset(new DataFetcherSharedMemory);
38 data_fetcher_->StartFetchingDeviceData(consumer_type);
41 void DeviceInertialSensorService::RemoveConsumer(ConsumerType consumer_type) {
42 if (!ChangeNumberConsumers(consumer_type, -1))
43 return;
45 if (GetNumberConsumers(consumer_type) == 0)
46 data_fetcher_->StopFetchingDeviceData(consumer_type);
49 bool DeviceInertialSensorService::ChangeNumberConsumers(
50 ConsumerType consumer_type, int delta) {
51 DCHECK(thread_checker_.CalledOnValidThread());
52 if (is_shutdown_)
53 return false;
55 switch (consumer_type) {
56 case CONSUMER_TYPE_MOTION:
57 num_motion_readers_ += delta;
58 DCHECK_GE(num_motion_readers_, 0);
59 return true;
60 case CONSUMER_TYPE_ORIENTATION:
61 num_orientation_readers_ += delta;
62 DCHECK_GE(num_orientation_readers_ , 0);
63 return true;
64 case CONSUMER_TYPE_LIGHT:
65 num_light_readers_ += delta;
66 DCHECK_GE(num_light_readers_, 0);
67 return true;
68 default:
69 NOTREACHED();
71 return false;
74 int DeviceInertialSensorService::GetNumberConsumers(
75 ConsumerType consumer_type) const {
76 switch (consumer_type) {
77 case CONSUMER_TYPE_MOTION:
78 return num_motion_readers_;
79 case CONSUMER_TYPE_ORIENTATION:
80 return num_orientation_readers_;
81 case CONSUMER_TYPE_LIGHT:
82 return num_light_readers_;
83 default:
84 NOTREACHED();
86 return 0;
89 base::SharedMemoryHandle
90 DeviceInertialSensorService::GetSharedMemoryHandleForProcess(
91 ConsumerType consumer_type, base::ProcessHandle handle) {
92 DCHECK(thread_checker_.CalledOnValidThread());
93 return data_fetcher_->GetSharedMemoryHandleForProcess(consumer_type, handle);
96 void DeviceInertialSensorService::Shutdown() {
97 if (data_fetcher_) {
98 data_fetcher_->Shutdown();
99 data_fetcher_.reset();
101 is_shutdown_ = true;
104 void DeviceInertialSensorService::SetDataFetcherForTesting(
105 DataFetcherSharedMemory* test_data_fetcher) {
106 if (data_fetcher_)
107 data_fetcher_->Shutdown();
108 data_fetcher_.reset(test_data_fetcher);
111 } // namespace content