Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / browser / device_sensors / device_inertial_sensor_service.cc
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.
4
5 #include "content/browser/device_sensors/device_inertial_sensor_service.h"
6
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"
11 #include "content/public/browser/render_process_host.h"
12
13 namespace content {
14
15 DeviceInertialSensorService::DeviceInertialSensorService()
16     : num_motion_readers_(0),
17       num_orientation_readers_(0),
18       is_shutdown_(false) {
19 }
20
21 DeviceInertialSensorService::~DeviceInertialSensorService() {
22 }
23
24 DeviceInertialSensorService* DeviceInertialSensorService::GetInstance() {
25   return Singleton<DeviceInertialSensorService,
26                    LeakySingletonTraits<DeviceInertialSensorService> >::get();
27 }
28
29 void DeviceInertialSensorService::AddConsumer(ConsumerType consumer_type) {
30   if (!ChangeNumberConsumers(consumer_type, 1))
31     return;
32
33   DCHECK(GetNumberConsumers(consumer_type));
34
35   if (!data_fetcher_)
36     data_fetcher_.reset(new DataFetcherSharedMemory);
37   data_fetcher_->StartFetchingDeviceData(consumer_type);
38 }
39
40 void DeviceInertialSensorService::RemoveConsumer(ConsumerType consumer_type) {
41   if (!ChangeNumberConsumers(consumer_type, -1))
42     return;
43
44   if (GetNumberConsumers(consumer_type) == 0)
45     data_fetcher_->StopFetchingDeviceData(consumer_type);
46 }
47
48 bool DeviceInertialSensorService::ChangeNumberConsumers(
49     ConsumerType consumer_type, int delta) {
50   DCHECK(thread_checker_.CalledOnValidThread());
51   if (is_shutdown_)
52     return false;
53
54   switch (consumer_type) {
55     case CONSUMER_TYPE_MOTION:
56       num_motion_readers_ += delta;
57       DCHECK_GE(num_motion_readers_, 0);
58       return true;
59     case CONSUMER_TYPE_ORIENTATION:
60       num_orientation_readers_ += delta;
61       DCHECK_GE(num_orientation_readers_ , 0);
62       return true;
63     default:
64       NOTREACHED();
65   }
66   return false;
67 }
68
69 int DeviceInertialSensorService::GetNumberConsumers(
70     ConsumerType consumer_type) const {
71   switch (consumer_type) {
72     case CONSUMER_TYPE_MOTION:
73       return num_motion_readers_;
74     case CONSUMER_TYPE_ORIENTATION:
75       return num_orientation_readers_;
76     default:
77       NOTREACHED();
78   }
79   return 0;
80 }
81
82 base::SharedMemoryHandle
83 DeviceInertialSensorService::GetSharedMemoryHandleForProcess(
84     ConsumerType consumer_type, base::ProcessHandle handle) {
85   DCHECK(thread_checker_.CalledOnValidThread());
86   return data_fetcher_->GetSharedMemoryHandleForProcess(consumer_type, handle);
87 }
88
89 void DeviceInertialSensorService::Shutdown() {
90   data_fetcher_.reset();
91   is_shutdown_ = true;
92 }
93
94 void DeviceInertialSensorService::SetDataFetcherForTests(
95     DataFetcherSharedMemory* test_data_fetcher) {
96   data_fetcher_.reset(test_data_fetcher);
97 }
98
99 }  // namespace content