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

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

Issue 29424659: Noissue - generalize storing of `JsValue`s in `JsEngine` (Closed) Base URL: https://github.com/adblockplus/libadblockplus.git
Patch Set: add empty line Created April 28, 2017, 10:49 a.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 | « no previous file | src/JsEngine.cpp » ('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 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 typedef std::shared_ptr<ScopedV8Isolate> ScopedV8IsolatePtr; 82 typedef std::shared_ptr<ScopedV8Isolate> ScopedV8IsolatePtr;
83 83
84 /** 84 /**
85 * JavaScript engine used by `FilterEngine`, wraps v8. 85 * JavaScript engine used by `FilterEngine`, wraps v8.
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 struct JsWeakValuesList
93 {
94 ~JsWeakValuesList();
95 std::vector<std::unique_ptr<v8::Persistent<v8::Value>>> values;
96 };
97 typedef std::list<JsWeakValuesList> JsWeakValuesLists;
92 public: 98 public:
93 /** 99 /**
94 * Event callback function. 100 * Event callback function.
95 */ 101 */
96 typedef std::function<void(JsValueList&& params)> EventCallback; 102 typedef std::function<void(JsValueList&& params)> EventCallback;
97 103
98 /** 104 /**
99 * Callback function returning false when current connection is not allowed 105 * Callback function returning false when current connection is not allowed
100 * e.g. because it is a metered connection. 106 * e.g. because it is a metered connection.
101 */ 107 */
102 typedef std::function<bool()> IsConnectionAllowedCallback; 108 typedef std::function<bool()> IsConnectionAllowedCallback;
103 109
104 /** 110 /**
105 * Maps events to callback functions. 111 * Maps events to callback functions.
106 */ 112 */
107 typedef std::map<std::string, EventCallback> EventMap; 113 typedef std::map<std::string, EventCallback> EventMap;
108 114
109 /** 115 /**
116 * An opaque structure representing ID of stored JsValueList.
117 *
118 */
119 class JsWeakValuesID
120 {
121 friend class JsEngine;
122 JsWeakValuesLists::const_iterator iterator;
123 };
124
125 /**
110 * Creates a new JavaScript engine instance. 126 * Creates a new JavaScript engine instance.
111 * @param appInfo Information about the app. 127 * @param appInfo Information about the app.
112 * @param timer Implementation of timer. 128 * @param timer Implementation of timer.
113 * @param isolate v8::Isolate wrapper. This parameter should be considered 129 * @param isolate v8::Isolate wrapper. This parameter should be considered
114 * 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.
115 * @return New `JsEngine` instance. 131 * @return New `JsEngine` instance.
116 */ 132 */
117 static JsEnginePtr New(const AppInfo& appInfo = AppInfo(), 133 static JsEnginePtr New(const AppInfo& appInfo = AppInfo(),
118 TimerPtr timer = CreateDefaultTimer(), 134 TimerPtr timer = CreateDefaultTimer(),
119 const ScopedV8IsolatePtr& isolate = ScopedV8IsolatePtr(new ScopedV8Isolate ())); 135 const ScopedV8IsolatePtr& isolate = ScopedV8IsolatePtr(new ScopedV8Isolate ()));
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
197 /** 213 /**
198 * Returns a `JsEngine` instance contained in a `v8::Arguments` object. 214 * Returns a `JsEngine` instance contained in a `v8::Arguments` object.
199 * Use this in callbacks created via `NewCallback()` to retrieve the current 215 * Use this in callbacks created via `NewCallback()` to retrieve the current
200 * `JsEngine`. 216 * `JsEngine`.
201 * @param arguments `v8::Arguments` object containing the `JsEngine` 217 * @param arguments `v8::Arguments` object containing the `JsEngine`
202 * instance. 218 * instance.
203 * @return `JsEngine` instance from `v8::Arguments`. 219 * @return `JsEngine` instance from `v8::Arguments`.
204 */ 220 */
205 static JsEnginePtr FromArguments(const v8::Arguments& arguments); 221 static JsEnginePtr FromArguments(const v8::Arguments& arguments);
206 222
223 /**
224 * Stores `JsValue`s in a way they don't keep a strong reference to
225 * `JsEngine` and which are destroyed when `JsEngine` is destroyed. These
226 * methods should be used when one needs to carry a JsValue in a callback
227 * directly or indirectly passed to `JsEngine`.
228 * The method is thread-safe.
229 * @param `JsValueList` to store.
230 * @return `JsWeakValuesID` of stored values which allows to restore them
231 * later.
232 */
233 JsWeakValuesID StoreJsValues(const JsValueList& values);
234
235 /**
236 * Extracts and removes from `JsEngine` earlier stored `JsValue`s.
237 * The method is thread-safe.
238 * @param id `JsWeakValuesID` of values.
239 * @return `JsValueList` of stored values.
240 */
241 JsValueList TakeJsValues(const JsWeakValuesID& id);
242
207 /* 243 /*
208 * Private functionality required to implement timers. 244 * Private functionality required to implement timers.
209 * @param arguments `v8::Arguments` is the arguments received in C++ 245 * @param arguments `v8::Arguments` is the arguments received in C++
210 * callback associated for global setTimeout method. 246 * callback associated for global setTimeout method.
211 */ 247 */
212 static void ScheduleTimer(const v8::Arguments& arguments); 248 static void ScheduleTimer(const v8::Arguments& arguments);
213 249
214 /** 250 /**
215 * Converts v8 arguments to `JsValue` objects. 251 * Converts v8 arguments to `JsValue` objects.
216 * @param arguments `v8::Arguments` object containing the arguments to 252 * @param arguments `v8::Arguments` object containing the arguments to
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
281 317
282 /** 318 /**
283 * Returns a pointer to associated v8::Isolate. 319 * Returns a pointer to associated v8::Isolate.
284 */ 320 */
285 v8::Isolate* GetIsolate() 321 v8::Isolate* GetIsolate()
286 { 322 {
287 return isolate->Get(); 323 return isolate->Get();
288 } 324 }
289 325
290 private: 326 private:
291 struct TimerTask 327 void CallTimerTask(const JsWeakValuesID& timerParamsID);
292 {
293 ~TimerTask();
294 std::vector<std::unique_ptr<v8::Persistent<v8::Value>>> arguments;
295 };
296 typedef std::list<TimerTask> TimerTasks;
297 void CallTimerTask(const TimerTasks::const_iterator& timerTaskIterator);
298 328
299 explicit JsEngine(const ScopedV8IsolatePtr& isolate, TimerPtr timer); 329 explicit JsEngine(const ScopedV8IsolatePtr& isolate, TimerPtr timer);
300 330
301 JsValue GetGlobalObject(); 331 JsValue GetGlobalObject();
302 332
303 /// Isolate must be disposed only after disposing of all objects which are 333 /// Isolate must be disposed only after disposing of all objects which are
304 /// using it. 334 /// using it.
305 ScopedV8IsolatePtr isolate; 335 ScopedV8IsolatePtr isolate;
306 336
307 FileSystemPtr fileSystem; 337 FileSystemPtr fileSystem;
308 WebRequestPtr webRequest; 338 WebRequestPtr webRequest;
309 LogSystemPtr logSystem; 339 LogSystemPtr logSystem;
310 std::unique_ptr<v8::Persistent<v8::Context>> context; 340 std::unique_ptr<v8::Persistent<v8::Context>> context;
311 EventMap eventCallbacks; 341 EventMap eventCallbacks;
312 std::mutex eventCallbacksMutex; 342 std::mutex eventCallbacksMutex;
313 mutable std::mutex isConnectionAllowedMutex; 343 mutable std::mutex isConnectionAllowedMutex;
314 IsConnectionAllowedCallback isConnectionAllowed; 344 IsConnectionAllowedCallback isConnectionAllowed;
315 TimerTasks timerTasks; 345 JsWeakValuesLists jsWeakValuesLists;
346 std::mutex jsWeakValuesListsMutex;
316 TimerPtr timer; 347 TimerPtr timer;
317 }; 348 };
318 } 349 }
319 350
320 #endif 351 #endif
OLDNEW
« no previous file with comments | « no previous file | src/JsEngine.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld