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 #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_FRAME_DOM_TIMER_H_
28 #define THIRD_PARTY_BLINK_RENDERER_CORE_FRAME_DOM_TIMER_H_
29 
30 #include "base/memory/scoped_refptr.h"
31 #include "third_party/blink/renderer/core/core_export.h"
32 #include "third_party/blink/renderer/core/execution_context/execution_context_lifecycle_observer.h"
33 #include "third_party/blink/renderer/core/probe/async_task_id.h"
34 #include "third_party/blink/renderer/platform/bindings/name_client.h"
35 #include "third_party/blink/renderer/platform/heap/handle.h"
36 #include "third_party/blink/renderer/platform/timer.h"
37 
38 namespace blink {
39 
40 class ExecutionContext;
41 class ScheduledAction;
42 
43 class CORE_EXPORT DOMTimer final : public GarbageCollected<DOMTimer>,
44                                    public ExecutionContextLifecycleObserver,
45                                    public TimerBase,
46                                    public NameClient {
47   USING_GARBAGE_COLLECTED_MIXIN(DOMTimer);
48   USING_PRE_FINALIZER(DOMTimer, Dispose);
49 
50  public:
51   // Creates a new timer owned by the ExecutionContext, starts it and returns
52   // its ID.
53   static int Install(ExecutionContext*,
54                      ScheduledAction*,
55                      base::TimeDelta timeout,
56                      bool single_shot);
57   static void RemoveByID(ExecutionContext*, int timeout_id);
58 
59   DOMTimer(ExecutionContext*,
60            ScheduledAction*,
61            base::TimeDelta interval,
62            bool single_shot,
63            int timeout_id);
64   ~DOMTimer() override;
65 
66   // ExecutionContextLifecycleObserver
67   void ContextDestroyed() override;
68 
69   // Pre finalizer is needed to promptly stop this Timer object.
70   // Otherwise timer events might fire at an object that's slated for
71   // destruction (when lazily swept), but some of its members (m_action) may
72   // already have been finalized & must not be accessed.
73   void Dispose();
74 
75   void Trace(Visitor*) override;
NameInHeapSnapshot()76   const char* NameInHeapSnapshot() const override { return "DOMTimer"; }
77 
78   void Stop() override;
79 
80  private:
81   void Fired() override;
82 
83   int timeout_id_;
84   int nesting_level_;
85   probe::AsyncTaskId async_task_id_;
86   Member<ScheduledAction> action_;
87 };
88 
89 }  // namespace blink
90 
91 #endif  // THIRD_PARTY_BLINK_RENDERER_CORE_FRAME_DOM_TIMER_H_
92