Home | History | Annotate | Download | only in device_orientation
      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/DeviceOrientationController.h"
      7 
      8 #include "core/dom/Document.h"
      9 #include "modules/EventModules.h"
     10 #include "modules/device_orientation/DeviceOrientationData.h"
     11 #include "modules/device_orientation/DeviceOrientationDispatcher.h"
     12 #include "modules/device_orientation/DeviceOrientationEvent.h"
     13 
     14 namespace blink {
     15 
     16 DeviceOrientationController::DeviceOrientationController(Document& document)
     17     : DeviceSingleWindowEventController(document)
     18 {
     19 }
     20 
     21 DeviceOrientationController::~DeviceOrientationController()
     22 {
     23     stopUpdating();
     24 }
     25 
     26 void DeviceOrientationController::didUpdateData()
     27 {
     28     if (m_overrideOrientationData)
     29         return;
     30     dispatchDeviceEvent(lastEvent());
     31 }
     32 
     33 const char* DeviceOrientationController::supplementName()
     34 {
     35     return "DeviceOrientationController";
     36 }
     37 
     38 DeviceOrientationController& DeviceOrientationController::from(Document& document)
     39 {
     40     DeviceOrientationController* controller = static_cast<DeviceOrientationController*>(DocumentSupplement::from(document, supplementName()));
     41     if (!controller) {
     42         controller = new DeviceOrientationController(document);
     43         DocumentSupplement::provideTo(document, supplementName(), adoptPtrWillBeNoop(controller));
     44     }
     45     return *controller;
     46 }
     47 
     48 DeviceOrientationData* DeviceOrientationController::lastData() const
     49 {
     50     return m_overrideOrientationData ? m_overrideOrientationData.get() : DeviceOrientationDispatcher::instance().latestDeviceOrientationData();
     51 }
     52 
     53 bool DeviceOrientationController::hasLastData()
     54 {
     55     return lastData();
     56 }
     57 
     58 void DeviceOrientationController::registerWithDispatcher()
     59 {
     60     DeviceOrientationDispatcher::instance().addController(this);
     61 }
     62 
     63 void DeviceOrientationController::unregisterWithDispatcher()
     64 {
     65     DeviceOrientationDispatcher::instance().removeController(this);
     66 }
     67 
     68 PassRefPtrWillBeRawPtr<Event> DeviceOrientationController::lastEvent() const
     69 {
     70     return DeviceOrientationEvent::create(eventTypeName(), lastData());
     71 }
     72 
     73 bool DeviceOrientationController::isNullEvent(Event* event) const
     74 {
     75     DeviceOrientationEvent* orientationEvent = toDeviceOrientationEvent(event);
     76     return !orientationEvent->orientation()->canProvideEventData();
     77 }
     78 
     79 const AtomicString& DeviceOrientationController::eventTypeName() const
     80 {
     81     return EventTypeNames::deviceorientation;
     82 }
     83 
     84 void DeviceOrientationController::setOverride(DeviceOrientationData* deviceOrientationData)
     85 {
     86     ASSERT(deviceOrientationData);
     87     m_overrideOrientationData = deviceOrientationData;
     88     dispatchDeviceEvent(lastEvent());
     89 }
     90 
     91 void DeviceOrientationController::clearOverride()
     92 {
     93     if (!m_overrideOrientationData)
     94         return;
     95     m_overrideOrientationData.clear();
     96     if (lastData())
     97         didUpdateData();
     98 }
     99 
    100 void DeviceOrientationController::trace(Visitor* visitor)
    101 {
    102     visitor->trace(m_overrideOrientationData);
    103     DeviceSingleWindowEventController::trace(visitor);
    104     DocumentSupplement::trace(visitor);
    105 }
    106 
    107 } // namespace blink
    108