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-2017 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 27 matching lines...) Expand all Loading... |
38 class Isolate; | 38 class Isolate; |
39 class Value; | 39 class Value; |
40 class Context; | 40 class Context; |
41 template<typename T> class FunctionCallbackInfo; | 41 template<typename T> class FunctionCallbackInfo; |
42 typedef void(*FunctionCallback)(const FunctionCallbackInfo<v8::Value>& info); | 42 typedef void(*FunctionCallback)(const FunctionCallbackInfo<v8::Value>& info); |
43 } | 43 } |
44 | 44 |
45 namespace AdblockPlus | 45 namespace AdblockPlus |
46 { | 46 { |
47 class JsEngine; | 47 class JsEngine; |
| 48 class Platform; |
48 | 49 |
49 /** | 50 /** |
50 * Shared smart pointer to a `JsEngine` instance. | 51 * Shared smart pointer to a `JsEngine` instance. |
51 */ | 52 */ |
52 typedef std::shared_ptr<JsEngine> JsEnginePtr; | 53 typedef std::shared_ptr<JsEngine> JsEnginePtr; |
53 | 54 |
54 /** | 55 /** |
55 * A factory to construct DefaultTimer. | |
56 */ | |
57 TimerPtr CreateDefaultTimer(); | |
58 | |
59 /** | |
60 * A factory to construct DefaultFileSystem. | |
61 */ | |
62 FileSystemPtr CreateDefaultFileSystem(const Scheduler& scheduler); | |
63 | |
64 /** | |
65 * A factory to construct DefaultWebRequest. | |
66 */ | |
67 WebRequestPtr CreateDefaultWebRequest(const Scheduler& scheduler); | |
68 | |
69 /** | |
70 * A factory to construct LogSystem. | |
71 */ | |
72 LogSystemPtr CreateDefaultLogSystem(); | |
73 | |
74 /** | |
75 * Scope based isolate manager. Creates a new isolate instance on | 56 * Scope based isolate manager. Creates a new isolate instance on |
76 * constructing and disposes it on destructing. | 57 * constructing and disposes it on destructing. |
77 */ | 58 */ |
78 class ScopedV8Isolate | 59 class ScopedV8Isolate |
79 { | 60 { |
80 public: | 61 public: |
81 ScopedV8Isolate(); | 62 ScopedV8Isolate(); |
82 ~ScopedV8Isolate(); | 63 ~ScopedV8Isolate(); |
83 v8::Isolate* Get() | 64 v8::Isolate* Get() |
84 { | 65 { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 */ | 103 */ |
123 class JsWeakValuesID | 104 class JsWeakValuesID |
124 { | 105 { |
125 friend class JsEngine; | 106 friend class JsEngine; |
126 JsWeakValuesLists::const_iterator iterator; | 107 JsWeakValuesLists::const_iterator iterator; |
127 }; | 108 }; |
128 | 109 |
129 /** | 110 /** |
130 * Creates a new JavaScript engine instance. | 111 * Creates a new JavaScript engine instance. |
131 * | 112 * |
132 * When a parameter value is nullptr the corresponding default | |
133 * implementation is chosen. | |
134 * | |
135 * @param appInfo Information about the app. | 113 * @param appInfo Information about the app. |
136 * @param timer Implementation of timer. | 114 * @param platform AdblockPlus platform providing with necessary |
137 * @param fileSystem Implementation of filesystem. | 115 * dependencies. |
138 * @param webRequest Implementation of web request. | |
139 * @param logSystem Implementation of log system. | |
140 * @return New `JsEngine` instance. | 116 * @return New `JsEngine` instance. |
141 */ | 117 */ |
142 static JsEnginePtr New(const AppInfo& appInfo = AppInfo(), | 118 static JsEnginePtr New(const AppInfo& appInfo, Platform& platform); |
143 TimerPtr timer = nullptr, FileSystemPtr fileSystem = nullptr, | |
144 WebRequestPtr webRequest = nullptr, LogSystemPtr logSystem = nullptr); | |
145 | |
146 /** | 119 /** |
147 * Registers the callback function for an event. | 120 * Registers the callback function for an event. |
148 * @param eventName Event name. Note that this can be any string - it's a | 121 * @param eventName Event name. Note that this can be any string - it's a |
149 * general purpose event handling mechanism. | 122 * general purpose event handling mechanism. |
150 * @param callback Event callback function. | 123 * @param callback Event callback function. |
151 */ | 124 */ |
152 void SetEventCallback(const std::string& eventName, const EventCallback& cal
lback); | 125 void SetEventCallback(const std::string& eventName, const EventCallback& cal
lback); |
153 | 126 |
154 /** | 127 /** |
155 * Removes the callback function for an event. | 128 * Removes the callback function for an event. |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 | 238 |
266 /** | 239 /** |
267 * Converts v8 arguments to `JsValue` objects. | 240 * Converts v8 arguments to `JsValue` objects. |
268 * @param arguments `v8::FunctionCallbackInfo` object containing the argumen
ts to | 241 * @param arguments `v8::FunctionCallbackInfo` object containing the argumen
ts to |
269 * convert. | 242 * convert. |
270 * @return List of arguments converted to `const JsValue` objects. | 243 * @return List of arguments converted to `const JsValue` objects. |
271 */ | 244 */ |
272 JsValueList ConvertArguments(const v8::FunctionCallbackInfo<v8::Value>& argu
ments); | 245 JsValueList ConvertArguments(const v8::FunctionCallbackInfo<v8::Value>& argu
ments); |
273 | 246 |
274 /** | 247 /** |
275 * Private functionality. | |
276 * @return The asynchronous IFileSystem implementation. | |
277 */ | |
278 FileSystemPtr GetAsyncFileSystem() const; | |
279 | |
280 /** | |
281 * Private functionality. | |
282 * @return The LogSystem implementation. | |
283 */ | |
284 LogSystem& GetLogSystem(); | |
285 | |
286 /** | |
287 * Sets a global property that can be accessed by all the scripts. | 248 * Sets a global property that can be accessed by all the scripts. |
288 * @param name Name of the property to set. | 249 * @param name Name of the property to set. |
289 * @param value Value of the property to set. | 250 * @param value Value of the property to set. |
290 */ | 251 */ |
291 void SetGlobalProperty(const std::string& name, const AdblockPlus::JsValue&
value); | 252 void SetGlobalProperty(const std::string& name, const AdblockPlus::JsValue&
value); |
292 | 253 |
293 /** | 254 /** |
294 * Returns a pointer to associated v8::Isolate. | 255 * Returns a pointer to associated v8::Isolate. |
295 */ | 256 */ |
296 v8::Isolate* GetIsolate() | 257 v8::Isolate* GetIsolate() |
297 { | 258 { |
298 return isolate.Get(); | 259 return isolate.Get(); |
299 } | 260 } |
300 | 261 |
301 /** | 262 /** |
302 * Notifies JS engine about critically low memory what should cause a | 263 * Notifies JS engine about critically low memory what should cause a |
303 * garbage collection. | 264 * garbage collection. |
304 */ | 265 */ |
305 void NotifyLowMemory(); | 266 void NotifyLowMemory(); |
| 267 |
| 268 /** |
| 269 * Private functionality. |
| 270 */ |
| 271 Platform& GetPlatform() |
| 272 { |
| 273 return platform; |
| 274 } |
306 private: | 275 private: |
307 void CallTimerTask(const JsWeakValuesID& timerParamsID); | 276 void CallTimerTask(const JsWeakValuesID& timerParamsID); |
308 | 277 |
309 explicit JsEngine(TimerPtr timer, FileSystemPtr fileSystem, WebRequestPtr we
bRequest, LogSystemPtr logSystem); | 278 explicit JsEngine(Platform& platform); |
310 | 279 |
311 JsValue GetGlobalObject(); | 280 JsValue GetGlobalObject(); |
312 | 281 |
313 /// Isolate must be disposed only after disposing of all objects which are | 282 /// Isolate must be disposed only after disposing of all objects which are |
314 /// using it. | 283 /// using it. |
315 ScopedV8Isolate isolate; | 284 ScopedV8Isolate isolate; |
| 285 Platform& platform; |
316 | 286 |
317 FileSystemPtr fileSystem; | |
318 LogSystemPtr logSystem; | |
319 std::unique_ptr<v8::Global<v8::Context>> context; | 287 std::unique_ptr<v8::Global<v8::Context>> context; |
320 EventMap eventCallbacks; | 288 EventMap eventCallbacks; |
321 std::mutex eventCallbacksMutex; | 289 std::mutex eventCallbacksMutex; |
322 JsWeakValuesLists jsWeakValuesLists; | 290 JsWeakValuesLists jsWeakValuesLists; |
323 std::mutex jsWeakValuesListsMutex; | 291 std::mutex jsWeakValuesListsMutex; |
324 TimerPtr timer; | |
325 WebRequestPtr webRequest; | |
326 }; | 292 }; |
327 } | 293 } |
328 | 294 |
329 #endif | 295 #endif |
OLD | NEW |