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-2016 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 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 namespace AdblockPlus | 43 namespace AdblockPlus |
44 { | 44 { |
45 class JsEngine; | 45 class JsEngine; |
46 | 46 |
47 /** | 47 /** |
48 * Shared smart pointer to a `JsEngine` instance. | 48 * Shared smart pointer to a `JsEngine` instance. |
49 */ | 49 */ |
50 typedef std::shared_ptr<JsEngine> JsEnginePtr; | 50 typedef std::shared_ptr<JsEngine> JsEnginePtr; |
51 | 51 |
52 /** | 52 /** |
53 * Scope based isolate manager. Creates a new isolate instance on | |
54 * constructing and disposes it on destructing. | |
55 */ | |
56 class ScopedV8Isolate | |
57 { | |
58 public: | |
59 ScopedV8Isolate(); | |
60 ~ScopedV8Isolate(); | |
61 v8::Isolate* Get() | |
62 { | |
63 return isolate; | |
64 } | |
65 private: | |
66 ScopedV8Isolate(const ScopedV8Isolate&); | |
67 ScopedV8Isolate& operator=(const ScopedV8Isolate&); | |
68 | |
69 v8::Isolate* isolate; | |
70 }; | |
71 | |
72 /** | |
73 * Shared smart pointer to ScopedV8Isolate instance; | |
74 */ | |
75 typedef std::shared_ptr<ScopedV8Isolate> ScopedV8IsolatePtr; | |
76 | |
77 /** | |
78 * JavaScript engine used by `FilterEngine`, wraps v8. | 53 * JavaScript engine used by `FilterEngine`, wraps v8. |
79 */ | 54 */ |
80 class JsEngine : public std::enable_shared_from_this<JsEngine> | 55 class JsEngine : public std::enable_shared_from_this<JsEngine> |
81 { | 56 { |
82 public: | 57 public: |
83 /** | 58 /** |
84 * Event callback function. | 59 * Event callback function. |
85 */ | 60 */ |
86 typedef std::function<void(JsValueList& params)> EventCallback; | 61 typedef std::function<void(JsValueList& params)> EventCallback; |
87 | 62 |
88 /** | 63 /** |
89 * Maps events to callback functions. | 64 * Maps events to callback functions. |
90 */ | 65 */ |
91 typedef std::map<std::string, EventCallback> EventMap; | 66 typedef std::map<std::string, EventCallback> EventMap; |
92 | 67 |
93 /** | 68 /** |
94 * Creates a new JavaScript engine instance. | 69 * Creates a new JavaScript engine instance. |
95 * @param appInfo Information about the app. | 70 * @param appInfo Information about the app. |
96 * @param isolate v8::Isolate wrapper. This parameter should be considered | 71 * @param isolate v8::Isolate wrapper. This parameter should be considered |
97 * as a temporary hack for tests, it will go away. Issue #3593. | 72 * as a temporary hack for tests, it will go away. Issue #3593. |
98 * @return New `JsEngine` instance. | 73 * @return New `JsEngine` instance. |
99 */ | 74 */ |
100 static JsEnginePtr New(const AppInfo& appInfo = AppInfo(), const ScopedV8Iso
latePtr& isolate = ScopedV8IsolatePtr(new ScopedV8Isolate())); | 75 static JsEnginePtr New(const AppInfo& appInfo = AppInfo()); |
101 | 76 |
102 /** | 77 /** |
103 * Registers the callback function for an event. | 78 * Registers the callback function for an event. |
104 * @param eventName Event name. Note that this can be any string - it's a | 79 * @param eventName Event name. Note that this can be any string - it's a |
105 * general purpose event handling mechanism. | 80 * general purpose event handling mechanism. |
106 * @param callback Event callback function. | 81 * @param callback Event callback function. |
107 */ | 82 */ |
108 void SetEventCallback(const std::string& eventName, EventCallback callback); | 83 void SetEventCallback(const std::string& eventName, EventCallback callback); |
109 | 84 |
110 /** | 85 /** |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 /** | 211 /** |
237 * Sets a global property that can be accessed by all the scripts. | 212 * Sets a global property that can be accessed by all the scripts. |
238 * @param name Name of the property to set. | 213 * @param name Name of the property to set. |
239 * @param value Value of the property to set. | 214 * @param value Value of the property to set. |
240 */ | 215 */ |
241 void SetGlobalProperty(const std::string& name, AdblockPlus::JsValuePtr valu
e); | 216 void SetGlobalProperty(const std::string& name, AdblockPlus::JsValuePtr valu
e); |
242 | 217 |
243 /** | 218 /** |
244 * Returns a pointer to associated v8::Isolate. | 219 * Returns a pointer to associated v8::Isolate. |
245 */ | 220 */ |
246 v8::Isolate* GetIsolate() | 221 v8::Isolate* GetIsolate() const |
247 { | 222 { |
248 return isolate->Get(); | 223 return isolate; |
249 } | 224 } |
250 | 225 |
| 226 /// Ordinary destructor |
| 227 ~JsEngine(); |
| 228 |
251 protected: | 229 protected: |
252 explicit JsEngine(const ScopedV8IsolatePtr& isolate); | 230 /** |
| 231 * Non-public constructor enforces factory use. |
| 232 * |
| 233 * @param isolate |
| 234 * v8 isolate obtained from its factory |
| 235 */ |
| 236 explicit JsEngine(v8::Isolate* isolate); |
253 | 237 |
254 /** | 238 /** |
255 * Retrieve the global object as a JsValuePtr | 239 * Retrieve the global object as a JsValuePtr |
256 * | 240 * |
257 * \par Precondition | 241 * \par Precondition |
258 * - Requires a v8 execution scope already present | 242 * - Requires a v8 execution scope already present |
259 */ | 243 */ |
260 JsValuePtr GetGlobalObject(); | 244 JsValuePtr GetGlobalObject(); |
261 | 245 |
262 /// Isolate must be disposed only after disposing of all objects which are | 246 /** |
263 /// using it. | 247 * Isolate member must be have life span that encompasses any other object |
264 ScopedV8IsolatePtr isolate; | 248 * that uses it. Thus it's declared first so that it is constructed first |
| 249 * and destroyed last. |
| 250 */ |
| 251 v8::Isolate* isolate; |
265 | 252 |
266 FileSystemPtr fileSystem; | 253 FileSystemPtr fileSystem; |
267 WebRequestPtr webRequest; | 254 WebRequestPtr webRequest; |
268 LogSystemPtr logSystem; | 255 LogSystemPtr logSystem; |
269 EventMap eventCallbacks; | 256 EventMap eventCallbacks; |
270 }; | 257 }; |
271 } | 258 } |
272 | 259 |
273 #endif | 260 #endif |
OLD | NEW |