| 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-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2016 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 <map> | 22 #include <map> |
| 22 #include <stdexcept> | 23 #include <stdexcept> |
| 23 #include <stdint.h> | 24 #include <stdint.h> |
| 24 #include <string> | 25 #include <string> |
| 25 #include <AdblockPlus/AppInfo.h> | 26 #include <AdblockPlus/AppInfo.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 | |
| 32 #include "tr1_memory.h" | |
| 33 | 31 |
| 34 namespace v8 | 32 namespace v8 |
| 35 { | 33 { |
| 36 class Arguments; | 34 class Arguments; |
| 37 class Isolate; | 35 class Isolate; |
| 38 class Value; | 36 class Value; |
| 39 class Context; | 37 class Context; |
| 40 template<class T> class Handle; | 38 template<class T> class Handle; |
| 41 typedef Handle<Value>(*InvocationCallback)(const Arguments &args); | 39 typedef Handle<Value>(*InvocationCallback)(const Arguments &args); |
| 42 } | 40 } |
| 43 | 41 |
| 44 namespace AdblockPlus | 42 namespace AdblockPlus |
| 45 { | 43 { |
| 46 class JsEngine; | 44 class JsEngine; |
| 47 | 45 |
| 48 /** | 46 /** |
| 49 * Shared smart pointer to a `JsEngine` instance. | 47 * Shared smart pointer to a `JsEngine` instance. |
| 50 */ | 48 */ |
| 51 typedef std::tr1::shared_ptr<JsEngine> JsEnginePtr; | 49 typedef std::shared_ptr<JsEngine> JsEnginePtr; |
| 52 | |
| 53 /** | |
| 54 * Scope based isolate manager. Creates a new isolate instance on | |
| 55 * constructing and disposes it on destructing. | |
| 56 */ | |
| 57 class ScopedV8Isolate | |
| 58 { | |
| 59 public: | |
| 60 ScopedV8Isolate(); | |
| 61 ~ScopedV8Isolate(); | |
| 62 protected: | |
| 63 v8::Isolate* isolate; | |
| 64 }; | |
| 65 | 50 |
| 66 /** | 51 /** |
| 67 * JavaScript engine used by `FilterEngine`, wraps v8. | 52 * JavaScript engine used by `FilterEngine`, wraps v8. |
| 68 * | |
| 69 * It's inherited from ScopedV8Isolate to have isolate disposed only after | |
| 70 * disposing of all objects which are using it. | |
| 71 */ | 53 */ |
| 72 class JsEngine : public std::tr1::enable_shared_from_this<JsEngine>, protected
ScopedV8Isolate | 54 class JsEngine : public std::enable_shared_from_this<JsEngine> |
| 73 { | 55 { |
| 74 friend class JsValue; | 56 friend class JsValue; |
| 75 friend class JsContext; | 57 friend class JsContext; |
| 58 |
| 76 public: | 59 public: |
| 77 /** | 60 /** |
| 78 * Event callback function. | 61 * Event callback function. |
| 79 */ | 62 */ |
| 80 typedef std::tr1::function<void(JsValueList& params)> EventCallback; | 63 typedef std::function<void(JsValueList& params)> EventCallback; |
| 81 | 64 |
| 82 /** | 65 /** |
| 83 * Maps events to callback functions. | 66 * Maps events to callback functions. |
| 84 */ | 67 */ |
| 85 typedef std::map<std::string, EventCallback> EventMap; | 68 typedef std::map<std::string, EventCallback> EventMap; |
| 86 | 69 |
| 87 /** | 70 /** |
| 88 * Creates a new JavaScript engine instance. | 71 * Creates a new JavaScript engine instance. |
| 89 * @param appInfo Information about the app. | 72 * @param appInfo Information about the app. |
| 90 * @return New `JsEngine` instance. | 73 * @return New `JsEngine` instance. |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 218 | 201 |
| 219 /** | 202 /** |
| 220 * Sets the `LogSystem` implementation used for logging (e.g. to handle | 203 * Sets the `LogSystem` implementation used for logging (e.g. to handle |
| 221 * `console.log()` calls from JavaScript). | 204 * `console.log()` calls from JavaScript). |
| 222 * Setting this is optional, the engine will use a `DefaultLogSystem` | 205 * Setting this is optional, the engine will use a `DefaultLogSystem` |
| 223 * instance by default, which might be sufficient. | 206 * instance by default, which might be sufficient. |
| 224 * @param The `LogSystem` instance to use. | 207 * @param The `LogSystem` instance to use. |
| 225 */ | 208 */ |
| 226 void SetLogSystem(LogSystemPtr val); | 209 void SetLogSystem(LogSystemPtr val); |
| 227 | 210 |
| 211 /** |
| 212 * Sets a global property that can be accessed by all the scripts. |
| 213 * @param name Name of the property to set. |
| 214 * @param value Value of the property to set. |
| 215 */ |
| 216 void SetGlobalProperty(const std::string& name, AdblockPlus::JsValuePtr valu
e); |
| 217 |
| 228 private: | 218 private: |
| 229 JsEngine(); | 219 JsEngine(); |
| 220 |
| 230 FileSystemPtr fileSystem; | 221 FileSystemPtr fileSystem; |
| 231 WebRequestPtr webRequest; | 222 WebRequestPtr webRequest; |
| 232 LogSystemPtr logSystem; | 223 LogSystemPtr logSystem; |
| 224 v8::Isolate* isolate; |
| 233 std::unique_ptr<v8::Persistent<v8::Context>> context; | 225 std::unique_ptr<v8::Persistent<v8::Context>> context; |
| 234 EventMap eventCallbacks; | 226 EventMap eventCallbacks; |
| 227 JsValuePtr globalJsObject; |
| 235 }; | 228 }; |
| 236 } | 229 } |
| 237 | 230 |
| 238 #endif | 231 #endif |
| LEFT | RIGHT |