OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 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 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
105 timerTaskIterator->arguments.emplace_back(new v8::Persistent<v8::Value>(jsEn
gine->GetIsolate(), arguments[i])); | 105 timerTaskIterator->arguments.emplace_back(new v8::Persistent<v8::Value>(jsEn
gine->GetIsolate(), arguments[i])); |
106 | 106 |
107 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; | 107 std::weak_ptr<JsEngine> weakJsEngine = jsEngine; |
108 jsEngine->timer->SetTimer(std::chrono::milliseconds(arguments[1]->IntegerValue
()), [weakJsEngine, timerTaskIterator] | 108 jsEngine->timer->SetTimer(std::chrono::milliseconds(arguments[1]->IntegerValue
()), [weakJsEngine, timerTaskIterator] |
109 { | 109 { |
110 if (auto jsEngine = weakJsEngine.lock()) | 110 if (auto jsEngine = weakJsEngine.lock()) |
111 jsEngine->CallTimerTask(timerTaskIterator); | 111 jsEngine->CallTimerTask(timerTaskIterator); |
112 }); | 112 }); |
113 } | 113 } |
114 | 114 |
115 void JsEngine::CallTimerTask(TimerTasks::const_iterator timerTaskIterator) | 115 void JsEngine::CallTimerTask(const TimerTasks::const_iterator& timerTaskIterator
) |
116 { | 116 { |
117 const JsContext context(shared_from_this()); | 117 const JsContext context(shared_from_this()); |
118 JsValue callback(shared_from_this(), v8::Local<v8::Value>::New(GetIsolate(), *
timerTaskIterator->arguments[0])); | 118 JsValue callback(shared_from_this(), v8::Local<v8::Value>::New(GetIsolate(), *
timerTaskIterator->arguments[0])); |
119 JsConstValueList callbackArgs; | 119 JsConstValueList callbackArgs; |
120 for (int i = 2; i < timerTaskIterator->arguments.size(); i++) | 120 for (int i = 2; i < timerTaskIterator->arguments.size(); i++) |
121 callbackArgs.emplace_back(new JsValue(shared_from_this(), | 121 callbackArgs.emplace_back(new JsValue(shared_from_this(), |
122 v8::Local<v8::Value>::New(GetIsolate(), *timerTaskIterator->arguments[i]))); | 122 v8::Local<v8::Value>::New(GetIsolate(), *timerTaskIterator->arguments[i]))); |
123 callback.Call(callbackArgs); | 123 callback.Call(callbackArgs); |
124 timerTasks.erase(timerTaskIterator); | 124 timerTasks.erase(timerTaskIterator); |
125 } | 125 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 const v8::TryCatch tryCatch; | 162 const v8::TryCatch tryCatch; |
163 const v8::Handle<v8::Script> script = CompileScript(GetIsolate(), source, | 163 const v8::Handle<v8::Script> script = CompileScript(GetIsolate(), source, |
164 filename); | 164 filename); |
165 CheckTryCatch(tryCatch); | 165 CheckTryCatch(tryCatch); |
166 v8::Local<v8::Value> result = script->Run(); | 166 v8::Local<v8::Value> result = script->Run(); |
167 CheckTryCatch(tryCatch); | 167 CheckTryCatch(tryCatch); |
168 return JsValuePtr(new JsValue(shared_from_this(), result)); | 168 return JsValuePtr(new JsValue(shared_from_this(), result)); |
169 } | 169 } |
170 | 170 |
171 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, | 171 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, |
172 AdblockPlus::JsEngine::EventCallback callback) | 172 const AdblockPlus::JsEngine::EventCallback& callback) |
173 { | 173 { |
174 if (!callback) | 174 if (!callback) |
175 { | 175 { |
176 RemoveEventCallback(eventName); | 176 RemoveEventCallback(eventName); |
177 return; | 177 return; |
178 } | 178 } |
179 std::lock_guard<std::mutex> lock(eventCallbacksMutex); | 179 std::lock_guard<std::mutex> lock(eventCallbacksMutex); |
180 eventCallbacks[eventName] = callback; | 180 eventCallbacks[eventName] = callback; |
181 } | 181 } |
182 | 182 |
183 void AdblockPlus::JsEngine::RemoveEventCallback(const std::string& eventName) | 183 void AdblockPlus::JsEngine::RemoveEventCallback(const std::string& eventName) |
184 { | 184 { |
185 std::lock_guard<std::mutex> lock(eventCallbacksMutex); | 185 std::lock_guard<std::mutex> lock(eventCallbacksMutex); |
186 eventCallbacks.erase(eventName); | 186 eventCallbacks.erase(eventName); |
187 } | 187 } |
188 | 188 |
189 void AdblockPlus::JsEngine::TriggerEvent(const std::string& eventName, AdblockPl
us::JsValueList& params) | 189 void AdblockPlus::JsEngine::TriggerEvent(const std::string& eventName, const Adb
lockPlus::JsValueList& params) |
190 { | 190 { |
191 EventCallback callback; | 191 EventCallback callback; |
192 { | 192 { |
193 std::lock_guard<std::mutex> lock(eventCallbacksMutex); | 193 std::lock_guard<std::mutex> lock(eventCallbacksMutex); |
194 auto it = eventCallbacks.find(eventName); | 194 auto it = eventCallbacks.find(eventName); |
195 if (it == eventCallbacks.end()) | 195 if (it == eventCallbacks.end()) |
196 return; | 196 return; |
197 callback = it->second; | 197 callback = it->second; |
198 } | 198 } |
199 callback(params); | 199 callback(params); |
(...skipping 24 matching lines...) Expand all Loading... |
224 return JsValuePtr(new JsValue(shared_from_this(), v8::Boolean::New(val))); | 224 return JsValuePtr(new JsValue(shared_from_this(), v8::Boolean::New(val))); |
225 } | 225 } |
226 | 226 |
227 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject() | 227 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject() |
228 { | 228 { |
229 const JsContext context(shared_from_this()); | 229 const JsContext context(shared_from_this()); |
230 return JsValuePtr(new JsValue(shared_from_this(), v8::Object::New())); | 230 return JsValuePtr(new JsValue(shared_from_this(), v8::Object::New())); |
231 } | 231 } |
232 | 232 |
233 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewCallback( | 233 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewCallback( |
234 v8::InvocationCallback callback) | 234 const v8::InvocationCallback& callback) |
235 { | 235 { |
236 const JsContext context(shared_from_this()); | 236 const JsContext context(shared_from_this()); |
237 | 237 |
238 // Note: we are leaking this weak pointer, no obvious way to destroy it when | 238 // Note: we are leaking this weak pointer, no obvious way to destroy it when |
239 // it's no longer used | 239 // it's no longer used |
240 std::weak_ptr<JsEngine>* data = | 240 std::weak_ptr<JsEngine>* data = |
241 new std::weak_ptr<JsEngine>(shared_from_this()); | 241 new std::weak_ptr<JsEngine>(shared_from_this()); |
242 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback, | 242 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback, |
243 v8::External::New(data)); | 243 v8::External::New(data)); |
244 return JsValuePtr(new JsValue(shared_from_this(), templ->GetFunction())); | 244 return JsValuePtr(new JsValue(shared_from_this(), templ->GetFunction())); |
(...skipping 18 matching lines...) Expand all Loading... |
263 for (int i = 0; i < arguments.Length(); i++) | 263 for (int i = 0; i < arguments.Length(); i++) |
264 list.push_back(JsValuePtr(new JsValue(shared_from_this(), arguments[i]))); | 264 list.push_back(JsValuePtr(new JsValue(shared_from_this(), arguments[i]))); |
265 return list; | 265 return list; |
266 } | 266 } |
267 | 267 |
268 AdblockPlus::FileSystemPtr AdblockPlus::JsEngine::GetFileSystem() const | 268 AdblockPlus::FileSystemPtr AdblockPlus::JsEngine::GetFileSystem() const |
269 { | 269 { |
270 return fileSystem; | 270 return fileSystem; |
271 } | 271 } |
272 | 272 |
273 void AdblockPlus::JsEngine::SetFileSystem(AdblockPlus::FileSystemPtr val) | 273 void AdblockPlus::JsEngine::SetFileSystem(const AdblockPlus::FileSystemPtr& val) |
274 { | 274 { |
275 if (!val) | 275 if (!val) |
276 throw std::runtime_error("FileSystem cannot be null"); | 276 throw std::runtime_error("FileSystem cannot be null"); |
277 | 277 |
278 fileSystem = val; | 278 fileSystem = val; |
279 } | 279 } |
280 | 280 |
281 AdblockPlus::WebRequestPtr AdblockPlus::JsEngine::GetWebRequest() const | 281 AdblockPlus::WebRequestPtr AdblockPlus::JsEngine::GetWebRequest() const |
282 { | 282 { |
283 return webRequest; | 283 return webRequest; |
284 } | 284 } |
285 | 285 |
286 void AdblockPlus::JsEngine::SetWebRequest(AdblockPlus::WebRequestPtr val) | 286 void AdblockPlus::JsEngine::SetWebRequest(const AdblockPlus::WebRequestPtr& val) |
287 { | 287 { |
288 if (!val) | 288 if (!val) |
289 throw std::runtime_error("WebRequest cannot be null"); | 289 throw std::runtime_error("WebRequest cannot be null"); |
290 | 290 |
291 webRequest = val; | 291 webRequest = val; |
292 } | 292 } |
293 | 293 |
294 void AdblockPlus::JsEngine::SetIsConnectionAllowedCallback(const IsConnectionAll
owedCallback& callback) | 294 void AdblockPlus::JsEngine::SetIsConnectionAllowedCallback(const IsConnectionAll
owedCallback& callback) |
295 { | 295 { |
296 std::lock_guard<std::mutex> lock(isConnectionAllowedMutex); | 296 std::lock_guard<std::mutex> lock(isConnectionAllowedMutex); |
(...skipping 12 matching lines...) Expand all Loading... |
309 localCopy = isConnectionAllowed; | 309 localCopy = isConnectionAllowed; |
310 } | 310 } |
311 return !localCopy || localCopy(); | 311 return !localCopy || localCopy(); |
312 } | 312 } |
313 | 313 |
314 AdblockPlus::LogSystemPtr AdblockPlus::JsEngine::GetLogSystem() const | 314 AdblockPlus::LogSystemPtr AdblockPlus::JsEngine::GetLogSystem() const |
315 { | 315 { |
316 return logSystem; | 316 return logSystem; |
317 } | 317 } |
318 | 318 |
319 void AdblockPlus::JsEngine::SetLogSystem(AdblockPlus::LogSystemPtr val) | 319 void AdblockPlus::JsEngine::SetLogSystem(const AdblockPlus::LogSystemPtr& val) |
320 { | 320 { |
321 if (!val) | 321 if (!val) |
322 throw std::runtime_error("LogSystem cannot be null"); | 322 throw std::runtime_error("LogSystem cannot be null"); |
323 | 323 |
324 logSystem = val; | 324 logSystem = val; |
325 } | 325 } |
326 | 326 |
327 | 327 |
328 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, | 328 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, |
329 AdblockPlus::JsValuePtr value) | 329 const AdblockPlus::JsValuePtr& val
ue) |
330 { | 330 { |
331 auto global = GetGlobalObject(); | 331 auto global = GetGlobalObject(); |
332 if (!global) | 332 if (!global) |
333 throw std::runtime_error("Global object cannot be null"); | 333 throw std::runtime_error("Global object cannot be null"); |
334 global->SetProperty(name, value); | 334 global->SetProperty(name, value); |
335 } | 335 } |
OLD | NEW |