Refactor microphone/camera permission requests
[chromium-blink-merge.git] / gin / modules / timer.cc
blob8aac8a6492560871bfb6c33ba57e30cf951f2224
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"
7 #include "base/bind.h"
8 #include "gin/object_template_builder.h"
9 #include "gin/per_context_data.h"
11 namespace gin {
13 namespace {
15 v8::Local<v8::String> GetHiddenPropertyName(v8::Isolate* isolate) {
16 return gin::StringToSymbol(isolate, "::gin::Timer");
19 } // namespace
21 // Timer =======================================================================
23 gin::WrapperInfo Timer::kWrapperInfo = { gin::kEmbedderNativeGin };
25 // static
26 Handle<Timer> Timer::Create(TimerType type, v8::Isolate* isolate, int delay_ms,
27 v8::Local<v8::Function> function) {
28 return CreateHandle(isolate, new Timer(isolate, type == TYPE_REPEATING,
29 delay_ms, function));
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_)));
42 Timer::Timer(v8::Isolate* isolate, bool repeating, int delay_ms,
43 v8::Local<v8::Function> function)
44 : timer_(false, repeating),
45 runner_(PerContextData::From(
46 isolate->GetCurrentContext())->runner()->GetWeakPtr()),
47 weak_factory_(this) {
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()));
54 Timer::~Timer() {
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;
65 Runner::Scope scope(runner_.get());
66 v8::Isolate* isolate = runner_->GetContextHolder()->isolate();
67 v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(
68 GetWrapper(isolate)->GetHiddenValue(GetHiddenPropertyName(isolate)));
69 runner_->Call(function, v8::Undefined(isolate), 0, NULL);
73 // TimerModule =================================================================
75 const char TimerModule::kName[] = "timer";
76 WrapperInfo TimerModule::kWrapperInfo = { kEmbedderNativeGin };
78 // static
79 Handle<TimerModule> TimerModule::Create(v8::Isolate* isolate) {
80 return CreateHandle(isolate, new TimerModule());
83 // static
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));
103 } // namespace gin