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