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

Side by Side Diff: src/JsEngine.cpp

Issue 29389576: Issue 4694 - JsEngine::{SetEventCallback,RemoveEventCallback,TriggerEvent} should be thread safe. (Closed)
Patch Set: Created March 20, 2017, 1:59 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
« no previous file with comments | « include/AdblockPlus/JsEngine.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 filename); 110 filename);
111 CheckTryCatch(tryCatch); 111 CheckTryCatch(tryCatch);
112 v8::Local<v8::Value> result = script->Run(); 112 v8::Local<v8::Value> result = script->Run();
113 CheckTryCatch(tryCatch); 113 CheckTryCatch(tryCatch);
114 return JsValuePtr(new JsValue(shared_from_this(), result)); 114 return JsValuePtr(new JsValue(shared_from_this(), result));
115 } 115 }
116 116
117 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName, 117 void AdblockPlus::JsEngine::SetEventCallback(const std::string& eventName,
118 AdblockPlus::JsEngine::EventCallback callback) 118 AdblockPlus::JsEngine::EventCallback callback)
119 { 119 {
120 std::lock_guard<std::mutex> lock(eventCallbacksMutex);
120 eventCallbacks[eventName] = callback; 121 eventCallbacks[eventName] = callback;
121 } 122 }
122 123
123 void AdblockPlus::JsEngine::RemoveEventCallback(const std::string& eventName) 124 void AdblockPlus::JsEngine::RemoveEventCallback(const std::string& eventName)
124 { 125 {
126 std::lock_guard<std::mutex> lock(eventCallbacksMutex);
125 eventCallbacks.erase(eventName); 127 eventCallbacks.erase(eventName);
126 } 128 }
127 129
128 void AdblockPlus::JsEngine::TriggerEvent(const std::string& eventName, AdblockPl us::JsValueList& params) 130 void AdblockPlus::JsEngine::TriggerEvent(const std::string& eventName, AdblockPl us::JsValueList& params)
129 { 131 {
130 EventMap::iterator it = eventCallbacks.find(eventName); 132 EventCallback callback;
131 if (it != eventCallbacks.end()) 133 {
132 it->second(params); 134 std::lock_guard<std::mutex> lock(eventCallbacksMutex);
135 auto it = eventCallbacks.find(eventName);
136 if (it == eventCallbacks.end())
137 return;
138 callback = it->second;
139 }
140 callback(params);
Oleksandr 2017/03/20 20:00:42 Nit: Maybe check if callback is not null JIC?
sergei 2017/03/20 20:08:13 What about such check in SetEventCallback? Maybe e
sergei 2017/03/20 20:23:24 Actually, I think it should be in another commit.
Oleksandr 2017/03/20 20:25:25 Fair enough. It isn't that big of an issue IMO, so
133 } 141 }
134 142
135 void AdblockPlus::JsEngine::Gc() 143 void AdblockPlus::JsEngine::Gc()
136 { 144 {
137 while (!v8::V8::IdleNotification()); 145 while (!v8::V8::IdleNotification());
138 } 146 }
139 147
140 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val) 148 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val)
141 { 149 {
142 const JsContext context(shared_from_this()); 150 const JsContext context(shared_from_this());
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
265 273
266 274
267 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name, 275 void AdblockPlus::JsEngine::SetGlobalProperty(const std::string& name,
268 AdblockPlus::JsValuePtr value) 276 AdblockPlus::JsValuePtr value)
269 { 277 {
270 auto global = GetGlobalObject(); 278 auto global = GetGlobalObject();
271 if (!global) 279 if (!global)
272 throw std::runtime_error("Global object cannot be null"); 280 throw std::runtime_error("Global object cannot be null");
273 global->SetProperty(name, value); 281 global->SetProperty(name, value);
274 } 282 }
OLDNEW
« no previous file with comments | « include/AdblockPlus/JsEngine.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld