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

Side by Side Diff: src/FilterEngine.cpp

Issue 29377064: Issue 4944 - Make creation of FilterEngine asynchronous. (Closed)
Patch Set: rebase and address comments Created March 16, 2017, 3:47 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 | « shell/src/Main.cpp ('k') | test/FilterEngine.cpp » ('j') | 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include <algorithm> 18 #include <algorithm>
19 #include <cctype> 19 #include <cctype>
20 #include <functional> 20 #include <functional>
21 #include <string> 21 #include <string>
22 22
23 #include <AdblockPlus.h> 23 #include <AdblockPlus.h>
24 #include "JsContext.h" 24 #include "JsContext.h"
25 #include "Thread.h" 25 #include "Thread.h"
26 #include <mutex>
27 #include <condition_variable>
26 28
27 using namespace AdblockPlus; 29 using namespace AdblockPlus;
28 30
29 extern std::string jsSources[]; 31 extern std::string jsSources[];
30 32
31 Filter::Filter(JsValue&& value) 33 Filter::Filter(JsValue&& value)
32 : JsValue(std::move(value)) 34 : JsValue(std::move(value))
33 { 35 {
34 if (!IsObject()) 36 if (!IsObject())
35 throw std::runtime_error("JavaScript value is not an object"); 37 throw std::runtime_error("JavaScript value is not an object");
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 params.push_back(shared_from_this()); 129 params.push_back(shared_from_this());
128 JsValuePtr result = func->Call(params); 130 JsValuePtr result = func->Call(params);
129 return result->AsBool(); 131 return result->AsBool();
130 } 132 }
131 133
132 bool Subscription::operator==(const Subscription& subscription) const 134 bool Subscription::operator==(const Subscription& subscription) const
133 { 135 {
134 return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsSt ring(); 136 return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsSt ring();
135 } 137 }
136 138
137 FilterEngine::FilterEngine(JsEnginePtr jsEngine, 139 namespace
138 const FilterEngine::Prefs& preconfiguredPrefs)
139 : jsEngine(jsEngine), initialized(false), firstRun(false), updateCheckId(0)
140 { 140 {
141 jsEngine->SetEventCallback("_init", std::bind(&FilterEngine::InitDone, 141 class Sync
142 this, std::placeholders::_1)); 142 {
143 public:
144 Sync()
145 :initialized(false)
146 {
143 147
148 }
149 void Wait()
150 {
151 std::unique_lock<std::mutex> lock(mutex);
152 while (!initialized)
153 cv.wait(lock);
154 }
155 void Set()
156 {
157 {
158 std::unique_lock<std::mutex> lock(mutex);
159 initialized = true;
160 }
161 cv.notify_all();
162 }
163 private:
164 std::mutex mutex;
165 std::condition_variable cv;
166 bool initialized;
167 };
168 }
169
170 FilterEngine::FilterEngine(const JsEnginePtr& jsEngine)
171 : jsEngine(jsEngine), firstRun(false), updateCheckId(0)
172 {
173 }
174
175 void FilterEngine::CreateAsync(const JsEnginePtr& jsEngine,
176 const FilterEngine::OnCreatedCallback& onCreated,
177 const FilterEngine::CreationParameters& params)
178 {
179 FilterEnginePtr filterEngine(new FilterEngine(jsEngine));
180 auto sync = std::make_shared<Sync>();
181 jsEngine->SetEventCallback("_init", [jsEngine, filterEngine, onCreated, sync]( JsValueList& params)
144 { 182 {
145 // Lock the JS engine while we are loading scripts, no timeouts should fire 183 filterEngine->firstRun = params.size() && params[0]->AsBool();
146 // until we are done. 184 sync->Set();
147 const JsContext context(jsEngine); 185 onCreated(filterEngine);
186 jsEngine->RemoveEventCallback("_init");
187 });
148 188
149 // Set the preconfigured prefs 189 // Lock the JS engine while we are loading scripts, no timeouts should fire
150 JsValuePtr preconfiguredPrefsObject = jsEngine->NewObject(); 190 // until we are done.
151 for (FilterEngine::Prefs::const_iterator it = preconfiguredPrefs.begin(); 191 const JsContext context(jsEngine);
152 it != preconfiguredPrefs.end(); it++) 192
153 { 193 // Set the preconfigured prefs
154 preconfiguredPrefsObject->SetProperty(it->first, it->second); 194 JsValuePtr preconfiguredPrefsObject = jsEngine->NewObject();
155 } 195 for (FilterEngine::Prefs::const_iterator it = params.preconfiguredPrefs.begin( );
156 jsEngine->SetGlobalProperty("_preconfiguredPrefs", preconfiguredPrefsObject) ; 196 it != params.preconfiguredPrefs.end(); it++)
157 // Load adblockplus scripts 197 {
158 for (int i = 0; !jsSources[i].empty(); i += 2) 198 preconfiguredPrefsObject->SetProperty(it->first, it->second);
159 jsEngine->Evaluate(jsSources[i + 1], jsSources[i]);
160 } 199 }
200 jsEngine->SetGlobalProperty("_preconfiguredPrefs", preconfiguredPrefsObject);
201 // Load adblockplus scripts
202 for (int i = 0; !jsSources[i].empty(); i += 2)
203 jsEngine->Evaluate(jsSources[i + 1], jsSources[i]);
204 }
161 205
162 // TODO: This should really be implemented via a conditional variable 206 FilterEnginePtr FilterEngine::Create(const JsEnginePtr& jsEngine,
163 while (!initialized) 207 const FilterEngine::CreationParameters& params)
164 ::Sleep(10); 208 {
209 FilterEnginePtr retValue;
210 Sync sync;
211 CreateAsync(jsEngine, [&retValue, &sync](const FilterEnginePtr& filterEngine)
212 {
213 retValue = filterEngine;
214 sync.Set();
215 }, params);
216 sync.Wait();
217 return retValue;
165 } 218 }
166 219
167 namespace 220 namespace
168 { 221 {
169 typedef std::map<FilterEngine::ContentType, std::string> ContentTypeMap; 222 typedef std::map<FilterEngine::ContentType, std::string> ContentTypeMap;
170 223
171 ContentTypeMap CreateContentTypeMap() 224 ContentTypeMap CreateContentTypeMap()
172 { 225 {
173 ContentTypeMap contentTypes; 226 ContentTypeMap contentTypes;
174 contentTypes[FilterEngine::CONTENT_TYPE_OTHER] = "OTHER"; 227 contentTypes[FilterEngine::CONTENT_TYPE_OTHER] = "OTHER";
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 std::transform(contentType.begin(), contentType.end(), contentTypeUpper.begin( ), ::toupper); 259 std::transform(contentType.begin(), contentType.end(), contentTypeUpper.begin( ), ::toupper);
207 for (ContentTypeMap::const_iterator it = contentTypes.begin(); 260 for (ContentTypeMap::const_iterator it = contentTypes.begin();
208 it != contentTypes.end(); it++) 261 it != contentTypes.end(); it++)
209 { 262 {
210 if (it->second == contentTypeUpper) 263 if (it->second == contentTypeUpper)
211 return it->first; 264 return it->first;
212 } 265 }
213 throw std::invalid_argument("Cannot convert argument to ContentType"); 266 throw std::invalid_argument("Cannot convert argument to ContentType");
214 } 267 }
215 268
216 void FilterEngine::InitDone(JsValueList& params)
217 {
218 jsEngine->RemoveEventCallback("_init");
219 initialized = true;
220 firstRun = params.size() && params[0]->AsBool();
221 }
222
223 bool FilterEngine::IsFirstRun() const 269 bool FilterEngine::IsFirstRun() const
224 { 270 {
225 return firstRun; 271 return firstRun;
226 } 272 }
227 273
228 FilterPtr FilterEngine::GetFilter(const std::string& text) 274 FilterPtr FilterEngine::GetFilter(const std::string& text)
229 { 275 {
230 JsValuePtr func = jsEngine->Evaluate("API.getFilterFromText"); 276 JsValuePtr func = jsEngine->Evaluate("API.getFilterFromText");
231 JsValueList params; 277 JsValueList params;
232 params.push_back(jsEngine->NewValue(text)); 278 params.push_back(jsEngine->NewValue(text));
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after
506 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent Url); 552 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent Url);
507 if (filter) 553 if (filter)
508 { 554 {
509 return filter; 555 return filter;
510 } 556 }
511 currentUrl = parentUrl; 557 currentUrl = parentUrl;
512 } 558 }
513 while (urlIterator != documentUrls.end()); 559 while (urlIterator != documentUrls.end());
514 return FilterPtr(); 560 return FilterPtr();
515 } 561 }
OLDNEW
« no previous file with comments | « shell/src/Main.cpp ('k') | test/FilterEngine.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld