| Left: | ||
| Right: |
| 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 27 matching lines...) Expand all Loading... | |
| 38 class Isolate; | 38 class Isolate; |
| 39 class Value; | 39 class Value; |
| 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 | |
| 49 /** | |
| 50 * Shared smart pointer to a `JsEngine` instance. | |
| 51 */ | |
| 48 typedef std::tr1::shared_ptr<JsEngine> JsEnginePtr; | 52 typedef std::tr1::shared_ptr<JsEngine> JsEnginePtr; |
| 49 | 53 |
| 54 /** | |
| 55 * JavaScript engine used by `FilterEngine`, wraps v8. | |
| 56 */ | |
| 50 class JsEngine : public std::tr1::enable_shared_from_this<JsEngine> | 57 class JsEngine : public std::tr1::enable_shared_from_this<JsEngine> |
| 51 { | 58 { |
| 52 friend class JsValue; | 59 friend class JsValue; |
| 53 friend class JsContext; | 60 friend class JsContext; |
| 54 | 61 |
| 55 public: | 62 public: |
| 63 /** | |
| 64 * Event callback function. | |
| 65 */ | |
| 56 typedef std::tr1::function<void(JsValueList& params)> EventCallback; | 66 typedef std::tr1::function<void(JsValueList& params)> EventCallback; |
| 67 | |
| 68 /** | |
| 69 * Maps events to callback functions. | |
| 70 */ | |
| 57 typedef std::map<std::string, EventCallback> EventMap; | 71 typedef std::map<std::string, EventCallback> EventMap; |
| 58 | 72 |
| 73 /** | |
| 74 * Creates a new JavaScript engine instance. | |
| 75 * @param appInfo Information about the app, used for update checks. | |
| 76 * @return New `JsEngine` instance. | |
| 77 */ | |
| 59 static JsEnginePtr New(const AppInfo& appInfo = AppInfo()); | 78 static JsEnginePtr New(const AppInfo& appInfo = AppInfo()); |
| 79 | |
| 80 /** | |
| 81 * Registers the callback function for an event. | |
|
Wladimir Palant
2014/08/29 18:18:50
Want to note that currently only one callback can
Wladimir Palant
2014/09/01 17:36:56
While I see that "set" makes it fairly obvious tha
Felix Dahlke
2014/09/02 05:24:51
I don't think it's a good place actually. Logicall
| |
| 82 * @param eventName Event name. | |
| 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); |
| 94 | 212 |
| 95 private: | 213 private: |
| 96 JsEngine(); | 214 JsEngine(); |
| 97 | 215 |
| 98 FileSystemPtr fileSystem; | 216 FileSystemPtr fileSystem; |
| 99 WebRequestPtr webRequest; | 217 WebRequestPtr webRequest; |
| 100 LogSystemPtr logSystem; | 218 LogSystemPtr logSystem; |
| 101 v8::Isolate* isolate; | 219 v8::Isolate* isolate; |
| 102 V8ValueHolder<v8::Context> context; | 220 V8ValueHolder<v8::Context> context; |
| 103 EventMap eventCallbacks; | 221 EventMap eventCallbacks; |
| 104 }; | 222 }; |
| 105 } | 223 } |
| 106 | 224 |
| 107 #endif | 225 #endif |
| OLD | NEW |