1/*
2 * Copyright (C) 2008, 2014 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 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 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#pragma once
28
29#include "SuspendableTimer.h"
30#include "UserGestureIndicator.h"
31#include <memory>
32#include <wtf/MonotonicTime.h>
33#include <wtf/RefCounted.h>
34#include <wtf/Seconds.h>
35
36namespace WebCore {
37
38class DOMTimerFireState;
39class Document;
40class HTMLPlugInElement;
41class ScheduledAction;
42
43class DOMTimer final : public RefCounted<DOMTimer>, public SuspendableTimer {
44 WTF_MAKE_NONCOPYABLE(DOMTimer);
45 WTF_MAKE_FAST_ALLOCATED;
46public:
47 virtual ~DOMTimer();
48
49 static Seconds defaultMinimumInterval() { return 4_ms; }
50 static Seconds defaultAlignmentInterval() { return 0_s; }
51 static Seconds defaultAlignmentIntervalInLowPowerMode() { return 30_ms; }
52 static Seconds nonInteractedCrossOriginFrameAlignmentInterval() { return 30_ms; }
53 static Seconds hiddenPageAlignmentInterval() { return 1_s; }
54
55 // Creates a new timer owned by specified ScriptExecutionContext, starts it
56 // and returns its Id.
57 static int install(ScriptExecutionContext&, std::unique_ptr<ScheduledAction>, Seconds timeout, bool singleShot);
58 static void removeById(ScriptExecutionContext&, int timeoutId);
59
60 // Notify that the interval may need updating (e.g. because the minimum interval
61 // setting for the context has changed).
62 void updateTimerIntervalIfNecessary();
63
64 static void scriptDidInteractWithPlugin(HTMLPlugInElement&);
65
66private:
67 DOMTimer(ScriptExecutionContext&, std::unique_ptr<ScheduledAction>, Seconds interval, bool singleShot);
68 friend class Internals;
69
70 WEBCORE_EXPORT Seconds intervalClampedToMinimum() const;
71
72 bool isDOMTimersThrottlingEnabled(Document&) const;
73 void updateThrottlingStateIfNecessary(const DOMTimerFireState&);
74
75 // SuspendableTimer
76 void fired() override;
77 void didStop() override;
78 WEBCORE_EXPORT Optional<MonotonicTime> alignedFireTime(MonotonicTime) const override;
79
80 // ActiveDOMObject API.
81 const char* activeDOMObjectName() const override;
82
83 enum TimerThrottleState {
84 Undetermined,
85 ShouldThrottle,
86 ShouldNotThrottle
87 };
88
89 int m_timeoutId;
90 int m_nestingLevel;
91 std::unique_ptr<ScheduledAction> m_action;
92 Seconds m_originalInterval;
93 TimerThrottleState m_throttleState;
94 Seconds m_currentTimerInterval;
95 RefPtr<UserGestureToken> m_userGestureTokenToForward;
96};
97
98} // namespace WebCore
99