blob: 42dcf5697a90a8214c02b68c786021ce8c746a81 [file] [log] [blame]
// Copyright 2015 The Cobalt Authors. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
#include <memory>
#include <utility>
#include <vector>
#include "base/containers/hash_tables.h"
#include "base/memory/ref_counted.h"
#include "base/optional.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "cobalt/base/application_state.h"
#include "cobalt/base/debugger_hooks.h"
#include "cobalt/script/callback_function.h"
#include "cobalt/script/script_value.h"
#include "cobalt/script/wrappable.h"
namespace cobalt {
namespace dom {
class WindowTimers {
typedef script::CallbackFunction<void()> TimerCallback;
typedef script::ScriptValue<TimerCallback> TimerCallbackArg;
explicit WindowTimers(script::Wrappable* const owner,
const base::DebuggerHooks& debugger_hooks,
base::ApplicationState application_state)
: owner_(owner),
application_state_(application_state) {}
~WindowTimers() {}
int SetTimeout(const TimerCallbackArg& handler, int timeout);
void ClearTimeout(int handle);
int SetInterval(const TimerCallbackArg& handler, int timeout);
void ClearInterval(int handle);
void ClearAllIntervalsAndTimeouts();
// When called, it will irreversibly put the WindowTimers object in an
// inactive state where timer callbacks are ignored. This is useful when
// we're in the process of shutting down and wish to drain the JavaScript
// event queue without adding more on to the end of it.
void DisableCallbacks();
void SetApplicationState(base::ApplicationState state);
class Timer : public base::RefCounted<Timer> {
enum TimerType { kOneShot, kRepeating };
Timer(TimerType type, script::Wrappable* const owner,
const TimerCallbackArg& callback, int timeout, int handle,
WindowTimers* window_timers);
base::internal::TimerBase* timer() { return timer_.get(); }
TimerCallbackArg::Reference& callback_reference() { return callback_; }
// Pause this timer. The timer will not fire when paused.
void Pause();
// Start or Resume this timer. If the timer was paused and the desired run
// time is in the past, it will fire immediately.
void StartOrResume();
~Timer() {}
// Create and start a timer of the specified TimerClass type.
template <class TimerClass>
std::unique_ptr<base::internal::TimerBase> CreateAndStart();
TimerType type_;
std::unique_ptr<base::internal::TimerBase> timer_;
TimerCallbackArg::Reference callback_;
int timeout_;
int handle_;
WindowTimers* window_timers_;
// Store the desired run tim of a paused timer.
base::Optional<base::TimeTicks> desired_run_time_;
friend class base::RefCounted<Timer>;
typedef base::hash_map<int, scoped_refptr<Timer> > Timers;
// Try to add a new timer of the given type, return the handle or 0 when
// failed.
int TryAddNewTimer(Timer::TimerType type, const TimerCallbackArg& handler,
int timeout);
// Returns a positive integer timer handle that hasn't been assigned, or 0
// if none can be found.
int GetFreeTimerHandle();
// This callback, when called by Timer, runs the callback in TimerInfo
// and removes the handle if necessary.
void RunTimerCallback(int handle);
Timers timers_;
int current_timer_index_ = 0;
script::Wrappable* const owner_;
const base::DebuggerHooks& debugger_hooks_;
// Set to false when we're about to shutdown, to ensure that no new JavaScript
// is fired as we are waiting for it to drain.
bool callbacks_active_ = true;
base::ApplicationState application_state_;
} // namespace dom
} // namespace cobalt