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

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

Issue 29661564: Template out V8 and prepare for other JS engine implementations
Patch Set: Created Jan. 10, 2018, 1:29 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 | « no previous file | 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-present eyeo GmbH 3 * Copyright (C) 2006-present 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 26 matching lines...) Expand all
37 { 37 {
38 class Isolate; 38 class Isolate;
39 class Value; 39 class Value;
40 class Context; 40 class Context;
41 template<typename T> class FunctionCallbackInfo; 41 template<typename T> class FunctionCallbackInfo;
42 typedef void(*FunctionCallback)(const FunctionCallbackInfo<v8::Value>& info); 42 typedef void(*FunctionCallback)(const FunctionCallbackInfo<v8::Value>& info);
43 } 43 }
44 44
45 namespace AdblockPlus 45 namespace AdblockPlus
46 { 46 {
47 class JsEngine;
48 class Platform; 47 class Platform;
48 template <typename T> class JsEngineTemplate;
49 49
50 /** 50 /**
51 * Shared smart pointer to a `JsEngine` instance. 51 * Shared smart pointer to a `JsEngine` instance.
52 */ 52 */
53 typedef std::shared_ptr<JsEngine> JsEnginePtr; 53 typedef std::shared_ptr<JsEngine> JsEnginePtr;
54 54
55 /** 55 /**
56 * Provides with isolate. The main aim of this iterface is to delegate a 56 * Provides with isolate. The main aim of this iterface is to delegate a
57 * proper initialization and deinitialization of v8::Isolate to an embedder. 57 * proper initialization and deinitialization of v8::Isolate to an embedder.
58 */ 58 */
59 struct IV8IsolateProvider 59 struct IV8IsolateProvider
60 { 60 {
61 virtual ~IV8IsolateProvider() {} 61 virtual ~IV8IsolateProvider() {}
62 62
63 /** 63 /**
64 * Returns v8::Isolate. All subsequent calls of this method should return 64 * Returns v8::Isolate. All subsequent calls of this method should return
65 * the same pointer to v8::Isolate as the first call. 65 * the same pointer to v8::Isolate as the first call.
66 */ 66 */
67 virtual v8::Isolate* Get() = 0; 67 virtual v8::Isolate* Get() = 0;
68 }; 68 };
69 69
70 template <typename T>
71 struct JsWeakValuesList
72 {
73 ~JsWeakValuesList();
74 std::vector<T> values;
75 };
76 typedef std::list<JsWeakValuesList<v8::Global<v8::Value>>> JsWeakValuesLists;
77
78 /**
79 * An opaque structure representing ID of stored JsValueList.
80 */
81 class JsWeakValuesID
82 {
83 template <typename T> friend class JsEngineTemplate;
84 JsWeakValuesLists::const_iterator iterator;
85 };
86
87
70 /** 88 /**
71 * JavaScript engine used by `FilterEngine`, wraps v8. 89 * JavaScript engine used by `FilterEngine`, wraps v8.
72 */ 90 */
73 class JsEngine : public std::enable_shared_from_this<JsEngine> 91 template <typename T> class JsEngineTemplate : public std::enable_shared_from_ this<JsEngineTemplate<T>>
74 { 92 {
75 friend class JsValue; 93 template <typename T> friend class JsValueTemplate;
76 friend class JsContext; 94 template <typename T> friend class JsContextTemplate;
77 95
78 struct JsWeakValuesList
79 {
80 ~JsWeakValuesList();
81 std::vector<v8::Global<v8::Value>> values;
82 };
83 typedef std::list<JsWeakValuesList> JsWeakValuesLists;
84 public: 96 public:
85 /** 97 /**
86 * Event callback function. 98 * Event callback function.
87 */ 99 */
88 typedef std::function<void(JsValueList&& params)> EventCallback; 100 typedef std::function<void(JsValueList&& params)> EventCallback;
89 101
90 /** 102 /**
91 * Maps events to callback functions. 103 * Maps events to callback functions.
92 */ 104 */
93 typedef std::map<std::string, EventCallback> EventMap; 105 typedef std::map<std::string, EventCallback> EventMap;
94 106
95 /** 107 /**
96 * An opaque structure representing ID of stored JsValueList.
97 */
98 class JsWeakValuesID
99 {
100 friend class JsEngine;
101 JsWeakValuesLists::const_iterator iterator;
102 };
103
104 /**
105 * Creates a new JavaScript engine instance. 108 * Creates a new JavaScript engine instance.
106 * 109 *
107 * @param appInfo Information about the app. 110 * @param appInfo Information about the app.
108 * @param platform AdblockPlus platform providing with necessary 111 * @param platform AdblockPlus platform providing with necessary
109 * dependencies. 112 * dependencies.
110 * @param isolate A provider of v8::Isolate, if the value is nullptr then 113 * @param isolate A provider of v8::Isolate, if the value is nullptr then
111 * a default implementation is used. 114 * a default implementation is used.
112 * @return New `JsEngine` instance. 115 * @return New `JsEngine` instance.
113 */ 116 */
114 static JsEnginePtr New(const AppInfo& appInfo, Platform& platform, std::uniq ue_ptr<IV8IsolateProvider> isolate = nullptr); 117 static JsEnginePtr New(const AppInfo& appInfo, Platform& platform, std::uniq ue_ptr<IV8IsolateProvider> isolate = nullptr);
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 */ 182 */
180 JsValue NewObject(); 183 JsValue NewObject();
181 184
182 /** 185 /**
183 * Creates a JavaScript function that invokes a C++ callback. 186 * Creates a JavaScript function that invokes a C++ callback.
184 * @param callback C++ callback to invoke. The callback receives a 187 * @param callback C++ callback to invoke. The callback receives a
185 * `v8::FunctionCallbackInfo` object and can use `FromArguments()` to retrieve 188 * `v8::FunctionCallbackInfo` object and can use `FromArguments()` to retrieve
186 * the current `JsEngine`. 189 * the current `JsEngine`.
187 * @return New `JsValue` instance. 190 * @return New `JsValue` instance.
188 */ 191 */
189 JsValue NewCallback(const v8::FunctionCallback& callback); 192 JsValue NewCallback(const typename T::CallbackFunc& callback);
190 193
191 /** 194 /**
192 * Returns a `JsEngine` instance contained in a `v8::FunctionCallbackInfo` o bject. 195 * Returns a `JsEngine` instance contained in a `v8::FunctionCallbackInfo` o bject.
193 * Use this in callbacks created via `NewCallback()` to retrieve the current 196 * Use this in callbacks created via `NewCallback()` to retrieve the current
194 * `JsEngine`. 197 * `JsEngine`.
195 * @param arguments `v8::FunctionCallbackInfo` object containing the `JsEngi ne` 198 * @param arguments `v8::FunctionCallbackInfo` object containing the `JsEngi ne`
196 * instance. 199 * instance.
197 * @return `JsEngine` instance from `v8::FunctionCallbackInfo`. 200 * @return `JsEngine` instance from `v8::FunctionCallbackInfo`.
198 */ 201 */
199 static JsEnginePtr FromArguments(const v8::FunctionCallbackInfo<v8::Value>& arguments); 202 static JsEnginePtr FromArguments(const typename T::CallbackFuncInfo& argumen ts);
200 203
201 /** 204 /**
202 * Stores `JsValue`s in a way they don't keep a strong reference to 205 * Stores `JsValue`s in a way they don't keep a strong reference to
203 * `JsEngine` and which are destroyed when `JsEngine` is destroyed. These 206 * `JsEngine` and which are destroyed when `JsEngine` is destroyed. These
204 * methods should be used when one needs to carry a JsValue in a callback 207 * methods should be used when one needs to carry a JsValue in a callback
205 * directly or indirectly passed to `JsEngine`. 208 * directly or indirectly passed to `JsEngine`.
206 * The method is thread-safe. 209 * The method is thread-safe.
207 * @param `JsValueList` to store. 210 * @param `JsValueList` to store.
208 * @return `JsWeakValuesID` of stored values which allows to restore them 211 * @return `JsWeakValuesID` of stored values which allows to restore them
209 * later. 212 * later.
210 */ 213 */
211 JsWeakValuesID StoreJsValues(const JsValueList& values); 214 JsWeakValuesID StoreJsValues(const JsValueList& values);
212 215
213 /** 216 /**
214 * Extracts and removes from `JsEngine` earlier stored `JsValue`s. 217 * Extracts and removes from `JsEngine` earlier stored `JsValue`s.
215 * The method is thread-safe. 218 * The method is thread-safe.
216 * @param id `JsWeakValuesID` of values. 219 * @param id `JsWeakValuesID` of values.
217 * @return `JsValueList` of stored values. 220 * @return `JsValueList` of stored values.
218 */ 221 */
219 JsValueList TakeJsValues(const JsWeakValuesID& id); 222 JsValueList TakeJsValues(const JsWeakValuesID& id);
220 223
221 /* 224 /*
222 * Private functionality required to implement timers. 225 * Private functionality required to implement timers.
223 * @param arguments `v8::FunctionCallbackInfo` is the arguments received in C++ 226 * @param arguments `v8::FunctionCallbackInfo` is the arguments received in C++
224 * callback associated for global setTimeout method. 227 * callback associated for global setTimeout method.
225 */ 228 */
226 static void ScheduleTimer(const v8::FunctionCallbackInfo<v8::Value>& argumen ts); 229 static void ScheduleTimer(const typename T::CallbackFuncInfo& arguments);
227 230
228 /* 231 /*
229 * Private functionality required to implement web requests. 232 * Private functionality required to implement web requests.
230 * @param arguments `v8::FunctionCallbackInfo` is the arguments received in C++ 233 * @param arguments `v8::FunctionCallbackInfo` is the arguments received in C++
231 * callback associated for global GET method. 234 * callback associated for global GET method.
232 */ 235 */
233 static void ScheduleWebRequest(const v8::FunctionCallbackInfo<v8::Value>& ar guments); 236 static void ScheduleWebRequest(const typename T::CallbackFuncInfo& arguments );
234 237
235 /** 238 /**
236 * Converts v8 arguments to `JsValue` objects. 239 * Converts v8 arguments to `JsValue` objects.
237 * @param arguments `v8::FunctionCallbackInfo` object containing the argumen ts to 240 * @param arguments `v8::FunctionCallbackInfo` object containing the argumen ts to
238 * convert. 241 * convert.
239 * @return List of arguments converted to `const JsValue` objects. 242 * @return List of arguments converted to `const JsValue` objects.
240 */ 243 */
241 JsValueList ConvertArguments(const v8::FunctionCallbackInfo<v8::Value>& argu ments); 244 JsValueList ConvertArguments(const typename T::CallbackFuncInfo& arguments);
242 245
243 /** 246 /**
244 * Sets a global property that can be accessed by all the scripts. 247 * Sets a global property that can be accessed by all the scripts.
245 * @param name Name of the property to set. 248 * @param name Name of the property to set.
246 * @param value Value of the property to set. 249 * @param value Value of the property to set.
247 */ 250 */
248 void SetGlobalProperty(const std::string& name, const AdblockPlus::JsValue& value); 251 void SetGlobalProperty(const std::string& name, const AdblockPlus::JsValue& value);
249 252
250 /** 253 /**
251 * Returns a pointer to associated v8::Isolate. 254 * Returns a pointer to associated v8::Isolate.
252 */ 255 */
253 v8::Isolate* GetIsolate() 256 typename T::Isolate* GetIsolate()
254 { 257 {
255 return isolate->Get(); 258 return isolate->Get();
256 } 259 }
257 260
258 /** 261 /**
259 * Notifies JS engine about critically low memory what should cause a 262 * Notifies JS engine about critically low memory what should cause a
260 * garbage collection. 263 * garbage collection.
261 */ 264 */
262 void NotifyLowMemory(); 265 void NotifyLowMemory();
263 266
264 /** 267 /**
265 * Private functionality. 268 * Private functionality.
266 */ 269 */
267 Platform& GetPlatform() 270 Platform& GetPlatform()
268 { 271 {
269 return platform; 272 return platform;
270 } 273 }
271 private: 274 private:
272 void CallTimerTask(const JsWeakValuesID& timerParamsID); 275 void CallTimerTask(const JsWeakValuesID& timerParamsID);
273 276
274 explicit JsEngine(Platform& platform, std::unique_ptr<IV8IsolateProvider> is olate); 277 explicit JsEngineTemplate(Platform& platform, std::unique_ptr<IV8IsolateProv ider> isolate);
275 278
276 JsValue GetGlobalObject(); 279 JsValue GetGlobalObject();
277 280
278 Platform& platform; 281 Platform& platform;
279 /// Isolate must be disposed only after disposing of all objects which are 282 /// Isolate must be disposed only after disposing of all objects which are
280 /// using it. 283 /// using it.
281 std::unique_ptr<IV8IsolateProvider> isolate; 284 std::unique_ptr<IV8IsolateProvider> isolate;
282 285
283 std::unique_ptr<v8::Global<v8::Context>> context; 286 std::unique_ptr<typename T::GlobalContext> context;
284 EventMap eventCallbacks; 287 EventMap eventCallbacks;
285 std::mutex eventCallbacksMutex; 288 std::mutex eventCallbacksMutex;
286 JsWeakValuesLists jsWeakValuesLists; 289 JsWeakValuesLists jsWeakValuesLists;
287 std::mutex jsWeakValuesListsMutex; 290 std::mutex jsWeakValuesListsMutex;
288 }; 291 };
289 } 292 }
290 293
291 #endif 294 #endif
OLDNEW
« no previous file with comments | « no previous file | include/AdblockPlus/JsValue.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld