OLD | NEW |
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 "JsContext.h" |
24 #include "Thread.h" | 25 #include "Thread.h" |
25 | 26 |
26 using namespace AdblockPlus; | 27 using namespace AdblockPlus; |
27 | 28 |
28 extern std::string jsSources[]; | 29 extern std::string jsSources[]; |
29 | 30 |
30 Filter::Filter(JsValuePtr value) | 31 Filter::Filter(JsValuePtr value) |
31 : JsValue(value) | 32 : JsValue(value) |
32 { | 33 { |
33 if (!IsObject()) | 34 if (!IsObject()) |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 bool Subscription::operator==(const Subscription& subscription) const | 132 bool Subscription::operator==(const Subscription& subscription) const |
132 { | 133 { |
133 return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsSt
ring(); | 134 return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsSt
ring(); |
134 } | 135 } |
135 | 136 |
136 FilterEngine::FilterEngine(JsEnginePtr jsEngine) | 137 FilterEngine::FilterEngine(JsEnginePtr jsEngine) |
137 : jsEngine(jsEngine), initialized(false), firstRun(false) | 138 : jsEngine(jsEngine), initialized(false), firstRun(false) |
138 { | 139 { |
139 jsEngine->SetEventCallback("init", std::tr1::bind(&FilterEngine::InitDone, | 140 jsEngine->SetEventCallback("init", std::tr1::bind(&FilterEngine::InitDone, |
140 this, std::tr1::placeholders::_1)); | 141 this, std::tr1::placeholders::_1)); |
141 for (int i = 0; !jsSources[i].empty(); i += 2) | 142 |
142 jsEngine->Evaluate(jsSources[i + 1], jsSources[i]); | 143 { |
| 144 // Lock the JS engine while we are loading scripts, no timeouts should fire |
| 145 // until we are done. |
| 146 const JsContext context(jsEngine); |
| 147 for (int i = 0; !jsSources[i].empty(); i += 2) |
| 148 jsEngine->Evaluate(jsSources[i + 1], jsSources[i]); |
| 149 } |
143 | 150 |
144 // TODO: This should really be implemented via a conditional variable | 151 // TODO: This should really be implemented via a conditional variable |
145 while (!initialized) | 152 while (!initialized) |
146 ::Sleep(10); | 153 ::Sleep(10); |
147 } | 154 } |
148 | 155 |
149 void FilterEngine::InitDone(JsValueList& params) | 156 void FilterEngine::InitDone(JsValueList& params) |
150 { | 157 { |
151 jsEngine->RemoveEventCallback("init"); | 158 jsEngine->RemoveEventCallback("init"); |
152 initialized = true; | 159 initialized = true; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
199 JsValuePtr func = jsEngine->Evaluate("API.getRecommendedSubscriptions"); | 206 JsValuePtr func = jsEngine->Evaluate("API.getRecommendedSubscriptions"); |
200 JsValueList values = func->Call()->AsList(); | 207 JsValueList values = func->Call()->AsList(); |
201 std::vector<SubscriptionPtr> result; | 208 std::vector<SubscriptionPtr> result; |
202 for (JsValueList::iterator it = values.begin(); it != values.end(); it++) | 209 for (JsValueList::iterator it = values.begin(); it != values.end(); it++) |
203 result.push_back(SubscriptionPtr(new Subscription(*it))); | 210 result.push_back(SubscriptionPtr(new Subscription(*it))); |
204 return result; | 211 return result; |
205 } | 212 } |
206 | 213 |
207 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, | 214 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, |
208 const std::string& contentType, | 215 const std::string& contentType, |
209 const std::string& documentUrl) | 216 const std::string& documentUrl) const |
210 { | 217 { |
211 JsValuePtr func = jsEngine->Evaluate("API.checkFilterMatch"); | 218 JsValuePtr func = jsEngine->Evaluate("API.checkFilterMatch"); |
212 JsValueList params; | 219 JsValueList params; |
213 params.push_back(jsEngine->NewValue(url)); | 220 params.push_back(jsEngine->NewValue(url)); |
214 params.push_back(jsEngine->NewValue(contentType)); | 221 params.push_back(jsEngine->NewValue(contentType)); |
215 params.push_back(jsEngine->NewValue(documentUrl)); | 222 params.push_back(jsEngine->NewValue(documentUrl)); |
216 JsValuePtr result = func->Call(params); | 223 JsValuePtr result = func->Call(params); |
217 if (!result->IsNull()) | 224 if (!result->IsNull()) |
218 return FilterPtr(new Filter(result)); | 225 return FilterPtr(new Filter(result)); |
219 else | 226 else |
220 return FilterPtr(); | 227 return FilterPtr(); |
221 } | 228 } |
222 | 229 |
223 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri
ng& domain) const | 230 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri
ng& domain) const |
224 { | 231 { |
225 JsValuePtr func = jsEngine->Evaluate("API.getElementHidingSelectors"); | 232 JsValuePtr func = jsEngine->Evaluate("API.getElementHidingSelectors"); |
226 JsValueList params; | 233 JsValueList params; |
227 params.push_back(jsEngine->NewValue(domain)); | 234 params.push_back(jsEngine->NewValue(domain)); |
228 JsValueList result = func->Call(params)->AsList(); | 235 JsValueList result = func->Call(params)->AsList(); |
229 std::vector<std::string> selectors; | 236 std::vector<std::string> selectors; |
230 for (JsValueList::iterator it = result.begin(); it != result.end(); ++it) | 237 for (JsValueList::iterator it = result.begin(); it != result.end(); ++it) |
231 selectors.push_back((*it)->AsString()); | 238 selectors.push_back((*it)->AsString()); |
232 return selectors; | 239 return selectors; |
233 } | 240 } |
| 241 |
| 242 JsValuePtr FilterEngine::GetPref(const std::string& pref) const |
| 243 { |
| 244 JsValuePtr func = jsEngine->Evaluate("API.getPref"); |
| 245 JsValueList params; |
| 246 params.push_back(jsEngine->NewValue(pref)); |
| 247 return func->Call(params); |
| 248 } |
| 249 |
| 250 void FilterEngine::SetPref(const std::string& pref, JsValuePtr value) |
| 251 { |
| 252 JsValuePtr func = jsEngine->Evaluate("API.setPref"); |
| 253 JsValueList params; |
| 254 params.push_back(jsEngine->NewValue(pref)); |
| 255 params.push_back(value); |
| 256 func->Call(params); |
| 257 } |
OLD | NEW |