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

Side by Side Diff: src/FilterEngine.cpp

Issue 10420020: Made sure FilterEngine instances are always initialized (Closed)
Patch Set: Created May 16, 2013, 2:27 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 | « lib/init.js ('k') | src/GlobalJsObject.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 <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 Eyeo GmbH 3 * Copyright (C) 2006-2013 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 "Thread.h"
24 25
25 using namespace AdblockPlus; 26 using namespace AdblockPlus;
26 27
27 extern std::string jsSources[]; 28 extern std::string jsSources[];
28 29
29 Filter::Filter(JsValuePtr value) 30 Filter::Filter(JsValuePtr value)
30 : JsValue(value) 31 : JsValue(value)
31 { 32 {
32 if (!IsObject()) 33 if (!IsObject())
33 throw std::runtime_error("JavaScript value is not an object"); 34 throw std::runtime_error("JavaScript value is not an object");
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 params.push_back(shared_from_this()); 126 params.push_back(shared_from_this());
126 JsValuePtr result = func->Call(params); 127 JsValuePtr result = func->Call(params);
127 return result->AsBool(); 128 return result->AsBool();
128 } 129 }
129 130
130 bool Subscription::operator==(const Subscription& subscription) const 131 bool Subscription::operator==(const Subscription& subscription) const
131 { 132 {
132 return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsSt ring(); 133 return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsSt ring();
133 } 134 }
134 135
135 FilterEngine::FilterEngine(JsEnginePtr jsEngine) : jsEngine(jsEngine) 136 FilterEngine::FilterEngine(JsEnginePtr jsEngine)
137 : jsEngine(jsEngine), initialized(false)
136 { 138 {
139 jsEngine->SetInitCallback(std::tr1::bind(&FilterEngine::InitDone, this));
137 for (int i = 0; !jsSources[i].empty(); i += 2) 140 for (int i = 0; !jsSources[i].empty(); i += 2)
138 jsEngine->Evaluate(jsSources[i + 1], jsSources[i]); 141 jsEngine->Evaluate(jsSources[i + 1], jsSources[i]);
142
143 // TODO: This should really be implemented via a conditional variable
144 while (!initialized)
145 ::Sleep(10);
139 } 146 }
140 147
141 bool FilterEngine::IsInitialized() const 148 void FilterEngine::InitDone()
142 { 149 {
143 return jsEngine->Evaluate("_abpInitialized")->AsBool(); 150 initialized = true;
144 } 151 }
145 152
146 FilterPtr FilterEngine::GetFilter(const std::string& text) 153 FilterPtr FilterEngine::GetFilter(const std::string& text)
147 { 154 {
148 JsValuePtr func = jsEngine->Evaluate("API.getFilterFromText"); 155 JsValuePtr func = jsEngine->Evaluate("API.getFilterFromText");
149 JsValueList params; 156 JsValueList params;
150 params.push_back(jsEngine->NewValue(text)); 157 params.push_back(jsEngine->NewValue(text));
151 return FilterPtr(new Filter(func->Call(params))); 158 return FilterPtr(new Filter(func->Call(params)));
152 } 159 }
153 160
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 { 216 {
210 JsValuePtr func = jsEngine->Evaluate("API.getElementHidingSelectors"); 217 JsValuePtr func = jsEngine->Evaluate("API.getElementHidingSelectors");
211 JsValueList params; 218 JsValueList params;
212 params.push_back(jsEngine->NewValue(domain)); 219 params.push_back(jsEngine->NewValue(domain));
213 JsValueList result = func->Call(params)->AsList(); 220 JsValueList result = func->Call(params)->AsList();
214 std::vector<std::string> selectors; 221 std::vector<std::string> selectors;
215 for (JsValueList::iterator it = result.begin(); it != result.end(); ++it) 222 for (JsValueList::iterator it = result.begin(); it != result.end(); ++it)
216 selectors.push_back((*it)->AsString()); 223 selectors.push_back((*it)->AsString());
217 return selectors; 224 return selectors;
218 } 225 }
OLDNEW
« no previous file with comments | « lib/init.js ('k') | src/GlobalJsObject.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld