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 "config.h" 6 #include "modules/device_orientation/DeviceMotionController.h" 7 8 #include "core/dom/Document.h" 9 #include "modules/EventModules.h" 10 #include "modules/device_orientation/DeviceMotionData.h" 11 #include "modules/device_orientation/DeviceMotionDispatcher.h" 12 #include "modules/device_orientation/DeviceMotionEvent.h" 13 14 namespace WebCore { 15 16 DeviceMotionController::DeviceMotionController(Document& document) 17 : DeviceSingleWindowEventController(document) 18 { 19 } 20 21 DeviceMotionController::~DeviceMotionController() 22 { 23 stopUpdating(); 24 } 25 26 const char* DeviceMotionController::supplementName() 27 { 28 return "DeviceMotionController"; 29 } 30 31 DeviceMotionController& DeviceMotionController::from(Document& document) 32 { 33 DeviceMotionController* controller = static_cast<DeviceMotionController*>(DocumentSupplement::from(document, supplementName())); 34 if (!controller) { 35 controller = new DeviceMotionController(document); 36 DocumentSupplement::provideTo(document, supplementName(), adoptPtrWillBeNoop(controller)); 37 } 38 return *controller; 39 } 40 41 bool DeviceMotionController::hasLastData() 42 { 43 return DeviceMotionDispatcher::instance().latestDeviceMotionData(); 44 } 45 46 void DeviceMotionController::registerWithDispatcher() 47 { 48 DeviceMotionDispatcher::instance().addController(this); 49 } 50 51 void DeviceMotionController::unregisterWithDispatcher() 52 { 53 DeviceMotionDispatcher::instance().removeController(this); 54 } 55 56 PassRefPtrWillBeRawPtr<Event> DeviceMotionController::lastEvent() const 57 { 58 return DeviceMotionEvent::create(EventTypeNames::devicemotion, DeviceMotionDispatcher::instance().latestDeviceMotionData()); 59 } 60 61 bool DeviceMotionController::isNullEvent(Event* event) const 62 { 63 DeviceMotionEvent* motionEvent = toDeviceMotionEvent(event); 64 return !motionEvent->deviceMotionData()->canProvideEventData(); 65 } 66 67 const AtomicString& DeviceMotionController::eventTypeName() const 68 { 69 return EventTypeNames::devicemotion; 70 } 71 72 } // namespace WebCore 73