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-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 <functional> |
22 #include <map> | 22 #include <map> |
23 #include <stdexcept> | 23 #include <stdexcept> |
24 #include <stdint.h> | 24 #include <stdint.h> |
25 #include <string> | 25 #include <string> |
26 #include <AdblockPlus/AppInfo.h> | 26 #include <AdblockPlus/AppInfo.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 | 31 |
32 #include "V8ValueHolder.h" | |
33 | |
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::shared_ptr<JsEngine> JsEnginePtr; | 49 typedef std::shared_ptr<JsEngine> JsEnginePtr; |
52 | 50 |
53 /** | 51 /** |
| 52 * Scope based isolate manager. Creates a new isolate instance on |
| 53 * constructing and disposes it on destructing. |
| 54 */ |
| 55 class ScopedV8Isolate |
| 56 { |
| 57 public: |
| 58 ScopedV8Isolate(); |
| 59 ~ScopedV8Isolate(); |
| 60 v8::Isolate* Get() |
| 61 { |
| 62 return isolate; |
| 63 } |
| 64 private: |
| 65 ScopedV8Isolate(const ScopedV8Isolate&); |
| 66 ScopedV8Isolate& operator=(const ScopedV8Isolate&); |
| 67 |
| 68 v8::Isolate* isolate; |
| 69 }; |
| 70 |
| 71 /** |
| 72 * Shared smart pointer to ScopedV8Isolate instance; |
| 73 */ |
| 74 typedef std::shared_ptr<ScopedV8Isolate> ScopedV8IsolatePtr; |
| 75 |
| 76 /** |
54 * JavaScript engine used by `FilterEngine`, wraps v8. | 77 * JavaScript engine used by `FilterEngine`, wraps v8. |
55 */ | 78 */ |
56 class JsEngine : public std::enable_shared_from_this<JsEngine> | 79 class JsEngine : public std::enable_shared_from_this<JsEngine> |
57 { | 80 { |
58 friend class JsValue; | 81 friend class JsValue; |
59 friend class JsContext; | 82 friend class JsContext; |
60 | 83 |
61 public: | 84 public: |
62 /** | 85 /** |
63 * Event callback function. | 86 * Event callback function. |
64 */ | 87 */ |
65 typedef std::function<void(JsValueList& params)> EventCallback; | 88 typedef std::function<void(JsValueList& params)> EventCallback; |
66 | 89 |
67 /** | 90 /** |
68 * Maps events to callback functions. | 91 * Maps events to callback functions. |
69 */ | 92 */ |
70 typedef std::map<std::string, EventCallback> EventMap; | 93 typedef std::map<std::string, EventCallback> EventMap; |
71 | 94 |
72 /** | 95 /** |
73 * Creates a new JavaScript engine instance. | 96 * Creates a new JavaScript engine instance. |
74 * @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. |
75 * @return New `JsEngine` instance. | 100 * @return New `JsEngine` instance. |
76 */ | 101 */ |
77 static JsEnginePtr New(const AppInfo& appInfo = AppInfo()); | 102 static JsEnginePtr New(const AppInfo& appInfo = AppInfo(), const ScopedV8Iso
latePtr& isolate = ScopedV8IsolatePtr(new ScopedV8Isolate())); |
78 | 103 |
79 /** | 104 /** |
80 * Registers the callback function for an event. | 105 * Registers the callback function for an event. |
81 * @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 |
82 * general purpose event handling mechanism. | 107 * general purpose event handling mechanism. |
83 * @param callback Event callback function. | 108 * @param callback Event callback function. |
84 */ | 109 */ |
85 void SetEventCallback(const std::string& eventName, EventCallback callback); | 110 void SetEventCallback(const std::string& eventName, EventCallback callback); |
86 | 111 |
87 /** | 112 /** |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
210 */ | 235 */ |
211 void SetLogSystem(LogSystemPtr val); | 236 void SetLogSystem(LogSystemPtr val); |
212 | 237 |
213 /** | 238 /** |
214 * Sets a global property that can be accessed by all the scripts. | 239 * Sets a global property that can be accessed by all the scripts. |
215 * @param name Name of the property to set. | 240 * @param name Name of the property to set. |
216 * @param value Value of the property to set. | 241 * @param value Value of the property to set. |
217 */ | 242 */ |
218 void SetGlobalProperty(const std::string& name, AdblockPlus::JsValuePtr valu
e); | 243 void SetGlobalProperty(const std::string& name, AdblockPlus::JsValuePtr valu
e); |
219 | 244 |
| 245 /** |
| 246 * Returns a pointer to associated v8::Isolate. |
| 247 */ |
| 248 v8::Isolate* GetIsolate() |
| 249 { |
| 250 return isolate->Get(); |
| 251 } |
| 252 |
220 private: | 253 private: |
221 JsEngine(); | 254 explicit JsEngine(const ScopedV8IsolatePtr& isolate); |
| 255 |
| 256 JsValuePtr GetGlobalObject(); |
| 257 |
| 258 /// Isolate must be disposed only after disposing of all objects which are |
| 259 /// using it. |
| 260 ScopedV8IsolatePtr isolate; |
222 | 261 |
223 FileSystemPtr fileSystem; | 262 FileSystemPtr fileSystem; |
224 WebRequestPtr webRequest; | 263 WebRequestPtr webRequest; |
225 LogSystemPtr logSystem; | 264 LogSystemPtr logSystem; |
226 v8::Isolate* isolate; | 265 std::unique_ptr<v8::Persistent<v8::Context>> context; |
227 V8ValueHolder<v8::Context> context; | |
228 EventMap eventCallbacks; | 266 EventMap eventCallbacks; |
229 JsValuePtr globalJsObject; | |
230 }; | 267 }; |
231 } | 268 } |
232 | 269 |
233 #endif | 270 #endif |
OLD | NEW |