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

Side by Side Diff: src/JsEngine.cpp

Issue 29369479: Issue #4694 - Add mutex protection to JS event handling
Patch Set: Created Dec. 21, 2016, 7:35 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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 /* 107 /*
108 * Enter v8 scope for our context so that we can initialize its global object. 108 * Enter v8 scope for our context so that we can initialize its global object.
109 */ 109 */
110 const v8::Context::Scope contextScope(GetContextAsLocal()); 110 const v8::Context::Scope contextScope(GetContextAsLocal());
111 auto globalObject = GetGlobalObject(); 111 auto globalObject = GetGlobalObject();
112 auto propertyName = AdblockPlus::Utils::ToV8String(GetIsolate(), "setTimeout") ; 112 auto propertyName = AdblockPlus::Utils::ToV8String(GetIsolate(), "setTimeout") ;
113 globalObject->Set(propertyName, MakeCallback(::CallbackForSetTimeout)); 113 globalObject->Set(propertyName, MakeCallback(::CallbackForSetTimeout));
114 // TODO: Move the rest of the global object initializations here 114 // TODO: Move the rest of the global object initializations here
115 } 115 }
116 116
117 JsEngineInternal& AdblockPlus::JsEngine::Internal()
118 {
119 return *static_cast<JsEngineInternal*>(this);
120 }
121
117 /** 122 /**
118 * \par Design Notes 123 * \par Design Notes
119 * It is technically necessary to construct JsEngine instances *only* within a f actory. 124 * It is technically necessary to construct JsEngine instances *only* within a f actory.
120 * Initialization requires that certain transient v8 scopes be set up 125 * Initialization requires that certain transient v8 scopes be set up
121 * before initialization and torn down afterwards. 126 * before initialization and torn down afterwards.
122 * C++ has no syntax to use anything like a sentry object in the constructor its elf. 127 * C++ has no syntax to use anything like a sentry object in the constructor its elf.
123 * Thus we need to establish v8 scope within every C++ scope that constructs an object. 128 * Thus we need to establish v8 scope within every C++ scope that constructs an object.
124 */ 129 */
125 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo, cons t ScopedV8IsolatePtr& isolate) 130 AdblockPlus::JsEnginePtr AdblockPlus::JsEngine::New(const AppInfo& appInfo, cons t ScopedV8IsolatePtr& isolate)
126 { 131 {
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
194 const v8::TryCatch tryCatch; 199 const v8::TryCatch tryCatch;
195 const v8::Handle<v8::Script> script = CompileScript(GetIsolate(), source, 200 const v8::Handle<v8::Script> script = CompileScript(GetIsolate(), source,
196 filename); 201 filename);
197 CheckTryCatch(tryCatch); 202 CheckTryCatch(tryCatch);
198 v8::Local<v8::Value> result = script->Run(); 203 v8::Local<v8::Value> result = script->Run();
199 CheckTryCatch(tryCatch); 204 CheckTryCatch(tryCatch);
200 return JsValuePtr(new JsValue(shared_from_this(), result)); 205 return JsValuePtr(new JsValue(shared_from_this(), result));
201 } 206 }
202 207
203 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, 208 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName,
204 AdblockPlus::JsEngine::EventCallback callback) 209 AdblockPlus::JsEngine::EventCallback callback)
205 { 210 {
206 eventCallbacks[eventName] = callback; 211 Internal().eventMan.Set(eventName, callback);
207 } 212 }
208 213
209 void AdblockPlus::JsEngine::RemoveEventCallback(const std::string& eventName) 214 void AdblockPlus::JsEngine::RemoveEventCallback(const std::string& eventName)
210 { 215 {
211 eventCallbacks.erase(eventName); 216 Internal().eventMan.Remove(eventName);
212 } 217 }
213 218
214 void AdblockPlus::JsEngine::TriggerEvent(const std::string& eventName, AdblockPl us::JsValueList& params) 219 void AdblockPlus::JsEngine::TriggerEvent(const std::string& eventName, AdblockPl us::JsValueList& params)
215 { 220 {
216 EventMap::iterator it = eventCallbacks.find(eventName); 221 Internal().eventMan.Trigger(eventName, params);
217 if (it != eventCallbacks.end())
218 it->second(params);
219 } 222 }
220 223
221 void AdblockPlus::JsEngine::Gc() 224 void AdblockPlus::JsEngine::Gc()
222 { 225 {
223 while (!v8::V8::IdleNotification()); 226 while (!v8::V8::IdleNotification());
224 } 227 }
225 228
226 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val) 229 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val)
227 { 230 {
228 const JsContext context(shared_from_this()); 231 const JsContext context(shared_from_this());
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after
376 379
377 JsEngineInternal* ToInternal(AdblockPlus::JsEnginePtr p) 380 JsEngineInternal* ToInternal(AdblockPlus::JsEnginePtr p)
378 { 381 {
379 return static_cast<JsEngineInternal*>(p.get()); 382 return static_cast<JsEngineInternal*>(p.get());
380 } 383 }
381 384
382 JsEngineInternal* ToInternal(AdblockPlus::JsEngine* p) 385 JsEngineInternal* ToInternal(AdblockPlus::JsEngine* p)
383 { 386 {
384 return static_cast<JsEngineInternal*>(p); 387 return static_cast<JsEngineInternal*>(p);
385 } 388 }
OLDNEW

Powered by Google App Engine
This is Rietveld