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-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 |
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 <list> |
23 #include <stdexcept> | 24 #include <stdexcept> |
24 #include <stdint.h> | 25 #include <stdint.h> |
25 #include <string> | 26 #include <string> |
| 27 #include <mutex> |
26 #include <AdblockPlus/AppInfo.h> | 28 #include <AdblockPlus/AppInfo.h> |
27 #include <AdblockPlus/LogSystem.h> | 29 #include <AdblockPlus/LogSystem.h> |
28 #include <AdblockPlus/FileSystem.h> | 30 #include <AdblockPlus/FileSystem.h> |
29 #include <AdblockPlus/JsValue.h> | 31 #include <AdblockPlus/JsValue.h> |
30 #include <AdblockPlus/WebRequest.h> | 32 #include <AdblockPlus/WebRequest.h> |
31 | 33 |
32 namespace v8 | 34 namespace v8 |
33 { | 35 { |
34 class Arguments; | 36 class Arguments; |
35 class Isolate; | 37 class Isolate; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 friend class JsValue; | 83 friend class JsValue; |
82 friend class JsContext; | 84 friend class JsContext; |
83 | 85 |
84 public: | 86 public: |
85 /** | 87 /** |
86 * Event callback function. | 88 * Event callback function. |
87 */ | 89 */ |
88 typedef std::function<void(JsValueList& params)> EventCallback; | 90 typedef std::function<void(JsValueList& params)> EventCallback; |
89 | 91 |
90 /** | 92 /** |
| 93 * Callback function returning false when current connection is not allowed |
| 94 * e.g. because it is a metered connection. |
| 95 */ |
| 96 typedef std::function<bool()> IsConnectionAllowedCallback; |
| 97 |
| 98 /** |
91 * Maps events to callback functions. | 99 * Maps events to callback functions. |
92 */ | 100 */ |
93 typedef std::map<std::string, EventCallback> EventMap; | 101 typedef std::map<std::string, EventCallback> EventMap; |
94 | 102 |
95 /** | 103 /** |
96 * Creates a new JavaScript engine instance. | 104 * Creates a new JavaScript engine instance. |
97 * @param appInfo Information about the app. | 105 * @param appInfo Information about the app. |
98 * @param isolate v8::Isolate wrapper. This parameter should be considered | 106 * @param isolate v8::Isolate wrapper. This parameter should be considered |
99 * as a temporary hack for tests, it will go away. Issue #3593. | 107 * as a temporary hack for tests, it will go away. Issue #3593. |
100 * @return New `JsEngine` instance. | 108 * @return New `JsEngine` instance. |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 | 223 |
216 /** | 224 /** |
217 * Sets the `WebRequest` implementation used for XMLHttpRequests. | 225 * Sets the `WebRequest` implementation used for XMLHttpRequests. |
218 * Setting this is optional, the engine will use a `DefaultWebRequest` | 226 * Setting this is optional, the engine will use a `DefaultWebRequest` |
219 * instance by default, which might be sufficient. | 227 * instance by default, which might be sufficient. |
220 * @param The `WebRequest` instance to use. | 228 * @param The `WebRequest` instance to use. |
221 */ | 229 */ |
222 void SetWebRequest(WebRequestPtr val); | 230 void SetWebRequest(WebRequestPtr val); |
223 | 231 |
224 /** | 232 /** |
| 233 * Registers the callback function to check whether current connection is |
| 234 * allowed for network requests. |
| 235 * @param callback callback function. |
| 236 */ |
| 237 void SetIsConnectionAllowedCallback(const IsConnectionAllowedCallback& callb
ack); |
| 238 |
| 239 /** |
| 240 * Checks whether current connection is allowed. If |
| 241 * IsConnectionAllowedCallback is not set then then it returns true. |
| 242 */ |
| 243 bool IsConnectionAllowed(); |
| 244 |
| 245 /** |
225 * @see `SetLogSystem()`. | 246 * @see `SetLogSystem()`. |
226 */ | 247 */ |
227 LogSystemPtr GetLogSystem(); | 248 LogSystemPtr GetLogSystem(); |
228 | 249 |
229 /** | 250 /** |
230 * Sets the `LogSystem` implementation used for logging (e.g. to handle | 251 * Sets the `LogSystem` implementation used for logging (e.g. to handle |
231 * `console.log()` calls from JavaScript). | 252 * `console.log()` calls from JavaScript). |
232 * Setting this is optional, the engine will use a `DefaultLogSystem` | 253 * Setting this is optional, the engine will use a `DefaultLogSystem` |
233 * instance by default, which might be sufficient. | 254 * instance by default, which might be sufficient. |
234 * @param The `LogSystem` instance to use. | 255 * @param The `LogSystem` instance to use. |
235 */ | 256 */ |
236 void SetLogSystem(LogSystemPtr val); | 257 void SetLogSystem(LogSystemPtr val); |
237 | 258 |
238 /** | 259 /** |
239 * Sets a global property that can be accessed by all the scripts. | 260 * Sets a global property that can be accessed by all the scripts. |
240 * @param name Name of the property to set. | 261 * @param name Name of the property to set. |
241 * @param value Value of the property to set. | 262 * @param value Value of the property to set. |
242 */ | 263 */ |
243 void SetGlobalProperty(const std::string& name, AdblockPlus::JsValuePtr valu
e); | 264 void SetGlobalProperty(const std::string& name, AdblockPlus::JsValuePtr valu
e); |
244 | 265 |
245 /** | 266 /** |
246 * Returns a pointer to associated v8::Isolate. | 267 * Returns a pointer to associated v8::Isolate. |
247 */ | 268 */ |
248 v8::Isolate* GetIsolate() | 269 v8::Isolate* GetIsolate() |
249 { | 270 { |
250 return isolate->Get(); | 271 return isolate->Get(); |
251 } | 272 } |
252 | 273 |
| 274 // Private functionality required to implement timers. |
| 275 struct TimerTaskInfo |
| 276 { |
| 277 ~TimerTaskInfo(); |
| 278 int delay; |
| 279 std::vector<std::unique_ptr<v8::Persistent<v8::Value>>> arguments; |
| 280 }; |
| 281 typedef std::list<TimerTaskInfo> TimerTaskInfos; |
| 282 struct TimerTask |
| 283 { |
| 284 std::weak_ptr<JsEngine> weakJsEngine; |
| 285 TimerTaskInfos::const_iterator taskInfoIterator; |
| 286 }; |
| 287 TimerTask CreateTimerTask(const v8::Arguments& arguments); |
| 288 void CallTimerTask(TimerTaskInfos::const_iterator taskInfoIterator); |
253 private: | 289 private: |
254 explicit JsEngine(const ScopedV8IsolatePtr& isolate); | 290 explicit JsEngine(const ScopedV8IsolatePtr& isolate); |
255 | 291 |
256 JsValuePtr GetGlobalObject(); | 292 JsValuePtr GetGlobalObject(); |
257 | 293 |
258 /// Isolate must be disposed only after disposing of all objects which are | 294 /// Isolate must be disposed only after disposing of all objects which are |
259 /// using it. | 295 /// using it. |
260 ScopedV8IsolatePtr isolate; | 296 ScopedV8IsolatePtr isolate; |
261 | 297 |
262 FileSystemPtr fileSystem; | 298 FileSystemPtr fileSystem; |
263 WebRequestPtr webRequest; | 299 WebRequestPtr webRequest; |
264 LogSystemPtr logSystem; | 300 LogSystemPtr logSystem; |
265 std::unique_ptr<v8::Persistent<v8::Context>> context; | 301 std::unique_ptr<v8::Persistent<v8::Context>> context; |
266 EventMap eventCallbacks; | 302 EventMap eventCallbacks; |
| 303 std::mutex eventCallbacksMutex; |
| 304 std::mutex isConnectionAllowedMutex; |
| 305 IsConnectionAllowedCallback isConnectionAllowed; |
| 306 TimerTaskInfos timerTaskInfos; |
267 }; | 307 }; |
268 } | 308 } |
269 | 309 |
270 #endif | 310 #endif |
OLD | NEW |