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: rebase only Created Jan. 22, 2016, 10:38 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 | libadblockplus.gyp » ('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-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* GetIsolate()
61 {
62 return isolate;
63 }
64 protected:
65 v8::Isolate* isolate;
66 };
67
68 /**
69 * Shared smart pointer to ScopedV8Isolate instance;
70 */
71 typedef std::shared_ptr<ScopedV8Isolate> ScopedV8IsolatePtr;
72
73 /**
52 * JavaScript engine used by `FilterEngine`, wraps v8. 74 * JavaScript engine used by `FilterEngine`, wraps v8.
53 */ 75 */
54 class JsEngine : public std::enable_shared_from_this<JsEngine> 76 class JsEngine : public std::enable_shared_from_this<JsEngine>
55 { 77 {
56 friend class JsValue; 78 friend class JsValue;
57 friend class JsContext; 79 friend class JsContext;
58 80
59 public: 81 public:
60 /** 82 /**
61 * Event callback function. 83 * Event callback function.
62 */ 84 */
63 typedef std::function<void(JsValueList& params)> EventCallback; 85 typedef std::function<void(JsValueList& params)> EventCallback;
64 86
65 /** 87 /**
66 * Maps events to callback functions. 88 * Maps events to callback functions.
67 */ 89 */
68 typedef std::map<std::string, EventCallback> EventMap; 90 typedef std::map<std::string, EventCallback> EventMap;
69 91
70 /** 92 /**
71 * Creates a new JavaScript engine instance. 93 * Creates a new JavaScript engine instance.
72 * @param appInfo Information about the app. 94 * @param appInfo Information about the app.
73 * @return New `JsEngine` instance. 95 * @return New `JsEngine` instance.
74 */ 96 */
75 static JsEnginePtr New(const AppInfo& appInfo = AppInfo()); 97 static JsEnginePtr New(const AppInfo& appInfo = AppInfo(), const ScopedV8Iso latePtr& isolate = ScopedV8IsolatePtr());
76 98
77 /** 99 /**
78 * Registers the callback function for an event. 100 * Registers the callback function for an event.
79 * @param eventName Event name. Note that this can be any string - it's a 101 * @param eventName Event name. Note that this can be any string - it's a
80 * general purpose event handling mechanism. 102 * general purpose event handling mechanism.
81 * @param callback Event callback function. 103 * @param callback Event callback function.
82 */ 104 */
83 void SetEventCallback(const std::string& eventName, EventCallback callback); 105 void SetEventCallback(const std::string& eventName, EventCallback callback);
84 106
85 /** 107 /**
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 */ 230 */
209 void SetLogSystem(LogSystemPtr val); 231 void SetLogSystem(LogSystemPtr val);
210 232
211 /** 233 /**
212 * Sets a global property that can be accessed by all the scripts. 234 * Sets a global property that can be accessed by all the scripts.
213 * @param name Name of the property to set. 235 * @param name Name of the property to set.
214 * @param value Value of the property to set. 236 * @param value Value of the property to set.
215 */ 237 */
216 void SetGlobalProperty(const std::string& name, AdblockPlus::JsValuePtr valu e); 238 void SetGlobalProperty(const std::string& name, AdblockPlus::JsValuePtr valu e);
217 239
240 /**
241 * Returns a pointer to associated v8::Isolate.
242 */
243 v8::Isolate* GetIsolate()
244 {
245 return isolate->GetIsolate();
246 }
247
218 private: 248 private:
219 JsEngine(); 249 explicit JsEngine(const ScopedV8IsolatePtr& isolate);
250
251 /// Isolate must be disposed only after disposing of all objects which are
252 /// using it.
253 ScopedV8IsolatePtr isolate;
220 254
221 FileSystemPtr fileSystem; 255 FileSystemPtr fileSystem;
222 WebRequestPtr webRequest; 256 WebRequestPtr webRequest;
223 LogSystemPtr logSystem; 257 LogSystemPtr logSystem;
224 v8::Isolate* isolate;
225 std::unique_ptr<v8::Persistent<v8::Context>> context; 258 std::unique_ptr<v8::Persistent<v8::Context>> context;
226 EventMap eventCallbacks; 259 EventMap eventCallbacks;
227 JsValuePtr globalJsObject; 260 JsValuePtr globalJsObject;
228 }; 261 };
229 } 262 }
230 263
231 #endif 264 #endif
OLDNEW
« no previous file with comments | « no previous file | libadblockplus.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld