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-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 typedef std::shared_ptr<ScopedV8Isolate> ScopedV8IsolatePtr; | 82 typedef std::shared_ptr<ScopedV8Isolate> ScopedV8IsolatePtr; |
83 | 83 |
84 /** | 84 /** |
85 * JavaScript engine used by `FilterEngine`, wraps v8. | 85 * JavaScript engine used by `FilterEngine`, wraps v8. |
86 */ | 86 */ |
87 class JsEngine : public std::enable_shared_from_this<JsEngine> | 87 class JsEngine : public std::enable_shared_from_this<JsEngine> |
88 { | 88 { |
89 friend class JsValue; | 89 friend class JsValue; |
90 friend class JsContext; | 90 friend class JsContext; |
91 | 91 |
| 92 struct JsWeakValuesList |
| 93 { |
| 94 ~JsWeakValuesList(); |
| 95 std::vector<std::unique_ptr<v8::Persistent<v8::Value>>> values; |
| 96 }; |
| 97 typedef std::list<JsWeakValuesList> JsWeakValuesLists; |
92 public: | 98 public: |
93 /** | 99 /** |
94 * Event callback function. | 100 * Event callback function. |
95 */ | 101 */ |
96 typedef std::function<void(JsValueList&& params)> EventCallback; | 102 typedef std::function<void(JsValueList&& params)> EventCallback; |
97 | 103 |
98 /** | 104 /** |
99 * Callback function returning false when current connection is not allowed | 105 * Callback function returning false when current connection is not allowed |
100 * e.g. because it is a metered connection. | 106 * e.g. because it is a metered connection. |
101 */ | 107 */ |
102 typedef std::function<bool()> IsConnectionAllowedCallback; | 108 typedef std::function<bool()> IsConnectionAllowedCallback; |
103 | 109 |
104 /** | 110 /** |
105 * Maps events to callback functions. | 111 * Maps events to callback functions. |
106 */ | 112 */ |
107 typedef std::map<std::string, EventCallback> EventMap; | 113 typedef std::map<std::string, EventCallback> EventMap; |
108 | 114 |
109 /** | 115 /** |
| 116 * An opaque structure representing ID of stored JsValueList. |
| 117 * |
| 118 */ |
| 119 class JsWeakValuesID |
| 120 { |
| 121 friend class JsEngine; |
| 122 JsWeakValuesLists::const_iterator iterator; |
| 123 }; |
| 124 |
| 125 /** |
110 * Creates a new JavaScript engine instance. | 126 * Creates a new JavaScript engine instance. |
111 * @param appInfo Information about the app. | 127 * @param appInfo Information about the app. |
112 * @param timer Implementation of timer. | 128 * @param timer Implementation of timer. |
113 * @param isolate v8::Isolate wrapper. This parameter should be considered | 129 * @param isolate v8::Isolate wrapper. This parameter should be considered |
114 * as a temporary hack for tests, it will go away. Issue #3593. | 130 * as a temporary hack for tests, it will go away. Issue #3593. |
115 * @return New `JsEngine` instance. | 131 * @return New `JsEngine` instance. |
116 */ | 132 */ |
117 static JsEnginePtr New(const AppInfo& appInfo = AppInfo(), | 133 static JsEnginePtr New(const AppInfo& appInfo = AppInfo(), |
118 TimerPtr timer = CreateDefaultTimer(), | 134 TimerPtr timer = CreateDefaultTimer(), |
119 const ScopedV8IsolatePtr& isolate = ScopedV8IsolatePtr(new ScopedV8Isolate
())); | 135 const ScopedV8IsolatePtr& isolate = ScopedV8IsolatePtr(new ScopedV8Isolate
())); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 /** | 213 /** |
198 * Returns a `JsEngine` instance contained in a `v8::Arguments` object. | 214 * Returns a `JsEngine` instance contained in a `v8::Arguments` object. |
199 * Use this in callbacks created via `NewCallback()` to retrieve the current | 215 * Use this in callbacks created via `NewCallback()` to retrieve the current |
200 * `JsEngine`. | 216 * `JsEngine`. |
201 * @param arguments `v8::Arguments` object containing the `JsEngine` | 217 * @param arguments `v8::Arguments` object containing the `JsEngine` |
202 * instance. | 218 * instance. |
203 * @return `JsEngine` instance from `v8::Arguments`. | 219 * @return `JsEngine` instance from `v8::Arguments`. |
204 */ | 220 */ |
205 static JsEnginePtr FromArguments(const v8::Arguments& arguments); | 221 static JsEnginePtr FromArguments(const v8::Arguments& arguments); |
206 | 222 |
| 223 /** |
| 224 * Stores `JsValue`s in a way they don't keep a strong reference to |
| 225 * `JsEngine` and which are destroyed when `JsEngine` is destroyed. These |
| 226 * methods should be used when one needs to carry a JsValue in a callback |
| 227 * directly or indirectly passed to `JsEngine`. |
| 228 * The method is thread-safe. |
| 229 * @param `JsValueList` to store. |
| 230 * @return `JsWeakValuesID` of stored values which allows to restore them |
| 231 * later. |
| 232 */ |
| 233 JsWeakValuesID StoreJsValues(const JsValueList& values); |
| 234 /** |
| 235 * Extracts and removes from `JsEngine` earlier stored `JsValue`s. |
| 236 * The method is thread-safe. |
| 237 * @param id `JsWeakValuesID` of values. |
| 238 * @return `JsValueList` of stored values. |
| 239 */ |
| 240 JsValueList TakeJsValues(const JsWeakValuesID& id); |
| 241 |
207 /* | 242 /* |
208 * Private functionality required to implement timers. | 243 * Private functionality required to implement timers. |
209 * @param arguments `v8::Arguments` is the arguments received in C++ | 244 * @param arguments `v8::Arguments` is the arguments received in C++ |
210 * callback associated for global setTimeout method. | 245 * callback associated for global setTimeout method. |
211 */ | 246 */ |
212 static void ScheduleTimer(const v8::Arguments& arguments); | 247 static void ScheduleTimer(const v8::Arguments& arguments); |
213 | 248 |
214 /** | 249 /** |
215 * Converts v8 arguments to `JsValue` objects. | 250 * Converts v8 arguments to `JsValue` objects. |
216 * @param arguments `v8::Arguments` object containing the arguments to | 251 * @param arguments `v8::Arguments` object containing the arguments to |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 | 316 |
282 /** | 317 /** |
283 * Returns a pointer to associated v8::Isolate. | 318 * Returns a pointer to associated v8::Isolate. |
284 */ | 319 */ |
285 v8::Isolate* GetIsolate() | 320 v8::Isolate* GetIsolate() |
286 { | 321 { |
287 return isolate->Get(); | 322 return isolate->Get(); |
288 } | 323 } |
289 | 324 |
290 private: | 325 private: |
291 struct TimerTask | 326 void CallTimerTask(const JsWeakValuesID& timerParamsID); |
292 { | |
293 ~TimerTask(); | |
294 std::vector<std::unique_ptr<v8::Persistent<v8::Value>>> arguments; | |
295 }; | |
296 typedef std::list<TimerTask> TimerTasks; | |
297 void CallTimerTask(const TimerTasks::const_iterator& timerTaskIterator); | |
298 | 327 |
299 explicit JsEngine(const ScopedV8IsolatePtr& isolate, TimerPtr timer); | 328 explicit JsEngine(const ScopedV8IsolatePtr& isolate, TimerPtr timer); |
300 | 329 |
301 JsValue GetGlobalObject(); | 330 JsValue GetGlobalObject(); |
302 | 331 |
303 /// Isolate must be disposed only after disposing of all objects which are | 332 /// Isolate must be disposed only after disposing of all objects which are |
304 /// using it. | 333 /// using it. |
305 ScopedV8IsolatePtr isolate; | 334 ScopedV8IsolatePtr isolate; |
306 | 335 |
307 FileSystemPtr fileSystem; | 336 FileSystemPtr fileSystem; |
308 WebRequestPtr webRequest; | 337 WebRequestPtr webRequest; |
309 LogSystemPtr logSystem; | 338 LogSystemPtr logSystem; |
310 std::unique_ptr<v8::Persistent<v8::Context>> context; | 339 std::unique_ptr<v8::Persistent<v8::Context>> context; |
311 EventMap eventCallbacks; | 340 EventMap eventCallbacks; |
312 std::mutex eventCallbacksMutex; | 341 std::mutex eventCallbacksMutex; |
313 mutable std::mutex isConnectionAllowedMutex; | 342 mutable std::mutex isConnectionAllowedMutex; |
314 IsConnectionAllowedCallback isConnectionAllowed; | 343 IsConnectionAllowedCallback isConnectionAllowed; |
315 TimerTasks timerTasks; | 344 JsWeakValuesLists jsWeakValuesLists; |
| 345 std::mutex jsWeakValuesListsMutex; |
316 TimerPtr timer; | 346 TimerPtr timer; |
317 }; | 347 }; |
318 } | 348 } |
319 | 349 |
320 #endif | 350 #endif |
OLD | NEW |