Home | History | Annotate | Download | only in frame
      1 /*
      2  * Copyright (C) 2008 Apple Inc. All Rights Reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  *
     25  */
     26 
     27 #include "config.h"
     28 #include "core/frame/DOMTimer.h"
     29 
     30 #include "core/dom/ExecutionContext.h"
     31 #include "core/inspector/InspectorInstrumentation.h"
     32 #include "wtf/CurrentTime.h"
     33 
     34 using namespace std;
     35 
     36 namespace WebCore {
     37 
     38 static const int maxIntervalForUserGestureForwarding = 1000; // One second matches Gecko.
     39 static const int maxTimerNestingLevel = 5;
     40 static const double oneMillisecond = 0.001;
     41 // Chromium uses a minimum timer interval of 4ms. We'd like to go
     42 // lower; however, there are poorly coded websites out there which do
     43 // create CPU-spinning loops.  Using 4ms prevents the CPU from
     44 // spinning too busily and provides a balance between CPU spinning and
     45 // the smallest possible interval timer.
     46 static const double minimumInterval = 0.004;
     47 
     48 static int timerNestingLevel = 0;
     49 
     50 static inline bool shouldForwardUserGesture(int interval, int nestingLevel)
     51 {
     52     return UserGestureIndicator::processingUserGesture()
     53         && interval <= maxIntervalForUserGestureForwarding
     54         && nestingLevel == 1; // Gestures should not be forwarded to nested timers.
     55 }
     56 
     57 double DOMTimer::hiddenPageAlignmentInterval()
     58 {
     59     // Timers on hidden pages are aligned so that they fire once per
     60     // second at most.
     61     return 1.0;
     62 }
     63 
     64 double DOMTimer::visiblePageAlignmentInterval()
     65 {
     66     // Alignment does not apply to timers on visible pages.
     67     return 0;
     68 }
     69 
     70 int DOMTimer::install(ExecutionContext* context, PassOwnPtr<ScheduledAction> action, int timeout, bool singleShot)
     71 {
     72     int timeoutID = context->installNewTimeout(action, timeout, singleShot);
     73     InspectorInstrumentation::didInstallTimer(context, timeoutID, timeout, singleShot);
     74     return timeoutID;
     75 }
     76 
     77 void DOMTimer::removeByID(ExecutionContext* context, int timeoutID)
     78 {
     79     context->removeTimeoutByID(timeoutID);
     80     InspectorInstrumentation::didRemoveTimer(context, timeoutID);
     81 }
     82 
     83 DOMTimer::DOMTimer(ExecutionContext* context, PassOwnPtr<ScheduledAction> action, int interval, bool singleShot, int timeoutID)
     84     : SuspendableTimer(context)
     85     , m_timeoutID(timeoutID)
     86     , m_nestingLevel(timerNestingLevel + 1)
     87     , m_action(action)
     88 {
     89     ASSERT(timeoutID > 0);
     90     if (shouldForwardUserGesture(interval, m_nestingLevel))
     91         m_userGestureToken = UserGestureIndicator::currentToken();
     92 
     93     double intervalMilliseconds = max(oneMillisecond, interval * oneMillisecond);
     94     if (intervalMilliseconds < minimumInterval && m_nestingLevel >= maxTimerNestingLevel)
     95         intervalMilliseconds = minimumInterval;
     96     if (singleShot)
     97         startOneShot(intervalMilliseconds);
     98     else
     99         startRepeating(intervalMilliseconds);
    100 }
    101 
    102 DOMTimer::~DOMTimer()
    103 {
    104 }
    105 
    106 int DOMTimer::timeoutID() const
    107 {
    108     return m_timeoutID;
    109 }
    110 
    111 void DOMTimer::fired()
    112 {
    113     ExecutionContext* context = executionContext();
    114     timerNestingLevel = m_nestingLevel;
    115     ASSERT(!context->activeDOMObjectsAreSuspended());
    116     // Only the first execution of a multi-shot timer should get an affirmative user gesture indicator.
    117     UserGestureIndicator gestureIndicator(m_userGestureToken.release());
    118 
    119     InspectorInstrumentationCookie cookie = InspectorInstrumentation::willFireTimer(context, m_timeoutID);
    120 
    121     // Simple case for non-one-shot timers.
    122     if (isActive()) {
    123         if (repeatInterval() && repeatInterval() < minimumInterval) {
    124             m_nestingLevel++;
    125             if (m_nestingLevel >= maxTimerNestingLevel)
    126                 augmentRepeatInterval(minimumInterval - repeatInterval());
    127         }
    128 
    129         // No access to member variables after this point, it can delete the timer.
    130         m_action->execute(context);
    131 
    132         InspectorInstrumentation::didFireTimer(cookie);
    133 
    134         return;
    135     }
    136 
    137     // Delete timer before executing the action for one-shot timers.
    138     OwnPtr<ScheduledAction> action = m_action.release();
    139 
    140     // This timer is being deleted; no access to member variables allowed after this point.
    141     context->removeTimeoutByID(m_timeoutID);
    142 
    143     action->execute(context);
    144 
    145     InspectorInstrumentation::didFireTimer(cookie);
    146 
    147     timerNestingLevel = 0;
    148 }
    149 
    150 void DOMTimer::contextDestroyed()
    151 {
    152     SuspendableTimer::contextDestroyed();
    153 }
    154 
    155 void DOMTimer::stop()
    156 {
    157     SuspendableTimer::stop();
    158     // Need to release JS objects potentially protected by ScheduledAction
    159     // because they can form circular references back to the ExecutionContext
    160     // which will cause a memory leak.
    161     m_action.clear();
    162 }
    163 
    164 double DOMTimer::alignedFireTime(double fireTime) const
    165 {
    166     double alignmentInterval = executionContext()->timerAlignmentInterval();
    167     if (alignmentInterval) {
    168         double currentTime = monotonicallyIncreasingTime();
    169         if (fireTime <= currentTime)
    170             return fireTime;
    171 
    172         // When a repeating timer is scheduled for exactly the
    173         // background page alignment interval, because it's impossible
    174         // for the timer to be rescheduled instantaneously, it misses
    175         // every other fire time. Avoid this by looking at the next
    176         // fire time rounded both down and up.
    177 
    178         double alignedTimeRoundedDown = floor(fireTime / alignmentInterval) * alignmentInterval;
    179         double alignedTimeRoundedUp = ceil(fireTime / alignmentInterval) * alignmentInterval;
    180 
    181         // If the version rounded down is in the past, discard it
    182         // immediately.
    183 
    184         if (alignedTimeRoundedDown <= currentTime)
    185             return alignedTimeRoundedUp;
    186 
    187         // Only use the rounded-down time if it's within a certain
    188         // tolerance of the fire time. This avoids speeding up timers
    189         // on background pages in the common case.
    190 
    191         if (fireTime - alignedTimeRoundedDown < minimumInterval)
    192             return alignedTimeRoundedDown;
    193 
    194         return alignedTimeRoundedUp;
    195     }
    196 
    197     return fireTime;
    198 }
    199 
    200 } // namespace WebCore
    201