| 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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 { | 46 { |
| 47 class JsEngine; | 47 class JsEngine; |
| 48 class Platform; | 48 class Platform; |
| 49 | 49 |
| 50 /** | 50 /** |
| 51 * Shared smart pointer to a `JsEngine` instance. | 51 * Shared smart pointer to a `JsEngine` instance. |
| 52 */ | 52 */ |
| 53 typedef std::shared_ptr<JsEngine> JsEnginePtr; | 53 typedef std::shared_ptr<JsEngine> JsEnginePtr; |
| 54 | 54 |
| 55 /** | 55 /** |
| 56 * Scope based isolate manager. Creates a new isolate instance on | 56 * Provides with isolate. The main aim of this iterface is to delegate a |
| 57 * constructing and disposes it on destructing. | 57 * proper initialization and deinitialization of v8::Isolate to an embedder. |
| 58 */ | 58 */ |
| 59 class ScopedV8Isolate | 59 struct IV8IsolateProvider |
| 60 { | 60 { |
| 61 public: | 61 virtual ~IV8IsolateProvider() {} |
| 62 ScopedV8Isolate(); | |
| 63 ~ScopedV8Isolate(); | |
| 64 v8::Isolate* Get() | |
| 65 { | |
| 66 return isolate; | |
| 67 } | |
| 68 private: | |
| 69 ScopedV8Isolate(const ScopedV8Isolate&); | |
| 70 ScopedV8Isolate& operator=(const ScopedV8Isolate&); | |
| 71 | 62 |
| 72 v8::Isolate* isolate; | 63 /** |
| 64 * Returns v8::Isolate. All subsequent calls of this method should return |
| 65 * the same pointer to v8::Isolate as the first call. |
| 66 */ |
| 67 virtual v8::Isolate* Get() = 0; |
| 73 }; | 68 }; |
| 74 | 69 |
| 75 /** | 70 /** |
| 76 * JavaScript engine used by `FilterEngine`, wraps v8. | 71 * JavaScript engine used by `FilterEngine`, wraps v8. |
| 77 */ | 72 */ |
| 78 class JsEngine : public std::enable_shared_from_this<JsEngine> | 73 class JsEngine : public std::enable_shared_from_this<JsEngine> |
| 79 { | 74 { |
| 80 friend class JsValue; | 75 friend class JsValue; |
| 81 friend class JsContext; | 76 friend class JsContext; |
| 82 | 77 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 105 friend class JsEngine; | 100 friend class JsEngine; |
| 106 JsWeakValuesLists::const_iterator iterator; | 101 JsWeakValuesLists::const_iterator iterator; |
| 107 }; | 102 }; |
| 108 | 103 |
| 109 /** | 104 /** |
| 110 * Creates a new JavaScript engine instance. | 105 * Creates a new JavaScript engine instance. |
| 111 * | 106 * |
| 112 * @param appInfo Information about the app. | 107 * @param appInfo Information about the app. |
| 113 * @param platform AdblockPlus platform providing with necessary | 108 * @param platform AdblockPlus platform providing with necessary |
| 114 * dependencies. | 109 * dependencies. |
| 110 * @param isolate A provider of v8::Isolate, if the value is nullptr then |
| 111 * a default implementation is used. |
| 115 * @return New `JsEngine` instance. | 112 * @return New `JsEngine` instance. |
| 116 */ | 113 */ |
| 117 static JsEnginePtr New(const AppInfo& appInfo, Platform& platform); | 114 static JsEnginePtr New(const AppInfo& appInfo, Platform& platform, std::uniq
ue_ptr<IV8IsolateProvider> isolate = nullptr); |
| 118 /** | 115 /** |
| 119 * Registers the callback function for an event. | 116 * Registers the callback function for an event. |
| 120 * @param eventName Event name. Note that this can be any string - it's a | 117 * @param eventName Event name. Note that this can be any string - it's a |
| 121 * general purpose event handling mechanism. | 118 * general purpose event handling mechanism. |
| 122 * @param callback Event callback function. | 119 * @param callback Event callback function. |
| 123 */ | 120 */ |
| 124 void SetEventCallback(const std::string& eventName, const EventCallback& cal
lback); | 121 void SetEventCallback(const std::string& eventName, const EventCallback& cal
lback); |
| 125 | 122 |
| 126 /** | 123 /** |
| 127 * Removes the callback function for an event. | 124 * Removes the callback function for an event. |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 248 * @param name Name of the property to set. | 245 * @param name Name of the property to set. |
| 249 * @param value Value of the property to set. | 246 * @param value Value of the property to set. |
| 250 */ | 247 */ |
| 251 void SetGlobalProperty(const std::string& name, const AdblockPlus::JsValue&
value); | 248 void SetGlobalProperty(const std::string& name, const AdblockPlus::JsValue&
value); |
| 252 | 249 |
| 253 /** | 250 /** |
| 254 * Returns a pointer to associated v8::Isolate. | 251 * Returns a pointer to associated v8::Isolate. |
| 255 */ | 252 */ |
| 256 v8::Isolate* GetIsolate() | 253 v8::Isolate* GetIsolate() |
| 257 { | 254 { |
| 258 return isolate.Get(); | 255 return isolate->Get(); |
| 259 } | 256 } |
| 260 | 257 |
| 261 /** | 258 /** |
| 262 * Notifies JS engine about critically low memory what should cause a | 259 * Notifies JS engine about critically low memory what should cause a |
| 263 * garbage collection. | 260 * garbage collection. |
| 264 */ | 261 */ |
| 265 void NotifyLowMemory(); | 262 void NotifyLowMemory(); |
| 266 | 263 |
| 267 /** | 264 /** |
| 268 * Private functionality. | 265 * Private functionality. |
| 269 */ | 266 */ |
| 270 Platform& GetPlatform() | 267 Platform& GetPlatform() |
| 271 { | 268 { |
| 272 return platform; | 269 return platform; |
| 273 } | 270 } |
| 274 private: | 271 private: |
| 275 void CallTimerTask(const JsWeakValuesID& timerParamsID); | 272 void CallTimerTask(const JsWeakValuesID& timerParamsID); |
| 276 | 273 |
| 277 explicit JsEngine(Platform& platform); | 274 explicit JsEngine(Platform& platform, std::unique_ptr<IV8IsolateProvider> is
olate); |
| 278 | 275 |
| 279 JsValue GetGlobalObject(); | 276 JsValue GetGlobalObject(); |
| 280 | 277 |
| 278 Platform& platform; |
| 281 /// Isolate must be disposed only after disposing of all objects which are | 279 /// Isolate must be disposed only after disposing of all objects which are |
| 282 /// using it. | 280 /// using it. |
| 283 ScopedV8Isolate isolate; | 281 std::unique_ptr<IV8IsolateProvider> isolate; |
| 284 Platform& platform; | |
| 285 | 282 |
| 286 std::unique_ptr<v8::Global<v8::Context>> context; | 283 std::unique_ptr<v8::Global<v8::Context>> context; |
| 287 EventMap eventCallbacks; | 284 EventMap eventCallbacks; |
| 288 std::mutex eventCallbacksMutex; | 285 std::mutex eventCallbacksMutex; |
| 289 JsWeakValuesLists jsWeakValuesLists; | 286 JsWeakValuesLists jsWeakValuesLists; |
| 290 std::mutex jsWeakValuesListsMutex; | 287 std::mutex jsWeakValuesListsMutex; |
| 291 }; | 288 }; |
| 292 } | 289 } |
| 293 | 290 |
| 294 #endif | 291 #endif |
| OLD | NEW |