OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 Eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include <stdint.h> | 23 #include <stdint.h> |
24 #include <string> | 24 #include <string> |
25 #include <AdblockPlus/AppInfo.h> | 25 #include <AdblockPlus/AppInfo.h> |
26 #include <AdblockPlus/tr1_functional.h> | 26 #include <AdblockPlus/tr1_functional.h> |
27 #include <AdblockPlus/LogSystem.h> | 27 #include <AdblockPlus/LogSystem.h> |
28 #include <AdblockPlus/FileSystem.h> | 28 #include <AdblockPlus/FileSystem.h> |
29 #include <AdblockPlus/JsValue.h> | 29 #include <AdblockPlus/JsValue.h> |
30 #include <AdblockPlus/WebRequest.h> | 30 #include <AdblockPlus/WebRequest.h> |
31 | 31 |
32 #include "tr1_memory.h" | 32 #include "tr1_memory.h" |
33 #include "V8ValueHolder.h" | |
34 | 33 |
35 namespace v8 | 34 namespace v8 |
36 { | 35 { |
37 class Arguments; | 36 class Arguments; |
38 class Isolate; | 37 class Isolate; |
39 class Value; | 38 class Value; |
40 class Context; | 39 class Context; |
41 template<class T> class Handle; | 40 template<class T> class Handle; |
42 typedef Handle<Value>(*InvocationCallback)(const Arguments &args); | 41 typedef Handle<Value>(*InvocationCallback)(const Arguments &args); |
| 42 typedef void(*FunctionCallback)(const FunctionCallbackInfo<Value>& info); |
43 } | 43 } |
44 | 44 |
45 namespace AdblockPlus | 45 namespace AdblockPlus |
46 { | 46 { |
47 class JsEngine; | 47 class JsEngine; |
48 | 48 |
49 /** | 49 /** |
50 * Shared smart pointer to a `JsEngine` instance. | 50 * Shared smart pointer to a `JsEngine` instance. |
51 */ | 51 */ |
52 typedef std::tr1::shared_ptr<JsEngine> JsEnginePtr; | 52 typedef std::shared_ptr<JsEngine> JsEnginePtr; |
| 53 |
| 54 // These classes are inherited by JsEngine to avoid any mistake in the order o
f object destroying and |
| 55 // disposing. The isolate should be disposed after the everything else. v8::Is
olate::Scope |
| 56 // requires a scope, there is no API to "close" it. |
| 57 class IsolateManagerJsEngine |
| 58 { |
| 59 protected: |
| 60 IsolateManagerJsEngine() |
| 61 : isolate{v8::Isolate::New()} |
| 62 { |
| 63 } |
| 64 ~IsolateManagerJsEngine() |
| 65 { |
| 66 isolate->Dispose(); |
| 67 } |
| 68 v8::Isolate* isolate; |
| 69 }; |
| 70 |
| 71 class IsolateScopeJsEngine : protected IsolateManagerJsEngine |
| 72 { |
| 73 protected: |
| 74 IsolateScopeJsEngine() |
| 75 : isolateScope{isolate} |
| 76 { |
| 77 } |
| 78 ~IsolateScopeJsEngine() |
| 79 { |
| 80 } |
| 81 v8::Isolate::Scope isolateScope; |
| 82 }; |
| 83 |
| 84 class ContextScopeJsEngine : protected IsolateScopeJsEngine |
| 85 { |
| 86 protected: |
| 87 ~ContextScopeJsEngine() |
| 88 { |
| 89 } |
| 90 v8::UniquePersistent<v8::Context> context; |
| 91 }; |
53 | 92 |
54 /** | 93 /** |
55 * JavaScript engine used by `FilterEngine`, wraps v8. | 94 * JavaScript engine used by `FilterEngine`, wraps v8. |
56 */ | 95 */ |
57 class JsEngine : public std::tr1::enable_shared_from_this<JsEngine> | 96 class JsEngine : public std::enable_shared_from_this<JsEngine>, protected Cont
extScopeJsEngine |
58 { | 97 { |
59 friend class JsValue; | 98 friend class JsValue; |
60 friend class JsContext; | 99 friend class JsContext; |
| 100 struct PrivateCtrArg{}; |
| 101 public: |
| 102 JsEngine(PrivateCtrArg); |
| 103 ~JsEngine(); |
61 | 104 |
62 public: | |
63 /** | 105 /** |
64 * Event callback function. | 106 * Event callback function. |
65 */ | 107 */ |
66 typedef std::tr1::function<void(JsValueList& params)> EventCallback; | 108 typedef std::function<void(JsValueList& params)> EventCallback; |
67 | 109 |
68 /** | 110 /** |
69 * Maps events to callback functions. | 111 * Maps events to callback functions. |
70 */ | 112 */ |
71 typedef std::map<std::string, EventCallback> EventMap; | 113 typedef std::map<std::string, EventCallback> EventMap; |
72 | 114 |
73 /** | 115 /** |
74 * Creates a new JavaScript engine instance. | 116 * Creates a new JavaScript engine instance. |
75 * @param appInfo Information about the app. | 117 * @param appInfo Information about the app. |
76 * @return New `JsEngine` instance. | 118 * @return New `JsEngine` instance. |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 */ | 186 */ |
145 JsValuePtr NewObject(); | 187 JsValuePtr NewObject(); |
146 | 188 |
147 /** | 189 /** |
148 * Creates a JavaScript function that invokes a C++ callback. | 190 * Creates a JavaScript function that invokes a C++ callback. |
149 * @param callback C++ callback to invoke. The callback receives a | 191 * @param callback C++ callback to invoke. The callback receives a |
150 * `v8::Arguments` object and can use `FromArguments()` to retrieve | 192 * `v8::Arguments` object and can use `FromArguments()` to retrieve |
151 * the current `JsEngine`. | 193 * the current `JsEngine`. |
152 * @return New `JsValue` instance. | 194 * @return New `JsValue` instance. |
153 */ | 195 */ |
154 JsValuePtr NewCallback(v8::InvocationCallback callback); | 196 JsValuePtr NewCallback(v8::FunctionCallback callback); |
155 | 197 |
156 /** | 198 /** |
157 * Returns a `JsEngine` instance contained in a `v8::Arguments` object. | 199 * Returns a `JsEngine` instance contained in a `v8::Arguments` object. |
158 * Use this in callbacks created via `NewCallback()` to retrieve the current | 200 * Use this in callbacks created via `NewCallback()` to retrieve the current |
159 * `JsEngine`. | 201 * `JsEngine`. |
160 * @param arguments `v8::Arguments` object containing the `JsEngine` | 202 * @param arguments `v8::Arguments` object containing the `JsEngine` |
161 * instance. | 203 * instance. |
162 * @return `JsEngine` instance from `v8::Arguments`. | 204 * @return `JsEngine` instance from `v8::Arguments`. |
163 */ | 205 */ |
164 static JsEnginePtr FromArguments(const v8::Arguments& arguments); | 206 static JsEnginePtr FromArguments(const v8::FunctionCallbackInfo<v8::Value>&
arguments); |
165 | 207 |
166 /** | 208 /** |
167 * Converts v8 arguments to `JsValue` objects. | 209 * Converts v8 arguments to `JsValue` objects. |
168 * @param arguments `v8::Arguments` object containing the arguments to | 210 * @param arguments `v8::Arguments` object containing the arguments to |
169 * convert. | 211 * convert. |
170 * @return List of arguments converted to `JsValue` objects. | 212 * @return List of arguments converted to `JsValue` objects. |
171 */ | 213 */ |
172 JsValueList ConvertArguments(const v8::Arguments& arguments); | 214 JsValueList ConvertArguments(const v8::FunctionCallbackInfo<v8::Value>& argu
ments); |
173 | 215 |
174 /** | 216 /** |
175 * @see `SetFileSystem()`. | 217 * @see `SetFileSystem()`. |
176 */ | 218 */ |
177 FileSystemPtr GetFileSystem(); | 219 FileSystemPtr GetFileSystem(); |
178 | 220 |
179 /** | 221 /** |
180 * Sets the `FileSystem` implementation used for all file I/O. | 222 * Sets the `FileSystem` implementation used for all file I/O. |
181 * Setting this is optional, the engine will use a `DefaultFileSystem` | 223 * Setting this is optional, the engine will use a `DefaultFileSystem` |
182 * instance by default, which might be sufficient. | 224 * instance by default, which might be sufficient. |
(...skipping 22 matching lines...) Expand all Loading... |
205 /** | 247 /** |
206 * Sets the `LogSystem` implementation used for logging (e.g. to handle | 248 * Sets the `LogSystem` implementation used for logging (e.g. to handle |
207 * `console.log()` calls from JavaScript). | 249 * `console.log()` calls from JavaScript). |
208 * Setting this is optional, the engine will use a `DefaultLogSystem` | 250 * Setting this is optional, the engine will use a `DefaultLogSystem` |
209 * instance by default, which might be sufficient. | 251 * instance by default, which might be sufficient. |
210 * @param The `LogSystem` instance to use. | 252 * @param The `LogSystem` instance to use. |
211 */ | 253 */ |
212 void SetLogSystem(LogSystemPtr val); | 254 void SetLogSystem(LogSystemPtr val); |
213 | 255 |
214 private: | 256 private: |
215 JsEngine(); | |
216 | |
217 FileSystemPtr fileSystem; | 257 FileSystemPtr fileSystem; |
218 WebRequestPtr webRequest; | 258 WebRequestPtr webRequest; |
219 LogSystemPtr logSystem; | 259 LogSystemPtr logSystem; |
220 v8::Isolate* isolate; | |
221 V8ValueHolder<v8::Context> context; | |
222 EventMap eventCallbacks; | 260 EventMap eventCallbacks; |
223 }; | 261 }; |
224 } | 262 } |
225 | 263 |
226 #endif | 264 #endif |
OLD | NEW |