Home | History | Annotate | Download | only in device_orientation
      1 /*
      2  * Copyright 2010 Apple Inc. All rights reserved.
      3  * Copyright (C) 2012 Samsung Electronics. All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  *  * Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  *  * Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
     18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     25  */
     26 
     27 #include "config.h"
     28 #include "modules/device_orientation/DeviceSensorEventController.h"
     29 
     30 #include "core/dom/Document.h"
     31 #include "core/page/DOMWindow.h"
     32 
     33 namespace WebCore {
     34 
     35 DeviceSensorEventController::DeviceSensorEventController(Document* document)
     36     : m_document(document)
     37     , m_isActive(false)
     38     , m_needsCheckingNullEvents(true)
     39     , m_timer(this, &DeviceSensorEventController::fireDeviceEvent)
     40 {
     41 }
     42 
     43 DeviceSensorEventController::~DeviceSensorEventController()
     44 {
     45     stopUpdating();
     46 }
     47 
     48 void DeviceSensorEventController::fireDeviceEvent(Timer<DeviceSensorEventController>* timer)
     49 {
     50     ASSERT_UNUSED(timer, timer == &m_timer);
     51     ASSERT(hasLastData());
     52 
     53     m_timer.stop();
     54     dispatchDeviceEvent(getLastEvent());
     55 }
     56 
     57 void DeviceSensorEventController::dispatchDeviceEvent(PassRefPtr<Event> prpEvent)
     58 {
     59     RefPtr<Event> event = prpEvent;
     60     if (m_document && m_document->domWindow()
     61         && !m_document->activeDOMObjectsAreSuspended()
     62         && !m_document->activeDOMObjectsAreStopped())
     63         m_document->domWindow()->dispatchEvent(event);
     64 
     65     if (m_needsCheckingNullEvents) {
     66         if (isNullEvent(event.get()))
     67             stopUpdating();
     68         else
     69             m_needsCheckingNullEvents = false;
     70     }
     71 }
     72 
     73 void DeviceSensorEventController::startUpdating()
     74 {
     75     if (m_isActive)
     76         return;
     77 
     78     if (hasLastData() && !m_timer.isActive()) {
     79         // Make sure to fire the device motion data as soon as possible.
     80         m_timer.startOneShot(0);
     81     }
     82 
     83     registerWithDispatcher();
     84     m_isActive = true;
     85 }
     86 
     87 void DeviceSensorEventController::stopUpdating()
     88 {
     89     if (!m_isActive)
     90         return;
     91 
     92     if (m_timer.isActive())
     93         m_timer.stop();
     94 
     95     unregisterWithDispatcher();
     96     m_isActive = false;
     97 }
     98 
     99 } // namespace WebCore
    100