blob: aa05b01aaf8c806452003816696137be7bed6557 [file] [log] [blame]
commit-bot@chromium.org0fc0dea2013-12-18 04:45:37 +00001/*
2 * Copyright 2013 Google Inc.
3 *
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 *
8 */
9
10#ifndef SkV8Example_Global_DEFINED
11#define SkV8Example_Global_DEFINED
12
13#include <v8.h>
14
15using namespace v8;
16
17#include "SkTypes.h"
18#include "SkEvent.h"
19
20class SkOSWindow;
21
22// Provides the global isolate and context for our V8 instance.
23// Also implements all the global level functions.
24class Global : SkNoncopyable {
25public:
26 Global(Isolate* isolate)
27 : fIsolate(isolate)
28 , fWindow(NULL)
29 {
30 gGlobal = this;
31 }
32 virtual ~Global() {}
33
34 // The script will be parsed into the context this Global contains.
35 bool parseScript(const char script[]);
36
37 Local<Context> getContext() {
38 return Local<Context>::New(fIsolate, fContext);
39 }
40
41 Isolate* getIsolate() {
42 return fIsolate;
43 }
44
45 void setWindow(SkOSWindow* win) {
46 fWindow = win;
47 }
48 SkOSWindow* getWindow() {
49 return fWindow;
50 }
51
52 void reportException(TryCatch* tryCatch);
53
54private:
55 Handle<Context> createRootContext();
56
57 static bool TimeOutProc(const SkEvent& evt);
58
59 // Static functions that implement the global JS functions we add to
60 // the context.
61 static void SetTimeout(const v8::FunctionCallbackInfo<v8::Value>& args);
62 static void Print(const v8::FunctionCallbackInfo<v8::Value>& args);
63 static void Inval(const v8::FunctionCallbackInfo<Value>& args);
64
65 Persistent<Context> fContext;
66 Isolate* fIsolate;
67 SkOSWindow* fWindow;
68 static Global* gGlobal;
69
70 // Handle to the function to call when the timeout triggers.
71 Persistent<Function> fTimeout;
72};
73
74#endif