LEFT | RIGHT |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2015 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 |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 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/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 #ifndef ADBLOCK_PLUS_JS_ENGINE_H | 18 #ifndef ADBLOCK_PLUS_JS_ENGINE_H |
19 #define ADBLOCK_PLUS_JS_ENGINE_H | 19 #define ADBLOCK_PLUS_JS_ENGINE_H |
20 | 20 |
21 #include <functional> | 21 #include <functional> |
22 #include <map> | 22 #include <map> |
23 #include <memory> | |
24 #include <stdexcept> | 23 #include <stdexcept> |
25 #include <stdint.h> | 24 #include <stdint.h> |
26 #include <string> | 25 #include <string> |
27 #include <AdblockPlus/AppInfo.h> | 26 #include <AdblockPlus/AppInfo.h> |
28 #include <AdblockPlus/LogSystem.h> | 27 #include <AdblockPlus/LogSystem.h> |
29 #include <AdblockPlus/FileSystem.h> | 28 #include <AdblockPlus/FileSystem.h> |
30 #include <AdblockPlus/JsValue.h> | 29 #include <AdblockPlus/JsValue.h> |
31 #include <AdblockPlus/WebRequest.h> | 30 #include <AdblockPlus/WebRequest.h> |
32 | 31 |
33 #include "V8ValueHolder.h" | 32 #include "V8ValueHolder.h" |
34 | 33 |
35 namespace v8 | 34 namespace v8 |
36 { | 35 { |
37 class Arguments; | 36 class Arguments; |
38 class Isolate; | 37 class Isolate; |
39 class Value; | 38 class Value; |
40 class Context; | 39 class Context; |
41 template<class T> class Handle; | 40 template<class T> class Handle; |
42 typedef Handle<Value>(*InvocationCallback)(const Arguments &args); | 41 typedef Handle<Value>(*InvocationCallback)(const Arguments &args); |
43 } | 42 } |
44 | 43 |
45 namespace AdblockPlus | 44 namespace AdblockPlus |
46 { | 45 { |
47 class JsEngine; | 46 class JsEngine; |
| 47 |
| 48 /** |
| 49 * Shared smart pointer to a `JsEngine` instance. |
| 50 */ |
48 typedef std::shared_ptr<JsEngine> JsEnginePtr; | 51 typedef std::shared_ptr<JsEngine> JsEnginePtr; |
49 | 52 |
| 53 /** |
| 54 * JavaScript engine used by `FilterEngine`, wraps v8. |
| 55 */ |
50 class JsEngine : public std::enable_shared_from_this<JsEngine> | 56 class JsEngine : public std::enable_shared_from_this<JsEngine> |
51 { | 57 { |
52 friend class JsValue; | 58 friend class JsValue; |
53 friend class JsContext; | 59 friend class JsContext; |
54 | 60 |
55 public: | 61 public: |
| 62 /** |
| 63 * Event callback function. |
| 64 */ |
56 typedef std::function<void(JsValueList& params)> EventCallback; | 65 typedef std::function<void(JsValueList& params)> EventCallback; |
| 66 |
| 67 /** |
| 68 * Maps events to callback functions. |
| 69 */ |
57 typedef std::map<std::string, EventCallback> EventMap; | 70 typedef std::map<std::string, EventCallback> EventMap; |
58 | 71 |
| 72 /** |
| 73 * Creates a new JavaScript engine instance. |
| 74 * @param appInfo Information about the app. |
| 75 * @return New `JsEngine` instance. |
| 76 */ |
59 static JsEnginePtr New(const AppInfo& appInfo = AppInfo()); | 77 static JsEnginePtr New(const AppInfo& appInfo = AppInfo()); |
| 78 |
| 79 /** |
| 80 * Registers the callback function for an event. |
| 81 * @param eventName Event name. Note that this can be any string - it's a |
| 82 * general purpose event handling mechanism. |
| 83 * @param callback Event callback function. |
| 84 */ |
60 void SetEventCallback(const std::string& eventName, EventCallback callback); | 85 void SetEventCallback(const std::string& eventName, EventCallback callback); |
| 86 |
| 87 /** |
| 88 * Removes the callback function for an event. |
| 89 * @param eventName Event name. |
| 90 */ |
61 void RemoveEventCallback(const std::string& eventName); | 91 void RemoveEventCallback(const std::string& eventName); |
| 92 |
| 93 /** |
| 94 * Triggers an event. |
| 95 * @param eventName Event name. |
| 96 * @param params Event parameters. |
| 97 */ |
62 void TriggerEvent(const std::string& eventName, JsValueList& params); | 98 void TriggerEvent(const std::string& eventName, JsValueList& params); |
| 99 |
| 100 /** |
| 101 * Evaluates a JavaScript expression. |
| 102 * @param source JavaScript expression to evaluate. |
| 103 * @param filename Optional file name for the expression, used in error |
| 104 * messages. |
| 105 * @return Result of the evaluated expression. |
| 106 */ |
63 JsValuePtr Evaluate(const std::string& source, | 107 JsValuePtr Evaluate(const std::string& source, |
64 const std::string& filename = ""); | 108 const std::string& filename = ""); |
| 109 |
| 110 /** |
| 111 * Initiates a garbage collection. |
| 112 */ |
65 void Gc(); | 113 void Gc(); |
| 114 |
| 115 //@{ |
| 116 /** |
| 117 * Creates a new JavaScript value. |
| 118 * @param val Value to convert. |
| 119 * @return New `JsValue` instance. |
| 120 */ |
66 JsValuePtr NewValue(const std::string& val); | 121 JsValuePtr NewValue(const std::string& val); |
67 JsValuePtr NewValue(int64_t val); | 122 JsValuePtr NewValue(int64_t val); |
68 JsValuePtr NewValue(bool val); | 123 JsValuePtr NewValue(bool val); |
69 inline JsValuePtr NewValue(const char* val) | 124 inline JsValuePtr NewValue(const char* val) |
70 { | 125 { |
71 return NewValue(std::string(val)); | 126 return NewValue(std::string(val)); |
72 } | 127 } |
73 inline JsValuePtr NewValue(int val) | 128 inline JsValuePtr NewValue(int val) |
74 { | 129 { |
75 return NewValue(static_cast<int64_t>(val)); | 130 return NewValue(static_cast<int64_t>(val)); |
76 } | 131 } |
77 #ifdef __APPLE__ | 132 #ifdef __APPLE__ |
78 inline JsValuePtr NewValue(long val) | 133 inline JsValuePtr NewValue(long val) |
79 { | 134 { |
80 return NewValue(static_cast<int64_t>(val)); | 135 return NewValue(static_cast<int64_t>(val)); |
81 } | 136 } |
82 #endif | 137 #endif |
| 138 //@} |
| 139 |
| 140 /** |
| 141 * Creates a new JavaScript object. |
| 142 * @return New `JsValue` instance. |
| 143 */ |
83 JsValuePtr NewObject(); | 144 JsValuePtr NewObject(); |
| 145 |
| 146 /** |
| 147 * Creates a JavaScript function that invokes a C++ callback. |
| 148 * @param callback C++ callback to invoke. The callback receives a |
| 149 * `v8::Arguments` object and can use `FromArguments()` to retrieve |
| 150 * the current `JsEngine`. |
| 151 * @return New `JsValue` instance. |
| 152 */ |
84 JsValuePtr NewCallback(v8::InvocationCallback callback); | 153 JsValuePtr NewCallback(v8::InvocationCallback callback); |
| 154 |
| 155 /** |
| 156 * Returns a `JsEngine` instance contained in a `v8::Arguments` object. |
| 157 * Use this in callbacks created via `NewCallback()` to retrieve the current |
| 158 * `JsEngine`. |
| 159 * @param arguments `v8::Arguments` object containing the `JsEngine` |
| 160 * instance. |
| 161 * @return `JsEngine` instance from `v8::Arguments`. |
| 162 */ |
85 static JsEnginePtr FromArguments(const v8::Arguments& arguments); | 163 static JsEnginePtr FromArguments(const v8::Arguments& arguments); |
| 164 |
| 165 /** |
| 166 * Converts v8 arguments to `JsValue` objects. |
| 167 * @param arguments `v8::Arguments` object containing the arguments to |
| 168 * convert. |
| 169 * @return List of arguments converted to `JsValue` objects. |
| 170 */ |
86 JsValueList ConvertArguments(const v8::Arguments& arguments); | 171 JsValueList ConvertArguments(const v8::Arguments& arguments); |
87 | 172 |
| 173 /** |
| 174 * @see `SetFileSystem()`. |
| 175 */ |
88 FileSystemPtr GetFileSystem(); | 176 FileSystemPtr GetFileSystem(); |
| 177 |
| 178 /** |
| 179 * Sets the `FileSystem` implementation used for all file I/O. |
| 180 * Setting this is optional, the engine will use a `DefaultFileSystem` |
| 181 * instance by default, which might be sufficient. |
| 182 * @param The `FileSystem` instance to use. |
| 183 */ |
89 void SetFileSystem(FileSystemPtr val); | 184 void SetFileSystem(FileSystemPtr val); |
| 185 |
| 186 /** |
| 187 * @see `SetWebRequest()`. |
| 188 */ |
90 WebRequestPtr GetWebRequest(); | 189 WebRequestPtr GetWebRequest(); |
| 190 |
| 191 /** |
| 192 * Sets the `WebRequest` implementation used for XMLHttpRequests. |
| 193 * Setting this is optional, the engine will use a `DefaultWebRequest` |
| 194 * instance by default, which might be sufficient. |
| 195 * @param The `WebRequest` instance to use. |
| 196 */ |
91 void SetWebRequest(WebRequestPtr val); | 197 void SetWebRequest(WebRequestPtr val); |
| 198 |
| 199 /** |
| 200 * @see `SetLogSystem()`. |
| 201 */ |
92 LogSystemPtr GetLogSystem(); | 202 LogSystemPtr GetLogSystem(); |
| 203 |
| 204 /** |
| 205 * Sets the `LogSystem` implementation used for logging (e.g. to handle |
| 206 * `console.log()` calls from JavaScript). |
| 207 * Setting this is optional, the engine will use a `DefaultLogSystem` |
| 208 * instance by default, which might be sufficient. |
| 209 * @param The `LogSystem` instance to use. |
| 210 */ |
93 void SetLogSystem(LogSystemPtr val); | 211 void SetLogSystem(LogSystemPtr val); |
| 212 |
| 213 /** |
| 214 * Sets a global property that can be accessed by all the scripts. |
| 215 * @param name Name of the property to set. |
| 216 * @param value Value of the property to set. |
| 217 */ |
| 218 void SetGlobalProperty(const std::string& name, AdblockPlus::JsValuePtr valu
e); |
94 | 219 |
95 private: | 220 private: |
96 JsEngine(); | 221 JsEngine(); |
97 | 222 |
98 FileSystemPtr fileSystem; | 223 FileSystemPtr fileSystem; |
99 WebRequestPtr webRequest; | 224 WebRequestPtr webRequest; |
100 LogSystemPtr logSystem; | 225 LogSystemPtr logSystem; |
101 v8::Isolate* isolate; | 226 v8::Isolate* isolate; |
102 V8ValueHolder<v8::Context> context; | 227 V8ValueHolder<v8::Context> context; |
103 EventMap eventCallbacks; | 228 EventMap eventCallbacks; |
| 229 JsValuePtr globalJsObject; |
104 }; | 230 }; |
105 } | 231 } |
106 | 232 |
107 #endif | 233 #endif |
LEFT | RIGHT |