1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "gin/modules/timer.h"
#include "base/bind.h"
#include "gin/object_template_builder.h"
#include "gin/per_context_data.h"
namespace gin {
namespace {
v8::Handle<v8::String> GetHiddenPropertyName(v8::Isolate* isolate) {
return gin::StringToSymbol(isolate, "::gin::Timer");
}
} // namespace
// Timer =======================================================================
gin::WrapperInfo Timer::kWrapperInfo = { gin::kEmbedderNativeGin };
// static
Handle<Timer> Timer::Create(TimerType type, v8::Isolate* isolate, int delay_ms,
v8::Handle<v8::Function> function) {
return CreateHandle(isolate, new Timer(isolate, type == TYPE_REPEATING,
delay_ms, function));
}
ObjectTemplateBuilder Timer::GetObjectTemplateBuilder(v8::Isolate* isolate) {
// We use Unretained() here because we directly own timer_, so we know it will
// be alive when these methods are called.
return Wrappable<Timer>::GetObjectTemplateBuilder(isolate)
.SetMethod("cancel",
base::Bind(&base::Timer::Stop, base::Unretained(&timer_)))
.SetMethod("reset",
base::Bind(&base::Timer::Reset, base::Unretained(&timer_)));
}
Timer::Timer(v8::Isolate* isolate, bool repeating, int delay_ms,
v8::Handle<v8::Function> function)
: weak_factory_(this),
timer_(false, repeating),
runner_(PerContextData::From(
isolate->GetCurrentContext())->runner()->GetWeakPtr()) {
GetWrapper(runner_->GetContextHolder()->isolate())->SetHiddenValue(
GetHiddenPropertyName(isolate), function);
timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(delay_ms),
base::Bind(&Timer::OnTimerFired, weak_factory_.GetWeakPtr()));
}
Timer::~Timer() {
}
void Timer::OnTimerFired() {
// This can happen in spite of the weak callback because it is possible for
// a gin::Handle<> to keep this object alive past when the isolate it is part
// of is destroyed.
if (!runner_.get()) {
return;
}
Runner::Scope scope(runner_.get());
v8::Isolate* isolate = runner_->GetContextHolder()->isolate();
v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast(
GetWrapper(isolate)->GetHiddenValue(GetHiddenPropertyName(isolate)));
runner_->Call(function, v8::Undefined(isolate), 0, NULL);
}
// TimerModule =================================================================
const char TimerModule::kName[] = "timer";
WrapperInfo TimerModule::kWrapperInfo = { kEmbedderNativeGin };
// static
Handle<TimerModule> TimerModule::Create(v8::Isolate* isolate) {
return CreateHandle(isolate, new TimerModule());
}
// static
v8::Local<v8::Value> TimerModule::GetModule(v8::Isolate* isolate) {
return Create(isolate)->GetWrapper(isolate);
}
TimerModule::TimerModule() {
}
TimerModule::~TimerModule() {
}
ObjectTemplateBuilder TimerModule::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return Wrappable<TimerModule>::GetObjectTemplateBuilder(isolate)
.SetMethod("createOneShot",
base::Bind(&Timer::Create, Timer::TYPE_ONE_SHOT))
.SetMethod("createRepeating",
base::Bind(&Timer::Create, Timer::TYPE_REPEATING));
}
} // namespace gin
|