Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: include/AdblockPlus/JsEngine.h

Issue 29508624: Issue 5473 - Update to use libadblockplus revision b4d6e55f2116 (Closed)
Patch Set: Created Aug. 7, 2017, 12:41 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « include/AdblockPlus/IFileSystem.h ('k') | include/AdblockPlus/JsValue.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 16 matching lines...) Expand all
27 #include <mutex> 27 #include <mutex>
28 #include <AdblockPlus/AppInfo.h> 28 #include <AdblockPlus/AppInfo.h>
29 #include <AdblockPlus/LogSystem.h> 29 #include <AdblockPlus/LogSystem.h>
30 #include <AdblockPlus/FileSystem.h> 30 #include <AdblockPlus/FileSystem.h>
31 #include <AdblockPlus/JsValue.h> 31 #include <AdblockPlus/JsValue.h>
32 #include <AdblockPlus/WebRequest.h> 32 #include <AdblockPlus/WebRequest.h>
33 #include <AdblockPlus/ITimer.h> 33 #include <AdblockPlus/ITimer.h>
34 34
35 namespace v8 35 namespace v8
36 { 36 {
37 class Arguments;
38 class Isolate; 37 class Isolate;
39 class Value; 38 class Value;
40 class Context; 39 class Context;
41 template<class T> class Handle; 40 template<typename T> class FunctionCallbackInfo;
42 typedef Handle<Value>(*InvocationCallback)(const Arguments &args); 41 typedef void(*FunctionCallback)(const FunctionCallbackInfo<v8::Value>& info);
43 } 42 }
44 43
45 namespace AdblockPlus 44 namespace AdblockPlus
46 { 45 {
47 class JsEngine; 46 class JsEngine;
48 47
49 /** 48 /**
50 * Shared smart pointer to a `JsEngine` instance. 49 * Shared smart pointer to a `JsEngine` instance.
51 */ 50 */
52 typedef std::shared_ptr<JsEngine> JsEnginePtr; 51 typedef std::shared_ptr<JsEngine> JsEnginePtr;
53 52
54 /** 53 /**
55 * A factory to construct DefaultTimer. 54 * A factory to construct DefaultTimer.
56 */ 55 */
57 TimerPtr CreateDefaultTimer(); 56 TimerPtr CreateDefaultTimer();
58 57
59 /** 58 /**
59 * A factory to construct DefaultFileSystem.
60 */
61 FileSystemPtr CreateDefaultFileSystem();
62
63 /**
60 * A factory to construct DefaultWebRequest. 64 * A factory to construct DefaultWebRequest.
61 */ 65 */
62 WebRequestPtr CreateDefaultWebRequest(); 66 WebRequestPtr CreateDefaultWebRequest();
63 67
64 /** 68 /**
65 * Scope based isolate manager. Creates a new isolate instance on 69 * Scope based isolate manager. Creates a new isolate instance on
66 * constructing and disposes it on destructing. 70 * constructing and disposes it on destructing.
67 */ 71 */
68 class ScopedV8Isolate 72 class ScopedV8Isolate
69 { 73 {
(...skipping 15 matching lines...) Expand all
85 * JavaScript engine used by `FilterEngine`, wraps v8. 89 * JavaScript engine used by `FilterEngine`, wraps v8.
86 */ 90 */
87 class JsEngine : public std::enable_shared_from_this<JsEngine> 91 class JsEngine : public std::enable_shared_from_this<JsEngine>
88 { 92 {
89 friend class JsValue; 93 friend class JsValue;
90 friend class JsContext; 94 friend class JsContext;
91 95
92 struct JsWeakValuesList 96 struct JsWeakValuesList
93 { 97 {
94 ~JsWeakValuesList(); 98 ~JsWeakValuesList();
95 std::vector<std::unique_ptr<v8::Persistent<v8::Value>>> values; 99 std::vector<v8::Global<v8::Value>> values;
96 }; 100 };
97 typedef std::list<JsWeakValuesList> JsWeakValuesLists; 101 typedef std::list<JsWeakValuesList> JsWeakValuesLists;
98 public: 102 public:
99 /** 103 /**
100 * Event callback function. 104 * Event callback function.
101 */ 105 */
102 typedef std::function<void(JsValueList&& params)> EventCallback; 106 typedef std::function<void(JsValueList&& params)> EventCallback;
103 107
104 /** 108 /**
105 * Maps events to callback functions. 109 * Maps events to callback functions.
106 */ 110 */
107 typedef std::map<std::string, EventCallback> EventMap; 111 typedef std::map<std::string, EventCallback> EventMap;
108 112
109 /** 113 /**
110 * An opaque structure representing ID of stored JsValueList. 114 * An opaque structure representing ID of stored JsValueList.
111 * 115 *
112 */ 116 */
113 class JsWeakValuesID 117 class JsWeakValuesID
114 { 118 {
115 friend class JsEngine; 119 friend class JsEngine;
116 JsWeakValuesLists::const_iterator iterator; 120 JsWeakValuesLists::const_iterator iterator;
117 }; 121 };
118 122
119 /** 123 /**
120 * Creates a new JavaScript engine instance. 124 * Creates a new JavaScript engine instance.
121 * @param appInfo Information about the app. 125 * @param appInfo Information about the app.
122 * @param timer Implementation of timer. 126 * @param timer Implementation of timer.
127 * @param fileSystem Implementation of filesystem.
123 * @param webRequest Implementation of web request. 128 * @param webRequest Implementation of web request.
124 * @param isolate v8::Isolate wrapper. This parameter should be considered
125 * as a temporary hack for tests, it will go away. Issue #3593.
126 * @return New `JsEngine` instance. 129 * @return New `JsEngine` instance.
127 */ 130 */
128 static JsEnginePtr New(const AppInfo& appInfo = AppInfo(), 131 static JsEnginePtr New(const AppInfo& appInfo = AppInfo(),
129 TimerPtr timer = CreateDefaultTimer(), 132 TimerPtr timer = CreateDefaultTimer(),
133 FileSystemPtr fileSystem = CreateDefaultFileSystem(),
130 WebRequestPtr webRequest = CreateDefaultWebRequest()); 134 WebRequestPtr webRequest = CreateDefaultWebRequest());
131 135
132 /** 136 /**
133 * Registers the callback function for an event. 137 * Registers the callback function for an event.
134 * @param eventName Event name. Note that this can be any string - it's a 138 * @param eventName Event name. Note that this can be any string - it's a
135 * general purpose event handling mechanism. 139 * general purpose event handling mechanism.
136 * @param callback Event callback function. 140 * @param callback Event callback function.
137 */ 141 */
138 void SetEventCallback(const std::string& eventName, const EventCallback& cal lback); 142 void SetEventCallback(const std::string& eventName, const EventCallback& cal lback);
139 143
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 196
193 /** 197 /**
194 * Creates a new JavaScript object. 198 * Creates a new JavaScript object.
195 * @return New `JsValue` instance. 199 * @return New `JsValue` instance.
196 */ 200 */
197 JsValue NewObject(); 201 JsValue NewObject();
198 202
199 /** 203 /**
200 * Creates a JavaScript function that invokes a C++ callback. 204 * Creates a JavaScript function that invokes a C++ callback.
201 * @param callback C++ callback to invoke. The callback receives a 205 * @param callback C++ callback to invoke. The callback receives a
202 * `v8::Arguments` object and can use `FromArguments()` to retrieve 206 * `v8::FunctionCallbackInfo` object and can use `FromArguments()` to retrieve
203 * the current `JsEngine`. 207 * the current `JsEngine`.
204 * @return New `JsValue` instance. 208 * @return New `JsValue` instance.
205 */ 209 */
206 JsValue NewCallback(const v8::InvocationCallback& callback); 210 JsValue NewCallback(const v8::FunctionCallback& callback);
207 211
208 /** 212 /**
209 * Returns a `JsEngine` instance contained in a `v8::Arguments` object. 213 * Returns a `JsEngine` instance contained in a `v8::FunctionCallbackInfo` o bject.
210 * Use this in callbacks created via `NewCallback()` to retrieve the current 214 * Use this in callbacks created via `NewCallback()` to retrieve the current
211 * `JsEngine`. 215 * `JsEngine`.
212 * @param arguments `v8::Arguments` object containing the `JsEngine` 216 * @param arguments `v8::FunctionCallbackInfo` object containing the `JsEngi ne`
213 * instance. 217 * instance.
214 * @return `JsEngine` instance from `v8::Arguments`. 218 * @return `JsEngine` instance from `v8::FunctionCallbackInfo`.
215 */ 219 */
216 static JsEnginePtr FromArguments(const v8::Arguments& arguments); 220 static JsEnginePtr FromArguments(const v8::FunctionCallbackInfo<v8::Value>& arguments);
217 221
218 /** 222 /**
219 * Stores `JsValue`s in a way they don't keep a strong reference to 223 * Stores `JsValue`s in a way they don't keep a strong reference to
220 * `JsEngine` and which are destroyed when `JsEngine` is destroyed. These 224 * `JsEngine` and which are destroyed when `JsEngine` is destroyed. These
221 * methods should be used when one needs to carry a JsValue in a callback 225 * methods should be used when one needs to carry a JsValue in a callback
222 * directly or indirectly passed to `JsEngine`. 226 * directly or indirectly passed to `JsEngine`.
223 * The method is thread-safe. 227 * The method is thread-safe.
224 * @param `JsValueList` to store. 228 * @param `JsValueList` to store.
225 * @return `JsWeakValuesID` of stored values which allows to restore them 229 * @return `JsWeakValuesID` of stored values which allows to restore them
226 * later. 230 * later.
227 */ 231 */
228 JsWeakValuesID StoreJsValues(const JsValueList& values); 232 JsWeakValuesID StoreJsValues(const JsValueList& values);
229 233
230 /** 234 /**
231 * Extracts and removes from `JsEngine` earlier stored `JsValue`s. 235 * Extracts and removes from `JsEngine` earlier stored `JsValue`s.
232 * The method is thread-safe. 236 * The method is thread-safe.
233 * @param id `JsWeakValuesID` of values. 237 * @param id `JsWeakValuesID` of values.
234 * @return `JsValueList` of stored values. 238 * @return `JsValueList` of stored values.
235 */ 239 */
236 JsValueList TakeJsValues(const JsWeakValuesID& id); 240 JsValueList TakeJsValues(const JsWeakValuesID& id);
237 241
238 /* 242 /*
239 * Private functionality required to implement timers. 243 * Private functionality required to implement timers.
240 * @param arguments `v8::Arguments` is the arguments received in C++ 244 * @param arguments `v8::FunctionCallbackInfo` is the arguments received in C++
241 * callback associated for global setTimeout method. 245 * callback associated for global setTimeout method.
242 */ 246 */
243 static void ScheduleTimer(const v8::Arguments& arguments); 247 static void ScheduleTimer(const v8::FunctionCallbackInfo<v8::Value>& argumen ts);
244 248
245 /* 249 /*
246 * Private functionality required to implement web requests. 250 * Private functionality required to implement web requests.
247 * @param arguments `v8::Arguments` is the arguments received in C++ 251 * @param arguments `v8::FunctionCallbackInfo` is the arguments received in C++
248 * callback associated for global GET method. 252 * callback associated for global GET method.
249 */ 253 */
250 static void ScheduleWebRequest(const v8::Arguments& arguments); 254 static void ScheduleWebRequest(const v8::FunctionCallbackInfo<v8::Value>& ar guments);
251 255
252 /** 256 /**
253 * Converts v8 arguments to `JsValue` objects. 257 * Converts v8 arguments to `JsValue` objects.
254 * @param arguments `v8::Arguments` object containing the arguments to 258 * @param arguments `v8::FunctionCallbackInfo` object containing the argumen ts to
255 * convert. 259 * convert.
256 * @return List of arguments converted to `const JsValue` objects. 260 * @return List of arguments converted to `const JsValue` objects.
257 */ 261 */
258 JsValueList ConvertArguments(const v8::Arguments& arguments); 262 JsValueList ConvertArguments(const v8::FunctionCallbackInfo<v8::Value>& argu ments);
259 263
260 /** 264 /**
261 * @see `SetFileSystem()`. 265 * Private functionality.
266 * @return The asynchronous IFileSystem implementation.
262 */ 267 */
263 FileSystemPtr GetFileSystem() const; 268 FileSystemPtr GetAsyncFileSystem() const;
264 269
265 /** 270 /**
266 * Sets the `FileSystem` implementation used for all file I/O. 271 * Sets the synchronous `FileSystem` implementation used for all
267 * Setting this is optional, the engine will use a `DefaultFileSystem` 272 * file I/O. Setting this is optional, the engine will use the
268 * instance by default, which might be sufficient. 273 * implementation created by `CreateDefaultFileSystem()` by
274 * default, which might be sufficient.
269 * @param The `FileSystem` instance to use. 275 * @param The `FileSystem` instance to use.
270 */ 276 */
271 void SetFileSystem(const FileSystemPtr& val); 277 void SetFileSystem(const FileSystemSyncPtr& val);
272 278
273 /** 279 /**
274 * Sets the `WebRequest` implementation used for XMLHttpRequests. 280 * Sets the `WebRequest` implementation used for XMLHttpRequests.
275 * Setting this is optional, the engine will use a `DefaultWebRequest` 281 * Setting this is optional, the engine will use a `DefaultWebRequest`
276 * instance by default, which might be sufficient. 282 * instance by default, which might be sufficient.
277 * @param The `WebRequest` instance to use. 283 * @param The `WebRequest` instance to use.
278 */ 284 */
279 void SetWebRequest(const WebRequestSharedPtr& val); 285 void SetWebRequest(const WebRequestSharedPtr& val);
280 286
281 /** 287 /**
(...skipping 26 matching lines...) Expand all
308 } 314 }
309 315
310 /** 316 /**
311 * Notifies JS engine about critically low memory what should cause a 317 * Notifies JS engine about critically low memory what should cause a
312 * garbage collection. 318 * garbage collection.
313 */ 319 */
314 void NotifyLowMemory(); 320 void NotifyLowMemory();
315 private: 321 private:
316 void CallTimerTask(const JsWeakValuesID& timerParamsID); 322 void CallTimerTask(const JsWeakValuesID& timerParamsID);
317 323
318 explicit JsEngine(TimerPtr timer, WebRequestPtr webRequest); 324 explicit JsEngine(TimerPtr timer, FileSystemPtr fileSystem, WebRequestPtr we bRequest);
319 325
320 JsValue GetGlobalObject(); 326 JsValue GetGlobalObject();
321 327
322 /// Isolate must be disposed only after disposing of all objects which are 328 /// Isolate must be disposed only after disposing of all objects which are
323 /// using it. 329 /// using it.
324 ScopedV8Isolate isolate; 330 ScopedV8Isolate isolate;
325 331
326 FileSystemPtr fileSystem; 332 FileSystemPtr fileSystem;
327 LogSystemPtr logSystem; 333 LogSystemPtr logSystem;
328 std::unique_ptr<v8::Persistent<v8::Context>> context; 334 std::unique_ptr<v8::Global<v8::Context>> context;
329 EventMap eventCallbacks; 335 EventMap eventCallbacks;
330 std::mutex eventCallbacksMutex; 336 std::mutex eventCallbacksMutex;
331 JsWeakValuesLists jsWeakValuesLists; 337 JsWeakValuesLists jsWeakValuesLists;
332 std::mutex jsWeakValuesListsMutex; 338 std::mutex jsWeakValuesListsMutex;
333 TimerPtr timer; 339 TimerPtr timer;
334 WebRequestPtr webRequest; 340 WebRequestPtr webRequest;
335 WebRequestSharedPtr webRequestLegacy; 341 WebRequestSharedPtr webRequestLegacy;
336 }; 342 };
337 } 343 }
338 344
339 #endif 345 #endif
OLDNEW
« no previous file with comments | « include/AdblockPlus/IFileSystem.h ('k') | include/AdblockPlus/JsValue.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld