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.
4
5 #include "gin/modules/timer.h"
6
7 #include "base/bind.h"
8 #include "gin/object_template_builder.h"
9 #include "gin/per_context_data.h"
10
11 namespace gin {
12
13 namespace {
14
GetHiddenPropertyName(v8::Isolate * isolate)15 v8::Handle<v8::String> GetHiddenPropertyName(v8::Isolate* isolate) {
16 return gin::StringToSymbol(isolate, "::gin::Timer");
17 }
18
19 } // namespace
20
21 // Timer =======================================================================
22
23 gin::WrapperInfo Timer::kWrapperInfo = { gin::kEmbedderNativeGin };
24
25 // static
Create(TimerType type,v8::Isolate * isolate,int delay_ms,v8::Handle<v8::Function> function)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,
29 delay_ms, function));
30 }
31
GetObjectTemplateBuilder(v8::Isolate * isolate)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)
36 .SetMethod("cancel",
37 base::Bind(&base::Timer::Stop, base::Unretained(&timer_)))
38 .SetMethod("reset",
39 base::Bind(&base::Timer::Reset, base::Unretained(&timer_)));
40 }
41
Timer(v8::Isolate * isolate,bool repeating,int delay_ms,v8::Handle<v8::Function> function)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_->GetContextHolder()->isolate())->SetHiddenValue(
49 GetHiddenPropertyName(isolate), function);
50 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(delay_ms),
51 base::Bind(&Timer::OnTimerFired, weak_factory_.GetWeakPtr()));
52 }
53
~Timer()54 Timer::~Timer() {
55 }
56
OnTimerFired()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
60 // of is destroyed.
61 if (!runner_.get()) {
62 return;
63 }
64
65 Runner::Scope scope(runner_.get());
66 v8::Isolate* isolate = runner_->GetContextHolder()->isolate();
67 v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(
68 GetWrapper(isolate)->GetHiddenValue(GetHiddenPropertyName(isolate)));
69 runner_->Call(function, v8::Undefined(isolate), 0, NULL);
70 }
71
72
73 // TimerModule =================================================================
74
75 const char TimerModule::kName[] = "timer";
76 WrapperInfo TimerModule::kWrapperInfo = { kEmbedderNativeGin };
77
78 // static
Create(v8::Isolate * isolate)79 Handle<TimerModule> TimerModule::Create(v8::Isolate* isolate) {
80 return CreateHandle(isolate, new TimerModule());
81 }
82
83 // static
GetModule(v8::Isolate * isolate)84 v8::Local<v8::Value> TimerModule::GetModule(v8::Isolate* isolate) {
85 return Create(isolate)->GetWrapper(isolate);
86 }
87
TimerModule()88 TimerModule::TimerModule() {
89 }
90
~TimerModule()91 TimerModule::~TimerModule() {
92 }
93
GetObjectTemplateBuilder(v8::Isolate * isolate)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));
101 }
102
103 } // namespace gin
104