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

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

Issue 29395640: Issue 3595 - Get rid of detached threads for setTimeout (Closed)
Patch Set: only rebase Created March 27, 2017, 2:44 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 12 matching lines...) Expand all
23 #include <list> 23 #include <list>
24 #include <stdexcept> 24 #include <stdexcept>
25 #include <stdint.h> 25 #include <stdint.h>
26 #include <string> 26 #include <string>
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 34
34 namespace v8 35 namespace v8
35 { 36 {
36 class Arguments; 37 class Arguments;
37 class Isolate; 38 class Isolate;
38 class Value; 39 class Value;
39 class Context; 40 class Context;
40 template<class T> class Handle; 41 template<class T> class Handle;
41 typedef Handle<Value>(*InvocationCallback)(const Arguments &args); 42 typedef Handle<Value>(*InvocationCallback)(const Arguments &args);
42 } 43 }
43 44
44 namespace AdblockPlus 45 namespace AdblockPlus
45 { 46 {
46 class JsEngine; 47 class JsEngine;
47 48
48 /** 49 /**
49 * Shared smart pointer to a `JsEngine` instance. 50 * Shared smart pointer to a `JsEngine` instance.
50 */ 51 */
51 typedef std::shared_ptr<JsEngine> JsEnginePtr; 52 typedef std::shared_ptr<JsEngine> JsEnginePtr;
52 53
53 /** 54 /**
55 * A factory to construct DefaultTimer.
56 */
57 TimerPtr CreateDefaultTimer();
58
59 /**
54 * Scope based isolate manager. Creates a new isolate instance on 60 * Scope based isolate manager. Creates a new isolate instance on
55 * constructing and disposes it on destructing. 61 * constructing and disposes it on destructing.
56 */ 62 */
57 class ScopedV8Isolate 63 class ScopedV8Isolate
58 { 64 {
59 public: 65 public:
60 ScopedV8Isolate(); 66 ScopedV8Isolate();
61 ~ScopedV8Isolate(); 67 ~ScopedV8Isolate();
62 v8::Isolate* Get() 68 v8::Isolate* Get()
63 { 69 {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 typedef std::function<bool()> IsConnectionAllowedCallback; 102 typedef std::function<bool()> IsConnectionAllowedCallback;
97 103
98 /** 104 /**
99 * Maps events to callback functions. 105 * Maps events to callback functions.
100 */ 106 */
101 typedef std::map<std::string, EventCallback> EventMap; 107 typedef std::map<std::string, EventCallback> EventMap;
102 108
103 /** 109 /**
104 * Creates a new JavaScript engine instance. 110 * Creates a new JavaScript engine instance.
105 * @param appInfo Information about the app. 111 * @param appInfo Information about the app.
112 * @param timer Implementation of timer.
106 * @param isolate v8::Isolate wrapper. This parameter should be considered 113 * @param isolate v8::Isolate wrapper. This parameter should be considered
107 * as a temporary hack for tests, it will go away. Issue #3593. 114 * as a temporary hack for tests, it will go away. Issue #3593.
108 * @return New `JsEngine` instance. 115 * @return New `JsEngine` instance.
109 */ 116 */
110 static JsEnginePtr New(const AppInfo& appInfo = AppInfo(), const ScopedV8Iso latePtr& isolate = ScopedV8IsolatePtr(new ScopedV8Isolate())); 117 static JsEnginePtr New(const AppInfo& appInfo = AppInfo(),
118 TimerPtr timer = CreateDefaultTimer(),
119 const ScopedV8IsolatePtr& isolate = ScopedV8IsolatePtr(new ScopedV8Isolate ()));
111 120
112 /** 121 /**
113 * Registers the callback function for an event. 122 * Registers the callback function for an event.
114 * @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
115 * general purpose event handling mechanism. 124 * general purpose event handling mechanism.
116 * @param callback Event callback function. 125 * @param callback Event callback function.
117 */ 126 */
118 void SetEventCallback(const std::string& eventName, EventCallback callback); 127 void SetEventCallback(const std::string& eventName, EventCallback callback);
119 128
120 /** 129 /**
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 /** 197 /**
189 * Returns a `JsEngine` instance contained in a `v8::Arguments` object. 198 * Returns a `JsEngine` instance contained in a `v8::Arguments` object.
190 * Use this in callbacks created via `NewCallback()` to retrieve the current 199 * Use this in callbacks created via `NewCallback()` to retrieve the current
191 * `JsEngine`. 200 * `JsEngine`.
192 * @param arguments `v8::Arguments` object containing the `JsEngine` 201 * @param arguments `v8::Arguments` object containing the `JsEngine`
193 * instance. 202 * instance.
194 * @return `JsEngine` instance from `v8::Arguments`. 203 * @return `JsEngine` instance from `v8::Arguments`.
195 */ 204 */
196 static JsEnginePtr FromArguments(const v8::Arguments& arguments); 205 static JsEnginePtr FromArguments(const v8::Arguments& arguments);
197 206
207 /*
208 * Private functionality required to implement timers.
209 * @param arguments `v8::Arguments` is the arguments received in C++
210 * callback associated for global setTimeout method.
211 */
212 static void ScheduleTimer(const v8::Arguments& arguments);
213
198 /** 214 /**
199 * Converts v8 arguments to `JsValue` objects. 215 * Converts v8 arguments to `JsValue` objects.
200 * @param arguments `v8::Arguments` object containing the arguments to 216 * @param arguments `v8::Arguments` object containing the arguments to
201 * convert. 217 * convert.
202 * @return List of arguments converted to `JsValue` objects. 218 * @return List of arguments converted to `JsValue` objects.
203 */ 219 */
204 JsValueList ConvertArguments(const v8::Arguments& arguments); 220 JsValueList ConvertArguments(const v8::Arguments& arguments);
205 221
206 /** 222 /**
207 * @see `SetFileSystem()`. 223 * @see `SetFileSystem()`.
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
264 void SetGlobalProperty(const std::string& name, AdblockPlus::JsValuePtr valu e); 280 void SetGlobalProperty(const std::string& name, AdblockPlus::JsValuePtr valu e);
265 281
266 /** 282 /**
267 * Returns a pointer to associated v8::Isolate. 283 * Returns a pointer to associated v8::Isolate.
268 */ 284 */
269 v8::Isolate* GetIsolate() 285 v8::Isolate* GetIsolate()
270 { 286 {
271 return isolate->Get(); 287 return isolate->Get();
272 } 288 }
273 289
274 // Private functionality required to implement timers. 290 private:
275 struct TimerTaskInfo 291 struct TimerTask
276 { 292 {
277 ~TimerTaskInfo(); 293 ~TimerTask();
278 int delay;
279 std::vector<std::unique_ptr<v8::Persistent<v8::Value>>> arguments; 294 std::vector<std::unique_ptr<v8::Persistent<v8::Value>>> arguments;
280 }; 295 };
281 typedef std::list<TimerTaskInfo> TimerTaskInfos; 296 typedef std::list<TimerTask> TimerTasks;
282 struct TimerTask 297 void CallTimerTask(TimerTasks::const_iterator timerTaskIterator);
283 { 298
284 std::weak_ptr<JsEngine> weakJsEngine; 299 explicit JsEngine(const ScopedV8IsolatePtr& isolate, TimerPtr timer);
285 TimerTaskInfos::const_iterator taskInfoIterator;
286 };
287 TimerTask CreateTimerTask(const v8::Arguments& arguments);
288 void CallTimerTask(TimerTaskInfos::const_iterator taskInfoIterator);
289 private:
290 explicit JsEngine(const ScopedV8IsolatePtr& isolate);
291 300
292 JsValuePtr GetGlobalObject(); 301 JsValuePtr GetGlobalObject();
293 302
294 /// 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
295 /// using it. 304 /// using it.
296 ScopedV8IsolatePtr isolate; 305 ScopedV8IsolatePtr isolate;
297 306
298 FileSystemPtr fileSystem; 307 FileSystemPtr fileSystem;
299 WebRequestPtr webRequest; 308 WebRequestPtr webRequest;
300 LogSystemPtr logSystem; 309 LogSystemPtr logSystem;
301 std::unique_ptr<v8::Persistent<v8::Context>> context; 310 std::unique_ptr<v8::Persistent<v8::Context>> context;
302 EventMap eventCallbacks; 311 EventMap eventCallbacks;
303 std::mutex eventCallbacksMutex; 312 std::mutex eventCallbacksMutex;
304 std::mutex isConnectionAllowedMutex; 313 std::mutex isConnectionAllowedMutex;
305 IsConnectionAllowedCallback isConnectionAllowed; 314 IsConnectionAllowedCallback isConnectionAllowed;
306 TimerTaskInfos timerTaskInfos; 315 TimerTasks timerTasks;
316 TimerPtr timer;
307 }; 317 };
308 } 318 }
309 319
310 #endif 320 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld