Index: include/AdblockPlus/JsEngine.h |
diff --git a/include/AdblockPlus/JsEngine.h b/include/AdblockPlus/JsEngine.h |
index ea6616e93082d3c8c33fe053ebbd5befd71544a9..f54b98febd3d256ac88fa027c572aae948bd24bc 100644 |
--- a/include/AdblockPlus/JsEngine.h |
+++ b/include/AdblockPlus/JsEngine.h |
@@ -89,17 +89,17 @@ namespace AdblockPlus |
friend class JsValue; |
friend class JsContext; |
+ struct JsWeakValuesList |
+ { |
+ ~JsWeakValuesList(); |
+ std::vector<std::unique_ptr<v8::Persistent<v8::Value>>> values; |
+ }; |
+ typedef std::list<JsWeakValuesList> JsWeakValuesLists; |
public: |
/** |
* Event callback function. |
*/ |
- typedef std::function<void(JsValueList& params)> EventCallback; |
- |
- /** |
- * Callback function returning false when current connection is not allowed |
- * e.g. because it is a metered connection. |
- */ |
- typedef std::function<bool()> IsConnectionAllowedCallback; |
+ typedef std::function<void(JsValueList&& params)> EventCallback; |
/** |
* Maps events to callback functions. |
@@ -107,6 +107,16 @@ namespace AdblockPlus |
typedef std::map<std::string, EventCallback> EventMap; |
/** |
+ * An opaque structure representing ID of stored JsValueList. |
+ * |
+ */ |
+ class JsWeakValuesID |
+ { |
+ friend class JsEngine; |
+ JsWeakValuesLists::const_iterator iterator; |
+ }; |
+ |
+ /** |
* Creates a new JavaScript engine instance. |
* @param appInfo Information about the app. |
* @param timer Implementation of timer. |
@@ -124,7 +134,7 @@ namespace AdblockPlus |
* general purpose event handling mechanism. |
* @param callback Event callback function. |
*/ |
- void SetEventCallback(const std::string& eventName, EventCallback callback); |
+ void SetEventCallback(const std::string& eventName, const EventCallback& callback); |
/** |
* Removes the callback function for an event. |
@@ -137,7 +147,7 @@ namespace AdblockPlus |
* @param eventName Event name. |
* @param params Event parameters. |
*/ |
- void TriggerEvent(const std::string& eventName, JsValueList& params); |
+ void TriggerEvent(const std::string& eventName, JsValueList&& params); |
/** |
* Evaluates a JavaScript expression. |
@@ -146,7 +156,7 @@ namespace AdblockPlus |
* messages. |
* @return Result of the evaluated expression. |
*/ |
- JsValuePtr Evaluate(const std::string& source, |
+ JsValue Evaluate(const std::string& source, |
const std::string& filename = ""); |
/** |
@@ -160,19 +170,19 @@ namespace AdblockPlus |
* @param val Value to convert. |
* @return New `JsValue` instance. |
*/ |
- JsValuePtr NewValue(const std::string& val); |
- JsValuePtr NewValue(int64_t val); |
- JsValuePtr NewValue(bool val); |
- inline JsValuePtr NewValue(const char* val) |
+ JsValue NewValue(const std::string& val); |
+ JsValue NewValue(int64_t val); |
+ JsValue NewValue(bool val); |
+ inline JsValue NewValue(const char* val) |
{ |
return NewValue(std::string(val)); |
} |
- inline JsValuePtr NewValue(int val) |
+ inline JsValue NewValue(int val) |
{ |
return NewValue(static_cast<int64_t>(val)); |
} |
#ifdef __APPLE__ |
- inline JsValuePtr NewValue(long val) |
+ inline JsValue NewValue(long val) |
{ |
return NewValue(static_cast<int64_t>(val)); |
} |
@@ -183,7 +193,7 @@ namespace AdblockPlus |
* Creates a new JavaScript object. |
* @return New `JsValue` instance. |
*/ |
- JsValuePtr NewObject(); |
+ JsValue NewObject(); |
/** |
* Creates a JavaScript function that invokes a C++ callback. |
@@ -192,7 +202,7 @@ namespace AdblockPlus |
* the current `JsEngine`. |
* @return New `JsValue` instance. |
*/ |
- JsValuePtr NewCallback(v8::InvocationCallback callback); |
+ JsValue NewCallback(const v8::InvocationCallback& callback); |
/** |
* Returns a `JsEngine` instance contained in a `v8::Arguments` object. |
@@ -204,6 +214,26 @@ namespace AdblockPlus |
*/ |
static JsEnginePtr FromArguments(const v8::Arguments& arguments); |
+ /** |
+ * Stores `JsValue`s in a way they don't keep a strong reference to |
+ * `JsEngine` and which are destroyed when `JsEngine` is destroyed. These |
+ * methods should be used when one needs to carry a JsValue in a callback |
+ * directly or indirectly passed to `JsEngine`. |
+ * The method is thread-safe. |
+ * @param `JsValueList` to store. |
+ * @return `JsWeakValuesID` of stored values which allows to restore them |
+ * later. |
+ */ |
+ JsWeakValuesID StoreJsValues(const JsValueList& values); |
+ |
+ /** |
+ * Extracts and removes from `JsEngine` earlier stored `JsValue`s. |
+ * The method is thread-safe. |
+ * @param id `JsWeakValuesID` of values. |
+ * @return `JsValueList` of stored values. |
+ */ |
+ JsValueList TakeJsValues(const JsWeakValuesID& id); |
+ |
/* |
* Private functionality required to implement timers. |
* @param arguments `v8::Arguments` is the arguments received in C++ |
@@ -215,14 +245,14 @@ namespace AdblockPlus |
* Converts v8 arguments to `JsValue` objects. |
* @param arguments `v8::Arguments` object containing the arguments to |
* convert. |
- * @return List of arguments converted to `JsValue` objects. |
+ * @return List of arguments converted to `const JsValue` objects. |
*/ |
JsValueList ConvertArguments(const v8::Arguments& arguments); |
/** |
* @see `SetFileSystem()`. |
*/ |
- FileSystemPtr GetFileSystem(); |
+ FileSystemPtr GetFileSystem() const; |
/** |
* Sets the `FileSystem` implementation used for all file I/O. |
@@ -230,12 +260,12 @@ namespace AdblockPlus |
* instance by default, which might be sufficient. |
* @param The `FileSystem` instance to use. |
*/ |
- void SetFileSystem(FileSystemPtr val); |
+ void SetFileSystem(const FileSystemPtr& val); |
/** |
* @see `SetWebRequest()`. |
*/ |
- WebRequestPtr GetWebRequest(); |
+ WebRequestPtr GetWebRequest() const; |
/** |
* Sets the `WebRequest` implementation used for XMLHttpRequests. |
@@ -243,25 +273,12 @@ namespace AdblockPlus |
* instance by default, which might be sufficient. |
* @param The `WebRequest` instance to use. |
*/ |
- void SetWebRequest(WebRequestPtr val); |
- |
- /** |
- * Registers the callback function to check whether current connection is |
- * allowed for network requests. |
- * @param callback callback function. |
- */ |
- void SetIsConnectionAllowedCallback(const IsConnectionAllowedCallback& callback); |
- |
- /** |
- * Checks whether current connection is allowed. If |
- * IsConnectionAllowedCallback is not set then then it returns true. |
- */ |
- bool IsConnectionAllowed(); |
+ void SetWebRequest(const WebRequestPtr& val); |
/** |
* @see `SetLogSystem()`. |
*/ |
- LogSystemPtr GetLogSystem(); |
+ LogSystemPtr GetLogSystem() const; |
/** |
* Sets the `LogSystem` implementation used for logging (e.g. to handle |
@@ -270,14 +287,14 @@ namespace AdblockPlus |
* instance by default, which might be sufficient. |
* @param The `LogSystem` instance to use. |
*/ |
- void SetLogSystem(LogSystemPtr val); |
+ void SetLogSystem(const LogSystemPtr& val); |
/** |
* Sets a global property that can be accessed by all the scripts. |
* @param name Name of the property to set. |
* @param value Value of the property to set. |
*/ |
- void SetGlobalProperty(const std::string& name, AdblockPlus::JsValuePtr value); |
+ void SetGlobalProperty(const std::string& name, const AdblockPlus::JsValue& value); |
/** |
* Returns a pointer to associated v8::Isolate. |
@@ -288,17 +305,11 @@ namespace AdblockPlus |
} |
private: |
- struct TimerTask |
- { |
- ~TimerTask(); |
- std::vector<std::unique_ptr<v8::Persistent<v8::Value>>> arguments; |
- }; |
- typedef std::list<TimerTask> TimerTasks; |
- void CallTimerTask(TimerTasks::const_iterator timerTaskIterator); |
+ void CallTimerTask(const JsWeakValuesID& timerParamsID); |
explicit JsEngine(const ScopedV8IsolatePtr& isolate, TimerPtr timer); |
- JsValuePtr GetGlobalObject(); |
+ JsValue GetGlobalObject(); |
/// Isolate must be disposed only after disposing of all objects which are |
/// using it. |
@@ -310,9 +321,8 @@ namespace AdblockPlus |
std::unique_ptr<v8::Persistent<v8::Context>> context; |
EventMap eventCallbacks; |
std::mutex eventCallbacksMutex; |
- std::mutex isConnectionAllowedMutex; |
- IsConnectionAllowedCallback isConnectionAllowed; |
- TimerTasks timerTasks; |
+ JsWeakValuesLists jsWeakValuesLists; |
+ std::mutex jsWeakValuesListsMutex; |
TimerPtr timer; |
}; |
} |