| 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 | |
| 98 /** | |
| 99 * Callback function returning false when current connection is not allowed | |
| 100 * e.g. because it is a metered connection. | |
| 101 */ | |
| 102 typedef std::function<bool()> IsConnectionAllowedCallback; | |
| 103 | 103 |
| 104 /** | 104 /** |
| 105 * Maps events to callback functions. | 105 * Maps events to callback functions. |
| 106 */ | 106 */ |
| 107 typedef std::map<std::string, EventCallback> EventMap; | 107 typedef std::map<std::string, EventCallback> EventMap; |
| 108 | 108 |
| 109 /** | 109 /** |
| 110 * An opaque structure representing ID of stored JsValueList. |
| 111 * |
| 112 */ |
| 113 class JsWeakValuesID |
| 114 { |
| 115 friend class JsEngine; |
| 116 JsWeakValuesLists::const_iterator iterator; |
| 117 }; |
| 118 |
| 119 /** |
| 110 * Creates a new JavaScript engine instance. | 120 * Creates a new JavaScript engine instance. |
| 111 * @param appInfo Information about the app. | 121 * @param appInfo Information about the app. |
| 112 * @param timer Implementation of timer. | 122 * @param timer Implementation of timer. |
| 113 * @param isolate v8::Isolate wrapper. This parameter should be considered | 123 * @param isolate v8::Isolate wrapper. This parameter should be considered |
| 114 * as a temporary hack for tests, it will go away. Issue #3593. | 124 * as a temporary hack for tests, it will go away. Issue #3593. |
| 115 * @return New `JsEngine` instance. | 125 * @return New `JsEngine` instance. |
| 116 */ | 126 */ |
| 117 static JsEnginePtr New(const AppInfo& appInfo = AppInfo(), | 127 static JsEnginePtr New(const AppInfo& appInfo = AppInfo(), |
| 118 TimerPtr timer = CreateDefaultTimer(), | 128 TimerPtr timer = CreateDefaultTimer(), |
| 119 const ScopedV8IsolatePtr& isolate = ScopedV8IsolatePtr(new ScopedV8Isolate
())); | 129 const ScopedV8IsolatePtr& isolate = ScopedV8IsolatePtr(new ScopedV8Isolate
())); |
| 120 | 130 |
| 121 /** | 131 /** |
| 122 * Registers the callback function for an event. | 132 * Registers the callback function for an event. |
| 123 * @param eventName Event name. Note that this can be any string - it's a | 133 * @param eventName Event name. Note that this can be any string - it's a |
| 124 * general purpose event handling mechanism. | 134 * general purpose event handling mechanism. |
| 125 * @param callback Event callback function. | 135 * @param callback Event callback function. |
| 126 */ | 136 */ |
| 127 void SetEventCallback(const std::string& eventName, EventCallback callback); | 137 void SetEventCallback(const std::string& eventName, const EventCallback& cal
lback); |
| 128 | 138 |
| 129 /** | 139 /** |
| 130 * Removes the callback function for an event. | 140 * Removes the callback function for an event. |
| 131 * @param eventName Event name. | 141 * @param eventName Event name. |
| 132 */ | 142 */ |
| 133 void RemoveEventCallback(const std::string& eventName); | 143 void RemoveEventCallback(const std::string& eventName); |
| 134 | 144 |
| 135 /** | 145 /** |
| 136 * Triggers an event. | 146 * Triggers an event. |
| 137 * @param eventName Event name. | 147 * @param eventName Event name. |
| 138 * @param params Event parameters. | 148 * @param params Event parameters. |
| 139 */ | 149 */ |
| 140 void TriggerEvent(const std::string& eventName, JsValueList& params); | 150 void TriggerEvent(const std::string& eventName, JsValueList&& params); |
| 141 | 151 |
| 142 /** | 152 /** |
| 143 * Evaluates a JavaScript expression. | 153 * Evaluates a JavaScript expression. |
| 144 * @param source JavaScript expression to evaluate. | 154 * @param source JavaScript expression to evaluate. |
| 145 * @param filename Optional file name for the expression, used in error | 155 * @param filename Optional file name for the expression, used in error |
| 146 * messages. | 156 * messages. |
| 147 * @return Result of the evaluated expression. | 157 * @return Result of the evaluated expression. |
| 148 */ | 158 */ |
| 149 JsValuePtr Evaluate(const std::string& source, | 159 JsValue Evaluate(const std::string& source, |
| 150 const std::string& filename = ""); | 160 const std::string& filename = ""); |
| 151 | 161 |
| 152 /** | 162 /** |
| 153 * Initiates a garbage collection. | 163 * Initiates a garbage collection. |
| 154 */ | 164 */ |
| 155 void Gc(); | 165 void Gc(); |
| 156 | 166 |
| 157 //@{ | 167 //@{ |
| 158 /** | 168 /** |
| 159 * Creates a new JavaScript value. | 169 * Creates a new JavaScript value. |
| 160 * @param val Value to convert. | 170 * @param val Value to convert. |
| 161 * @return New `JsValue` instance. | 171 * @return New `JsValue` instance. |
| 162 */ | 172 */ |
| 163 JsValuePtr NewValue(const std::string& val); | 173 JsValue NewValue(const std::string& val); |
| 164 JsValuePtr NewValue(int64_t val); | 174 JsValue NewValue(int64_t val); |
| 165 JsValuePtr NewValue(bool val); | 175 JsValue NewValue(bool val); |
| 166 inline JsValuePtr NewValue(const char* val) | 176 inline JsValue NewValue(const char* val) |
| 167 { | 177 { |
| 168 return NewValue(std::string(val)); | 178 return NewValue(std::string(val)); |
| 169 } | 179 } |
| 170 inline JsValuePtr NewValue(int val) | 180 inline JsValue NewValue(int val) |
| 171 { | 181 { |
| 172 return NewValue(static_cast<int64_t>(val)); | 182 return NewValue(static_cast<int64_t>(val)); |
| 173 } | 183 } |
| 174 #ifdef __APPLE__ | 184 #ifdef __APPLE__ |
| 175 inline JsValuePtr NewValue(long val) | 185 inline JsValue NewValue(long val) |
| 176 { | 186 { |
| 177 return NewValue(static_cast<int64_t>(val)); | 187 return NewValue(static_cast<int64_t>(val)); |
| 178 } | 188 } |
| 179 #endif | 189 #endif |
| 180 //@} | 190 //@} |
| 181 | 191 |
| 182 /** | 192 /** |
| 183 * Creates a new JavaScript object. | 193 * Creates a new JavaScript object. |
| 184 * @return New `JsValue` instance. | 194 * @return New `JsValue` instance. |
| 185 */ | 195 */ |
| 186 JsValuePtr NewObject(); | 196 JsValue NewObject(); |
| 187 | 197 |
| 188 /** | 198 /** |
| 189 * Creates a JavaScript function that invokes a C++ callback. | 199 * Creates a JavaScript function that invokes a C++ callback. |
| 190 * @param callback C++ callback to invoke. The callback receives a | 200 * @param callback C++ callback to invoke. The callback receives a |
| 191 * `v8::Arguments` object and can use `FromArguments()` to retrieve | 201 * `v8::Arguments` object and can use `FromArguments()` to retrieve |
| 192 * the current `JsEngine`. | 202 * the current `JsEngine`. |
| 193 * @return New `JsValue` instance. | 203 * @return New `JsValue` instance. |
| 194 */ | 204 */ |
| 195 JsValuePtr NewCallback(v8::InvocationCallback callback); | 205 JsValue NewCallback(const v8::InvocationCallback& callback); |
| 196 | 206 |
| 197 /** | 207 /** |
| 198 * Returns a `JsEngine` instance contained in a `v8::Arguments` object. | 208 * Returns a `JsEngine` instance contained in a `v8::Arguments` object. |
| 199 * Use this in callbacks created via `NewCallback()` to retrieve the current | 209 * Use this in callbacks created via `NewCallback()` to retrieve the current |
| 200 * `JsEngine`. | 210 * `JsEngine`. |
| 201 * @param arguments `v8::Arguments` object containing the `JsEngine` | 211 * @param arguments `v8::Arguments` object containing the `JsEngine` |
| 202 * instance. | 212 * instance. |
| 203 * @return `JsEngine` instance from `v8::Arguments`. | 213 * @return `JsEngine` instance from `v8::Arguments`. |
| 204 */ | 214 */ |
| 205 static JsEnginePtr FromArguments(const v8::Arguments& arguments); | 215 static JsEnginePtr FromArguments(const v8::Arguments& arguments); |
| 206 | 216 |
| 217 /** |
| 218 * Stores `JsValue`s in a way they don't keep a strong reference to |
| 219 * `JsEngine` and which are destroyed when `JsEngine` is destroyed. These |
| 220 * methods should be used when one needs to carry a JsValue in a callback |
| 221 * directly or indirectly passed to `JsEngine`. |
| 222 * The method is thread-safe. |
| 223 * @param `JsValueList` to store. |
| 224 * @return `JsWeakValuesID` of stored values which allows to restore them |
| 225 * later. |
| 226 */ |
| 227 JsWeakValuesID StoreJsValues(const JsValueList& values); |
| 228 |
| 229 /** |
| 230 * Extracts and removes from `JsEngine` earlier stored `JsValue`s. |
| 231 * The method is thread-safe. |
| 232 * @param id `JsWeakValuesID` of values. |
| 233 * @return `JsValueList` of stored values. |
| 234 */ |
| 235 JsValueList TakeJsValues(const JsWeakValuesID& id); |
| 236 |
| 207 /* | 237 /* |
| 208 * Private functionality required to implement timers. | 238 * Private functionality required to implement timers. |
| 209 * @param arguments `v8::Arguments` is the arguments received in C++ | 239 * @param arguments `v8::Arguments` is the arguments received in C++ |
| 210 * callback associated for global setTimeout method. | 240 * callback associated for global setTimeout method. |
| 211 */ | 241 */ |
| 212 static void ScheduleTimer(const v8::Arguments& arguments); | 242 static void ScheduleTimer(const v8::Arguments& arguments); |
| 213 | 243 |
| 214 /** | 244 /** |
| 215 * Converts v8 arguments to `JsValue` objects. | 245 * Converts v8 arguments to `JsValue` objects. |
| 216 * @param arguments `v8::Arguments` object containing the arguments to | 246 * @param arguments `v8::Arguments` object containing the arguments to |
| 217 * convert. | 247 * convert. |
| 218 * @return List of arguments converted to `JsValue` objects. | 248 * @return List of arguments converted to `const JsValue` objects. |
| 219 */ | 249 */ |
| 220 JsValueList ConvertArguments(const v8::Arguments& arguments); | 250 JsValueList ConvertArguments(const v8::Arguments& arguments); |
| 221 | 251 |
| 222 /** | 252 /** |
| 223 * @see `SetFileSystem()`. | 253 * @see `SetFileSystem()`. |
| 224 */ | 254 */ |
| 225 FileSystemPtr GetFileSystem(); | 255 FileSystemPtr GetFileSystem() const; |
| 226 | 256 |
| 227 /** | 257 /** |
| 228 * Sets the `FileSystem` implementation used for all file I/O. | 258 * Sets the `FileSystem` implementation used for all file I/O. |
| 229 * Setting this is optional, the engine will use a `DefaultFileSystem` | 259 * Setting this is optional, the engine will use a `DefaultFileSystem` |
| 230 * instance by default, which might be sufficient. | 260 * instance by default, which might be sufficient. |
| 231 * @param The `FileSystem` instance to use. | 261 * @param The `FileSystem` instance to use. |
| 232 */ | 262 */ |
| 233 void SetFileSystem(FileSystemPtr val); | 263 void SetFileSystem(const FileSystemPtr& val); |
| 234 | 264 |
| 235 /** | 265 /** |
| 236 * @see `SetWebRequest()`. | 266 * @see `SetWebRequest()`. |
| 237 */ | 267 */ |
| 238 WebRequestPtr GetWebRequest(); | 268 WebRequestPtr GetWebRequest() const; |
| 239 | 269 |
| 240 /** | 270 /** |
| 241 * Sets the `WebRequest` implementation used for XMLHttpRequests. | 271 * Sets the `WebRequest` implementation used for XMLHttpRequests. |
| 242 * Setting this is optional, the engine will use a `DefaultWebRequest` | 272 * Setting this is optional, the engine will use a `DefaultWebRequest` |
| 243 * instance by default, which might be sufficient. | 273 * instance by default, which might be sufficient. |
| 244 * @param The `WebRequest` instance to use. | 274 * @param The `WebRequest` instance to use. |
| 245 */ | 275 */ |
| 246 void SetWebRequest(WebRequestPtr val); | 276 void SetWebRequest(const WebRequestPtr& val); |
| 247 | |
| 248 /** | |
| 249 * Registers the callback function to check whether current connection is | |
| 250 * allowed for network requests. | |
| 251 * @param callback callback function. | |
| 252 */ | |
| 253 void SetIsConnectionAllowedCallback(const IsConnectionAllowedCallback& callb
ack); | |
| 254 | |
| 255 /** | |
| 256 * Checks whether current connection is allowed. If | |
| 257 * IsConnectionAllowedCallback is not set then then it returns true. | |
| 258 */ | |
| 259 bool IsConnectionAllowed(); | |
| 260 | 277 |
| 261 /** | 278 /** |
| 262 * @see `SetLogSystem()`. | 279 * @see `SetLogSystem()`. |
| 263 */ | 280 */ |
| 264 LogSystemPtr GetLogSystem(); | 281 LogSystemPtr GetLogSystem() const; |
| 265 | 282 |
| 266 /** | 283 /** |
| 267 * Sets the `LogSystem` implementation used for logging (e.g. to handle | 284 * Sets the `LogSystem` implementation used for logging (e.g. to handle |
| 268 * `console.log()` calls from JavaScript). | 285 * `console.log()` calls from JavaScript). |
| 269 * Setting this is optional, the engine will use a `DefaultLogSystem` | 286 * Setting this is optional, the engine will use a `DefaultLogSystem` |
| 270 * instance by default, which might be sufficient. | 287 * instance by default, which might be sufficient. |
| 271 * @param The `LogSystem` instance to use. | 288 * @param The `LogSystem` instance to use. |
| 272 */ | 289 */ |
| 273 void SetLogSystem(LogSystemPtr val); | 290 void SetLogSystem(const LogSystemPtr& val); |
| 274 | 291 |
| 275 /** | 292 /** |
| 276 * Sets a global property that can be accessed by all the scripts. | 293 * Sets a global property that can be accessed by all the scripts. |
| 277 * @param name Name of the property to set. | 294 * @param name Name of the property to set. |
| 278 * @param value Value of the property to set. | 295 * @param value Value of the property to set. |
| 279 */ | 296 */ |
| 280 void SetGlobalProperty(const std::string& name, AdblockPlus::JsValuePtr valu
e); | 297 void SetGlobalProperty(const std::string& name, const AdblockPlus::JsValue&
value); |
| 281 | 298 |
| 282 /** | 299 /** |
| 283 * Returns a pointer to associated v8::Isolate. | 300 * Returns a pointer to associated v8::Isolate. |
| 284 */ | 301 */ |
| 285 v8::Isolate* GetIsolate() | 302 v8::Isolate* GetIsolate() |
| 286 { | 303 { |
| 287 return isolate->Get(); | 304 return isolate->Get(); |
| 288 } | 305 } |
| 289 | 306 |
| 290 private: | 307 private: |
| 291 struct TimerTask | 308 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(TimerTasks::const_iterator timerTaskIterator); | |
| 298 | 309 |
| 299 explicit JsEngine(const ScopedV8IsolatePtr& isolate, TimerPtr timer); | 310 explicit JsEngine(const ScopedV8IsolatePtr& isolate, TimerPtr timer); |
| 300 | 311 |
| 301 JsValuePtr GetGlobalObject(); | 312 JsValue GetGlobalObject(); |
| 302 | 313 |
| 303 /// Isolate must be disposed only after disposing of all objects which are | 314 /// Isolate must be disposed only after disposing of all objects which are |
| 304 /// using it. | 315 /// using it. |
| 305 ScopedV8IsolatePtr isolate; | 316 ScopedV8IsolatePtr isolate; |
| 306 | 317 |
| 307 FileSystemPtr fileSystem; | 318 FileSystemPtr fileSystem; |
| 308 WebRequestPtr webRequest; | 319 WebRequestPtr webRequest; |
| 309 LogSystemPtr logSystem; | 320 LogSystemPtr logSystem; |
| 310 std::unique_ptr<v8::Persistent<v8::Context>> context; | 321 std::unique_ptr<v8::Persistent<v8::Context>> context; |
| 311 EventMap eventCallbacks; | 322 EventMap eventCallbacks; |
| 312 std::mutex eventCallbacksMutex; | 323 std::mutex eventCallbacksMutex; |
| 313 std::mutex isConnectionAllowedMutex; | 324 JsWeakValuesLists jsWeakValuesLists; |
| 314 IsConnectionAllowedCallback isConnectionAllowed; | 325 std::mutex jsWeakValuesListsMutex; |
| 315 TimerTasks timerTasks; | |
| 316 TimerPtr timer; | 326 TimerPtr timer; |
| 317 }; | 327 }; |
| 318 } | 328 } |
| 319 | 329 |
| 320 #endif | 330 #endif |
| OLD | NEW |