summaryrefslogtreecommitdiffstats
path: root/gin/modules/timer.h
blob: 1ec0b931607972b7b0934016b3046fc3545e485c (plain)
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
// 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::Timer timer_;
  base::WeakPtr<gin::Runner> runner_;
  base::WeakPtrFactory<Timer> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(Timer);
};


class GIN_EXPORT TimerModule : public Wrappable<TimerModule> {
 public:
  static const char kName[];
  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_