| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/device_sensors/device_inertial_sensor_service.h" | 5 #include "device/device_sensors/device_inertial_sensor_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/singleton.h" | 9 #include "base/memory/singleton.h" |
| 10 #include "content/browser/device_sensors/data_fetcher_shared_memory.h" | 10 #include "device/device_sensors/data_fetcher_shared_memory.h" |
| 11 | 11 |
| 12 namespace content { | 12 namespace device { |
| 13 | 13 |
| 14 DeviceInertialSensorService::DeviceInertialSensorService() | 14 DeviceInertialSensorService::DeviceInertialSensorService() |
| 15 : num_light_readers_(0), | 15 : num_light_readers_(0), |
| 16 num_motion_readers_(0), | 16 num_motion_readers_(0), |
| 17 num_orientation_readers_(0), | 17 num_orientation_readers_(0), |
| 18 is_shutdown_(false) { | 18 is_shutdown_(false) { |
| 19 } | 19 } |
| 20 | 20 |
| 21 DeviceInertialSensorService::~DeviceInertialSensorService() { | 21 DeviceInertialSensorService::~DeviceInertialSensorService() { |
| 22 } | 22 } |
| 23 | 23 |
| 24 DeviceInertialSensorService* DeviceInertialSensorService::GetInstance() { | 24 DeviceInertialSensorService* DeviceInertialSensorService::GetInstance() { |
| 25 return Singleton<DeviceInertialSensorService, | 25 return Singleton<DeviceInertialSensorService, |
| 26 LeakySingletonTraits<DeviceInertialSensorService> >::get(); | 26 LeakySingletonTraits<DeviceInertialSensorService>>::get(); |
| 27 } | 27 } |
| 28 | 28 |
| 29 void DeviceInertialSensorService::AddConsumer(ConsumerType consumer_type) { | 29 void DeviceInertialSensorService::AddConsumer(ConsumerType consumer_type) { |
| 30 LOG(WARNING) << "AddConsumer " << consumer_type; |
| 30 if (!ChangeNumberConsumers(consumer_type, 1)) | 31 if (!ChangeNumberConsumers(consumer_type, 1)) |
| 31 return; | 32 return; |
| 32 | 33 |
| 33 DCHECK(GetNumberConsumers(consumer_type)); | 34 DCHECK(GetNumberConsumers(consumer_type)); |
| 34 | 35 |
| 35 if (!data_fetcher_) | 36 if (!data_fetcher_) |
| 36 data_fetcher_.reset(new DataFetcherSharedMemory); | 37 data_fetcher_.reset(new DataFetcherSharedMemory); |
| 37 data_fetcher_->StartFetchingDeviceData(consumer_type); | 38 data_fetcher_->StartFetchingDeviceData(consumer_type); |
| 38 } | 39 } |
| 39 | 40 |
| 40 void DeviceInertialSensorService::RemoveConsumer(ConsumerType consumer_type) { | 41 void DeviceInertialSensorService::RemoveConsumer(ConsumerType consumer_type) { |
| 42 LOG(WARNING) << "RemoveConsumer " << consumer_type; |
| 41 if (!ChangeNumberConsumers(consumer_type, -1)) | 43 if (!ChangeNumberConsumers(consumer_type, -1)) |
| 42 return; | 44 return; |
| 43 | 45 |
| 44 if (GetNumberConsumers(consumer_type) == 0) | 46 if (GetNumberConsumers(consumer_type) == 0) |
| 45 data_fetcher_->StopFetchingDeviceData(consumer_type); | 47 data_fetcher_->StopFetchingDeviceData(consumer_type); |
| 46 } | 48 } |
| 47 | |
| 48 bool DeviceInertialSensorService::ChangeNumberConsumers( | 49 bool DeviceInertialSensorService::ChangeNumberConsumers( |
| 49 ConsumerType consumer_type, int delta) { | 50 ConsumerType consumer_type, |
| 51 int delta) { |
| 50 DCHECK(thread_checker_.CalledOnValidThread()); | 52 DCHECK(thread_checker_.CalledOnValidThread()); |
| 51 if (is_shutdown_) | 53 if (is_shutdown_) |
| 52 return false; | 54 return false; |
| 53 | 55 |
| 54 switch (consumer_type) { | 56 switch (consumer_type) { |
| 55 case CONSUMER_TYPE_MOTION: | 57 case CONSUMER_TYPE_MOTION: |
| 56 num_motion_readers_ += delta; | 58 num_motion_readers_ += delta; |
| 57 DCHECK_GE(num_motion_readers_, 0); | 59 DCHECK_GE(num_motion_readers_, 0); |
| 58 return true; | 60 return true; |
| 59 case CONSUMER_TYPE_ORIENTATION: | 61 case CONSUMER_TYPE_ORIENTATION: |
| 60 num_orientation_readers_ += delta; | 62 num_orientation_readers_ += delta; |
| 61 DCHECK_GE(num_orientation_readers_ , 0); | 63 DCHECK_GE(num_orientation_readers_, 0); |
| 62 return true; | 64 return true; |
| 63 case CONSUMER_TYPE_LIGHT: | 65 case CONSUMER_TYPE_LIGHT: |
| 64 num_light_readers_ += delta; | 66 num_light_readers_ += delta; |
| 65 DCHECK_GE(num_light_readers_, 0); | 67 DCHECK_GE(num_light_readers_, 0); |
| 66 return true; | 68 return true; |
| 67 default: | 69 default: |
| 68 NOTREACHED(); | 70 NOTREACHED(); |
| 69 } | 71 } |
| 70 return false; | 72 return false; |
| 71 } | 73 } |
| 72 | 74 |
| 73 int DeviceInertialSensorService::GetNumberConsumers( | 75 int DeviceInertialSensorService::GetNumberConsumers( |
| 74 ConsumerType consumer_type) const { | 76 ConsumerType consumer_type) const { |
| 75 switch (consumer_type) { | 77 switch (consumer_type) { |
| 76 case CONSUMER_TYPE_MOTION: | 78 case CONSUMER_TYPE_MOTION: |
| 77 return num_motion_readers_; | 79 return num_motion_readers_; |
| 78 case CONSUMER_TYPE_ORIENTATION: | 80 case CONSUMER_TYPE_ORIENTATION: |
| 79 return num_orientation_readers_; | 81 return num_orientation_readers_; |
| 80 case CONSUMER_TYPE_LIGHT: | 82 case CONSUMER_TYPE_LIGHT: |
| 81 return num_light_readers_; | 83 return num_light_readers_; |
| 82 default: | 84 default: |
| 83 NOTREACHED(); | 85 NOTREACHED(); |
| 84 } | 86 } |
| 85 return 0; | 87 return 0; |
| 86 } | 88 } |
| 87 | 89 |
| 88 base::SharedMemoryHandle | 90 base::SharedMemoryHandle |
| 89 DeviceInertialSensorService::GetSharedMemoryHandleForProcess( | 91 DeviceInertialSensorService::GetSharedMemoryHandleForProcess( |
| 90 ConsumerType consumer_type, base::ProcessHandle handle) { | 92 ConsumerType consumer_type, |
| 93 base::ProcessHandle handle) { |
| 94 LOG(WARNING) << "GetSharedMemoryHandleForProcess " << consumer_type; |
| 91 DCHECK(thread_checker_.CalledOnValidThread()); | 95 DCHECK(thread_checker_.CalledOnValidThread()); |
| 92 return data_fetcher_->GetSharedMemoryHandleForProcess(consumer_type, handle); | 96 return data_fetcher_->GetSharedMemoryHandleForProcess(consumer_type, handle); |
| 93 } | 97 } |
| 94 | 98 |
| 95 void DeviceInertialSensorService::Shutdown() { | 99 void DeviceInertialSensorService::Shutdown() { |
| 96 if (data_fetcher_) { | 100 if (data_fetcher_) { |
| 97 data_fetcher_->Shutdown(); | 101 data_fetcher_->Shutdown(); |
| 98 data_fetcher_.reset(); | 102 data_fetcher_.reset(); |
| 99 } | 103 } |
| 100 is_shutdown_ = true; | 104 is_shutdown_ = true; |
| 101 } | 105 } |
| 102 | 106 |
| 103 void DeviceInertialSensorService::SetDataFetcherForTesting( | 107 void DeviceInertialSensorService::SetDataFetcherForTesting( |
| 104 DataFetcherSharedMemory* test_data_fetcher) { | 108 DataFetcherSharedMemory* test_data_fetcher) { |
| 105 if (data_fetcher_) | 109 if (data_fetcher_) |
| 106 data_fetcher_->Shutdown(); | 110 data_fetcher_->Shutdown(); |
| 107 data_fetcher_.reset(test_data_fetcher); | 111 data_fetcher_.reset(test_data_fetcher); |
| 108 } | 112 } |
| 109 | 113 |
| 110 } // namespace content | 114 } // namespace device |
| OLD | NEW |