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