forked from sanyaade-mobiledev/chromium.src
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a simple one shot and repeating timer API for Mojo.js.
Various further rework of bindings stuff to facilitate. Review URL: https://codereview.chromium.org/120043008 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@242284 0039d316-1c4b-4281-b951-d872f2087c98
- Loading branch information
aa@chromium.org
committed
Dec 21, 2013
1 parent
91b82ad
commit 2491f14
Showing
27 changed files
with
339 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// 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) { | ||
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_->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() { | ||
if (!runner_) | ||
return; | ||
Runner::Scope scope(runner_.get()); | ||
v8::Handle<v8::Function> function = v8::Handle<v8::Function>::Cast( | ||
GetWrapper(runner_->isolate())->GetHiddenValue( | ||
GetHiddenPropertyName(runner_->isolate()))); | ||
runner_->Call(function, v8::Undefined(runner_->isolate()), 0, NULL); | ||
} | ||
|
||
|
||
// TimerModule | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// 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. | ||
|
||
#ifndef GIN_MODULES_TIMER_H_ | ||
#define GIN_MODULES_TIMER_H_ | ||
|
||
#include "base/memory/weak_ptr.h" | ||
#include "base/timer/timer.h" | ||
#include "gin/gin_export.h" | ||
#include "gin/handle.h" | ||
#include "gin/runner.h" | ||
#include "gin/wrappable.h" | ||
#include "v8/include/v8.h" | ||
|
||
namespace gin { | ||
|
||
class ObjectTemplateBuilder; | ||
|
||
// A simple scriptable timer that can work in one-shot or repeating mode. | ||
class GIN_EXPORT Timer : public Wrappable<Timer> { | ||
public: | ||
enum TimerType { | ||
TYPE_ONE_SHOT, | ||
TYPE_REPEATING | ||
}; | ||
|
||
static WrapperInfo kWrapperInfo; | ||
static Handle<Timer> Create(TimerType type, v8::Isolate* isolate, | ||
int delay_ms, v8::Handle<v8::Function> function); | ||
|
||
virtual ObjectTemplateBuilder GetObjectTemplateBuilder( | ||
v8::Isolate* isolate) OVERRIDE; | ||
|
||
private: | ||
Timer(v8::Isolate* isolate, bool repeating, int delay_ms, | ||
v8::Handle<v8::Function> function); | ||
virtual ~Timer(); | ||
void OnTimerFired(); | ||
|
||
base::WeakPtrFactory<Timer> weak_factory_; | ||
base::Timer timer_; | ||
base::WeakPtr<gin::Runner> runner_; | ||
}; | ||
|
||
|
||
class GIN_EXPORT TimerModule : public Wrappable<TimerModule> { | ||
public: | ||
static WrapperInfo kWrapperInfo; | ||
static Handle<TimerModule> Create(v8::Isolate* isolate); | ||
static v8::Local<v8::Value> GetModule(v8::Isolate* isolate); | ||
|
||
private: | ||
TimerModule(); | ||
virtual ~TimerModule(); | ||
|
||
virtual ObjectTemplateBuilder GetObjectTemplateBuilder( | ||
v8::Isolate* isolate) OVERRIDE; | ||
}; | ||
|
||
} // namespace gin | ||
|
||
#endif // GIN_MODULES_TIMER_H_ |
Oops, something went wrong.