OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2017 eyeo GmbH | 3 * Copyright (C) 2006-present 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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 #ifndef ADBLOCK_PLUS_JS_ENGINE_H | 18 #ifndef ADBLOCK_PLUS_JS_ENGINE_H |
19 #define ADBLOCK_PLUS_JS_ENGINE_H | 19 #define ADBLOCK_PLUS_JS_ENGINE_H |
20 | 20 |
21 #include <functional> | 21 #include <functional> |
22 #include <map> | 22 #include <map> |
23 #include <list> | 23 #include <list> |
24 #include <stdexcept> | 24 #include <stdexcept> |
25 #include <stdint.h> | 25 #include <stdint.h> |
26 #include <string> | 26 #include <string> |
27 #include <mutex> | 27 #include <mutex> |
28 #include <AdblockPlus/AppInfo.h> | 28 #include <AdblockPlus/AppInfo.h> |
29 #include <AdblockPlus/LogSystem.h> | 29 #include <AdblockPlus/LogSystem.h> |
30 #include <AdblockPlus/FileSystem.h> | 30 #include <AdblockPlus/IFileSystem.h> |
31 #include <AdblockPlus/JsValue.h> | 31 #include <AdblockPlus/JsValue.h> |
32 #include <AdblockPlus/WebRequest.h> | 32 #include <AdblockPlus/IWebRequest.h> |
33 #include <AdblockPlus/ITimer.h> | 33 #include <AdblockPlus/ITimer.h> |
| 34 #include <AdblockPlus/Scheduler.h> |
34 | 35 |
35 namespace v8 | 36 namespace v8 |
36 { | 37 { |
37 class Isolate; | 38 class Isolate; |
38 class Value; | 39 class Value; |
39 class Context; | 40 class Context; |
40 template<typename T> class FunctionCallbackInfo; | 41 template<typename T> class FunctionCallbackInfo; |
41 typedef void(*FunctionCallback)(const FunctionCallbackInfo<v8::Value>& info); | 42 typedef void(*FunctionCallback)(const FunctionCallbackInfo<v8::Value>& info); |
42 } | 43 } |
43 | 44 |
44 namespace AdblockPlus | 45 namespace AdblockPlus |
45 { | 46 { |
46 class JsEngine; | 47 class JsEngine; |
| 48 class Platform; |
47 | 49 |
48 /** | 50 /** |
49 * Shared smart pointer to a `JsEngine` instance. | 51 * Shared smart pointer to a `JsEngine` instance. |
50 */ | 52 */ |
51 typedef std::shared_ptr<JsEngine> JsEnginePtr; | 53 typedef std::shared_ptr<JsEngine> JsEnginePtr; |
52 | 54 |
53 /** | 55 /** |
54 * A factory to construct DefaultTimer. | 56 * Provides with isolate. The main aim of this iterface is to delegate a |
| 57 * proper initialization and deinitialization of v8::Isolate to an embedder. |
55 */ | 58 */ |
56 TimerPtr CreateDefaultTimer(); | 59 struct IV8IsolateProvider |
| 60 { |
| 61 virtual ~IV8IsolateProvider() {} |
57 | 62 |
58 /** | 63 /** |
59 * A factory to construct DefaultFileSystem. | 64 * Returns v8::Isolate. All subsequent calls of this method should return |
60 */ | 65 * the same pointer to v8::Isolate as the first call. |
61 FileSystemPtr CreateDefaultFileSystem(); | 66 */ |
62 | 67 virtual v8::Isolate* Get() = 0; |
63 /** | |
64 * A factory to construct DefaultWebRequest. | |
65 */ | |
66 WebRequestPtr CreateDefaultWebRequest(); | |
67 | |
68 /** | |
69 * Scope based isolate manager. Creates a new isolate instance on | |
70 * constructing and disposes it on destructing. | |
71 */ | |
72 class ScopedV8Isolate | |
73 { | |
74 public: | |
75 ScopedV8Isolate(); | |
76 ~ScopedV8Isolate(); | |
77 v8::Isolate* Get() | |
78 { | |
79 return isolate; | |
80 } | |
81 private: | |
82 ScopedV8Isolate(const ScopedV8Isolate&); | |
83 ScopedV8Isolate& operator=(const ScopedV8Isolate&); | |
84 | |
85 v8::Isolate* isolate; | |
86 }; | 68 }; |
87 | 69 |
88 /** | 70 /** |
89 * JavaScript engine used by `FilterEngine`, wraps v8. | 71 * JavaScript engine used by `FilterEngine`, wraps v8. |
90 */ | 72 */ |
91 class JsEngine : public std::enable_shared_from_this<JsEngine> | 73 class JsEngine : public std::enable_shared_from_this<JsEngine> |
92 { | 74 { |
93 friend class JsValue; | 75 friend class JsValue; |
94 friend class JsContext; | 76 friend class JsContext; |
95 | 77 |
96 struct JsWeakValuesList | 78 struct JsWeakValuesList |
97 { | 79 { |
98 ~JsWeakValuesList(); | 80 ~JsWeakValuesList(); |
99 std::vector<v8::Global<v8::Value>> values; | 81 std::vector<v8::Global<v8::Value>> values; |
100 }; | 82 }; |
101 typedef std::list<JsWeakValuesList> JsWeakValuesLists; | 83 typedef std::list<JsWeakValuesList> JsWeakValuesLists; |
102 public: | 84 public: |
103 /** | 85 /** |
104 * Event callback function. | 86 * Event callback function. |
105 */ | 87 */ |
106 typedef std::function<void(JsValueList&& params)> EventCallback; | 88 typedef std::function<void(JsValueList&& params)> EventCallback; |
107 | 89 |
108 /** | 90 /** |
109 * Maps events to callback functions. | 91 * Maps events to callback functions. |
110 */ | 92 */ |
111 typedef std::map<std::string, EventCallback> EventMap; | 93 typedef std::map<std::string, EventCallback> EventMap; |
112 | 94 |
113 /** | 95 /** |
114 * An opaque structure representing ID of stored JsValueList. | 96 * An opaque structure representing ID of stored JsValueList. |
115 * | |
116 */ | 97 */ |
117 class JsWeakValuesID | 98 class JsWeakValuesID |
118 { | 99 { |
119 friend class JsEngine; | 100 friend class JsEngine; |
120 JsWeakValuesLists::const_iterator iterator; | 101 JsWeakValuesLists::const_iterator iterator; |
121 }; | 102 }; |
122 | 103 |
123 /** | 104 /** |
124 * Creates a new JavaScript engine instance. | 105 * Creates a new JavaScript engine instance. |
| 106 * |
125 * @param appInfo Information about the app. | 107 * @param appInfo Information about the app. |
126 * @param timer Implementation of timer. | 108 * @param platform AdblockPlus platform providing with necessary |
127 * @param fileSystem Implementation of filesystem. | 109 * dependencies. |
128 * @param webRequest Implementation of web request. | 110 * @param isolate A provider of v8::Isolate, if the value is nullptr then |
| 111 * a default implementation is used. |
129 * @return New `JsEngine` instance. | 112 * @return New `JsEngine` instance. |
130 */ | 113 */ |
131 static JsEnginePtr New(const AppInfo& appInfo = AppInfo(), | 114 static JsEnginePtr New(const AppInfo& appInfo, Platform& platform, std::uniq
ue_ptr<IV8IsolateProvider> isolate = nullptr); |
132 TimerPtr timer = CreateDefaultTimer(), | |
133 FileSystemPtr fileSystem = CreateDefaultFileSystem(), | |
134 WebRequestPtr webRequest = CreateDefaultWebRequest()); | |
135 | |
136 /** | 115 /** |
137 * Registers the callback function for an event. | 116 * Registers the callback function for an event. |
138 * @param eventName Event name. Note that this can be any string - it's a | 117 * @param eventName Event name. Note that this can be any string - it's a |
139 * general purpose event handling mechanism. | 118 * general purpose event handling mechanism. |
140 * @param callback Event callback function. | 119 * @param callback Event callback function. |
141 */ | 120 */ |
142 void SetEventCallback(const std::string& eventName, const EventCallback& cal
lback); | 121 void SetEventCallback(const std::string& eventName, const EventCallback& cal
lback); |
143 | 122 |
144 /** | 123 /** |
145 * Removes the callback function for an event. | 124 * Removes the callback function for an event. |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 | 234 |
256 /** | 235 /** |
257 * Converts v8 arguments to `JsValue` objects. | 236 * Converts v8 arguments to `JsValue` objects. |
258 * @param arguments `v8::FunctionCallbackInfo` object containing the argumen
ts to | 237 * @param arguments `v8::FunctionCallbackInfo` object containing the argumen
ts to |
259 * convert. | 238 * convert. |
260 * @return List of arguments converted to `const JsValue` objects. | 239 * @return List of arguments converted to `const JsValue` objects. |
261 */ | 240 */ |
262 JsValueList ConvertArguments(const v8::FunctionCallbackInfo<v8::Value>& argu
ments); | 241 JsValueList ConvertArguments(const v8::FunctionCallbackInfo<v8::Value>& argu
ments); |
263 | 242 |
264 /** | 243 /** |
265 * Private functionality. | |
266 * @return The asynchronous IFileSystem implementation. | |
267 */ | |
268 FileSystemPtr GetAsyncFileSystem() const; | |
269 | |
270 /** | |
271 * Sets the synchronous `FileSystem` implementation used for all | |
272 * file I/O. Setting this is optional, the engine will use the | |
273 * implementation created by `CreateDefaultFileSystem()` by | |
274 * default, which might be sufficient. | |
275 * @param The `FileSystem` instance to use. | |
276 */ | |
277 void SetFileSystem(const FileSystemSyncPtr& val); | |
278 | |
279 /** | |
280 * Sets the `WebRequest` implementation used for XMLHttpRequests. | |
281 * Setting this is optional, the engine will use a `DefaultWebRequest` | |
282 * instance by default, which might be sufficient. | |
283 * @param The `WebRequest` instance to use. | |
284 */ | |
285 void SetWebRequest(const WebRequestSharedPtr& val); | |
286 | |
287 /** | |
288 * @see `SetLogSystem()`. | |
289 */ | |
290 LogSystemPtr GetLogSystem() const; | |
291 | |
292 /** | |
293 * Sets the `LogSystem` implementation used for logging (e.g. to handle | |
294 * `console.log()` calls from JavaScript). | |
295 * Setting this is optional, the engine will use a `DefaultLogSystem` | |
296 * instance by default, which might be sufficient. | |
297 * @param The `LogSystem` instance to use. | |
298 */ | |
299 void SetLogSystem(const LogSystemPtr& val); | |
300 | |
301 /** | |
302 * Sets a global property that can be accessed by all the scripts. | 244 * Sets a global property that can be accessed by all the scripts. |
303 * @param name Name of the property to set. | 245 * @param name Name of the property to set. |
304 * @param value Value of the property to set. | 246 * @param value Value of the property to set. |
305 */ | 247 */ |
306 void SetGlobalProperty(const std::string& name, const AdblockPlus::JsValue&
value); | 248 void SetGlobalProperty(const std::string& name, const AdblockPlus::JsValue&
value); |
307 | 249 |
308 /** | 250 /** |
309 * Returns a pointer to associated v8::Isolate. | 251 * Returns a pointer to associated v8::Isolate. |
310 */ | 252 */ |
311 v8::Isolate* GetIsolate() | 253 v8::Isolate* GetIsolate() |
312 { | 254 { |
313 return isolate.Get(); | 255 return isolate->Get(); |
314 } | 256 } |
315 | 257 |
316 /** | 258 /** |
317 * Notifies JS engine about critically low memory what should cause a | 259 * Notifies JS engine about critically low memory what should cause a |
318 * garbage collection. | 260 * garbage collection. |
319 */ | 261 */ |
320 void NotifyLowMemory(); | 262 void NotifyLowMemory(); |
| 263 |
| 264 /** |
| 265 * Private functionality. |
| 266 */ |
| 267 Platform& GetPlatform() |
| 268 { |
| 269 return platform; |
| 270 } |
321 private: | 271 private: |
322 void CallTimerTask(const JsWeakValuesID& timerParamsID); | 272 void CallTimerTask(const JsWeakValuesID& timerParamsID); |
323 | 273 |
324 explicit JsEngine(TimerPtr timer, FileSystemPtr fileSystem, WebRequestPtr we
bRequest); | 274 explicit JsEngine(Platform& platform, std::unique_ptr<IV8IsolateProvider> is
olate); |
325 | 275 |
326 JsValue GetGlobalObject(); | 276 JsValue GetGlobalObject(); |
327 | 277 |
| 278 Platform& platform; |
328 /// Isolate must be disposed only after disposing of all objects which are | 279 /// Isolate must be disposed only after disposing of all objects which are |
329 /// using it. | 280 /// using it. |
330 ScopedV8Isolate isolate; | 281 std::unique_ptr<IV8IsolateProvider> isolate; |
331 | 282 |
332 FileSystemPtr fileSystem; | |
333 LogSystemPtr logSystem; | |
334 std::unique_ptr<v8::Global<v8::Context>> context; | 283 std::unique_ptr<v8::Global<v8::Context>> context; |
335 EventMap eventCallbacks; | 284 EventMap eventCallbacks; |
336 std::mutex eventCallbacksMutex; | 285 std::mutex eventCallbacksMutex; |
337 JsWeakValuesLists jsWeakValuesLists; | 286 JsWeakValuesLists jsWeakValuesLists; |
338 std::mutex jsWeakValuesListsMutex; | 287 std::mutex jsWeakValuesListsMutex; |
339 TimerPtr timer; | |
340 WebRequestPtr webRequest; | |
341 WebRequestSharedPtr webRequestLegacy; | |
342 }; | 288 }; |
343 } | 289 } |
344 | 290 |
345 #endif | 291 #endif |
OLD | NEW |