| 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 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`, uses v8. | |
|
Wladimir Palant
2014/08/28 17:24:40
"uses v8" => "wraps v8"?
Felix Dahlke
2014/08/28 22:56:41
Done.
| |
| 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 AppInfo Information about the app, used for update checks. | |
|
Wladimir Palant
2014/08/28 17:24:40
"AppInfo" is unnecessary here?
Felix Dahlke
2014/08/28 22:56:41
Done.
| |
| 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 Name of the event. | |
|
Wladimir Palant
2014/08/28 17:24:40
Here and below, why not just "Event name" and "Eve
Felix Dahlke
2014/08/28 22:56:41
Done.
| |
| 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 Name of the event. | |
| 86 */ | |
| 61 void RemoveEventCallback(const std::string& eventName); | 87 void RemoveEventCallback(const std::string& eventName); |
| 88 | |
| 89 /** | |
| 90 * Triggers an event. | |
| 91 * @param eventName Name of the event. | |
| 92 * @param params Parameters for the event. | |
| 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 evalute. | |
|
Wladimir Palant
2014/08/28 17:24:40
"evaluate"
Felix Dahlke
2014/08/28 22:56:41
Done.
| |
| 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. | |
|
Wladimir Palant
2014/08/28 17:24:40
I don't think you can use an article here...
Felix Dahlke
2014/08/28 22:56:41
I do, here's one example found in the wild: http:/
| |
| 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 new JavaScript callback. | |
|
Wladimir Palant
2014/08/28 17:24:40
The thing about documentation is: it should be hel
Felix Dahlke
2014/08/28 22:56:41
Done.
| |
| 144 * @param callback Callback to convert. | |
| 145 * @return New `JsValue` instance. | |
| 146 */ | |
| 84 JsValuePtr NewCallback(v8::InvocationCallback callback); | 147 JsValuePtr NewCallback(v8::InvocationCallback callback); |
| 148 | |
| 149 /** | |
| 150 * Returns a `JsEngine` instance contained in a `v8::Arguments` object. | |
| 151 * @param arguments `v8::Arguments` object containing the `JsEngine` | |
| 152 * instance. | |
| 153 * @return `JsEngine` instance. | |
| 154 */ | |
| 85 static JsEnginePtr FromArguments(const v8::Arguments& arguments); | 155 static JsEnginePtr FromArguments(const v8::Arguments& arguments); |
| 156 | |
| 157 /** | |
| 158 * Converts v8 arguments to `JsValue` objects. | |
| 159 * @param arguments `v8::Arguments` object containing the arguments to | |
| 160 * convert. | |
| 161 * @return List of arguments converted to `JsValue` objects. | |
| 162 */ | |
| 86 JsValueList ConvertArguments(const v8::Arguments& arguments); | 163 JsValueList ConvertArguments(const v8::Arguments& arguments); |
| 87 | 164 |
| 165 /** | |
| 166 * @see SetFileSystem(). | |
| 167 */ | |
| 88 FileSystemPtr GetFileSystem(); | 168 FileSystemPtr GetFileSystem(); |
| 169 | |
| 170 /** | |
| 171 * Sets the `FileSystem` implementation used for all file I/O. | |
| 172 * Setting this is optional, the engine will use a `DefaultFileSystem` | |
| 173 * instance by default, which should normally be sufficient. | |
|
Wladimir Palant
2014/08/28 17:24:40
"normally be sufficient"? "might be sufficient" is
Felix Dahlke
2014/08/28 22:56:41
Done.
| |
| 174 * @param The `FileSystem` instance to use. | |
| 175 */ | |
| 89 void SetFileSystem(FileSystemPtr val); | 176 void SetFileSystem(FileSystemPtr val); |
| 177 | |
| 178 /** | |
| 179 * @see SetWebRequest(). | |
| 180 */ | |
| 90 WebRequestPtr GetWebRequest(); | 181 WebRequestPtr GetWebRequest(); |
| 182 | |
| 183 /** | |
| 184 * Sets the `WebRequest` implementation used for XMLHttpRequests. | |
| 185 * Setting this is optional, the engine will use a `DefaultWebRequest` | |
| 186 * instance by default, which should normally be sufficient. | |
| 187 * @param The `WebRequest` instance to use. | |
| 188 */ | |
| 91 void SetWebRequest(WebRequestPtr val); | 189 void SetWebRequest(WebRequestPtr val); |
| 190 | |
| 191 /** | |
| 192 * @see SetLogSystem(). | |
| 193 */ | |
| 92 LogSystemPtr GetLogSystem(); | 194 LogSystemPtr GetLogSystem(); |
| 195 | |
| 196 /** | |
| 197 * Sets the `LogSystem` implementation used for logging (e.g. console.log). | |
|
Wladimir Palant
2014/08/28 17:24:40
"e.g. to handle console.log() calls from JavaScrip
Felix Dahlke
2014/08/28 22:56:41
Done.
| |
| 198 * Setting this is optional, the engine will use a `DefaultLogSystem` | |
| 199 * instance by default, which should normally be sufficient. | |
| 200 * @param The `FileSystem` instance to use. | |
|
Wladimir Palant
2014/08/28 17:24:40
FileSystem => LogSystem?
Felix Dahlke
2014/08/28 22:56:41
Done.
| |
| 201 */ | |
| 93 void SetLogSystem(LogSystemPtr val); | 202 void SetLogSystem(LogSystemPtr val); |
| 94 | 203 |
| 95 private: | 204 private: |
| 96 JsEngine(); | 205 JsEngine(); |
| 97 | 206 |
| 98 FileSystemPtr fileSystem; | 207 FileSystemPtr fileSystem; |
| 99 WebRequestPtr webRequest; | 208 WebRequestPtr webRequest; |
| 100 LogSystemPtr logSystem; | 209 LogSystemPtr logSystem; |
| 101 v8::Isolate* isolate; | 210 v8::Isolate* isolate; |
| 102 V8ValueHolder<v8::Context> context; | 211 V8ValueHolder<v8::Context> context; |
| 103 EventMap eventCallbacks; | 212 EventMap eventCallbacks; |
| 104 }; | 213 }; |
| 105 } | 214 } |
| 106 | 215 |
| 107 #endif | 216 #endif |
| OLD | NEW |