Left: | ||
Right: |
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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
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 public: | 92 public: |
93 /** | 93 /** |
94 * Event callback function. | 94 * Event callback function. |
95 */ | 95 */ |
96 typedef std::function<void(JsValueList& params)> EventCallback; | 96 typedef std::function<void(const JsValueList& params)> EventCallback; |
97 | 97 |
98 /** | 98 /** |
99 * Callback function returning false when current connection is not allowed | 99 * Callback function returning false when current connection is not allowed |
100 * e.g. because it is a metered connection. | 100 * e.g. because it is a metered connection. |
101 */ | 101 */ |
102 typedef std::function<bool()> IsConnectionAllowedCallback; | 102 typedef std::function<bool()> IsConnectionAllowedCallback; |
103 | 103 |
104 /** | 104 /** |
105 * Maps events to callback functions. | 105 * Maps events to callback functions. |
106 */ | 106 */ |
(...skipping 10 matching lines...) Expand all Loading... | |
117 static JsEnginePtr New(const AppInfo& appInfo = AppInfo(), | 117 static JsEnginePtr New(const AppInfo& appInfo = AppInfo(), |
118 TimerPtr timer = CreateDefaultTimer(), | 118 TimerPtr timer = CreateDefaultTimer(), |
119 const ScopedV8IsolatePtr& isolate = ScopedV8IsolatePtr(new ScopedV8Isolate ())); | 119 const ScopedV8IsolatePtr& isolate = ScopedV8IsolatePtr(new ScopedV8Isolate ())); |
120 | 120 |
121 /** | 121 /** |
122 * Registers the callback function for an event. | 122 * Registers the callback function for an event. |
123 * @param eventName Event name. Note that this can be any string - it's a | 123 * @param eventName Event name. Note that this can be any string - it's a |
124 * general purpose event handling mechanism. | 124 * general purpose event handling mechanism. |
125 * @param callback Event callback function. | 125 * @param callback Event callback function. |
126 */ | 126 */ |
127 void SetEventCallback(const std::string& eventName, EventCallback callback); | 127 void SetEventCallback(const std::string& eventName, const EventCallback& cal lback); |
128 | 128 |
129 /** | 129 /** |
130 * Removes the callback function for an event. | 130 * Removes the callback function for an event. |
131 * @param eventName Event name. | 131 * @param eventName Event name. |
132 */ | 132 */ |
133 void RemoveEventCallback(const std::string& eventName); | 133 void RemoveEventCallback(const std::string& eventName); |
134 | 134 |
135 /** | 135 /** |
136 * Triggers an event. | 136 * Triggers an event. |
137 * @param eventName Event name. | 137 * @param eventName Event name. |
138 * @param params Event parameters. | 138 * @param params Event parameters. |
139 */ | 139 */ |
140 void TriggerEvent(const std::string& eventName, JsValueList& params); | 140 void TriggerEvent(const std::string& eventName, const JsValueList& params); |
141 | 141 |
142 /** | 142 /** |
143 * Evaluates a JavaScript expression. | 143 * Evaluates a JavaScript expression. |
144 * @param source JavaScript expression to evaluate. | 144 * @param source JavaScript expression to evaluate. |
145 * @param filename Optional file name for the expression, used in error | 145 * @param filename Optional file name for the expression, used in error |
146 * messages. | 146 * messages. |
147 * @return Result of the evaluated expression. | 147 * @return Result of the evaluated expression. |
148 */ | 148 */ |
149 JsValuePtr Evaluate(const std::string& source, | 149 JsValuePtr Evaluate(const std::string& source, |
150 const std::string& filename = ""); | 150 const std::string& filename = ""); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
185 */ | 185 */ |
186 JsValuePtr NewObject(); | 186 JsValuePtr NewObject(); |
187 | 187 |
188 /** | 188 /** |
189 * Creates a JavaScript function that invokes a C++ callback. | 189 * Creates a JavaScript function that invokes a C++ callback. |
190 * @param callback C++ callback to invoke. The callback receives a | 190 * @param callback C++ callback to invoke. The callback receives a |
191 * `v8::Arguments` object and can use `FromArguments()` to retrieve | 191 * `v8::Arguments` object and can use `FromArguments()` to retrieve |
192 * the current `JsEngine`. | 192 * the current `JsEngine`. |
193 * @return New `JsValue` instance. | 193 * @return New `JsValue` instance. |
194 */ | 194 */ |
195 JsValuePtr NewCallback(v8::InvocationCallback callback); | 195 JsValuePtr NewCallback(const v8::InvocationCallback& callback); |
196 | 196 |
197 /** | 197 /** |
198 * Returns a `JsEngine` instance contained in a `v8::Arguments` object. | 198 * Returns a `JsEngine` instance contained in a `v8::Arguments` object. |
199 * Use this in callbacks created via `NewCallback()` to retrieve the current | 199 * Use this in callbacks created via `NewCallback()` to retrieve the current |
200 * `JsEngine`. | 200 * `JsEngine`. |
201 * @param arguments `v8::Arguments` object containing the `JsEngine` | 201 * @param arguments `v8::Arguments` object containing the `JsEngine` |
202 * instance. | 202 * instance. |
203 * @return `JsEngine` instance from `v8::Arguments`. | 203 * @return `JsEngine` instance from `v8::Arguments`. |
204 */ | 204 */ |
205 static JsEnginePtr FromArguments(const v8::Arguments& arguments); | 205 static JsEnginePtr FromArguments(const v8::Arguments& arguments); |
(...skipping 17 matching lines...) Expand all Loading... | |
223 * @see `SetFileSystem()`. | 223 * @see `SetFileSystem()`. |
224 */ | 224 */ |
225 FileSystemPtr GetFileSystem() const; | 225 FileSystemPtr GetFileSystem() const; |
226 | 226 |
227 /** | 227 /** |
228 * Sets the `FileSystem` implementation used for all file I/O. | 228 * Sets the `FileSystem` implementation used for all file I/O. |
229 * Setting this is optional, the engine will use a `DefaultFileSystem` | 229 * Setting this is optional, the engine will use a `DefaultFileSystem` |
230 * instance by default, which might be sufficient. | 230 * instance by default, which might be sufficient. |
231 * @param The `FileSystem` instance to use. | 231 * @param The `FileSystem` instance to use. |
232 */ | 232 */ |
233 void SetFileSystem(FileSystemPtr val); | 233 void SetFileSystem(const FileSystemPtr& val); |
234 | 234 |
235 /** | 235 /** |
236 * @see `SetWebRequest()`. | 236 * @see `SetWebRequest()`. |
237 */ | 237 */ |
238 WebRequestPtr GetWebRequest() const; | 238 WebRequestPtr GetWebRequest() const; |
239 | 239 |
240 /** | 240 /** |
241 * Sets the `WebRequest` implementation used for XMLHttpRequests. | 241 * Sets the `WebRequest` implementation used for XMLHttpRequests. |
242 * Setting this is optional, the engine will use a `DefaultWebRequest` | 242 * Setting this is optional, the engine will use a `DefaultWebRequest` |
243 * instance by default, which might be sufficient. | 243 * instance by default, which might be sufficient. |
244 * @param The `WebRequest` instance to use. | 244 * @param The `WebRequest` instance to use. |
245 */ | 245 */ |
246 void SetWebRequest(WebRequestPtr val); | 246 void SetWebRequest(const WebRequestPtr& val); |
247 | 247 |
248 /** | 248 /** |
249 * Registers the callback function to check whether current connection is | 249 * Registers the callback function to check whether current connection is |
250 * allowed for network requests. | 250 * allowed for network requests. |
251 * @param callback callback function. | 251 * @param callback callback function. |
252 */ | 252 */ |
253 void SetIsConnectionAllowedCallback(const IsConnectionAllowedCallback& callb ack); | 253 void SetIsConnectionAllowedCallback(const IsConnectionAllowedCallback& callb ack); |
254 | 254 |
255 /** | 255 /** |
256 * Checks whether current connection is allowed. If | 256 * Checks whether current connection is allowed. If |
257 * IsConnectionAllowedCallback is not set then then it returns true. | 257 * IsConnectionAllowedCallback is not set then then it returns true. |
258 */ | 258 */ |
259 bool IsConnectionAllowed() const; | 259 bool IsConnectionAllowed() const; |
260 | 260 |
261 /** | 261 /** |
262 * @see `SetLogSystem()`. | 262 * @see `SetLogSystem()`. |
263 */ | 263 */ |
264 LogSystemPtr GetLogSystem() const; | 264 LogSystemPtr GetLogSystem() const; |
265 | 265 |
266 /** | 266 /** |
267 * Sets the `LogSystem` implementation used for logging (e.g. to handle | 267 * Sets the `LogSystem` implementation used for logging (e.g. to handle |
268 * `console.log()` calls from JavaScript). | 268 * `console.log()` calls from JavaScript). |
269 * Setting this is optional, the engine will use a `DefaultLogSystem` | 269 * Setting this is optional, the engine will use a `DefaultLogSystem` |
270 * instance by default, which might be sufficient. | 270 * instance by default, which might be sufficient. |
271 * @param The `LogSystem` instance to use. | 271 * @param The `LogSystem` instance to use. |
272 */ | 272 */ |
273 void SetLogSystem(LogSystemPtr val); | 273 void SetLogSystem(const LogSystemPtr& val); |
274 | 274 |
275 /** | 275 /** |
276 * Sets a global property that can be accessed by all the scripts. | 276 * Sets a global property that can be accessed by all the scripts. |
277 * @param name Name of the property to set. | 277 * @param name Name of the property to set. |
278 * @param value Value of the property to set. | 278 * @param value Value of the property to set. |
279 */ | 279 */ |
280 void SetGlobalProperty(const std::string& name, AdblockPlus::JsValuePtr valu e); | 280 void SetGlobalProperty(const std::string& name, const AdblockPlus::JsValuePt r& value); |
281 | 281 |
282 /** | 282 /** |
283 * Returns a pointer to associated v8::Isolate. | 283 * Returns a pointer to associated v8::Isolate. |
284 */ | 284 */ |
285 v8::Isolate* GetIsolate() | 285 v8::Isolate* GetIsolate() |
286 { | 286 { |
287 return isolate->Get(); | 287 return isolate->Get(); |
288 } | 288 } |
289 | 289 |
290 private: | 290 private: |
291 struct TimerTask | 291 struct TimerTask |
292 { | 292 { |
293 ~TimerTask(); | 293 ~TimerTask(); |
294 std::vector<std::unique_ptr<v8::Persistent<v8::Value>>> arguments; | 294 std::vector<std::unique_ptr<v8::Persistent<v8::Value>>> arguments; |
295 }; | 295 }; |
296 typedef std::list<TimerTask> TimerTasks; | 296 typedef std::list<TimerTask> TimerTasks; |
297 void CallTimerTask(TimerTasks::const_iterator timerTaskIterator); | 297 void CallTimerTask(const TimerTasks::const_iterator& timerTaskIterator); |
sergei
2017/04/12 13:34:30
I'm not sure that passing of const_iterator of std
hub
2017/04/12 15:06:14
still more than a plain copy since it is not a PoD
| |
298 | 298 |
299 explicit JsEngine(const ScopedV8IsolatePtr& isolate, TimerPtr timer); | 299 explicit JsEngine(const ScopedV8IsolatePtr& isolate, TimerPtr timer); |
300 | 300 |
301 JsValuePtr GetGlobalObject(); | 301 JsValuePtr GetGlobalObject(); |
302 | 302 |
303 /// Isolate must be disposed only after disposing of all objects which are | 303 /// Isolate must be disposed only after disposing of all objects which are |
304 /// using it. | 304 /// using it. |
305 ScopedV8IsolatePtr isolate; | 305 ScopedV8IsolatePtr isolate; |
306 | 306 |
307 FileSystemPtr fileSystem; | 307 FileSystemPtr fileSystem; |
308 WebRequestPtr webRequest; | 308 WebRequestPtr webRequest; |
309 LogSystemPtr logSystem; | 309 LogSystemPtr logSystem; |
310 std::unique_ptr<v8::Persistent<v8::Context>> context; | 310 std::unique_ptr<v8::Persistent<v8::Context>> context; |
311 EventMap eventCallbacks; | 311 EventMap eventCallbacks; |
312 std::mutex eventCallbacksMutex; | 312 std::mutex eventCallbacksMutex; |
313 mutable std::mutex isConnectionAllowedMutex; | 313 mutable std::mutex isConnectionAllowedMutex; |
314 IsConnectionAllowedCallback isConnectionAllowed; | 314 IsConnectionAllowedCallback isConnectionAllowed; |
315 TimerTasks timerTasks; | 315 TimerTasks timerTasks; |
316 TimerPtr timer; | 316 TimerPtr timer; |
317 }; | 317 }; |
318 } | 318 } |
319 | 319 |
320 #endif | 320 #endif |
OLD | NEW |