| LEFT | RIGHT | 
|---|
| 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 16 matching lines...) Expand all  Loading... | 
| 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/FileSystem.h> | 
| 31 #include <AdblockPlus/JsValue.h> | 31 #include <AdblockPlus/JsValue.h> | 
| 32 #include <AdblockPlus/WebRequest.h> | 32 #include <AdblockPlus/WebRequest.h> | 
| 33 #include <AdblockPlus/ITimer.h> | 33 #include <AdblockPlus/ITimer.h> | 
| 34 | 34 | 
| 35 namespace v8 | 35 namespace v8 | 
| 36 { | 36 { | 
| 37   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<typename T> class FunctionCallbackInfo; | 
| 42   typedef Handle<Value>(*InvocationCallback)(const Arguments &args); | 41   typedef void(*FunctionCallback)(const FunctionCallbackInfo<v8::Value>& info); | 
| 43 } | 42 } | 
| 44 | 43 | 
| 45 namespace AdblockPlus | 44 namespace AdblockPlus | 
| 46 { | 45 { | 
| 47   class JsEngine; | 46   class JsEngine; | 
| 48 | 47 | 
| 49   /** | 48   /** | 
| 50    * Shared smart pointer to a `JsEngine` instance. | 49    * Shared smart pointer to a `JsEngine` instance. | 
| 51    */ | 50    */ | 
| 52   typedef std::shared_ptr<JsEngine> JsEnginePtr; | 51   typedef std::shared_ptr<JsEngine> JsEnginePtr; | 
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 90    * JavaScript engine used by `FilterEngine`, wraps v8. | 89    * JavaScript engine used by `FilterEngine`, wraps v8. | 
| 91    */ | 90    */ | 
| 92   class JsEngine : public std::enable_shared_from_this<JsEngine> | 91   class JsEngine : public std::enable_shared_from_this<JsEngine> | 
| 93   { | 92   { | 
| 94     friend class JsValue; | 93     friend class JsValue; | 
| 95     friend class JsContext; | 94     friend class JsContext; | 
| 96 | 95 | 
| 97     struct JsWeakValuesList | 96     struct JsWeakValuesList | 
| 98     { | 97     { | 
| 99       ~JsWeakValuesList(); | 98       ~JsWeakValuesList(); | 
| 100       std::vector<std::unique_ptr<v8::Persistent<v8::Value>>> values; | 99       std::vector<v8::Global<v8::Value>> values; | 
| 101     }; | 100     }; | 
| 102     typedef std::list<JsWeakValuesList> JsWeakValuesLists; | 101     typedef std::list<JsWeakValuesList> JsWeakValuesLists; | 
| 103   public: | 102   public: | 
| 104     /** | 103     /** | 
| 105      * Event callback function. | 104      * Event callback function. | 
| 106      */ | 105      */ | 
| 107     typedef std::function<void(JsValueList&& params)> EventCallback; | 106     typedef std::function<void(JsValueList&& params)> EventCallback; | 
| 108 | 107 | 
| 109     /** | 108     /** | 
| 110      * Maps events to callback functions. | 109      * Maps events to callback functions. | 
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 199 | 198 | 
| 200     /** | 199     /** | 
| 201      * Creates a new JavaScript object. | 200      * Creates a new JavaScript object. | 
| 202      * @return New `JsValue` instance. | 201      * @return New `JsValue` instance. | 
| 203      */ | 202      */ | 
| 204     JsValue NewObject(); | 203     JsValue NewObject(); | 
| 205 | 204 | 
| 206     /** | 205     /** | 
| 207      * Creates a JavaScript function that invokes a C++ callback. | 206      * Creates a JavaScript function that invokes a C++ callback. | 
| 208      * @param callback C++ callback to invoke. The callback receives a | 207      * @param callback C++ callback to invoke. The callback receives a | 
| 209      *        `v8::Arguments` object and can use `FromArguments()` to retrieve | 208      *        `v8::FunctionCallbackInfo` object and can use `FromArguments()` to
      retrieve | 
| 210      *        the current `JsEngine`. | 209      *        the current `JsEngine`. | 
| 211      * @return New `JsValue` instance. | 210      * @return New `JsValue` instance. | 
| 212      */ | 211      */ | 
| 213     JsValue NewCallback(const v8::InvocationCallback& callback); | 212     JsValue NewCallback(const v8::FunctionCallback& callback); | 
| 214 | 213 | 
| 215     /** | 214     /** | 
| 216      * Returns a `JsEngine` instance contained in a `v8::Arguments` object. | 215      * Returns a `JsEngine` instance contained in a `v8::FunctionCallbackInfo` o
     bject. | 
| 217      * Use this in callbacks created via `NewCallback()` to retrieve the current | 216      * Use this in callbacks created via `NewCallback()` to retrieve the current | 
| 218      * `JsEngine`. | 217      * `JsEngine`. | 
| 219      * @param arguments `v8::Arguments` object containing the `JsEngine` | 218      * @param arguments `v8::FunctionCallbackInfo` object containing the `JsEngi
     ne` | 
| 220      *        instance. | 219      *        instance. | 
| 221      * @return `JsEngine` instance from `v8::Arguments`. | 220      * @return `JsEngine` instance from `v8::FunctionCallbackInfo`. | 
| 222      */ | 221      */ | 
| 223     static JsEnginePtr FromArguments(const v8::Arguments& arguments); | 222     static JsEnginePtr FromArguments(const v8::FunctionCallbackInfo<v8::Value>& 
     arguments); | 
| 224 | 223 | 
| 225     /** | 224     /** | 
| 226      * Stores `JsValue`s in a way they don't keep a strong reference to | 225      * Stores `JsValue`s in a way they don't keep a strong reference to | 
| 227      * `JsEngine` and which are destroyed when `JsEngine` is destroyed. These | 226      * `JsEngine` and which are destroyed when `JsEngine` is destroyed. These | 
| 228      * methods should be used when one needs to carry a JsValue in a callback | 227      * methods should be used when one needs to carry a JsValue in a callback | 
| 229      * directly or indirectly passed to `JsEngine`. | 228      * directly or indirectly passed to `JsEngine`. | 
| 230      * The method is thread-safe. | 229      * The method is thread-safe. | 
| 231      * @param `JsValueList` to store. | 230      * @param `JsValueList` to store. | 
| 232      * @return `JsWeakValuesID` of stored values which allows to restore them | 231      * @return `JsWeakValuesID` of stored values which allows to restore them | 
| 233      * later. | 232      * later. | 
| 234      */ | 233      */ | 
| 235     JsWeakValuesID StoreJsValues(const JsValueList& values); | 234     JsWeakValuesID StoreJsValues(const JsValueList& values); | 
| 236 | 235 | 
| 237     /** | 236     /** | 
| 238      * Extracts and removes from `JsEngine` earlier stored `JsValue`s. | 237      * Extracts and removes from `JsEngine` earlier stored `JsValue`s. | 
| 239      * The method is thread-safe. | 238      * The method is thread-safe. | 
| 240      * @param id `JsWeakValuesID` of values. | 239      * @param id `JsWeakValuesID` of values. | 
| 241      * @return `JsValueList` of stored values. | 240      * @return `JsValueList` of stored values. | 
| 242      */ | 241      */ | 
| 243     JsValueList TakeJsValues(const JsWeakValuesID& id); | 242     JsValueList TakeJsValues(const JsWeakValuesID& id); | 
| 244 | 243 | 
| 245     /* | 244     /* | 
| 246      * Private functionality required to implement timers. | 245      * Private functionality required to implement timers. | 
| 247      * @param arguments `v8::Arguments` is the arguments received in C++ | 246      * @param arguments `v8::FunctionCallbackInfo` is the arguments received in 
     C++ | 
| 248      * callback associated for global setTimeout method. | 247      * callback associated for global setTimeout method. | 
| 249      */ | 248      */ | 
| 250     static void ScheduleTimer(const v8::Arguments& arguments); | 249     static void ScheduleTimer(const v8::FunctionCallbackInfo<v8::Value>& argumen
     ts); | 
| 251 | 250 | 
| 252     /* | 251     /* | 
| 253      * Private functionality required to implement web requests. | 252      * Private functionality required to implement web requests. | 
| 254      * @param arguments `v8::Arguments` is the arguments received in C++ | 253      * @param arguments `v8::FunctionCallbackInfo` is the arguments received in 
     C++ | 
| 255      * callback associated for global GET method. | 254      * callback associated for global GET method. | 
| 256      */ | 255      */ | 
| 257     static void ScheduleWebRequest(const v8::Arguments& arguments); | 256     static void ScheduleWebRequest(const v8::FunctionCallbackInfo<v8::Value>& ar
     guments); | 
| 258 | 257 | 
| 259     /** | 258     /** | 
| 260      * Converts v8 arguments to `JsValue` objects. | 259      * Converts v8 arguments to `JsValue` objects. | 
| 261      * @param arguments `v8::Arguments` object containing the arguments to | 260      * @param arguments `v8::FunctionCallbackInfo` object containing the argumen
     ts to | 
| 262      *        convert. | 261      *        convert. | 
| 263      * @return List of arguments converted to `const JsValue` objects. | 262      * @return List of arguments converted to `const JsValue` objects. | 
| 264      */ | 263      */ | 
| 265     JsValueList ConvertArguments(const v8::Arguments& arguments); | 264     JsValueList ConvertArguments(const v8::FunctionCallbackInfo<v8::Value>& argu
     ments); | 
| 266 | 265 | 
| 267     /** | 266     /** | 
|  | 267      * Private functionality. | 
| 268      * @return The asynchronous IFileSystem implementation. | 268      * @return The asynchronous IFileSystem implementation. | 
| 269      */ | 269      */ | 
| 270     FileSystemPtr GetFileSystem() const; | 270     FileSystemPtr GetAsyncFileSystem() const; | 
|  | 271 | 
|  | 272     /** | 
|  | 273      * Sets the synchronous `FileSystem` implementation used for all | 
|  | 274      * file I/O. Setting this is optional, the engine will use the | 
|  | 275      * implementation created by `CreateDefaultFileSystem()` by | 
|  | 276      * default, which might be sufficient. | 
|  | 277      * @param The `FileSystem` instance to use. | 
|  | 278      */ | 
|  | 279     void SetFileSystem(const FileSystemSyncPtr& val); | 
| 271 | 280 | 
| 272     /** | 281     /** | 
| 273      * Sets the `WebRequest` implementation used for XMLHttpRequests. | 282      * Sets the `WebRequest` implementation used for XMLHttpRequests. | 
| 274      * Setting this is optional, the engine will use a `DefaultWebRequest` | 283      * Setting this is optional, the engine will use a `DefaultWebRequest` | 
| 275      * instance by default, which might be sufficient. | 284      * instance by default, which might be sufficient. | 
| 276      * @param The `WebRequest` instance to use. | 285      * @param The `WebRequest` instance to use. | 
| 277      */ | 286      */ | 
| 278     void SetWebRequest(const WebRequestSharedPtr& val); | 287     void SetWebRequest(const WebRequestSharedPtr& val); | 
| 279 | 288 | 
| 280     /** | 289     /** | 
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 317     explicit JsEngine(TimerPtr timer, FileSystemPtr fileSystem, WebRequestPtr we
     bRequest); | 326     explicit JsEngine(TimerPtr timer, FileSystemPtr fileSystem, WebRequestPtr we
     bRequest); | 
| 318 | 327 | 
| 319     JsValue GetGlobalObject(); | 328     JsValue GetGlobalObject(); | 
| 320 | 329 | 
| 321     /// Isolate must be disposed only after disposing of all objects which are | 330     /// Isolate must be disposed only after disposing of all objects which are | 
| 322     /// using it. | 331     /// using it. | 
| 323     ScopedV8Isolate isolate; | 332     ScopedV8Isolate isolate; | 
| 324 | 333 | 
| 325     FileSystemPtr fileSystem; | 334     FileSystemPtr fileSystem; | 
| 326     LogSystemPtr logSystem; | 335     LogSystemPtr logSystem; | 
| 327     std::unique_ptr<v8::Persistent<v8::Context>> context; | 336     std::unique_ptr<v8::Global<v8::Context>> context; | 
| 328     EventMap eventCallbacks; | 337     EventMap eventCallbacks; | 
| 329     std::mutex eventCallbacksMutex; | 338     std::mutex eventCallbacksMutex; | 
| 330     JsWeakValuesLists jsWeakValuesLists; | 339     JsWeakValuesLists jsWeakValuesLists; | 
| 331     std::mutex jsWeakValuesListsMutex; | 340     std::mutex jsWeakValuesListsMutex; | 
| 332     TimerPtr timer; | 341     TimerPtr timer; | 
| 333     WebRequestPtr webRequest; | 342     WebRequestPtr webRequest; | 
| 334     WebRequestSharedPtr webRequestLegacy; | 343     WebRequestSharedPtr webRequestLegacy; | 
| 335   }; | 344   }; | 
| 336 } | 345 } | 
| 337 | 346 | 
| 338 #endif | 347 #endif | 
| LEFT | RIGHT | 
|---|