diff options
18 files changed, 781 insertions, 517 deletions
diff --git a/content/browser/browser_main_loop.cc b/content/browser/browser_main_loop.cc index 82a312e8..8bf6503 100644 --- a/content/browser/browser_main_loop.cc +++ b/content/browser/browser_main_loop.cc @@ -23,7 +23,7 @@ #include "base/threading/thread_restrictions.h" #include "base/timer/hi_res_timer_manager.h" #include "content/browser/browser_thread_impl.h" -#include "content/browser/device_orientation/device_motion_service.h" +#include "content/browser/device_orientation/device_inertial_sensor_service.h" #include "content/browser/download/save_file_manager.h" #include "content/browser/gamepad/gamepad_service.h" #include "content/browser/gpu/browser_gpu_channel_host_factory.h" @@ -796,7 +796,7 @@ void BrowserMainLoop::ShutdownThreadsAndCleanUp() { // Must happen after the I/O thread is shutdown since this class lives on the // I/O thread and isn't threadsafe. GamepadService::GetInstance()->Terminate(); - DeviceMotionService::GetInstance()->Shutdown(); + DeviceInertialSensorService::GetInstance()->Shutdown(); URLDataManager::DeleteDataSources(); #endif // !defined(OS_IOS) diff --git a/content/browser/device_orientation/data_fetcher_shared_memory.h b/content/browser/device_orientation/data_fetcher_shared_memory.h index dc5e92a..9d378eb 100644 --- a/content/browser/device_orientation/data_fetcher_shared_memory.h +++ b/content/browser/device_orientation/data_fetcher_shared_memory.h @@ -5,48 +5,21 @@ #ifndef CONTENT_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_SHARED_MEMORY_H_ #define CONTENT_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_SHARED_MEMORY_H_ -#include "content/browser/device_orientation/device_data.h" -#include "content/common/device_motion_hardware_buffer.h" - -namespace WebKit { -class WebDeviceMotionData; -} +#include "content/browser/device_orientation/data_fetcher_shared_memory_base.h" namespace content { -class CONTENT_EXPORT DataFetcherSharedMemory { +class CONTENT_EXPORT DataFetcherSharedMemory + : public DataFetcherSharedMemoryBase { + public: - DataFetcherSharedMemory() - : device_motion_buffer_(NULL), - started_(false) { } + DataFetcherSharedMemory(); virtual ~DataFetcherSharedMemory(); - // Returns true if this fetcher needs explicit calls to fetch the data. - // Called from any thread. - virtual bool NeedsPolling(); - - // If this fetcher NeedsPolling() is true, this method will update the - // buffer with the latest device motion data. - // Returns true if there was any motion data to update the buffer with. - // Called from the DeviceMotionProvider::PollingThread. - virtual bool FetchDeviceMotionDataIntoBuffer(); - - // Returns true if the relevant sensors could be successfully activated. - // This method should be called before any calls to - // FetchDeviceMotionDataIntoBuffer(). - // If NeedsPolling() is true this method should be called from the - // PollingThread. - virtual bool StartFetchingDeviceMotionData( - DeviceMotionHardwareBuffer* buffer); - - // Indicates to the fetcher to stop fetching device data. - // If NeedsPolling() is true this method should be called from the - // PollingThread. - virtual void StopFetchingDeviceMotionData(); - private: - DeviceMotionHardwareBuffer* device_motion_buffer_; - bool started_; + + virtual bool Start(ConsumerType consumer_type) OVERRIDE; + virtual bool Stop(ConsumerType consumer_type) OVERRIDE; DISALLOW_COPY_AND_ASSIGN(DataFetcherSharedMemory); }; diff --git a/content/browser/device_orientation/data_fetcher_shared_memory_android.cc b/content/browser/device_orientation/data_fetcher_shared_memory_android.cc index 8f6e202..8d4a9d0 100644 --- a/content/browser/device_orientation/data_fetcher_shared_memory_android.cc +++ b/content/browser/device_orientation/data_fetcher_shared_memory_android.cc @@ -2,39 +2,52 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "data_fetcher_shared_memory.h" +#include "content/browser/device_orientation/data_fetcher_shared_memory.h" #include "base/logging.h" -#include "data_fetcher_impl_android.h" +#include "content/browser/device_orientation/data_fetcher_impl_android.h" +#include "content/common/device_motion_hardware_buffer.h" +#include "content/common/device_orientation/device_orientation_hardware_buffer.h" namespace content { -DataFetcherSharedMemory::~DataFetcherSharedMemory() { - if (started_) - StopFetchingDeviceMotionData(); +DataFetcherSharedMemory::DataFetcherSharedMemory() { } -bool DataFetcherSharedMemory::NeedsPolling() { - return false; +DataFetcherSharedMemory::~DataFetcherSharedMemory() { } -bool DataFetcherSharedMemory::FetchDeviceMotionDataIntoBuffer() { - NOTREACHED(); +bool DataFetcherSharedMemory::Start(ConsumerType consumer_type) { + switch (consumer_type) { + case CONSUMER_TYPE_MOTION: + if (void* buffer = InitSharedMemoryBuffer(consumer_type, + sizeof(DeviceMotionHardwareBuffer))) { + return DataFetcherImplAndroid::GetInstance()-> + StartFetchingDeviceMotionData( + static_cast<DeviceMotionHardwareBuffer*>(buffer)); + } + break; + case CONSUMER_TYPE_ORIENTATION: + NOTIMPLEMENTED(); + break; + default: + NOTREACHED(); + } return false; } -bool DataFetcherSharedMemory::StartFetchingDeviceMotionData( - DeviceMotionHardwareBuffer* buffer) { - DCHECK(buffer); - device_motion_buffer_ = buffer; - started_ = DataFetcherImplAndroid::GetInstance()-> - StartFetchingDeviceMotionData(buffer); - return started_; -} - -void DataFetcherSharedMemory::StopFetchingDeviceMotionData() { - DataFetcherImplAndroid::GetInstance()->StopFetchingDeviceMotionData(); - started_ = false; +bool DataFetcherSharedMemory::Stop(ConsumerType consumer_type) { + switch (consumer_type) { + case CONSUMER_TYPE_MOTION: + DataFetcherImplAndroid::GetInstance()->StopFetchingDeviceMotionData(); + return true; + case CONSUMER_TYPE_ORIENTATION: + NOTIMPLEMENTED(); + break; + default: + NOTREACHED(); + } + return false; } } // namespace content diff --git a/content/browser/device_orientation/data_fetcher_shared_memory_base.cc b/content/browser/device_orientation/data_fetcher_shared_memory_base.cc new file mode 100644 index 0000000..167b787 --- /dev/null +++ b/content/browser/device_orientation/data_fetcher_shared_memory_base.cc @@ -0,0 +1,197 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "content/browser/device_orientation/data_fetcher_shared_memory_base.h" + +#include "base/bind.h" +#include "base/logging.h" +#include "base/stl_util.h" +#include "base/threading/thread.h" +#include "base/timer/timer.h" + +namespace content { + +namespace { +const int kPeriodInMilliseconds = 100; +} + +class DataFetcherSharedMemoryBase::PollingThread : public base::Thread { + public: + PollingThread(const char* name, DataFetcherSharedMemoryBase* fetcher); + virtual ~PollingThread(); + + void SetConsumers(int consumers_bitmask); + unsigned GetConsumersBitmask() const { return consumers_bitmask_; } + + private: + + void DoPoll(); + + unsigned consumers_bitmask_; + DataFetcherSharedMemoryBase* fetcher_; + scoped_ptr<base::RepeatingTimer<PollingThread> > timer_; + + DISALLOW_COPY_AND_ASSIGN(PollingThread); +}; + +// --- PollingThread methods + +DataFetcherSharedMemoryBase::PollingThread::PollingThread( + const char* name, DataFetcherSharedMemoryBase* fetcher) + : base::Thread(name), consumers_bitmask_(0), fetcher_(fetcher) { +} + +DataFetcherSharedMemoryBase::PollingThread::~PollingThread() { +} + +void DataFetcherSharedMemoryBase::PollingThread::SetConsumers( + int consumers_bitmask) { + consumers_bitmask_ = consumers_bitmask; + if (!consumers_bitmask_) { + timer_.reset(); // will also stop the timer. + return; + } + + if (!timer_) + timer_.reset(new base::RepeatingTimer<PollingThread>()); + + timer_->Start(FROM_HERE, + base::TimeDelta::FromMilliseconds(kPeriodInMilliseconds), + this, &PollingThread::DoPoll); +} + +void DataFetcherSharedMemoryBase::PollingThread::DoPoll() { + DCHECK(fetcher_); + DCHECK(consumers_bitmask_); + fetcher_->Fetch(consumers_bitmask_); +} + +// --- end of PollingThread methods + +DataFetcherSharedMemoryBase::DataFetcherSharedMemoryBase() + : started_consumers_(0) { +} + +DataFetcherSharedMemoryBase::~DataFetcherSharedMemoryBase() { + StopFetchingDeviceData(CONSUMER_TYPE_MOTION); + StopFetchingDeviceData(CONSUMER_TYPE_ORIENTATION); + + // make sure polling thread stops asap. + if (polling_thread_) + polling_thread_->Stop(); + + STLDeleteContainerPairSecondPointers(shared_memory_map_.begin(), + shared_memory_map_.end()); +} + +bool DataFetcherSharedMemoryBase::StartFetchingDeviceData( + ConsumerType consumer_type) { + if (started_consumers_ & consumer_type) + return true; + + if (!Start(consumer_type)) + return false; + + started_consumers_ |= consumer_type; + + if (IsPolling()) { + if (!InitAndStartPollingThreadIfNecessary()) { + Stop(consumer_type); + started_consumers_ ^= consumer_type; + return false; + } + polling_thread_->message_loop()->PostTask( + FROM_HERE, + base::Bind(&PollingThread::SetConsumers, + base::Unretained(polling_thread_.get()), + started_consumers_)); + } + return true; +} + +bool DataFetcherSharedMemoryBase::StopFetchingDeviceData( + ConsumerType consumer_type) { + if (!(started_consumers_ & consumer_type)) + return true; + + if (!Stop(consumer_type)) + return false; + + started_consumers_ ^= consumer_type; + + if (IsPolling()) { + polling_thread_->message_loop()->PostTask( + FROM_HERE, + base::Bind(&PollingThread::SetConsumers, + base::Unretained(polling_thread_.get()), + started_consumers_)); + } + return true; +} + +base::SharedMemoryHandle +DataFetcherSharedMemoryBase::GetSharedMemoryHandleForProcess( + ConsumerType consumer_type, base::ProcessHandle process) { + SharedMemoryMap::const_iterator it = shared_memory_map_.find(consumer_type); + if (it == shared_memory_map_.end()) + return base::SharedMemory::NULLHandle(); + + base::SharedMemoryHandle renderer_handle; + it->second->ShareToProcess(process, &renderer_handle); + return renderer_handle; +} + +bool DataFetcherSharedMemoryBase::InitAndStartPollingThreadIfNecessary() { + if (polling_thread_) + return true; + + polling_thread_.reset( + new PollingThread("Inertial Device Sensor poller", this)); + + if (!polling_thread_->Start()) { + LOG(ERROR) << "Failed to start inertial sensor data polling thread"; + return false; + } + return true; +} + +void DataFetcherSharedMemoryBase::Fetch(unsigned consumer_bitmask) { + NOTIMPLEMENTED(); +} + +bool DataFetcherSharedMemoryBase::IsPolling() const { + return false; +} + +base::SharedMemory* DataFetcherSharedMemoryBase::InitSharedMemory( + ConsumerType consumer_type, size_t buffer_size) { + SharedMemoryMap::const_iterator it = shared_memory_map_.find(consumer_type); + if (it != shared_memory_map_.end()) + return it->second; + + base::SharedMemory* new_shared_mem = new base::SharedMemory; + if (new_shared_mem->CreateAndMapAnonymous(buffer_size)) { + if (void* mem = new_shared_mem->memory()) { + memset(mem, 0, buffer_size); + shared_memory_map_[consumer_type] = new_shared_mem; + return new_shared_mem; + } + } + LOG(ERROR) << "Failed to initialize shared memory"; + return NULL; +} + +void* DataFetcherSharedMemoryBase::InitSharedMemoryBuffer( + ConsumerType consumer_type, size_t buffer_size) { + if (base::SharedMemory* shared_memory = InitSharedMemory(consumer_type, + buffer_size)) + return shared_memory->memory(); + return NULL; +} + +base::MessageLoop* DataFetcherSharedMemoryBase::GetPollingMessageLoop() const { + return polling_thread_ ? polling_thread_->message_loop() : NULL; +} + +} // namespace content diff --git a/content/browser/device_orientation/data_fetcher_shared_memory_base.h b/content/browser/device_orientation/data_fetcher_shared_memory_base.h new file mode 100644 index 0000000..a449596 --- /dev/null +++ b/content/browser/device_orientation/data_fetcher_shared_memory_base.h @@ -0,0 +1,85 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CONTENT_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_SHARED_MEMORY_BASE_H_ +#define CONTENT_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_SHARED_MEMORY_BASE_H_ + +#include <map> + +#include "base/memory/scoped_ptr.h" +#include "base/memory/shared_memory.h" +#include "base/message_loop/message_loop.h" +#include "content/browser/device_orientation/inertial_sensor_consts.h" +#include "content/common/content_export.h" + +namespace content { + +// Sensor data fetchers should derive from this base class and implement +// the abstract Start() and Stop() methods. +// If the fetcher requires polling it should also implement IsPolling() +// to return true and the Fetch() method which will be called from the +// polling thread to fetch data at regular intervals. +class CONTENT_EXPORT DataFetcherSharedMemoryBase { + public: + + // Starts updating the shared memory buffer with sensor data at + // regular intervals. Returns true if the relevant sensors could + // be successfully activated. + bool StartFetchingDeviceData(ConsumerType consumer_type); + + // Stops updating the shared memory buffer. Returns true if the + // relevant sensors could be successfully deactivated. + bool StopFetchingDeviceData(ConsumerType consumer_type); + + // Returns the shared memory handle of the device sensor data + // duplicated into the given process. This method should only be + // called after a call to StartFetchingDeviceData method with + // corresponding |consumer_type| parameter. + base::SharedMemoryHandle GetSharedMemoryHandleForProcess( + ConsumerType consumer_type, base::ProcessHandle process); + + protected: + class PollingThread; + + DataFetcherSharedMemoryBase(); + virtual ~DataFetcherSharedMemoryBase(); + + void* InitSharedMemoryBuffer(ConsumerType consumer_type, size_t buffer_size); + + // Returns the message loop of the polling thread. + // Returns NULL if there is no polling thread. + base::MessageLoop* GetPollingMessageLoop() const; + + // If IsPolling() is true this method is called from the |polling_thread_| + // at regular intervals. + virtual void Fetch(unsigned consumer_bitmask); + + // Returns true if this fetcher needs to use a polling thread to + // fetch the sensor data. + virtual bool IsPolling() const; + + // Start() method should call InitSharedMemoryBuffer() and cache the obtained + // pointer for efficienty and thread-safety. + virtual bool Start(ConsumerType consumer_type) = 0; + virtual bool Stop(ConsumerType consumer_type) = 0; + + private: + bool InitAndStartPollingThreadIfNecessary(); + base::SharedMemory* InitSharedMemory(ConsumerType consumer_type, + size_t buffer_size); + + unsigned started_consumers_; + + scoped_ptr<PollingThread> polling_thread_; + + // Owning pointers. Objects in the map are deleted in dtor. + typedef std::map<ConsumerType, base::SharedMemory*> SharedMemoryMap; + SharedMemoryMap shared_memory_map_; + + DISALLOW_COPY_AND_ASSIGN(DataFetcherSharedMemoryBase); +}; + +} + +#endif // CONTENT_BROWSER_DEVICE_ORIENTATION_DATA_FETCHER_SHARED_MEMORY_BASE_H_ diff --git a/content/browser/device_orientation/data_fetcher_shared_memory_base_unittest.cc b/content/browser/device_orientation/data_fetcher_shared_memory_base_unittest.cc new file mode 100644 index 0000000..b580872 --- /dev/null +++ b/content/browser/device_orientation/data_fetcher_shared_memory_base_unittest.cc @@ -0,0 +1,245 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "content/browser/device_orientation/data_fetcher_shared_memory_base.h" + +#include "base/logging.h" +#include "base/synchronization/waitable_event.h" +#include "base/threading/thread.h" +#include "content/common/device_motion_hardware_buffer.h" +#include "content/common/device_orientation/device_orientation_hardware_buffer.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace content { + +namespace { + +const int kPeriodInMilliseconds = 100; + + +class FakeDataFetcher : public DataFetcherSharedMemoryBase { + public: + FakeDataFetcher() + : start_(false, false), + stop_(false, false), + updated_motion_(false, false), + updated_orientation_(false, false) { + } + virtual ~FakeDataFetcher() { } + + bool Init(ConsumerType consumer_type) { + switch (consumer_type) { + case CONSUMER_TYPE_MOTION: + motion_buffer_ = static_cast<DeviceMotionHardwareBuffer*>( + InitSharedMemoryBuffer(consumer_type, + sizeof(DeviceMotionHardwareBuffer))); + break; + case CONSUMER_TYPE_ORIENTATION: + orientation_buffer_ = static_cast<DeviceOrientationHardwareBuffer*>( + InitSharedMemoryBuffer(consumer_type, + sizeof(DeviceOrientationHardwareBuffer))); + break; + default: + return false; + } + return true; + } + + void UpdateMotion() { + DeviceMotionHardwareBuffer* buffer = GetMotionBuffer(); + ASSERT_TRUE(buffer); + buffer->seqlock.WriteBegin(); + buffer->data.interval = kPeriodInMilliseconds; + buffer->seqlock.WriteEnd(); + updated_motion_.Signal(); + } + + void UpdateOrientation() { + DeviceOrientationHardwareBuffer* buffer = GetOrientationBuffer(); + ASSERT_TRUE(buffer); + buffer->seqlock.WriteBegin(); + buffer->data.alpha = 1; + buffer->seqlock.WriteEnd(); + updated_orientation_.Signal(); + } + + DeviceMotionHardwareBuffer* GetMotionBuffer() const { + return motion_buffer_; + } + + DeviceOrientationHardwareBuffer* GetOrientationBuffer() const { + return orientation_buffer_; + } + + void WaitForStart() { + start_.Wait(); + } + + void WaitForStop() { + stop_.Wait(); + } + + void WaitForUpdateMotion() { + updated_motion_.Wait(); + } + + void WaitForUpdateOrientation() { + updated_orientation_.Wait(); + } + + protected: + base::WaitableEvent start_; + base::WaitableEvent stop_; + base::WaitableEvent updated_motion_; + base::WaitableEvent updated_orientation_; + + private: + DeviceMotionHardwareBuffer* motion_buffer_; + DeviceOrientationHardwareBuffer* orientation_buffer_; + + DISALLOW_COPY_AND_ASSIGN(FakeDataFetcher); +}; + +class FakeNonPollingDataFetcher : public FakeDataFetcher { + public: + FakeNonPollingDataFetcher() { } + virtual ~FakeNonPollingDataFetcher() { } + + virtual bool Start(ConsumerType consumer_type) OVERRIDE { + Init(consumer_type); + switch (consumer_type) { + case CONSUMER_TYPE_MOTION: + UpdateMotion(); + break; + case CONSUMER_TYPE_ORIENTATION: + UpdateOrientation(); + break; + default: + return false; + } + start_.Signal(); + return true; + } + + virtual bool Stop(ConsumerType consumer_type) OVERRIDE { + stop_.Signal(); + return true; + } + + virtual bool IsPolling() const OVERRIDE { + return false; + } + + virtual void Fetch(unsigned consumer_bitmask) OVERRIDE { + FAIL() << "fetch should not be called, " + << "because this is a non-polling fetcher"; + } + + private: + + DISALLOW_COPY_AND_ASSIGN(FakeNonPollingDataFetcher); +}; + +class FakePollingDataFetcher : public FakeDataFetcher { + public: + FakePollingDataFetcher() { } + virtual ~FakePollingDataFetcher() { } + + virtual bool Start(ConsumerType consumer_type) OVERRIDE { + Init(consumer_type); + start_.Signal(); + return true; + } + + virtual bool Stop(ConsumerType consumer_type) OVERRIDE { + stop_.Signal(); + return true; + } + + virtual void Fetch(unsigned consumer_bitmask) OVERRIDE { + DCHECK(base::MessageLoop::current() == GetPollingMessageLoop()); + + if (consumer_bitmask & CONSUMER_TYPE_ORIENTATION) + UpdateOrientation(); + else if (consumer_bitmask & CONSUMER_TYPE_MOTION) + UpdateMotion(); + else + FAIL() << "wrong consumer bitmask"; + } + + virtual bool IsPolling() const OVERRIDE { + return true; + } + + private: + + DISALLOW_COPY_AND_ASSIGN(FakePollingDataFetcher); +}; + + +TEST(DataFetcherSharedMemoryBaseTest, DoesStartMotion) { + FakeNonPollingDataFetcher* mock_data_fetcher = + new FakeNonPollingDataFetcher(); + EXPECT_FALSE(mock_data_fetcher->IsPolling()); + + EXPECT_TRUE(mock_data_fetcher->StartFetchingDeviceData(CONSUMER_TYPE_MOTION)); + mock_data_fetcher->WaitForStart(); + + EXPECT_EQ(kPeriodInMilliseconds, + mock_data_fetcher->GetMotionBuffer()->data.interval); + + mock_data_fetcher->StopFetchingDeviceData(CONSUMER_TYPE_MOTION); + mock_data_fetcher->WaitForStop(); +} + +TEST(DataFetcherSharedMemoryBaseTest, DoesStartOrientation) { + FakeNonPollingDataFetcher* mock_data_fetcher = + new FakeNonPollingDataFetcher(); + EXPECT_FALSE(mock_data_fetcher->IsPolling()); + + EXPECT_TRUE(mock_data_fetcher->StartFetchingDeviceData( + CONSUMER_TYPE_ORIENTATION)); + mock_data_fetcher->WaitForStart(); + + EXPECT_EQ(1, mock_data_fetcher->GetOrientationBuffer()->data.alpha); + + mock_data_fetcher->StopFetchingDeviceData(CONSUMER_TYPE_ORIENTATION); + mock_data_fetcher->WaitForStop(); +} + +TEST(DataFetcherSharedMemoryBaseTest, DoesPollMotion) { + FakePollingDataFetcher* mock_data_fetcher = + new FakePollingDataFetcher(); + EXPECT_TRUE(mock_data_fetcher->IsPolling()); + + EXPECT_TRUE(mock_data_fetcher->StartFetchingDeviceData(CONSUMER_TYPE_MOTION)); + mock_data_fetcher->WaitForStart(); + mock_data_fetcher->WaitForUpdateMotion(); + + EXPECT_EQ(kPeriodInMilliseconds, + mock_data_fetcher->GetMotionBuffer()->data.interval); + + mock_data_fetcher->StopFetchingDeviceData(CONSUMER_TYPE_MOTION); + mock_data_fetcher->WaitForStop(); +} + +TEST(DataFetcherSharedMemoryBaseTest, DoesPollOrientation) { + FakePollingDataFetcher* mock_data_fetcher = + new FakePollingDataFetcher(); + EXPECT_TRUE(mock_data_fetcher->IsPolling()); + + EXPECT_TRUE(mock_data_fetcher->StartFetchingDeviceData( + CONSUMER_TYPE_ORIENTATION)); + mock_data_fetcher->WaitForStart(); + mock_data_fetcher->WaitForUpdateOrientation(); + + EXPECT_EQ(1, mock_data_fetcher->GetOrientationBuffer()->data.alpha); + + mock_data_fetcher->StopFetchingDeviceData(CONSUMER_TYPE_ORIENTATION); + mock_data_fetcher->WaitForStop(); +} + +} // namespace + +} // namespace content diff --git a/content/browser/device_orientation/data_fetcher_shared_memory_default.cc b/content/browser/device_orientation/data_fetcher_shared_memory_default.cc index 0f66fc7..693019f 100644 --- a/content/browser/device_orientation/data_fetcher_shared_memory_default.cc +++ b/content/browser/device_orientation/data_fetcher_shared_memory_default.cc @@ -5,39 +5,62 @@ #include "data_fetcher_shared_memory.h" #include "base/logging.h" +#include "content/common/device_motion_hardware_buffer.h" +#include "content/common/device_orientation/device_orientation_hardware_buffer.h" -namespace content { +namespace { -DataFetcherSharedMemory::~DataFetcherSharedMemory() { - if (started_) - StopFetchingDeviceMotionData(); +static bool SetMotionBuffer(content::DeviceMotionHardwareBuffer* buffer, + bool enabled) { + DCHECK(buffer); + buffer->seqlock.WriteBegin(); + buffer->data.allAvailableSensorsAreActive = enabled; + buffer->seqlock.WriteEnd(); + return true; } -bool DataFetcherSharedMemory::NeedsPolling() { - return false; } -bool DataFetcherSharedMemory::FetchDeviceMotionDataIntoBuffer() { - NOTREACHED(); - return false; +namespace content { + +DataFetcherSharedMemory::DataFetcherSharedMemory() { } -bool DataFetcherSharedMemory::StartFetchingDeviceMotionData( - DeviceMotionHardwareBuffer* buffer) { - DCHECK(buffer); - device_motion_buffer_ = buffer; - device_motion_buffer_->seqlock.WriteBegin(); - device_motion_buffer_->data.allAvailableSensorsAreActive = true; - device_motion_buffer_->seqlock.WriteEnd(); - started_ = true; - return true; +DataFetcherSharedMemory::~DataFetcherSharedMemory() { } -void DataFetcherSharedMemory::StopFetchingDeviceMotionData() { - device_motion_buffer_->seqlock.WriteBegin(); - device_motion_buffer_->data.allAvailableSensorsAreActive = false; - device_motion_buffer_->seqlock.WriteEnd(); - started_ = false; +bool DataFetcherSharedMemory::Start(ConsumerType consumer_type) { + switch (consumer_type) { + case CONSUMER_TYPE_MOTION: + if (void* buffer = InitSharedMemoryBuffer(consumer_type, + sizeof(DeviceMotionHardwareBuffer))) + return SetMotionBuffer( + static_cast<DeviceMotionHardwareBuffer*>(buffer), true); + break; + case CONSUMER_TYPE_ORIENTATION: + NOTIMPLEMENTED(); + break; + default: + NOTREACHED(); + } + return false; +} + +bool DataFetcherSharedMemory::Stop(ConsumerType consumer_type) { + switch (consumer_type) { + case CONSUMER_TYPE_MOTION: + if (void* buffer = InitSharedMemoryBuffer(consumer_type, + sizeof(DeviceMotionHardwareBuffer))) + return SetMotionBuffer( + static_cast<DeviceMotionHardwareBuffer*>(buffer), false); + break; + case CONSUMER_TYPE_ORIENTATION: + NOTIMPLEMENTED(); + break; + default: + NOTREACHED(); + } + return false; } } // namespace content diff --git a/content/browser/device_orientation/device_inertial_sensor_service.cc b/content/browser/device_orientation/device_inertial_sensor_service.cc new file mode 100644 index 0000000..ee90be1 --- /dev/null +++ b/content/browser/device_orientation/device_inertial_sensor_service.cc @@ -0,0 +1,94 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "content/browser/device_orientation/device_inertial_sensor_service.h" + +#include "base/bind.h" +#include "base/logging.h" +#include "base/memory/singleton.h" +#include "content/browser/device_orientation/data_fetcher_shared_memory.h" +#include "content/public/browser/render_process_host.h" + +namespace content { + +DeviceInertialSensorService::DeviceInertialSensorService() + : num_motion_readers_(0), + num_orientation_readers_(0), + is_shutdown_(false) { +} + +DeviceInertialSensorService::~DeviceInertialSensorService() { +} + +DeviceInertialSensorService* DeviceInertialSensorService::GetInstance() { + return Singleton<DeviceInertialSensorService, + LeakySingletonTraits<DeviceInertialSensorService> >::get(); +} + +void DeviceInertialSensorService::AddConsumer(ConsumerType consumer_type) { + if (!ChangeNumberConsumers(consumer_type, 1)) + return; + + DCHECK(GetNumberConsumers(consumer_type)); + + if (!data_fetcher_) + data_fetcher_.reset(new DataFetcherSharedMemory); + data_fetcher_->StartFetchingDeviceData(consumer_type); +} + +void DeviceInertialSensorService::RemoveConsumer(ConsumerType consumer_type) { + if (!ChangeNumberConsumers(consumer_type, -1)) + return; + + if (GetNumberConsumers(consumer_type) == 0) + data_fetcher_->StopFetchingDeviceData(consumer_type); +} + +bool DeviceInertialSensorService::ChangeNumberConsumers( + ConsumerType consumer_type, int delta) { + DCHECK(thread_checker_.CalledOnValidThread()); + if (is_shutdown_) + return false; + + switch (consumer_type) { + case CONSUMER_TYPE_MOTION: + num_motion_readers_ += delta; + DCHECK(num_motion_readers_ >= 0); + return true; + case CONSUMER_TYPE_ORIENTATION: + num_orientation_readers_ += delta; + DCHECK(num_orientation_readers_ >= 0); + return true; + default: + NOTREACHED(); + } + return false; +} + +int DeviceInertialSensorService::GetNumberConsumers( + ConsumerType consumer_type) const { + switch (consumer_type) { + case CONSUMER_TYPE_MOTION: + return num_motion_readers_; + case CONSUMER_TYPE_ORIENTATION: + return num_orientation_readers_; + default: + NOTREACHED(); + } + return 0; +} + +base::SharedMemoryHandle +DeviceInertialSensorService::GetSharedMemoryHandleForProcess( + ConsumerType consumer_type, base::ProcessHandle handle) { + DCHECK(thread_checker_.CalledOnValidThread()); + return data_fetcher_->GetSharedMemoryHandleForProcess(consumer_type, handle); +} + +void DeviceInertialSensorService::Shutdown() { + data_fetcher_.reset(); + is_shutdown_ = true; +} + +} // namespace content diff --git a/content/browser/device_orientation/device_motion_service.h b/content/browser/device_orientation/device_inertial_sensor_service.h index 0720bb9..028d155 100644 --- a/content/browser/device_orientation/device_motion_service.h +++ b/content/browser/device_orientation/device_inertial_sensor_service.h @@ -2,8 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#ifndef CONTENT_BROWSER_DEVICE_ORIENTATION_DEVICE_MOTION_SERVICE_H_ -#define CONTENT_BROWSER_DEVICE_ORIENTATION_DEVICE_MOTION_SERVICE_H_ +#ifndef CONTENT_BROWSER_DEVICE_ORIENTATION_DEVICE_INERTIAL_SENSOR_SERVICE_H_ +#define CONTENT_BROWSER_DEVICE_ORIENTATION_DEVICE_INERTIAL_SENSOR_SERVICE_H_ #include "base/basictypes.h" #include "base/callback_forward.h" @@ -11,55 +11,58 @@ #include "base/memory/shared_memory.h" #include "base/memory/singleton.h" #include "base/threading/thread_checker.h" +#include "content/browser/device_orientation/inertial_sensor_consts.h" #include "content/common/content_export.h" namespace content { class DataFetcherSharedMemory; -class DeviceMotionProvider; class RenderProcessHost; // Owns the DeviceMotionProvider (the background polling thread) and keeps // track of the number of consumers currently using the data (and pausing // the provider when not in use). -class CONTENT_EXPORT DeviceMotionService { +class CONTENT_EXPORT DeviceInertialSensorService { public: - // Returns the DeviceMotionService singleton. - static DeviceMotionService* GetInstance(); + // Returns the DeviceInertialSensorService singleton. + static DeviceInertialSensorService* GetInstance(); // Increments the number of users of the provider. The Provider is running // when there's > 0 users, and is paused when the count drops to 0. - // // Must be called on the I/O thread. - void AddConsumer(); + void AddConsumer(ConsumerType consumer_type); // Removes a consumer. Should be matched with an AddConsumer call. - // // Must be called on the I/O thread. - void RemoveConsumer(); + void RemoveConsumer(ConsumerType cosumer_type); // Returns the shared memory handle of the device motion data duplicated // into the given process. base::SharedMemoryHandle GetSharedMemoryHandleForProcess( - base::ProcessHandle handle); + ConsumerType consumer_type, base::ProcessHandle handle); - // Stop/join with the background thread in DeviceMotionProvider |provider_|. + // Stop/join with the background polling thread in |provider_|. void Shutdown(); private: - friend struct DefaultSingletonTraits<DeviceMotionService>; + friend struct DefaultSingletonTraits<DeviceInertialSensorService>; - DeviceMotionService(); - virtual ~DeviceMotionService(); + DeviceInertialSensorService(); + virtual ~DeviceInertialSensorService(); - int num_readers_; + bool ChangeNumberConsumers(ConsumerType consumer_type, + int delta); + int GetNumberConsumers(ConsumerType consumer_type) const; + + int num_motion_readers_; + int num_orientation_readers_; bool is_shutdown_; - scoped_ptr<DeviceMotionProvider> provider_; + scoped_ptr<DataFetcherSharedMemory> data_fetcher_; base::ThreadChecker thread_checker_; - DISALLOW_COPY_AND_ASSIGN(DeviceMotionService); + DISALLOW_COPY_AND_ASSIGN(DeviceInertialSensorService); }; } // namespace content -#endif // CONTENT_BROWSER_DEVICE_ORIENTATION_DEVICE_MOTION_SERVICE_H_ +#endif // CONTENT_BROWSER_DEVICE_ORIENTATION_DEVICE_INERTIAL_SENSOR_SERVICE_H_ diff --git a/content/browser/device_orientation/device_motion_message_filter.cc b/content/browser/device_orientation/device_motion_message_filter.cc index 3c619c6..bc33cac 100644 --- a/content/browser/device_orientation/device_motion_message_filter.cc +++ b/content/browser/device_orientation/device_motion_message_filter.cc @@ -4,7 +4,7 @@ #include "content/browser/device_orientation/device_motion_message_filter.h" -#include "content/browser/device_orientation/device_motion_service.h" +#include "content/browser/device_orientation/device_inertial_sensor_service.h" #include "content/common/device_orientation/device_motion_messages.h" namespace content { @@ -16,7 +16,8 @@ DeviceMotionMessageFilter::DeviceMotionMessageFilter() DeviceMotionMessageFilter::~DeviceMotionMessageFilter() { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); if (is_started_) - DeviceMotionService::GetInstance()->RemoveConsumer(); + DeviceInertialSensorService::GetInstance()->RemoveConsumer( + CONSUMER_TYPE_MOTION); } bool DeviceMotionMessageFilter::OnMessageReceived( @@ -40,7 +41,8 @@ void DeviceMotionMessageFilter::OnDeviceMotionStartPolling() { if (is_started_) return; is_started_ = true; - DeviceMotionService::GetInstance()->AddConsumer(); + DeviceInertialSensorService::GetInstance()->AddConsumer( + CONSUMER_TYPE_MOTION); DidStartDeviceMotionPolling(); } @@ -49,13 +51,15 @@ void DeviceMotionMessageFilter::OnDeviceMotionStopPolling() { if (!is_started_) return; is_started_ = false; - DeviceMotionService::GetInstance()->RemoveConsumer(); + DeviceInertialSensorService::GetInstance()->RemoveConsumer( + CONSUMER_TYPE_MOTION); } void DeviceMotionMessageFilter::DidStartDeviceMotionPolling() { Send(new DeviceMotionMsg_DidStartPolling( - DeviceMotionService::GetInstance()->GetSharedMemoryHandleForProcess( - PeerHandle()))); + DeviceInertialSensorService::GetInstance()-> + GetSharedMemoryHandleForProcess( + CONSUMER_TYPE_MOTION, PeerHandle()))); } } // namespace content diff --git a/content/browser/device_orientation/device_motion_provider.cc b/content/browser/device_orientation/device_motion_provider.cc deleted file mode 100644 index e88d5af..0000000 --- a/content/browser/device_orientation/device_motion_provider.cc +++ /dev/null @@ -1,172 +0,0 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "content/browser/device_orientation/device_motion_provider.h" - -#include "base/bind.h" -#include "base/logging.h" -#include "base/threading/thread.h" -#include "base/timer/timer.h" -#include "content/browser/device_orientation/data_fetcher_shared_memory.h" -#include "content/common/device_motion_hardware_buffer.h" - -namespace content { - -namespace { -const int kPeriodInMilliseconds = 100; -} - -class DeviceMotionProvider::PollingThread : public base::Thread { - public: - explicit PollingThread(const char* name); - virtual ~PollingThread(); - - void StartPolling(DataFetcherSharedMemory* fetcher, - DeviceMotionHardwareBuffer* buffer); - void StopPolling(); - - private: - void DoPoll(); - - scoped_ptr<base::RepeatingTimer<PollingThread> > timer_; - DataFetcherSharedMemory* fetcher_; - - DISALLOW_COPY_AND_ASSIGN(PollingThread); -}; - -// ---- PollingThread methods - -DeviceMotionProvider::PollingThread::PollingThread(const char* name) - : base::Thread(name) { -} - -DeviceMotionProvider::PollingThread::~PollingThread() { -} - -void DeviceMotionProvider::PollingThread::StartPolling( - DataFetcherSharedMemory* fetcher, DeviceMotionHardwareBuffer* buffer) { - DCHECK(base::MessageLoop::current() == message_loop()); - DCHECK(!timer_); - - fetcher_ = fetcher; - fetcher_->StartFetchingDeviceMotionData(buffer); - timer_.reset(new base::RepeatingTimer<PollingThread>()); - timer_->Start(FROM_HERE, - base::TimeDelta::FromMilliseconds(kPeriodInMilliseconds), - this, &PollingThread::DoPoll); -} - -void DeviceMotionProvider::PollingThread::StopPolling() { - DCHECK(base::MessageLoop::current() == message_loop()); - DCHECK(fetcher_); - // this will also stop the timer before killing it. - timer_.reset(); - fetcher_->StopFetchingDeviceMotionData(); -} - -void DeviceMotionProvider::PollingThread::DoPoll() { - DCHECK(base::MessageLoop::current() == message_loop()); - fetcher_->FetchDeviceMotionDataIntoBuffer(); -} - -// ---- end PollingThread methods - -DeviceMotionProvider::DeviceMotionProvider() - : is_started_(false) { - Initialize(); -} - -DeviceMotionProvider::DeviceMotionProvider( - scoped_ptr<DataFetcherSharedMemory> fetcher) - : is_started_(false) { - data_fetcher_ = fetcher.Pass(); - Initialize(); -} - -DeviceMotionProvider::~DeviceMotionProvider() { - StopFetchingDeviceMotionData(); - // make sure polling thread stops before data_fetcher_ gets deleted. - if (polling_thread_) - polling_thread_->Stop(); - data_fetcher_.reset(); -} - -void DeviceMotionProvider::Initialize() { - size_t data_size = sizeof(DeviceMotionHardwareBuffer); - bool res = device_motion_shared_memory_.CreateAndMapAnonymous(data_size); - // TODO(timvolodine): consider not crashing the browser if the check fails. - CHECK(res); - DeviceMotionHardwareBuffer* hwbuf = SharedMemoryAsHardwareBuffer(); - memset(hwbuf, 0, sizeof(DeviceMotionHardwareBuffer)); -} - -base::SharedMemoryHandle DeviceMotionProvider::GetSharedMemoryHandleForProcess( - base::ProcessHandle process) { - base::SharedMemoryHandle renderer_handle; - device_motion_shared_memory_.ShareToProcess(process, &renderer_handle); - return renderer_handle; -} - -void DeviceMotionProvider::StartFetchingDeviceMotionData() { - if (is_started_) - return; - - if (!data_fetcher_) - data_fetcher_.reset(new DataFetcherSharedMemory); - - if (data_fetcher_->NeedsPolling()) { - if (!polling_thread_) - CreateAndStartPollingThread(); - - polling_thread_->message_loop()->PostTask( - FROM_HERE, - base::Bind(&PollingThread::StartPolling, - base::Unretained(polling_thread_.get()), - data_fetcher_.get(), - SharedMemoryAsHardwareBuffer())); - } else { - data_fetcher_->StartFetchingDeviceMotionData( - SharedMemoryAsHardwareBuffer()); - } - - is_started_ = true; -} - -void DeviceMotionProvider::CreateAndStartPollingThread() { - polling_thread_.reset( - new PollingThread("Device Motion poller")); - - if (!polling_thread_->Start()) { - LOG(ERROR) << "Failed to start Device Motion data polling thread"; - return; - } -} - -void DeviceMotionProvider::StopFetchingDeviceMotionData() { - if (!is_started_) - return; - - DCHECK(data_fetcher_); - - if (data_fetcher_->NeedsPolling()) { - DCHECK(polling_thread_); - polling_thread_->message_loop()->PostTask( - FROM_HERE, - base::Bind(&PollingThread::StopPolling, - base::Unretained(polling_thread_.get()))); - } else { - data_fetcher_->StopFetchingDeviceMotionData(); - } - - is_started_ = false; -} - -DeviceMotionHardwareBuffer* -DeviceMotionProvider::SharedMemoryAsHardwareBuffer() { - void* mem = device_motion_shared_memory_.memory(); - CHECK(mem); - return static_cast<DeviceMotionHardwareBuffer*>(mem); -} - -} // namespace content diff --git a/content/browser/device_orientation/device_motion_provider.h b/content/browser/device_orientation/device_motion_provider.h deleted file mode 100644 index 8fd36de..0000000 --- a/content/browser/device_orientation/device_motion_provider.h +++ /dev/null @@ -1,56 +0,0 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CONTENT_BROWSER_DEVICE_ORIENTATION_DEVICE_MOTION_PROVIDER_H_ -#define CONTENT_BROWSER_DEVICE_ORIENTATION_DEVICE_MOTION_PROVIDER_H_ - -#include "base/memory/scoped_ptr.h" -#include "base/memory/shared_memory.h" -#include "content/common/content_export.h" -#include "content/common/device_motion_hardware_buffer.h" - -namespace content { -class DataFetcherSharedMemory; - -// This class owns the shared memory buffer for Device Motion and makes -// sure the data is fetched into that buffer. -// When DataFetcherSharedMemory::NeedsPolling() is true, it starts a -// background polling thread to make sure the data is fetched at regular -// intervals. -class CONTENT_EXPORT DeviceMotionProvider { - public: - DeviceMotionProvider(); - - // Creates provider with a custom fetcher. Used for testing. - explicit DeviceMotionProvider(scoped_ptr<DataFetcherSharedMemory> fetcher); - - virtual ~DeviceMotionProvider(); - - // Returns the shared memory handle of the device motion data duplicated - // into the given process. - base::SharedMemoryHandle GetSharedMemoryHandleForProcess( - base::ProcessHandle renderer_process); - - void StartFetchingDeviceMotionData(); - void StopFetchingDeviceMotionData(); - - private: - class PollingThread; - - void Initialize(); - void CreateAndStartPollingThread(); - - DeviceMotionHardwareBuffer* SharedMemoryAsHardwareBuffer(); - - base::SharedMemory device_motion_shared_memory_; - scoped_ptr<DataFetcherSharedMemory> data_fetcher_; - scoped_ptr<PollingThread> polling_thread_; - bool is_started_; - - DISALLOW_COPY_AND_ASSIGN(DeviceMotionProvider); -}; - -} // namespace content - -#endif // CONTENT_BROWSER_DEVICE_ORIENTATION_DEVICE_MOTION_PROVIDER_H_ diff --git a/content/browser/device_orientation/device_motion_provider_unittest.cc b/content/browser/device_orientation/device_motion_provider_unittest.cc deleted file mode 100644 index cd0f3d8..0000000 --- a/content/browser/device_orientation/device_motion_provider_unittest.cc +++ /dev/null @@ -1,98 +0,0 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "content/browser/device_orientation/device_motion_provider.h" - -#include "base/logging.h" -#include "base/memory/scoped_ptr.h" -#include "base/synchronization/waitable_event.h" -#include "content/browser/device_orientation/data_fetcher_shared_memory.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace content { - -namespace { - -const int kPeriodInMilliseconds = 100; - -class FakeDataFetcherSharedMemory : public DataFetcherSharedMemory { - public: - FakeDataFetcherSharedMemory() - : start_fetching_data_(false, false), - stop_fetching_data_(false, false), - fetched_data_(false, false) { - } - virtual ~FakeDataFetcherSharedMemory() { } - - virtual bool NeedsPolling() OVERRIDE { - return true; - } - - virtual bool FetchDeviceMotionDataIntoBuffer() OVERRIDE { - buffer_->seqlock.WriteBegin(); - buffer_->data.interval = kPeriodInMilliseconds; - buffer_->seqlock.WriteEnd(); - fetched_data_.Signal(); - return true; - } - - virtual bool StartFetchingDeviceMotionData( - DeviceMotionHardwareBuffer* buffer) OVERRIDE { - buffer_ = buffer; - start_fetching_data_.Signal(); - return true; - } - - virtual void StopFetchingDeviceMotionData() OVERRIDE { - stop_fetching_data_.Signal(); - } - - void WaitForStart() { - start_fetching_data_.Wait(); - } - - void WaitForStop() { - stop_fetching_data_.Wait(); - } - - void WaitForDataFetch() { - fetched_data_.Wait(); - } - - DeviceMotionHardwareBuffer* GetBuffer() { - return buffer_; - } - - private: - base::WaitableEvent start_fetching_data_; - base::WaitableEvent stop_fetching_data_; - base::WaitableEvent fetched_data_; - DeviceMotionHardwareBuffer* buffer_; - - DISALLOW_COPY_AND_ASSIGN(FakeDataFetcherSharedMemory); -}; - - -TEST(DeviceMotionProviderTest, DoesPolling) { - FakeDataFetcherSharedMemory* mock_data_fetcher = - new FakeDataFetcherSharedMemory(); - EXPECT_TRUE(mock_data_fetcher->NeedsPolling()); - - scoped_ptr<DeviceMotionProvider> provider(new DeviceMotionProvider( - scoped_ptr<DataFetcherSharedMemory>(mock_data_fetcher))); - - provider->StartFetchingDeviceMotionData(); - mock_data_fetcher->WaitForStart(); - mock_data_fetcher->WaitForDataFetch(); - - EXPECT_EQ(kPeriodInMilliseconds, - mock_data_fetcher->GetBuffer()->data.interval); - - provider->StopFetchingDeviceMotionData(); - mock_data_fetcher->WaitForStop(); -} - -} // namespace - -} // namespace content diff --git a/content/browser/device_orientation/device_motion_service.cc b/content/browser/device_orientation/device_motion_service.cc deleted file mode 100644 index 48b784b..0000000 --- a/content/browser/device_orientation/device_motion_service.cc +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "content/browser/device_orientation/device_motion_service.h" - -#include "base/bind.h" -#include "base/logging.h" -#include "base/memory/singleton.h" -#include "content/browser/device_orientation/device_motion_provider.h" -#include "content/public/browser/render_process_host.h" - -namespace content { - -DeviceMotionService::DeviceMotionService() - : num_readers_(0), - is_shutdown_(false) { -} - -DeviceMotionService::~DeviceMotionService() { -} - -DeviceMotionService* DeviceMotionService::GetInstance() { - return Singleton<DeviceMotionService, - LeakySingletonTraits<DeviceMotionService> >::get(); -} - -void DeviceMotionService::AddConsumer() { - DCHECK(thread_checker_.CalledOnValidThread()); - if (is_shutdown_) - return; - - num_readers_++; - DCHECK(num_readers_ > 0); - if (!provider_.get()) - provider_.reset(new DeviceMotionProvider); - provider_->StartFetchingDeviceMotionData(); -} - -void DeviceMotionService::RemoveConsumer() { - DCHECK(thread_checker_.CalledOnValidThread()); - if (is_shutdown_) - return; - - --num_readers_; - DCHECK(num_readers_ >= 0); - - if (num_readers_ == 0) { - LOG(INFO) << "ACTIVE service stop fetching"; - provider_->StopFetchingDeviceMotionData(); - } -} - -void DeviceMotionService::Shutdown() { - provider_.reset(); - is_shutdown_ = true; -} - -base::SharedMemoryHandle DeviceMotionService::GetSharedMemoryHandleForProcess( - base::ProcessHandle handle) { - DCHECK(thread_checker_.CalledOnValidThread()); - return provider_->GetSharedMemoryHandleForProcess(handle); -} - -} // namespace content diff --git a/content/browser/device_orientation/device_orientation_message_filter.cc b/content/browser/device_orientation/device_orientation_message_filter.cc index cbb1e67..f4acd94 100644 --- a/content/browser/device_orientation/device_orientation_message_filter.cc +++ b/content/browser/device_orientation/device_orientation_message_filter.cc @@ -4,7 +4,7 @@ #include "content/browser/device_orientation/device_orientation_message_filter.h" -#include "content/browser/device_orientation/device_motion_service.h" +#include "content/browser/device_orientation/device_inertial_sensor_service.h" #include "content/common/device_orientation/device_orientation_messages.h" namespace content { @@ -15,10 +15,9 @@ DeviceOrientationMessageFilter::DeviceOrientationMessageFilter() DeviceOrientationMessageFilter::~DeviceOrientationMessageFilter() { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); - if (is_started_) { - // TODO(timvolodine): insert a proper call to DeviceSensorService here, - // similar to DeviceMotionService::GetInstance()->RemoveConsumer(); - } + if (is_started_) + DeviceInertialSensorService::GetInstance()->RemoveConsumer( + CONSUMER_TYPE_ORIENTATION); } bool DeviceOrientationMessageFilter::OnMessageReceived( @@ -43,8 +42,8 @@ void DeviceOrientationMessageFilter::OnDeviceOrientationStartPolling() { if (is_started_) return; is_started_ = true; - // TODO(timvolodine): insert a proper call to DeviceSensorService here, - // similar to DeviceMotionService::GetInstance()->AddConsumer(); + DeviceInertialSensorService::GetInstance()->AddConsumer( + CONSUMER_TYPE_ORIENTATION); DidStartDeviceOrientationPolling(); } @@ -54,17 +53,16 @@ void DeviceOrientationMessageFilter::OnDeviceOrientationStopPolling() { if (!is_started_) return; is_started_ = false; - // TODO(timvolodine): insert a proper call to DeviceSensorService here, - // similar to DeviceMotionService::GetInstance()->RemoveConsumer(); + DeviceInertialSensorService::GetInstance()->RemoveConsumer( + CONSUMER_TYPE_ORIENTATION); } void DeviceOrientationMessageFilter::DidStartDeviceOrientationPolling() { - NOTIMPLEMENTED(); - // TODO(timvolodine): insert a proper call to the generalized Service here, - // similar to - // Send(new DeviceOrientationMsg_DidStartPolling( - // DeviceMotionService::GetInstance()->GetSharedMemoryHandleForProcess( - // PeerHandle()))); + Send(new DeviceOrientationMsg_DidStartPolling( + DeviceInertialSensorService::GetInstance()-> + GetSharedMemoryHandleForProcess( + CONSUMER_TYPE_ORIENTATION, + PeerHandle()))); } } // namespace content diff --git a/content/browser/device_orientation/inertial_sensor_consts.h b/content/browser/device_orientation/inertial_sensor_consts.h new file mode 100644 index 0000000..312b18b --- /dev/null +++ b/content/browser/device_orientation/inertial_sensor_consts.h @@ -0,0 +1,19 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CONTENT_BROWSER_DEVICE_ORIENTATION_INERTIAL_SENSOR_CONSTS_H_ +#define CONTENT_BROWSER_DEVICE_ORIENTATION_INERTIAL_SENSOR_CONSTS_H_ + +namespace content { + +// Constants related to the Device Motion/Device Orientation APIs. + +enum ConsumerType { + CONSUMER_TYPE_MOTION = 1 << 0, + CONSUMER_TYPE_ORIENTATION = 1 << 1, +}; + +} // namespace content + +#endif // CONTENT_BROWSER_DEVICE_ORIENTATION_INERTIAL_SENSOR_CONSTS_H_ diff --git a/content/content_browser.gypi b/content/content_browser.gypi index b1fbb95..d1afb07 100644 --- a/content/content_browser.gypi +++ b/content/content_browser.gypi @@ -400,17 +400,18 @@ 'browser/device_orientation/data_fetcher_orientation_android.cc', 'browser/device_orientation/data_fetcher_orientation_android.h', 'browser/device_orientation/data_fetcher_shared_memory_android.cc', + 'browser/device_orientation/data_fetcher_shared_memory_base.cc', + 'browser/device_orientation/data_fetcher_shared_memory_base.h', 'browser/device_orientation/data_fetcher_shared_memory_default.cc', 'browser/device_orientation/data_fetcher_shared_memory.h', 'browser/device_orientation/device_data.h', + 'browser/device_orientation/device_inertial_sensor_service.cc', + 'browser/device_orientation/device_inertial_sensor_service.h', 'browser/device_orientation/device_motion_message_filter.cc', 'browser/device_orientation/device_motion_message_filter.h', - 'browser/device_orientation/device_motion_provider.cc', - 'browser/device_orientation/device_motion_provider.h', - 'browser/device_orientation/device_motion_service.cc', - 'browser/device_orientation/device_motion_service.h', 'browser/device_orientation/device_orientation_message_filter.cc', 'browser/device_orientation/device_orientation_message_filter.h', + 'browser/device_orientation/inertial_sensor_consts.h', 'browser/device_orientation/message_filter.cc', 'browser/device_orientation/message_filter.h', 'browser/device_orientation/observer_delegate.cc', diff --git a/content/content_tests.gypi b/content/content_tests.gypi index ae05805..783106d 100644 --- a/content/content_tests.gypi +++ b/content/content_tests.gypi @@ -283,7 +283,7 @@ 'browser/byte_stream_unittest.cc', 'browser/child_process_security_policy_unittest.cc', 'browser/device_orientation/data_fetcher_impl_android_unittest.cc', - 'browser/device_orientation/device_motion_provider_unittest.cc', + 'browser/device_orientation/data_fetcher_shared_memory_base_unittest.cc', 'browser/device_orientation/provider_unittest.cc', 'browser/devtools/devtools_http_handler_unittest.cc', 'browser/devtools/devtools_manager_unittest.cc', |