| 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. | |
| 55 */ | |
| 56 TimerPtr CreateDefaultTimer(); | |
| 57 | |
| 58 /** | |
| 59 * A factory to construct DefaultFileSystem. | |
| 60 */ | |
| 61 FileSystemPtr CreateDefaultFileSystem(); | |
| 62 | |
| 63 /** | |
| 64 * A factory to construct DefaultWebRequest. | |
| 65 */ | |
| 66 WebRequestPtr CreateDefaultWebRequest(); | |
| 67 | |
| 68 /** | |
| 69 * Scope based isolate manager. Creates a new isolate instance on | 56 * Scope based isolate manager. Creates a new isolate instance on |
| 70 * constructing and disposes it on destructing. | 57 * constructing and disposes it on destructing. |
| 71 */ | 58 */ |
| 72 class ScopedV8Isolate | 59 class ScopedV8Isolate |
| 73 { | 60 { |
| 74 public: | 61 public: |
| 75 ScopedV8Isolate(); | 62 ScopedV8Isolate(); |
| 76 ~ScopedV8Isolate(); | 63 ~ScopedV8Isolate(); |
| 77 v8::Isolate* Get() | 64 v8::Isolate* Get() |
| 78 { | 65 { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 * | 102 * |
| 116 */ | 103 */ |
| 117 class JsWeakValuesID | 104 class JsWeakValuesID |
| 118 { | 105 { |
| 119 friend class JsEngine; | 106 friend class JsEngine; |
| 120 JsWeakValuesLists::const_iterator iterator; | 107 JsWeakValuesLists::const_iterator iterator; |
| 121 }; | 108 }; |
| 122 | 109 |
| 123 /** | 110 /** |
| 124 * Creates a new JavaScript engine instance. | 111 * Creates a new JavaScript engine instance. |
| 112 * |
| 125 * @param appInfo Information about the app. | 113 * @param appInfo Information about the app. |
| 126 * @param timer Implementation of timer. | 114 * @param platform AdblockPlus platform providing with necessary |
| 127 * @param fileSystem Implementation of filesystem. | 115 * dependencies. |
| 128 * @param webRequest Implementation of web request. | |
| 129 * @return New `JsEngine` instance. | 116 * @return New `JsEngine` instance. |
| 130 */ | 117 */ |
| 131 static JsEnginePtr New(const AppInfo& appInfo = AppInfo(), | 118 static JsEnginePtr New(const AppInfo& appInfo, Platform& platform); |
| 132 TimerPtr timer = CreateDefaultTimer(), | |
| 133 FileSystemPtr fileSystem = CreateDefaultFileSystem(), | |
| 134 WebRequestPtr webRequest = CreateDefaultWebRequest()); | |
| 135 | |
| 136 /** | 119 /** |
| 137 * Registers the callback function for an event. | 120 * Registers the callback function for an event. |
| 138 * @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 |
| 139 * general purpose event handling mechanism. | 122 * general purpose event handling mechanism. |
| 140 * @param callback Event callback function. | 123 * @param callback Event callback function. |
| 141 */ | 124 */ |
| 142 void SetEventCallback(const std::string& eventName, const EventCallback& cal
lback); | 125 void SetEventCallback(const std::string& eventName, const EventCallback& cal
lback); |
| 143 | 126 |
| 144 /** | 127 /** |
| 145 * 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... |
| 255 | 238 |
| 256 /** | 239 /** |
| 257 * Converts v8 arguments to `JsValue` objects. | 240 * Converts v8 arguments to `JsValue` objects. |
| 258 * @param arguments `v8::FunctionCallbackInfo` object containing the argumen
ts to | 241 * @param arguments `v8::FunctionCallbackInfo` object containing the argumen
ts to |
| 259 * convert. | 242 * convert. |
| 260 * @return List of arguments converted to `const JsValue` objects. | 243 * @return List of arguments converted to `const JsValue` objects. |
| 261 */ | 244 */ |
| 262 JsValueList ConvertArguments(const v8::FunctionCallbackInfo<v8::Value>& argu
ments); | 245 JsValueList ConvertArguments(const v8::FunctionCallbackInfo<v8::Value>& argu
ments); |
| 263 | 246 |
| 264 /** | 247 /** |
| 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. | 248 * Sets a global property that can be accessed by all the scripts. |
| 303 * @param name Name of the property to set. | 249 * @param name Name of the property to set. |
| 304 * @param value Value of the property to set. | 250 * @param value Value of the property to set. |
| 305 */ | 251 */ |
| 306 void SetGlobalProperty(const std::string& name, const AdblockPlus::JsValue&
value); | 252 void SetGlobalProperty(const std::string& name, const AdblockPlus::JsValue&
value); |
| 307 | 253 |
| 308 /** | 254 /** |
| 309 * Returns a pointer to associated v8::Isolate. | 255 * Returns a pointer to associated v8::Isolate. |
| 310 */ | 256 */ |
| 311 v8::Isolate* GetIsolate() | 257 v8::Isolate* GetIsolate() |
| 312 { | 258 { |
| 313 return isolate.Get(); | 259 return isolate.Get(); |
| 314 } | 260 } |
| 315 | 261 |
| 316 /** | 262 /** |
| 317 * Notifies JS engine about critically low memory what should cause a | 263 * Notifies JS engine about critically low memory what should cause a |
| 318 * garbage collection. | 264 * garbage collection. |
| 319 */ | 265 */ |
| 320 void NotifyLowMemory(); | 266 void NotifyLowMemory(); |
| 267 |
| 268 /** |
| 269 * Private functionality. |
| 270 */ |
| 271 Platform& GetPlatform() |
| 272 { |
| 273 return platform; |
| 274 } |
| 321 private: | 275 private: |
| 322 void CallTimerTask(const JsWeakValuesID& timerParamsID); | 276 void CallTimerTask(const JsWeakValuesID& timerParamsID); |
| 323 | 277 |
| 324 explicit JsEngine(TimerPtr timer, FileSystemPtr fileSystem, WebRequestPtr we
bRequest); | 278 explicit JsEngine(Platform& platform); |
| 325 | 279 |
| 326 JsValue GetGlobalObject(); | 280 JsValue GetGlobalObject(); |
| 327 | 281 |
| 328 /// 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 |
| 329 /// using it. | 283 /// using it. |
| 330 ScopedV8Isolate isolate; | 284 ScopedV8Isolate isolate; |
| 285 Platform& platform; |
| 331 | 286 |
| 332 FileSystemPtr fileSystem; | |
| 333 LogSystemPtr logSystem; | |
| 334 std::unique_ptr<v8::Global<v8::Context>> context; | 287 std::unique_ptr<v8::Global<v8::Context>> context; |
| 335 EventMap eventCallbacks; | 288 EventMap eventCallbacks; |
| 336 std::mutex eventCallbacksMutex; | 289 std::mutex eventCallbacksMutex; |
| 337 JsWeakValuesLists jsWeakValuesLists; | 290 JsWeakValuesLists jsWeakValuesLists; |
| 338 std::mutex jsWeakValuesListsMutex; | 291 std::mutex jsWeakValuesListsMutex; |
| 339 TimerPtr timer; | |
| 340 WebRequestPtr webRequest; | |
| 341 WebRequestSharedPtr webRequestLegacy; | |
| 342 }; | 292 }; |
| 343 } | 293 } |
| 344 | 294 |
| 345 #endif | 295 #endif |
| OLD | NEW |