libadblockplus
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
JsEngine.h
1 /*
2  * This file is part of Adblock Plus <https://adblockplus.org/>,
3  * Copyright (C) 2006-present eyeo GmbH
4  *
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
7  * published by the Free Software Foundation.
8  *
9  * Adblock Plus is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef ADBLOCK_PLUS_JS_ENGINE_H
19 #define ADBLOCK_PLUS_JS_ENGINE_H
20 
21 #include <functional>
22 #include <map>
23 #include <list>
24 #include <stdexcept>
25 #include <stdint.h>
26 #include <string>
27 #include <mutex>
28 #include <AdblockPlus/AppInfo.h>
29 #include <AdblockPlus/LogSystem.h>
30 #include <AdblockPlus/IFileSystem.h>
31 #include <AdblockPlus/JsValue.h>
32 #include <AdblockPlus/IWebRequest.h>
33 #include <AdblockPlus/ITimer.h>
34 #include <AdblockPlus/Scheduler.h>
35 
36 namespace v8
37 {
38  class Isolate;
39  class Value;
40  class Context;
41  template<typename T> class FunctionCallbackInfo;
42  typedef void(*FunctionCallback)(const FunctionCallbackInfo<Value>& info);
43 }
44 
45 namespace AdblockPlus
46 {
47  class JsEngine;
48  class Platform;
49 
53  typedef std::shared_ptr<JsEngine> JsEnginePtr;
54 
60  {
61  virtual ~IV8IsolateProvider() {}
62 
67  virtual v8::Isolate* Get() = 0;
68  };
69 
73  class JsEngine : public std::enable_shared_from_this<JsEngine>
74  {
75  friend class JsValue;
76  friend class JsContext;
77 
78  struct JsWeakValuesList
79  {
80  ~JsWeakValuesList();
81  std::vector<v8::Global<v8::Value>> values;
82  };
83  typedef std::list<JsWeakValuesList> JsWeakValuesLists;
84  public:
88  typedef std::function<void(JsValueList&& params)> EventCallback;
89 
93  typedef std::map<std::string, EventCallback> EventMap;
94 
99  {
100  friend class JsEngine;
101  JsWeakValuesLists::const_iterator iterator;
102  };
103 
114  static JsEnginePtr New(const AppInfo& appInfo, Platform& platform, std::unique_ptr<IV8IsolateProvider> isolate = nullptr);
121  void SetEventCallback(const std::string& eventName, const EventCallback& callback);
122 
127  void RemoveEventCallback(const std::string& eventName);
128 
134  void TriggerEvent(const std::string& eventName, JsValueList&& params);
135 
143  JsValue Evaluate(const std::string& source,
144  const std::string& filename = "");
145 
149  void Gc();
150 
152 
157  JsValue NewValue(const std::string& val);
158  JsValue NewValue(int64_t val);
159  JsValue NewValue(bool val);
160  inline JsValue NewValue(const char* val)
161  {
162  return NewValue(std::string(val));
163  }
164  inline JsValue NewValue(int val)
165  {
166  return NewValue(static_cast<int64_t>(val));
167  }
168 #ifdef __APPLE__
169  inline JsValue NewValue(long val)
170  {
171  return NewValue(static_cast<int64_t>(val));
172  }
173 #endif
174 
175 
180  JsValue NewObject();
181 
189  JsValue NewCallback(const v8::FunctionCallback& callback);
190 
199  static JsEnginePtr FromArguments(const v8::FunctionCallbackInfo<v8::Value>& arguments);
200 
211  JsWeakValuesID StoreJsValues(const JsValueList& values);
212 
219  JsValueList TakeJsValues(const JsWeakValuesID& id);
220 
227  JsValueList GetJsValues(const JsWeakValuesID& id);
228 
229  /*
230  * Private functionality required to implement timers.
231  * @param arguments `v8::FunctionCallbackInfo` is the arguments received in C++
232  * callback associated for global setTimeout method.
233  */
234  static void ScheduleTimer(const v8::FunctionCallbackInfo<v8::Value>& arguments);
235 
236  /*
237  * Private functionality required to implement web requests.
238  * @param arguments `v8::FunctionCallbackInfo` is the arguments received in C++
239  * callback associated for global GET method.
240  */
241  static void ScheduleWebRequest(const v8::FunctionCallbackInfo<v8::Value>& arguments);
242 
250 
256  void SetGlobalProperty(const std::string& name, const AdblockPlus::JsValue& value);
257 
261  v8::Isolate* GetIsolate()
262  {
263  return isolate->Get();
264  }
265 
270  void NotifyLowMemory();
271 
276  {
277  return platform;
278  }
279  private:
280  void CallTimerTask(const JsWeakValuesID& timerParamsID);
281 
282  explicit JsEngine(Platform& platform, std::unique_ptr<IV8IsolateProvider> isolate);
283 
284  JsValue GetGlobalObject();
285 
286  Platform& platform;
289  std::unique_ptr<IV8IsolateProvider> isolate;
290 
291  std::unique_ptr<v8::Global<v8::Context>> context;
292  EventMap eventCallbacks;
293  std::mutex eventCallbacksMutex;
294  JsWeakValuesLists jsWeakValuesLists;
295  std::mutex jsWeakValuesListsMutex;
296  };
297 }
298 
299 #endif
void RemoveEventCallback(const std::string &eventName)
Removes the callback function for an event.
std::function< void(JsValueList &&params)> EventCallback
Event callback function.
Definition: JsEngine.h:88
Platform & GetPlatform()
Private functionality.
Definition: JsEngine.h:275
Information about the app using libadblockplus.
Definition: AppInfo.h:28
JavaScript engine used by FilterEngine, wraps v8.
Definition: JsEngine.h:73
v8::Isolate * GetIsolate()
Returns a pointer to associated v8::Isolate.
Definition: JsEngine.h:261
JsValue NewValue(int val)
Creates a new JavaScript value.
Definition: JsEngine.h:164
void SetGlobalProperty(const std::string &name, const AdblockPlus::JsValue &value)
Sets a global property that can be accessed by all the scripts.
std::vector< AdblockPlus::JsValue > JsValueList
List of JavaScript values.
Definition: JsValue.h:48
Wrapper for JavaScript values.
Definition: JsValue.h:54
JsValueList TakeJsValues(const JsWeakValuesID &id)
Extracts and removes from JsEngine earlier stored JsValues.
JsValue NewValue(const std::string &val)
Creates a new JavaScript value.
JsValue NewCallback(const v8::FunctionCallback &callback)
Creates a JavaScript function that invokes a C++ callback.
static JsEnginePtr New(const AppInfo &appInfo, Platform &platform, std::unique_ptr< IV8IsolateProvider > isolate=nullptr)
Creates a new JavaScript engine instance.
JsValue NewValue(const char *val)
Creates a new JavaScript value.
Definition: JsEngine.h:160
Definition: JsEngine.h:41
JsValue NewObject()
Creates a new JavaScript object.
Main namespace of libadblockplus.
Definition: ActiveObject.h:23
Definition: JsEngine.h:36
AdblockPlus platform is the main component providing access to other modules.
Definition: Platform.h:44
void TriggerEvent(const std::string &eventName, JsValueList &&params)
Triggers an event.
JsValue Evaluate(const std::string &source, const std::string &filename="")
Evaluates a JavaScript expression.
void Gc()
Initiates a garbage collection.
JsValueList ConvertArguments(const v8::FunctionCallbackInfo< v8::Value > &arguments)
Converts v8 arguments to JsValue objects.
Provides with isolate.
Definition: JsEngine.h:59
static JsEnginePtr FromArguments(const v8::FunctionCallbackInfo< v8::Value > &arguments)
Returns a JsEngine instance contained in a v8::FunctionCallbackInfo object.
std::map< std::string, EventCallback > EventMap
Maps events to callback functions.
Definition: JsEngine.h:93
JsWeakValuesID StoreJsValues(const JsValueList &values)
Stores JsValues in a way they don't keep a strong reference to JsEngine and which are destroyed when ...
An opaque structure representing ID of stored JsValueList.
Definition: JsEngine.h:98
JsValueList GetJsValues(const JsWeakValuesID &id)
Extracts earlier stored JsValues from JsEgnine but does not remove them, one still must call TakeJsVa...
void NotifyLowMemory()
Notifies JS engine about critically low memory what should cause a garbage collection.
virtual v8::Isolate * Get()=0
Returns v8::Isolate.
void SetEventCallback(const std::string &eventName, const EventCallback &callback)
Registers the callback function for an event.
std::shared_ptr< JsEngine > JsEnginePtr
Shared smart pointer to a JsEngine instance.
Definition: JsEngine.h:48