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

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

Issue 29449592: Issue 5183 - Provide async interface for FileSystem (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Updated patch after review. Created June 16, 2017, 9:52 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
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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 * Shared smart pointer to a `JsEngine` instance. 49 * Shared smart pointer to a `JsEngine` instance.
50 */ 50 */
51 typedef std::shared_ptr<JsEngine> JsEnginePtr; 51 typedef std::shared_ptr<JsEngine> JsEnginePtr;
52 52
53 /** 53 /**
54 * A factory to construct DefaultTimer. 54 * A factory to construct DefaultTimer.
55 */ 55 */
56 TimerPtr CreateDefaultTimer(); 56 TimerPtr CreateDefaultTimer();
57 57
58 /** 58 /**
59 * A factory to construct DefaultFileSystem.
60 */
61 FileSystemPtr CreateDefaultFileSystem();
62
63 /**
59 * A factory to construct DefaultWebRequest. 64 * A factory to construct DefaultWebRequest.
60 */ 65 */
61 WebRequestPtr CreateDefaultWebRequest(); 66 WebRequestPtr CreateDefaultWebRequest();
62 67
63 /** 68 /**
64 * Scope based isolate manager. Creates a new isolate instance on 69 * Scope based isolate manager. Creates a new isolate instance on
65 * constructing and disposes it on destructing. 70 * constructing and disposes it on destructing.
66 */ 71 */
67 class ScopedV8Isolate 72 class ScopedV8Isolate
68 { 73 {
(...skipping 15 matching lines...) Expand all
84 * JavaScript engine used by `FilterEngine`, wraps v8. 89 * JavaScript engine used by `FilterEngine`, wraps v8.
85 */ 90 */
86 class JsEngine : public std::enable_shared_from_this<JsEngine> 91 class JsEngine : public std::enable_shared_from_this<JsEngine>
87 { 92 {
88 friend class JsValue; 93 friend class JsValue;
89 friend class JsContext; 94 friend class JsContext;
90 95
91 struct JsWeakValuesList 96 struct JsWeakValuesList
92 { 97 {
93 ~JsWeakValuesList(); 98 ~JsWeakValuesList();
94 std::vector<v8::Global<v8::Value>> values; 99 std::vector<v8::Global<v8::Value>> values;
sergei 2017/07/03 09:25:54 Wow, it seems to be rebased on new v8. That's very
hub 2017/07/04 19:58:27 Yes I did rebase on top of your patch. I was expec
95 }; 100 };
96 typedef std::list<JsWeakValuesList> JsWeakValuesLists; 101 typedef std::list<JsWeakValuesList> JsWeakValuesLists;
97 public: 102 public:
98 /** 103 /**
99 * Event callback function. 104 * Event callback function.
100 */ 105 */
101 typedef std::function<void(JsValueList&& params)> EventCallback; 106 typedef std::function<void(JsValueList&& params)> EventCallback;
102 107
103 /** 108 /**
104 * Maps events to callback functions. 109 * Maps events to callback functions.
105 */ 110 */
106 typedef std::map<std::string, EventCallback> EventMap; 111 typedef std::map<std::string, EventCallback> EventMap;
107 112
108 /** 113 /**
109 * An opaque structure representing ID of stored JsValueList. 114 * An opaque structure representing ID of stored JsValueList.
110 * 115 *
111 */ 116 */
112 class JsWeakValuesID 117 class JsWeakValuesID
113 { 118 {
114 friend class JsEngine; 119 friend class JsEngine;
115 JsWeakValuesLists::const_iterator iterator; 120 JsWeakValuesLists::const_iterator iterator;
116 }; 121 };
117 122
118 /** 123 /**
119 * Creates a new JavaScript engine instance. 124 * Creates a new JavaScript engine instance.
120 * @param appInfo Information about the app. 125 * @param appInfo Information about the app.
121 * @param timer Implementation of timer. 126 * @param timer Implementation of timer.
127 * @param fileSystem Implementation of filesystem.
122 * @param webRequest Implementation of web request. 128 * @param webRequest Implementation of web request.
123 * @param isolate v8::Isolate wrapper. This parameter should be considered 129 * @param isolate v8::Isolate wrapper. This parameter should be considered
124 * as a temporary hack for tests, it will go away. Issue #3593. 130 * as a temporary hack for tests, it will go away. Issue #3593.
125 * @return New `JsEngine` instance. 131 * @return New `JsEngine` instance.
126 */ 132 */
127 static JsEnginePtr New(const AppInfo& appInfo = AppInfo(), 133 static JsEnginePtr New(const AppInfo& appInfo = AppInfo(),
128 TimerPtr timer = CreateDefaultTimer(), 134 TimerPtr timer = CreateDefaultTimer(),
135 FileSystemPtr fileSystem = CreateDefaultFileSystem(),
129 WebRequestPtr webRequest = CreateDefaultWebRequest()); 136 WebRequestPtr webRequest = CreateDefaultWebRequest());
130 137
131 /** 138 /**
132 * Registers the callback function for an event. 139 * Registers the callback function for an event.
133 * @param eventName Event name. Note that this can be any string - it's a 140 * @param eventName Event name. Note that this can be any string - it's a
134 * general purpose event handling mechanism. 141 * general purpose event handling mechanism.
135 * @param callback Event callback function. 142 * @param callback Event callback function.
136 */ 143 */
137 void SetEventCallback(const std::string& eventName, const EventCallback& cal lback); 144 void SetEventCallback(const std::string& eventName, const EventCallback& cal lback);
138 145
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 257
251 /** 258 /**
252 * Converts v8 arguments to `JsValue` objects. 259 * Converts v8 arguments to `JsValue` objects.
253 * @param arguments `v8::FunctionCallbackInfo` object containing the argumen ts to 260 * @param arguments `v8::FunctionCallbackInfo` object containing the argumen ts to
254 * convert. 261 * convert.
255 * @return List of arguments converted to `const JsValue` objects. 262 * @return List of arguments converted to `const JsValue` objects.
256 */ 263 */
257 JsValueList ConvertArguments(const v8::FunctionCallbackInfo<v8::Value>& argu ments); 264 JsValueList ConvertArguments(const v8::FunctionCallbackInfo<v8::Value>& argu ments);
258 265
259 /** 266 /**
260 * @see `SetFileSystem()`. 267 * @return The asynchronous IFileSystem implementation.
261 */ 268 */
262 FileSystemPtr GetFileSystem() const; 269 FileSystemPtr GetAsyncFileSystem() const;
sergei 2017/07/03 09:25:54 From https://codereview.adblockplus.org/29449592/d
hub 2017/07/04 19:58:26 Acknowledged.
263 270
264 /** 271 /**
265 * Sets the `FileSystem` implementation used for all file I/O. 272 * Sets the synchronous `FileSystem` implementation used for all
266 * Setting this is optional, the engine will use a `DefaultFileSystem` 273 * file I/O. Setting this is optional, the engine will use a
267 * instance by default, which might be sufficient. 274 * `DefaultFileSystemSync` instance by default, which might be
sergei 2017/07/03 09:25:54 I think the comment should be a bit adjusted. By d
hub 2017/07/04 19:58:26 Acknowledged.
275 * sufficient.
268 * @param The `FileSystem` instance to use. 276 * @param The `FileSystem` instance to use.
269 */ 277 */
270 void SetFileSystem(const FileSystemPtr& val); 278 void SetFileSystem(const FileSystemSyncPtr& val);
271 279
272 /** 280 /**
273 * Sets the `WebRequest` implementation used for XMLHttpRequests. 281 * Sets the `WebRequest` implementation used for XMLHttpRequests.
274 * Setting this is optional, the engine will use a `DefaultWebRequest` 282 * Setting this is optional, the engine will use a `DefaultWebRequest`
275 * instance by default, which might be sufficient. 283 * instance by default, which might be sufficient.
276 * @param The `WebRequest` instance to use. 284 * @param The `WebRequest` instance to use.
277 */ 285 */
278 void SetWebRequest(const WebRequestSharedPtr& val); 286 void SetWebRequest(const WebRequestSharedPtr& val);
279 287
280 /** 288 /**
(...skipping 26 matching lines...) Expand all
307 } 315 }
308 316
309 /** 317 /**
310 * Notifies JS engine about critically low memory what should cause a 318 * Notifies JS engine about critically low memory what should cause a
311 * garbage collection. 319 * garbage collection.
312 */ 320 */
313 void NotifyLowMemory(); 321 void NotifyLowMemory();
314 private: 322 private:
315 void CallTimerTask(const JsWeakValuesID& timerParamsID); 323 void CallTimerTask(const JsWeakValuesID& timerParamsID);
316 324
317 explicit JsEngine(TimerPtr timer, WebRequestPtr webRequest); 325 explicit JsEngine(TimerPtr timer, FileSystemPtr fileSystem, WebRequestPtr we bRequest);
318 326
319 JsValue GetGlobalObject(); 327 JsValue GetGlobalObject();
320 328
321 /// Isolate must be disposed only after disposing of all objects which are 329 /// Isolate must be disposed only after disposing of all objects which are
322 /// using it. 330 /// using it.
323 ScopedV8Isolate isolate; 331 ScopedV8Isolate isolate;
324 332
325 FileSystemPtr fileSystem; 333 FileSystemPtr fileSystem;
334 FileSystemSyncPtr fileSystemLegacy;
326 LogSystemPtr logSystem; 335 LogSystemPtr logSystem;
327 std::unique_ptr<v8::Global<v8::Context>> context; 336 std::unique_ptr<v8::Global<v8::Context>> context;
328 EventMap eventCallbacks; 337 EventMap eventCallbacks;
329 std::mutex eventCallbacksMutex; 338 std::mutex eventCallbacksMutex;
330 JsWeakValuesLists jsWeakValuesLists; 339 JsWeakValuesLists jsWeakValuesLists;
331 std::mutex jsWeakValuesListsMutex; 340 std::mutex jsWeakValuesListsMutex;
332 TimerPtr timer; 341 TimerPtr timer;
333 WebRequestPtr webRequest; 342 WebRequestPtr webRequest;
334 WebRequestSharedPtr webRequestLegacy; 343 WebRequestSharedPtr webRequestLegacy;
335 }; 344 };
336 } 345 }
337 346
338 #endif 347 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld