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

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

Issue 6233220328718336: Issue #3593, #1197- fix isolate management (Closed)
Patch Set: address comments Created Jan. 27, 2016, 2:45 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 | libadblockplus.gyp » ('j') | src/JsEngine.cpp » ('J')
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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 namespace AdblockPlus 42 namespace AdblockPlus
43 { 43 {
44 class JsEngine; 44 class JsEngine;
45 45
46 /** 46 /**
47 * Shared smart pointer to a `JsEngine` instance. 47 * Shared smart pointer to a `JsEngine` instance.
48 */ 48 */
49 typedef std::shared_ptr<JsEngine> JsEnginePtr; 49 typedef std::shared_ptr<JsEngine> JsEnginePtr;
50 50
51 /** 51 /**
52 * Scope based isolate manager. Creates a new isolate instance on
53 * constructing and disposes it on destructing.
54 */
55 class ScopedV8Isolate
56 {
57 public:
58 ScopedV8Isolate();
59 ~ScopedV8Isolate();
60 v8::Isolate* Get()
61 {
62 return isolate;
63 }
64 private:
65 ScopedV8Isolate(const ScopedV8Isolate&);
66 ScopedV8Isolate& operator=(const ScopedV8Isolate&);
67
68 v8::Isolate* isolate;
69 };
70
71 /**
72 * Shared smart pointer to ScopedV8Isolate instance;
73 */
74 typedef std::shared_ptr<ScopedV8Isolate> ScopedV8IsolatePtr;
75
76 /**
52 * JavaScript engine used by `FilterEngine`, wraps v8. 77 * JavaScript engine used by `FilterEngine`, wraps v8.
53 */ 78 */
54 class JsEngine : public std::enable_shared_from_this<JsEngine> 79 class JsEngine : public std::enable_shared_from_this<JsEngine>
55 { 80 {
56 friend class JsValue; 81 friend class JsValue;
57 friend class JsContext; 82 friend class JsContext;
58 83
59 public: 84 public:
60 /** 85 /**
61 * Event callback function. 86 * Event callback function.
62 */ 87 */
63 typedef std::function<void(JsValueList& params)> EventCallback; 88 typedef std::function<void(JsValueList& params)> EventCallback;
64 89
65 /** 90 /**
66 * Maps events to callback functions. 91 * Maps events to callback functions.
67 */ 92 */
68 typedef std::map<std::string, EventCallback> EventMap; 93 typedef std::map<std::string, EventCallback> EventMap;
69 94
70 /** 95 /**
71 * Creates a new JavaScript engine instance. 96 * Creates a new JavaScript engine instance.
72 * @param appInfo Information about the app. 97 * @param appInfo Information about the app.
73 * @return New `JsEngine` instance. 98 * @return New `JsEngine` instance.
74 */ 99 */
75 static JsEnginePtr New(const AppInfo& appInfo = AppInfo()); 100 static JsEnginePtr New(const AppInfo& appInfo = AppInfo(), const ScopedV8Iso latePtr& isolate = ScopedV8IsolatePtr());
76 101
77 /** 102 /**
78 * Registers the callback function for an event. 103 * Registers the callback function for an event.
79 * @param eventName Event name. Note that this can be any string - it's a 104 * @param eventName Event name. Note that this can be any string - it's a
80 * general purpose event handling mechanism. 105 * general purpose event handling mechanism.
81 * @param callback Event callback function. 106 * @param callback Event callback function.
82 */ 107 */
83 void SetEventCallback(const std::string& eventName, EventCallback callback); 108 void SetEventCallback(const std::string& eventName, EventCallback callback);
84 109
85 /** 110 /**
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 */ 233 */
209 void SetLogSystem(LogSystemPtr val); 234 void SetLogSystem(LogSystemPtr val);
210 235
211 /** 236 /**
212 * Sets a global property that can be accessed by all the scripts. 237 * Sets a global property that can be accessed by all the scripts.
213 * @param name Name of the property to set. 238 * @param name Name of the property to set.
214 * @param value Value of the property to set. 239 * @param value Value of the property to set.
215 */ 240 */
216 void SetGlobalProperty(const std::string& name, AdblockPlus::JsValuePtr valu e); 241 void SetGlobalProperty(const std::string& name, AdblockPlus::JsValuePtr valu e);
217 242
243 /**
244 * Returns a pointer to associated v8::Isolate.
245 */
246 v8::Isolate* GetIsolate()
247 {
248 return isolate->Get();
249 }
250
218 private: 251 private:
219 JsEngine(); 252 explicit JsEngine(const ScopedV8IsolatePtr& isolate);
253
254 /// Isolate must be disposed only after disposing of all objects which are
255 /// using it.
256 ScopedV8IsolatePtr isolate;
Eric 2016/01/27 17:21:02 You should be able to declare this member 'const'.
sergei 2016/01/28 14:02:50 I don't think that it would be better here. I woul
Eric 2016/01/28 16:42:41 I've said before that this member should _not_ be
sergei 2016/01/29 11:31:14 I have just explained my point of view regarding u
Eric 2016/02/09 14:59:14 I am seriously beginning to think you don't actual
220 257
221 FileSystemPtr fileSystem; 258 FileSystemPtr fileSystem;
222 WebRequestPtr webRequest; 259 WebRequestPtr webRequest;
223 LogSystemPtr logSystem; 260 LogSystemPtr logSystem;
224 v8::Isolate* isolate;
225 std::unique_ptr<v8::Persistent<v8::Context>> context; 261 std::unique_ptr<v8::Persistent<v8::Context>> context;
226 EventMap eventCallbacks; 262 EventMap eventCallbacks;
227 JsValuePtr globalJsObject; 263 JsValuePtr globalJsObject;
228 }; 264 };
229 } 265 }
230 266
231 #endif 267 #endif
OLDNEW
« no previous file with comments | « no previous file | libadblockplus.gyp » ('j') | src/JsEngine.cpp » ('J')

Powered by Google App Engine
This is Rietveld