OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 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 29 matching lines...) Expand all Loading... |
40 class Context; | 40 class Context; |
41 template<class T> class Handle; | 41 template<class T> class Handle; |
42 typedef Handle<Value>(*InvocationCallback)(const Arguments &args); | 42 typedef Handle<Value>(*InvocationCallback)(const Arguments &args); |
43 } | 43 } |
44 | 44 |
45 namespace AdblockPlus | 45 namespace AdblockPlus |
46 { | 46 { |
47 class JsEngine; | 47 class JsEngine; |
48 typedef std::tr1::shared_ptr<JsEngine> JsEnginePtr; | 48 typedef std::tr1::shared_ptr<JsEngine> JsEnginePtr; |
49 | 49 |
| 50 /** |
| 51 * JavaScript engine used by `FilterEngine`, wraps v8. |
| 52 */ |
50 class JsEngine : public std::tr1::enable_shared_from_this<JsEngine> | 53 class JsEngine : public std::tr1::enable_shared_from_this<JsEngine> |
51 { | 54 { |
52 friend class JsValue; | 55 friend class JsValue; |
53 friend class JsContext; | 56 friend class JsContext; |
54 | 57 |
55 public: | 58 public: |
| 59 /** |
| 60 * Event callback function. |
| 61 */ |
56 typedef std::tr1::function<void(JsValueList& params)> EventCallback; | 62 typedef std::tr1::function<void(JsValueList& params)> EventCallback; |
| 63 |
| 64 /** |
| 65 * Maps events to callback functions. |
| 66 */ |
57 typedef std::map<std::string, EventCallback> EventMap; | 67 typedef std::map<std::string, EventCallback> EventMap; |
58 | 68 |
| 69 /** |
| 70 * Creates a new JavaScript engine instance. |
| 71 * @param appInfo Information about the app, used for update checks. |
| 72 * @return New `JsEngine` instance. |
| 73 */ |
59 static JsEnginePtr New(const AppInfo& appInfo = AppInfo()); | 74 static JsEnginePtr New(const AppInfo& appInfo = AppInfo()); |
| 75 |
| 76 /** |
| 77 * Registers the callback function for an event. |
| 78 * @param eventName Event name. |
| 79 * @param callback Event callback function. |
| 80 */ |
60 void SetEventCallback(const std::string& eventName, EventCallback callback); | 81 void SetEventCallback(const std::string& eventName, EventCallback callback); |
| 82 |
| 83 /** |
| 84 * Removes the callback function for an event. |
| 85 * @param eventName Event name. |
| 86 */ |
61 void RemoveEventCallback(const std::string& eventName); | 87 void RemoveEventCallback(const std::string& eventName); |
| 88 |
| 89 /** |
| 90 * Triggers an event. |
| 91 * @param eventName Event name. |
| 92 * @param params Event parameters. |
| 93 */ |
62 void TriggerEvent(const std::string& eventName, JsValueList& params); | 94 void TriggerEvent(const std::string& eventName, JsValueList& params); |
| 95 |
| 96 /** |
| 97 * Evaluates a JavaScript expression. |
| 98 * @param source JavaScript expression to evaluate. |
| 99 * @param filename Optional file name for the expression, used in error |
| 100 * messages. |
| 101 * @return Result of the evaluated expression. |
| 102 */ |
63 JsValuePtr Evaluate(const std::string& source, | 103 JsValuePtr Evaluate(const std::string& source, |
64 const std::string& filename = ""); | 104 const std::string& filename = ""); |
| 105 |
| 106 /** |
| 107 * Initiates a garbage collection. |
| 108 */ |
65 void Gc(); | 109 void Gc(); |
| 110 |
| 111 //@{ |
| 112 /** |
| 113 * Creates a new JavaScript value. |
| 114 * @param val Value to convert. |
| 115 * @return New `JsValue` instance. |
| 116 */ |
66 JsValuePtr NewValue(const std::string& val); | 117 JsValuePtr NewValue(const std::string& val); |
67 JsValuePtr NewValue(int64_t val); | 118 JsValuePtr NewValue(int64_t val); |
68 JsValuePtr NewValue(bool val); | 119 JsValuePtr NewValue(bool val); |
69 inline JsValuePtr NewValue(const char* val) | 120 inline JsValuePtr NewValue(const char* val) |
70 { | 121 { |
71 return NewValue(std::string(val)); | 122 return NewValue(std::string(val)); |
72 } | 123 } |
73 inline JsValuePtr NewValue(int val) | 124 inline JsValuePtr NewValue(int val) |
74 { | 125 { |
75 return NewValue(static_cast<int64_t>(val)); | 126 return NewValue(static_cast<int64_t>(val)); |
76 } | 127 } |
77 #ifdef __APPLE__ | 128 #ifdef __APPLE__ |
78 inline JsValuePtr NewValue(long val) | 129 inline JsValuePtr NewValue(long val) |
79 { | 130 { |
80 return NewValue(static_cast<int64_t>(val)); | 131 return NewValue(static_cast<int64_t>(val)); |
81 } | 132 } |
82 #endif | 133 #endif |
| 134 //@} |
| 135 |
| 136 /** |
| 137 * Creates a new JavaScript object. |
| 138 * @return New `JsValue` instance. |
| 139 */ |
83 JsValuePtr NewObject(); | 140 JsValuePtr NewObject(); |
| 141 |
| 142 /** |
| 143 * Creates a JavaScript function that invokes a C++ callback. |
| 144 * @param callback C++ callback to invoke. The callback receives a |
| 145 * `v8::Arguments` object and can use `FromArguments()` to retrieve |
| 146 * the current `JsEngine`. |
| 147 * @return New `JsValue` instance. |
| 148 */ |
84 JsValuePtr NewCallback(v8::InvocationCallback callback); | 149 JsValuePtr NewCallback(v8::InvocationCallback callback); |
| 150 |
| 151 /** |
| 152 * Returns a `JsEngine` instance contained in a `v8::Arguments` object. |
| 153 * Use this in callbacks created via `NewCallback()` to retrieve the current |
| 154 * `JsEngine`. |
| 155 * @param arguments `v8::Arguments` object containing the `JsEngine` |
| 156 * instance. |
| 157 * @return `JsEngine` instance from `v8::Arguments`. |
| 158 */ |
85 static JsEnginePtr FromArguments(const v8::Arguments& arguments); | 159 static JsEnginePtr FromArguments(const v8::Arguments& arguments); |
| 160 |
| 161 /** |
| 162 * Converts v8 arguments to `JsValue` objects. |
| 163 * @param arguments `v8::Arguments` object containing the arguments to |
| 164 * convert. |
| 165 * @return List of arguments converted to `JsValue` objects. |
| 166 */ |
86 JsValueList ConvertArguments(const v8::Arguments& arguments); | 167 JsValueList ConvertArguments(const v8::Arguments& arguments); |
87 | 168 |
| 169 /** |
| 170 * @see `SetFileSystem()`. |
| 171 */ |
88 FileSystemPtr GetFileSystem(); | 172 FileSystemPtr GetFileSystem(); |
| 173 |
| 174 /** |
| 175 * Sets the `FileSystem` implementation used for all file I/O. |
| 176 * Setting this is optional, the engine will use a `DefaultFileSystem` |
| 177 * instance by default, which might be sufficient. |
| 178 * @param The `FileSystem` instance to use. |
| 179 */ |
89 void SetFileSystem(FileSystemPtr val); | 180 void SetFileSystem(FileSystemPtr val); |
| 181 |
| 182 /** |
| 183 * @see `SetWebRequest()`. |
| 184 */ |
90 WebRequestPtr GetWebRequest(); | 185 WebRequestPtr GetWebRequest(); |
| 186 |
| 187 /** |
| 188 * Sets the `WebRequest` implementation used for XMLHttpRequests. |
| 189 * Setting this is optional, the engine will use a `DefaultWebRequest` |
| 190 * instance by default, which might be sufficient. |
| 191 * @param The `WebRequest` instance to use. |
| 192 */ |
91 void SetWebRequest(WebRequestPtr val); | 193 void SetWebRequest(WebRequestPtr val); |
| 194 |
| 195 /** |
| 196 * @see `SetLogSystem()`. |
| 197 */ |
92 LogSystemPtr GetLogSystem(); | 198 LogSystemPtr GetLogSystem(); |
| 199 |
| 200 /** |
| 201 * Sets the `LogSystem` implementation used for logging (e.g. to handle |
| 202 * `console.log()` calls from JavaScript). |
| 203 * Setting this is optional, the engine will use a `DefaultLogSystem` |
| 204 * instance by default, which might be sufficient. |
| 205 * @param The `LogSystem` instance to use. |
| 206 */ |
93 void SetLogSystem(LogSystemPtr val); | 207 void SetLogSystem(LogSystemPtr val); |
94 | 208 |
95 private: | 209 private: |
96 JsEngine(); | 210 JsEngine(); |
97 | 211 |
98 FileSystemPtr fileSystem; | 212 FileSystemPtr fileSystem; |
99 WebRequestPtr webRequest; | 213 WebRequestPtr webRequest; |
100 LogSystemPtr logSystem; | 214 LogSystemPtr logSystem; |
101 v8::Isolate* isolate; | 215 v8::Isolate* isolate; |
102 V8ValueHolder<v8::Context> context; | 216 V8ValueHolder<v8::Context> context; |
103 EventMap eventCallbacks; | 217 EventMap eventCallbacks; |
104 }; | 218 }; |
105 } | 219 } |
106 | 220 |
107 #endif | 221 #endif |
OLD | NEW |