OLD | NEW |
---|---|
(Empty) | |
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 | |
15 v8::Handle<v8::String> GetHiddenPropertyName(v8::Isolate* isolate) { | |
16 return gin::StringToSymbol(isolate, "::mojo::js::Timer"); | |
abarth-chromium
2013/12/21 03:37:06
::gin::Timer probably. We don't want to mention "
| |
17 } | |
abarth-chromium
2013/12/21 03:37:06
I talked with danno a bit about this pattern. App
Aaron Boodman
2013/12/21 21:34:41
Wow, cool. Note to self: http://webreflection.blog
Aaron Boodman
2013/12/21 22:02:18
Actually, i'm still not clear on how that would wo
| |
18 | |
19 } // namespace | |
20 | |
21 // Timer | |
22 | |
23 gin::WrapperInfo Timer::kWrapperInfo = { gin::kEmbedderNativeGin }; | |
24 | |
25 // static | |
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 | |
32 ObjectTemplateBuilder Timer::GetObjectTemplateBuilder(v8::Isolate* isolate) { | |
33 return Wrappable<Timer>::GetObjectTemplateBuilder(isolate) | |
34 .SetMethod("cancel", | |
35 base::Bind(&base::Timer::Stop, base::Unretained(&timer_))) | |
36 .SetMethod("reset", | |
37 base::Bind(&base::Timer::Reset, base::Unretained(&timer_))); | |
38 } | |
39 | |
40 Timer::Timer(v8::Isolate* isolate, bool repeating, int delay_ms, | |
41 v8::Handle<v8::Function> function) | |
42 : weak_factory_(this), | |
43 timer_(false, repeating), | |
44 runner_(PerContextData::From( | |
45 isolate->GetCurrentContext())->runner()->GetWeakPtr()) { | |
46 GetWrapper(runner_->isolate())->SetHiddenValue(GetHiddenPropertyName(isolate), | |
47 function); | |
48 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(delay_ms), | |
49 base::Bind(&Timer::OnTimerFired, weak_factory_.GetWeakPtr())); | |
50 } | |
51 | |
52 Timer::~Timer() { | |
53 } | |
54 | |
55 void Timer::OnTimerFired() { | |
56 Runner::Scope scope(runner_.get()); | |
abarth-chromium
2013/12/21 03:37:06
Should we check the runner_ for null?
Aaron Boodman
2013/12/21 21:34:41
I believe that cannot happen if everyone is playin
Aaron Boodman
2013/12/21 23:39:11
I made a test for this to prove I was right. You w
| |
57 v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast( | |
58 GetWrapper(runner_->isolate())->GetHiddenValue( | |
59 GetHiddenPropertyName(runner_->isolate()))); | |
60 v8::Handle<v8::Value> argv[] = {}; | |
61 runner_->Call(function, v8::Undefined(runner_->isolate()), 0, argv); | |
62 } | |
63 | |
64 | |
65 // TimerModule | |
66 | |
67 WrapperInfo TimerModule::kWrapperInfo = { kEmbedderNativeGin }; | |
68 | |
69 // static | |
70 Handle<TimerModule> TimerModule::Create(v8::Isolate* isolate) { | |
71 return CreateHandle(isolate, new TimerModule()); | |
72 } | |
73 | |
74 // static | |
75 v8::Local<v8::Value> TimerModule::GetModule(v8::Isolate* isolate) { | |
76 return Create(isolate)->GetWrapper(isolate); | |
77 } | |
78 | |
79 TimerModule::TimerModule() { | |
80 } | |
81 | |
82 TimerModule::~TimerModule() { | |
83 } | |
84 | |
85 ObjectTemplateBuilder TimerModule::GetObjectTemplateBuilder( | |
86 v8::Isolate* isolate) { | |
87 return Wrappable<TimerModule>::GetObjectTemplateBuilder(isolate) | |
88 .SetMethod("createOneShot", | |
89 base::Bind(&Timer::Create, Timer::TYPE_ONE_SHOT)) | |
90 .SetMethod("createRepeating", | |
91 base::Bind(&Timer::Create, Timer::TYPE_REPEATING)); | |
92 } | |
93 | |
94 } // namespace gin | |
OLD | NEW |