Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/JsEngine.cpp

Issue 29417605: Issue 5034 - Part 3: Create plain JsValue instead of JsValuePtr (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Created April 19, 2017, 5:54 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
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(const 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 JsValueList 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(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 }
126 126
127 AdblockPlus::JsEngine::JsEngine(const ScopedV8IsolatePtr& isolate, TimerPtr time r) 127 AdblockPlus::JsEngine::JsEngine(const ScopedV8IsolatePtr& isolate, TimerPtr time r)
128 : isolate(isolate) 128 : isolate(isolate)
129 , fileSystem(new DefaultFileSystem()) 129 , fileSystem(new DefaultFileSystem())
130 , webRequest(new DefaultWebRequest()) 130 , webRequest(new DefaultWebRequest())
131 , logSystem(new DefaultLogSystem()) 131 , logSystem(new DefaultLogSystem())
132 , timer(std::move(timer)) 132 , timer(std::move(timer))
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 std::lock_guard<std::mutex> lock(eventCallbacksMutex); 180 std::lock_guard<std::mutex> lock(eventCallbacksMutex);
181 eventCallbacks[eventName] = callback; 181 eventCallbacks[eventName] = callback;
182 } 182 }
183 183
184 void AdblockPlus::JsEngine::RemoveEventCallback(const std::string& eventName) 184 void AdblockPlus::JsEngine::RemoveEventCallback(const std::string& eventName)
185 { 185 {
186 std::lock_guard<std::mutex> lock(eventCallbacksMutex); 186 std::lock_guard<std::mutex> lock(eventCallbacksMutex);
187 eventCallbacks.erase(eventName); 187 eventCallbacks.erase(eventName);
188 } 188 }
189 189
190 void AdblockPlus::JsEngine::TriggerEvent(const std::string& eventName, const Adb lockPlus::JsConstValueList& params) 190 void AdblockPlus::JsEngine::TriggerEvent(const std::string& eventName, const Adb lockPlus::JsValueList& params)
191 { 191 {
192 EventCallback callback; 192 EventCallback callback;
193 { 193 {
194 std::lock_guard<std::mutex> lock(eventCallbacksMutex); 194 std::lock_guard<std::mutex> lock(eventCallbacksMutex);
195 auto it = eventCallbacks.find(eventName); 195 auto it = eventCallbacks.find(eventName);
196 if (it == eventCallbacks.end()) 196 if (it == eventCallbacks.end())
197 return; 197 return;
198 callback = it->second; 198 callback = it->second;
199 } 199 }
200 callback(params); 200 callback(params);
201 } 201 }
202 202
203 void AdblockPlus::JsEngine::Gc() 203 void AdblockPlus::JsEngine::Gc()
204 { 204 {
205 while (!v8::V8::IdleNotification()); 205 while (!v8::V8::IdleNotification());
206 } 206 }
207 207
208 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val) 208 AdblockPlus::JsValue AdblockPlus::JsEngine::NewValue(const std::string& val)
209 { 209 {
210 const JsContext context(shared_from_this()); 210 const JsContext context(shared_from_this());
211 return JsValuePtr(new JsValue(shared_from_this(), 211 return JsValue(shared_from_this(), Utils::ToV8String(GetIsolate(), val));
212 Utils::ToV8String(GetIsolate(), val)));
213 } 212 }
214 213
215 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val) 214 AdblockPlus::JsValue AdblockPlus::JsEngine::NewValue(int64_t val)
216 { 215 {
217 const JsContext context(shared_from_this()); 216 const JsContext context(shared_from_this());
218 return JsValuePtr(new JsValue(shared_from_this(), 217 return JsValue(shared_from_this(), v8::Number::New(GetIsolate(), val));
219 v8::Number::New(GetIsolate(), val)));
220 } 218 }
221 219
222 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val) 220 AdblockPlus::JsValue AdblockPlus::JsEngine::NewValue(bool val)
223 { 221 {
224 const JsContext context(shared_from_this()); 222 const JsContext context(shared_from_this());
225 return JsValuePtr(new JsValue(shared_from_this(), v8::Boolean::New(val))); 223 return JsValue(shared_from_this(), v8::Boolean::New(val));
226 } 224 }
227 225
228 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject() 226 AdblockPlus::JsValue AdblockPlus::JsEngine::NewObject()
229 { 227 {
230 const JsContext context(shared_from_this()); 228 const JsContext context(shared_from_this());
231 return JsValuePtr(new JsValue(shared_from_this(), v8::Object::New())); 229 return JsValue(shared_from_this(), v8::Object::New());
232 } 230 }
233 231
234 AdblockPlus::JsValue AdblockPlus::JsEngine::NewCallback( 232 AdblockPlus::JsValue AdblockPlus::JsEngine::NewCallback(
235 const v8::InvocationCallback& callback) 233 const v8::InvocationCallback& callback)
236 { 234 {
237 const JsContext context(shared_from_this()); 235 const JsContext context(shared_from_this());
238 236
239 // Note: we are leaking this weak pointer, no obvious way to destroy it when 237 // Note: we are leaking this weak pointer, no obvious way to destroy it when
240 // it's no longer used 238 // it's no longer used
241 std::weak_ptr<JsEngine>* data = 239 std::weak_ptr<JsEngine>* data =
242 new std::weak_ptr<JsEngine>(shared_from_this()); 240 new std::weak_ptr<JsEngine>(shared_from_this());
243 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback, 241 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback,
244 v8::External::New(data)); 242 v8::External::New(data));
245 return JsValue(shared_from_this(), templ->GetFunction()); 243 return JsValue(shared_from_this(), templ->GetFunction());
246 } 244 }
247 245
248 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::FromArguments(const v8::Argument s& arguments) 246 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::FromArguments(const v8::Argument s& arguments)
249 { 247 {
250 const v8::Local<const v8::External> external = 248 const v8::Local<const v8::External> external =
251 v8::Local<const v8::External>::Cast(arguments.Data()); 249 v8::Local<const v8::External>::Cast(arguments.Data());
252 std::weak_ptr<JsEngine>* data = 250 std::weak_ptr<JsEngine>* data =
253 static_cast<std::weak_ptr<JsEngine>*>(external->Value()); 251 static_cast<std::weak_ptr<JsEngine>*>(external->Value());
254 JsEnginePtr result = data->lock(); 252 JsEnginePtr result = data->lock();
255 if (!result) 253 if (!result)
256 throw std::runtime_error("Oops, our JsEngine is gone, how did that happen?") ; 254 throw std::runtime_error("Oops, our JsEngine is gone, how did that happen?") ;
257 return result; 255 return result;
258 } 256 }
259 257
260 AdblockPlus::JsConstValueList AdblockPlus::JsEngine::ConvertArguments(const v8:: Arguments& arguments) 258 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Argum ents& arguments)
261 { 259 {
262 const JsContext context(shared_from_this()); 260 const JsContext context(shared_from_this());
263 JsConstValueList list; 261 JsValueList list;
264 for (int i = 0; i < arguments.Length(); i++) 262 for (int i = 0; i < arguments.Length(); i++)
265 list.push_back(JsValuePtr(new JsValue(shared_from_this(), arguments[i]))); 263 list.push_back(JsValue(shared_from_this(), arguments[i]));
266 return list; 264 return list;
267 } 265 }
268 266
269 AdblockPlus::FileSystemPtr AdblockPlus::JsEngine::GetFileSystem() const 267 AdblockPlus::FileSystemPtr AdblockPlus::JsEngine::GetFileSystem() const
270 { 268 {
271 return fileSystem; 269 return fileSystem;
272 } 270 }
273 271
274 void AdblockPlus::JsEngine::SetFileSystem(const AdblockPlus::FileSystemPtr& val) 272 void AdblockPlus::JsEngine::SetFileSystem(const AdblockPlus::FileSystemPtr& val)
275 { 273 {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 logSystem = val; 323 logSystem = val;
326 } 324 }
327 325
328 326
329 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, 327 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name,
330 const AdblockPlus::JsValue& value) 328 const AdblockPlus::JsValue& value)
331 { 329 {
332 auto global = GetGlobalObject(); 330 auto global = GetGlobalObject();
333 global.SetProperty(name, value); 331 global.SetProperty(name, value);
334 } 332 }
OLDNEW

Powered by Google App Engine
This is Rietveld