1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "gin/modules/timer.h"
8 #include "gin/object_template_builder.h"
9 #include "gin/per_context_data.h"
15 v8::Handle
<v8::String
> GetHiddenPropertyName(v8::Isolate
* isolate
) {
16 return gin::StringToSymbol(isolate
, "::gin::Timer");
21 // Timer =======================================================================
23 gin::WrapperInfo
Timer::kWrapperInfo
= { gin::kEmbedderNativeGin
};
26 Handle
<Timer
> Timer::Create(TimerType type
, v8::Isolate
* isolate
, int delay_ms
,
27 v8::Handle
<v8::Function
> function
) {
28 return CreateHandle(isolate
, new Timer(isolate
, type
== TYPE_REPEATING
,
32 ObjectTemplateBuilder
Timer::GetObjectTemplateBuilder(v8::Isolate
* isolate
) {
33 // We use Unretained() here because we directly own timer_, so we know it will
34 // be alive when these methods are called.
35 return Wrappable
<Timer
>::GetObjectTemplateBuilder(isolate
)
37 base::Bind(&base::Timer::Stop
, base::Unretained(&timer_
)))
39 base::Bind(&base::Timer::Reset
, base::Unretained(&timer_
)));
42 Timer::Timer(v8::Isolate
* isolate
, bool repeating
, int delay_ms
,
43 v8::Handle
<v8::Function
> function
)
44 : weak_factory_(this),
45 timer_(false, repeating
),
46 runner_(PerContextData::From(
47 isolate
->GetCurrentContext())->runner()->GetWeakPtr()) {
48 GetWrapper(runner_
->isolate())->SetHiddenValue(GetHiddenPropertyName(isolate
),
50 timer_
.Start(FROM_HERE
, base::TimeDelta::FromMilliseconds(delay_ms
),
51 base::Bind(&Timer::OnTimerFired
, weak_factory_
.GetWeakPtr()));
57 void Timer::OnTimerFired() {
58 // This can happen in spite of the weak callback because it is possible for
59 // a gin::Handle<> to keep this object alive past when the isolate it is part
65 Runner::Scope
scope(runner_
.get());
66 v8::Handle
<v8::Function
> function
= v8::Handle
<v8::Function
>::Cast(
67 GetWrapper(runner_
->isolate())->GetHiddenValue(
68 GetHiddenPropertyName(runner_
->isolate())));
69 runner_
->Call(function
, v8::Undefined(runner_
->isolate()), 0, NULL
);
73 // TimerModule =================================================================
75 const char TimerModule::kName
[] = "timer";
76 WrapperInfo
TimerModule::kWrapperInfo
= { kEmbedderNativeGin
};
79 Handle
<TimerModule
> TimerModule::Create(v8::Isolate
* isolate
) {
80 return CreateHandle(isolate
, new TimerModule());
84 v8::Local
<v8::Value
> TimerModule::GetModule(v8::Isolate
* isolate
) {
85 return Create(isolate
)->GetWrapper(isolate
);
88 TimerModule::TimerModule() {
91 TimerModule::~TimerModule() {
94 ObjectTemplateBuilder
TimerModule::GetObjectTemplateBuilder(
95 v8::Isolate
* isolate
) {
96 return Wrappable
<TimerModule
>::GetObjectTemplateBuilder(isolate
)
97 .SetMethod("createOneShot",
98 base::Bind(&Timer::Create
, Timer::TYPE_ONE_SHOT
))
99 .SetMethod("createRepeating",
100 base::Bind(&Timer::Create
, Timer::TYPE_REPEATING
));