OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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 "device_motion_event_pump.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop.h" |
| 11 #include "base/shared_memory.h" |
| 12 #include "content/common/device_motion_messages.h" |
| 13 #include "content/public/renderer/render_thread.h" |
| 14 #include "third_party/WebKit/public/platform/WebDeviceMotionListener.h" |
| 15 |
| 16 namespace content { |
| 17 |
| 18 const double DeviceMotionEventPump::kPumpDelayMillis = 40; |
| 19 |
| 20 double DeviceMotionEventPump::GetDelayMillis() { |
| 21 return kPumpDelayMillis; |
| 22 } |
| 23 |
| 24 DeviceMotionEventPump::DeviceMotionEventPump() |
| 25 : listener_(0), state_(STOPPED) { |
| 26 } |
| 27 |
| 28 DeviceMotionEventPump::~DeviceMotionEventPump() { |
| 29 } |
| 30 |
| 31 bool DeviceMotionEventPump::SetListener( |
| 32 WebKit::WebDeviceMotionListener* listener) { |
| 33 listener_ = listener; |
| 34 if (listener_) |
| 35 return StartFetchingDeviceMotion(); |
| 36 return StopFetchingDeviceMotion(); |
| 37 } |
| 38 |
| 39 void DeviceMotionEventPump::SetDeviceMotionReader( |
| 40 scoped_ptr<DeviceMotionSharedMemoryReader> reader) { |
| 41 reader_.reset(reader.release()); |
| 42 } |
| 43 |
| 44 bool DeviceMotionEventPump::StartFetchingDeviceMotion() { |
| 45 DVLOG(2) << "start fetching device motion"; |
| 46 |
| 47 if (state_ != STOPPED) |
| 48 return false; |
| 49 |
| 50 DCHECK(!timer_.IsRunning()); |
| 51 |
| 52 if (RenderThread::Get()->Send(new DeviceMotionHostMsg_StartPolling())) { |
| 53 state_ = PENDING_START; |
| 54 return true; |
| 55 } |
| 56 return false; |
| 57 } |
| 58 |
| 59 bool DeviceMotionEventPump::StopFetchingDeviceMotion() { |
| 60 DVLOG(2) << "stop fetching device motion"; |
| 61 |
| 62 if (state_ == STOPPED) |
| 63 return true; |
| 64 |
| 65 DCHECK((state_ == PENDING_START && !timer_.IsRunning()) || |
| 66 (state_ == RUNNING && timer_.IsRunning())); |
| 67 |
| 68 if (timer_.IsRunning()) |
| 69 timer_.Stop(); |
| 70 RenderThread::Get()->Send(new DeviceMotionHostMsg_StopPolling()); |
| 71 state_ = STOPPED; |
| 72 return true; |
| 73 } |
| 74 |
| 75 void DeviceMotionEventPump::FireEvent() { |
| 76 DCHECK(listener_); |
| 77 WebKit::WebDeviceMotionData data; |
| 78 if (reader_->GetLatestData(&data) && data.allAvailableSensorsAreActive) |
| 79 listener_->didChangeDeviceMotion(data); |
| 80 } |
| 81 |
| 82 void DeviceMotionEventPump::Attach(RenderThread* thread) { |
| 83 if (!thread) |
| 84 return; |
| 85 thread->AddObserver(this); |
| 86 } |
| 87 |
| 88 void DeviceMotionEventPump::OnDidStartDeviceMotion( |
| 89 base::SharedMemoryHandle renderer_handle) { |
| 90 DVLOG(2) << "did start fetching device motion"; |
| 91 |
| 92 if (state_ != PENDING_START) |
| 93 return; |
| 94 |
| 95 DCHECK(!timer_.IsRunning()); |
| 96 if (!reader_) |
| 97 reader_.reset(new DeviceMotionSharedMemoryReader()); |
| 98 |
| 99 if (reader_->Initialize(renderer_handle)) { |
| 100 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kPumpDelayMillis), |
| 101 this, &DeviceMotionEventPump::FireEvent); |
| 102 state_ = RUNNING; |
| 103 } |
| 104 } |
| 105 |
| 106 bool DeviceMotionEventPump::OnControlMessageReceived( |
| 107 const IPC::Message& message) { |
| 108 bool handled = true; |
| 109 IPC_BEGIN_MESSAGE_MAP(DeviceMotionEventPump, message) |
| 110 IPC_MESSAGE_HANDLER(DeviceMotionMsg_DidStartPolling, |
| 111 OnDidStartDeviceMotion) |
| 112 IPC_MESSAGE_UNHANDLED(handled = false) |
| 113 IPC_END_MESSAGE_MAP() |
| 114 return handled; |
| 115 } |
| 116 |
| 117 } // namespace content |
OLD | NEW |