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

Delta Between Two Patch Sets: include/AdblockPlus/JsEngine.h

Issue 29442722: Issue 3593 - stop sharing v8::Isolate among tests (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Left Patch Set: Created May 19, 2017, 5:27 p.m.
Right Patch Set: address comments Created May 26, 2017, 10:54 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | src/JsEngine.cpp » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 eyeo GmbH
4 *
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
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
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/>.
16 */
17
18 #ifndef ADBLOCK_PLUS_JS_ENGINE_H
19 #define ADBLOCK_PLUS_JS_ENGINE_H
20
21 #include <functional>
22 #include <map>
23 #include <list>
24 #include <stdexcept>
25 #include <stdint.h>
26 #include <string>
27 #include <mutex>
28 #include <AdblockPlus/AppInfo.h>
29 #include <AdblockPlus/LogSystem.h>
30 #include <AdblockPlus/FileSystem.h>
31 #include <AdblockPlus/JsValue.h>
32 #include <AdblockPlus/WebRequest.h>
33 #include <AdblockPlus/ITimer.h>
34
35 namespace v8
36 {
37 class Arguments;
38 class Isolate;
39 class Value;
40 class Context;
41 template<class T> class Handle;
42 typedef Handle<Value>(*InvocationCallback)(const Arguments &args);
43 }
44
45 namespace AdblockPlus
46 {
47 class JsEngine;
48
49 /**
50 * Shared smart pointer to a `JsEngine` instance.
51 */
52 typedef std::shared_ptr<JsEngine> JsEnginePtr;
53
54 /**
55 * A factory to construct DefaultTimer.
56 */
57 TimerPtr CreateDefaultTimer();
58
59 /**
60 * A factory to construct DefaultWebRequest.
61 */
62 WebRequestPtr CreateDefaultWebRequest();
63
64 /**
65 * Scope based isolate manager. Creates a new isolate instance on
66 * constructing and disposes it on destructing.
67 */
68 class ScopedV8Isolate
69 {
70 public:
71 ScopedV8Isolate();
72 ~ScopedV8Isolate();
73 v8::Isolate* Get()
74 {
75 return isolate;
76 }
77 private:
78 ScopedV8Isolate(const ScopedV8Isolate&);
79 ScopedV8Isolate& operator=(const ScopedV8Isolate&);
80
81 v8::Isolate* isolate;
1 }; 82 };
2 83
3 /** 84 /**
4 * Shared smart pointer to ScopedV8Isolate instance;
5 */
6 typedef std::shared_ptr<ScopedV8Isolate> ScopedV8IsolatePtr;
7
8 /**
9 * JavaScript engine used by `FilterEngine`, wraps v8. 85 * JavaScript engine used by `FilterEngine`, wraps v8.
10 */ 86 */
11 class JsEngine : public std::enable_shared_from_this<JsEngine> 87 class JsEngine : public std::enable_shared_from_this<JsEngine>
88 {
89 friend class JsValue;
90 friend class JsContext;
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;
98 public:
99 /**
100 * Event callback function.
101 */
102 typedef std::function<void(JsValueList&& params)> EventCallback;
103
104 /**
105 * Maps events to callback functions.
106 */
107 typedef std::map<std::string, EventCallback> EventMap;
108
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 /**
120 * Creates a new JavaScript engine instance.
121 * @param appInfo Information about the app.
122 * @param timer Implementation of timer.
123 * @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.
127 */
128 static JsEnginePtr New(const AppInfo& appInfo = AppInfo(),
129 TimerPtr timer = CreateDefaultTimer(),
130 WebRequestPtr webRequest = CreateDefaultWebRequest());
131
132 /**
133 * Registers the callback function for an event.
134 * @param eventName Event name. Note that this can be any string - it's a
135 * general purpose event handling mechanism.
136 * @param callback Event callback function.
137 */
138 void SetEventCallback(const std::string& eventName, const EventCallback& cal lback);
139
140 /**
141 * Removes the callback function for an event.
142 * @param eventName Event name.
143 */
144 void RemoveEventCallback(const std::string& eventName);
145
146 /**
147 * Triggers an event.
148 * @param eventName Event name.
149 * @param params Event parameters.
150 */
151 void TriggerEvent(const std::string& eventName, JsValueList&& params);
152
153 /**
154 * Evaluates a JavaScript expression.
155 * @param source JavaScript expression to evaluate.
156 * @param filename Optional file name for the expression, used in error
157 * messages.
158 * @return Result of the evaluated expression.
159 */
160 JsValue Evaluate(const std::string& source,
161 const std::string& filename = "");
162
163 /**
164 * Initiates a garbage collection.
165 */
166 void Gc();
167
168 //@{
169 /**
170 * Creates a new JavaScript value.
171 * @param val Value to convert.
172 * @return New `JsValue` instance.
173 */
174 JsValue NewValue(const std::string& val);
175 JsValue NewValue(int64_t val);
176 JsValue NewValue(bool val);
177 inline JsValue NewValue(const char* val)
178 {
179 return NewValue(std::string(val));
180 }
181 inline JsValue NewValue(int val)
182 {
183 return NewValue(static_cast<int64_t>(val));
184 }
185 #ifdef __APPLE__
186 inline JsValue NewValue(long val)
187 {
188 return NewValue(static_cast<int64_t>(val));
189 }
190 #endif
191 //@}
192
193 /**
194 * Creates a new JavaScript object.
195 * @return New `JsValue` instance.
196 */
197 JsValue NewObject();
198
199 /**
200 * Creates a JavaScript function that invokes a C++ callback.
201 * @param callback C++ callback to invoke. The callback receives a
202 * `v8::Arguments` object and can use `FromArguments()` to retrieve
203 * the current `JsEngine`.
204 * @return New `JsValue` instance.
205 */
206 JsValue NewCallback(const v8::InvocationCallback& callback);
207
208 /**
209 * Returns a `JsEngine` instance contained in a `v8::Arguments` object.
210 * Use this in callbacks created via `NewCallback()` to retrieve the current
211 * `JsEngine`.
212 * @param arguments `v8::Arguments` object containing the `JsEngine`
213 * instance.
214 * @return `JsEngine` instance from `v8::Arguments`.
215 */
216 static JsEnginePtr FromArguments(const v8::Arguments& arguments);
217
218 /**
219 * 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
221 * methods should be used when one needs to carry a JsValue in a callback
222 * directly or indirectly passed to `JsEngine`.
223 * The method is thread-safe.
224 * @param `JsValueList` to store.
225 * @return `JsWeakValuesID` of stored values which allows to restore them
226 * later.
227 */
228 JsWeakValuesID StoreJsValues(const JsValueList& values);
229
230 /**
231 * Extracts and removes from `JsEngine` earlier stored `JsValue`s.
232 * The method is thread-safe.
233 * @param id `JsWeakValuesID` of values.
234 * @return `JsValueList` of stored values.
235 */
236 JsValueList TakeJsValues(const JsWeakValuesID& id);
237
238 /*
239 * Private functionality required to implement timers.
240 * @param arguments `v8::Arguments` is the arguments received in C++
241 * callback associated for global setTimeout method.
242 */
243 static void ScheduleTimer(const v8::Arguments& arguments);
244
245 /*
246 * Private functionality required to implement web requests.
247 * @param arguments `v8::Arguments` is the arguments received in C++
248 * callback associated for global GET method.
249 */
250 static void ScheduleWebRequest(const v8::Arguments& arguments);
251
252 /**
253 * Converts v8 arguments to `JsValue` objects.
254 * @param arguments `v8::Arguments` object containing the arguments to
255 * convert.
256 * @return List of arguments converted to `const JsValue` objects.
257 */
258 JsValueList ConvertArguments(const v8::Arguments& arguments);
259
260 /**
261 * @see `SetFileSystem()`.
262 */
263 FileSystemPtr GetFileSystem() const;
264
265 /**
266 * Sets the `FileSystem` implementation used for all file I/O.
267 * Setting this is optional, the engine will use a `DefaultFileSystem`
268 * instance by default, which might be sufficient.
269 * @param The `FileSystem` instance to use.
270 */
271 void SetFileSystem(const FileSystemPtr& val);
272
273 /**
274 * Sets the `WebRequest` implementation used for XMLHttpRequests.
275 * Setting this is optional, the engine will use a `DefaultWebRequest`
276 * instance by default, which might be sufficient.
277 * @param The `WebRequest` instance to use.
278 */
279 void SetWebRequest(const WebRequestSharedPtr& val);
280
281 /**
282 * @see `SetLogSystem()`.
283 */
284 LogSystemPtr GetLogSystem() const;
285
286 /**
287 * Sets the `LogSystem` implementation used for logging (e.g. to handle
288 * `console.log()` calls from JavaScript).
289 * Setting this is optional, the engine will use a `DefaultLogSystem`
290 * instance by default, which might be sufficient.
291 * @param The `LogSystem` instance to use.
292 */
293 void SetLogSystem(const LogSystemPtr& val);
294
295 /**
296 * Sets a global property that can be accessed by all the scripts.
297 * @param name Name of the property to set.
298 * @param value Value of the property to set.
299 */
300 void SetGlobalProperty(const std::string& name, const AdblockPlus::JsValue& value);
301
302 /**
303 * Returns a pointer to associated v8::Isolate.
304 */
305 v8::Isolate* GetIsolate()
306 {
307 return isolate.Get();
308 }
309
310 /**
311 * Notifies JS engine about critically low memory what should cause a
312 * garbage collection.
313 */
314 void NotifyLowMemory();
315 private:
316 void CallTimerTask(const JsWeakValuesID& timerParamsID);
317
318 explicit JsEngine(TimerPtr timer, WebRequestPtr webRequest);
319
320 JsValue GetGlobalObject();
321
322 /// Isolate must be disposed only after disposing of all objects which are
323 /// using it.
324 ScopedV8Isolate isolate;
325
326 FileSystemPtr fileSystem;
327 LogSystemPtr logSystem;
328 std::unique_ptr<v8::Persistent<v8::Context>> context;
329 EventMap eventCallbacks;
330 std::mutex eventCallbacksMutex;
331 JsWeakValuesLists jsWeakValuesLists;
332 std::mutex jsWeakValuesListsMutex;
333 TimerPtr timer;
334 WebRequestPtr webRequest;
335 WebRequestSharedPtr webRequestLegacy;
336 };
337 }
338
339 #endif
LEFTRIGHT
« no previous file | src/JsEngine.cpp » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld