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 template<typename T> class FunctionCallbackInfo; | 39 template<typename T> class FunctionCallbackInfo; |
42 typedef void(*FunctionCallback)(const FunctionCallbackInfo<v8::Value>& info); | 40 typedef void(*FunctionCallback)(const FunctionCallbackInfo<v8::Value>& info); |
43 } | 41 } |
44 | 42 |
45 namespace AdblockPlus | 43 namespace AdblockPlus |
46 { | 44 { |
47 class JsEngine; | 45 class JsEngine; |
48 | 46 |
49 /** | 47 /** |
50 * Shared smart pointer to a `JsEngine` instance. | 48 * Shared smart pointer to a `JsEngine` instance. |
51 */ | 49 */ |
52 typedef std::tr1::shared_ptr<JsEngine> JsEnginePtr; | 50 typedef std::shared_ptr<JsEngine> JsEnginePtr; |
53 | 51 |
54 /** | 52 /** |
55 * Scope based isolate manager. Creates a new isolate instance on | 53 * Scope based isolate manager. Creates a new isolate instance on |
56 * constructing and disposes it on destructing. | 54 * constructing and disposes it on destructing. |
57 */ | 55 */ |
58 class ScopedV8Isolate | 56 class ScopedV8Isolate |
59 { | 57 { |
60 public: | 58 public: |
61 ScopedV8Isolate(); | 59 ScopedV8Isolate(); |
62 ~ScopedV8Isolate(); | 60 ~ScopedV8Isolate(); |
63 protected: | 61 v8::Isolate* Get() |
| 62 { |
| 63 return isolate; |
| 64 } |
| 65 private: |
| 66 ScopedV8Isolate(const ScopedV8Isolate&); |
| 67 ScopedV8Isolate& operator=(const ScopedV8Isolate&); |
| 68 |
64 v8::Isolate* isolate; | 69 v8::Isolate* isolate; |
65 }; | 70 }; |
66 | 71 |
67 /** | 72 /** |
| 73 * Shared smart pointer to ScopedV8Isolate instance; |
| 74 */ |
| 75 typedef std::shared_ptr<ScopedV8Isolate> ScopedV8IsolatePtr; |
| 76 |
| 77 /** |
68 * JavaScript engine used by `FilterEngine`, wraps v8. | 78 * JavaScript engine used by `FilterEngine`, wraps v8. |
69 * | 79 */ |
70 * It's inherited from ScopedV8Isolate to have isolate disposed only after | 80 class JsEngine : public std::enable_shared_from_this<JsEngine> |
71 * disposing of all objects which are using it. | |
72 */ | |
73 class JsEngine : public std::tr1::enable_shared_from_this<JsEngine>, protected
ScopedV8Isolate | |
74 { | 81 { |
75 friend class JsValue; | 82 friend class JsValue; |
76 friend class JsContext; | 83 friend class JsContext; |
| 84 |
77 public: | 85 public: |
78 /** | 86 /** |
79 * Event callback function. | 87 * Event callback function. |
80 */ | 88 */ |
81 typedef std::tr1::function<void(JsValueList& params)> EventCallback; | 89 typedef std::function<void(JsValueList& params)> EventCallback; |
82 | 90 |
83 /** | 91 /** |
84 * Maps events to callback functions. | 92 * Maps events to callback functions. |
85 */ | 93 */ |
86 typedef std::map<std::string, EventCallback> EventMap; | 94 typedef std::map<std::string, EventCallback> EventMap; |
87 | 95 |
88 /** | 96 /** |
89 * Creates a new JavaScript engine instance. | 97 * Creates a new JavaScript engine instance. |
90 * @param appInfo Information about the app. | 98 * @param appInfo Information about the app. |
| 99 * @param isolate v8::Isolate wrapper. This parameter should be considered |
| 100 * as a temporary hack for tests, it will go away. Issue #3593. |
91 * @return New `JsEngine` instance. | 101 * @return New `JsEngine` instance. |
92 */ | 102 */ |
93 static JsEnginePtr New(const AppInfo& appInfo = AppInfo()); | 103 static JsEnginePtr New(const AppInfo& appInfo = AppInfo(), const ScopedV8Iso
latePtr& isolate = ScopedV8IsolatePtr(new ScopedV8Isolate())); |
94 | 104 |
95 /** | 105 /** |
96 * Registers the callback function for an event. | 106 * Registers the callback function for an event. |
97 * @param eventName Event name. Note that this can be any string - it's a | 107 * @param eventName Event name. Note that this can be any string - it's a |
98 * general purpose event handling mechanism. | 108 * general purpose event handling mechanism. |
99 * @param callback Event callback function. | 109 * @param callback Event callback function. |
100 */ | 110 */ |
101 void SetEventCallback(const std::string& eventName, EventCallback callback); | 111 void SetEventCallback(const std::string& eventName, EventCallback callback); |
102 | 112 |
103 /** | 113 /** |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 | 229 |
220 /** | 230 /** |
221 * Sets the `LogSystem` implementation used for logging (e.g. to handle | 231 * Sets the `LogSystem` implementation used for logging (e.g. to handle |
222 * `console.log()` calls from JavaScript). | 232 * `console.log()` calls from JavaScript). |
223 * Setting this is optional, the engine will use a `DefaultLogSystem` | 233 * Setting this is optional, the engine will use a `DefaultLogSystem` |
224 * instance by default, which might be sufficient. | 234 * instance by default, which might be sufficient. |
225 * @param The `LogSystem` instance to use. | 235 * @param The `LogSystem` instance to use. |
226 */ | 236 */ |
227 void SetLogSystem(LogSystemPtr val); | 237 void SetLogSystem(LogSystemPtr val); |
228 | 238 |
| 239 /** |
| 240 * Sets a global property that can be accessed by all the scripts. |
| 241 * @param name Name of the property to set. |
| 242 * @param value Value of the property to set. |
| 243 */ |
| 244 void SetGlobalProperty(const std::string& name, AdblockPlus::JsValuePtr valu
e); |
| 245 |
| 246 /** |
| 247 * Returns a pointer to associated v8::Isolate. |
| 248 */ |
| 249 v8::Isolate* GetIsolate() |
| 250 { |
| 251 return isolate->Get(); |
| 252 } |
| 253 |
229 private: | 254 private: |
230 JsEngine(); | 255 explicit JsEngine(const ScopedV8IsolatePtr& isolate); |
| 256 |
| 257 /// Isolate must be disposed only after disposing of all objects which are |
| 258 /// using it. |
| 259 ScopedV8IsolatePtr isolate; |
| 260 |
231 FileSystemPtr fileSystem; | 261 FileSystemPtr fileSystem; |
232 WebRequestPtr webRequest; | 262 WebRequestPtr webRequest; |
233 LogSystemPtr logSystem; | 263 LogSystemPtr logSystem; |
234 std::unique_ptr<v8::UniquePersistent<v8::Context>> context; | 264 std::unique_ptr<v8::UniquePersistent<v8::Context>> context; |
235 EventMap eventCallbacks; | 265 EventMap eventCallbacks; |
| 266 JsValuePtr globalJsObject; |
236 }; | 267 }; |
237 } | 268 } |
238 | 269 |
239 #endif | 270 #endif |
LEFT | RIGHT |