| LEFT | RIGHT |
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2015 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 * |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 params.push_back(shared_from_this()); | 127 params.push_back(shared_from_this()); |
| 128 JsValuePtr result = func->Call(params); | 128 JsValuePtr result = func->Call(params); |
| 129 return result->AsBool(); | 129 return result->AsBool(); |
| 130 } | 130 } |
| 131 | 131 |
| 132 bool Subscription::operator==(const Subscription& subscription) const | 132 bool Subscription::operator==(const Subscription& subscription) const |
| 133 { | 133 { |
| 134 return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsSt
ring(); | 134 return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsSt
ring(); |
| 135 } | 135 } |
| 136 | 136 |
| 137 FilterEngine::FilterEngine(JsEnginePtr jsEngine) | 137 FilterEngine::FilterEngine(JsEnginePtr jsEngine, |
| 138 const FilterEngine::Prefs& preconfiguredPrefs) |
| 138 : jsEngine(jsEngine), initialized(false), firstRun(false), updateCheckId(0) | 139 : jsEngine(jsEngine), initialized(false), firstRun(false), updateCheckId(0) |
| 139 { | 140 { |
| 140 jsEngine->SetEventCallback("init", std::bind(&FilterEngine::InitDone, | 141 jsEngine->SetEventCallback("_init", std::bind(&FilterEngine::InitDone, |
| 141 this, std::placeholders::_1)); | 142 this, std::placeholders::_1)); |
| 142 | 143 |
| 143 { | 144 { |
| 144 // Lock the JS engine while we are loading scripts, no timeouts should fire | 145 // Lock the JS engine while we are loading scripts, no timeouts should fire |
| 145 // until we are done. | 146 // until we are done. |
| 146 const JsContext context(jsEngine); | 147 const JsContext context(jsEngine); |
| 148 |
| 149 // Set the preconfigured prefs |
| 150 JsValuePtr preconfiguredPrefsObject = jsEngine->NewObject(); |
| 151 for (FilterEngine::Prefs::const_iterator it = preconfiguredPrefs.begin(); |
| 152 it != preconfiguredPrefs.end(); it++) |
| 153 { |
| 154 preconfiguredPrefsObject->SetProperty(it->first, it->second); |
| 155 } |
| 156 jsEngine->SetGlobalProperty("_preconfiguredPrefs", preconfiguredPrefsObject)
; |
| 157 // Load adblockplus scripts |
| 147 for (int i = 0; !jsSources[i].empty(); i += 2) | 158 for (int i = 0; !jsSources[i].empty(); i += 2) |
| 148 jsEngine->Evaluate(jsSources[i + 1], jsSources[i]); | 159 jsEngine->Evaluate(jsSources[i + 1], jsSources[i]); |
| 149 } | 160 } |
| 150 | 161 |
| 151 // TODO: This should really be implemented via a conditional variable | 162 // TODO: This should really be implemented via a conditional variable |
| 152 while (!initialized) | 163 while (!initialized) |
| 153 ::Sleep(10); | 164 ::Sleep(10); |
| 154 } | 165 } |
| 155 | 166 |
| 167 namespace |
| 168 { |
| 169 typedef std::map<FilterEngine::ContentType, std::string> ContentTypeMap; |
| 170 |
| 171 ContentTypeMap CreateContentTypeMap() |
| 172 { |
| 173 ContentTypeMap contentTypes; |
| 174 contentTypes[FilterEngine::CONTENT_TYPE_OTHER] = "OTHER"; |
| 175 contentTypes[FilterEngine::CONTENT_TYPE_SCRIPT] = "SCRIPT"; |
| 176 contentTypes[FilterEngine::CONTENT_TYPE_IMAGE] = "IMAGE"; |
| 177 contentTypes[FilterEngine::CONTENT_TYPE_STYLESHEET] = "STYLESHEET"; |
| 178 contentTypes[FilterEngine::CONTENT_TYPE_OBJECT] = "OBJECT"; |
| 179 contentTypes[FilterEngine::CONTENT_TYPE_SUBDOCUMENT] = "SUBDOCUMENT"; |
| 180 contentTypes[FilterEngine::CONTENT_TYPE_DOCUMENT] = "DOCUMENT"; |
| 181 contentTypes[FilterEngine::CONTENT_TYPE_XMLHTTPREQUEST] = "XMLHTTPREQUEST"; |
| 182 contentTypes[FilterEngine::CONTENT_TYPE_OBJECT_SUBREQUEST] = "OBJECT_SUBREQU
EST"; |
| 183 contentTypes[FilterEngine::CONTENT_TYPE_FONT] = "FONT"; |
| 184 contentTypes[FilterEngine::CONTENT_TYPE_MEDIA] = "MEDIA"; |
| 185 contentTypes[FilterEngine::CONTENT_TYPE_ELEMHIDE] = "ELEMHIDE"; |
| 186 return contentTypes; |
| 187 } |
| 188 } |
| 189 |
| 190 const ContentTypeMap FilterEngine::contentTypes = CreateContentTypeMap(); |
| 191 |
| 192 std::string FilterEngine::ContentTypeToString(ContentType contentType) |
| 193 { |
| 194 ContentTypeMap::const_iterator it = contentTypes.find(contentType); |
| 195 if (it != contentTypes.end()) |
| 196 return it->second; |
| 197 throw std::invalid_argument("Argument is not a valid ContentType"); |
| 198 } |
| 199 |
| 200 FilterEngine::ContentType FilterEngine::StringToContentType(const std::string& c
ontentType) |
| 201 { |
| 202 std::string contentTypeUpper = contentType; |
| 203 std::transform(contentType.begin(), contentType.end(), contentTypeUpper.begin(
), ::toupper); |
| 204 for (ContentTypeMap::const_iterator it = contentTypes.begin(); |
| 205 it != contentTypes.end(); it++) |
| 206 { |
| 207 if (it->second == contentTypeUpper) |
| 208 return it->first; |
| 209 } |
| 210 throw std::invalid_argument("Cannot convert argument to ContentType"); |
| 211 } |
| 212 |
| 156 void FilterEngine::InitDone(JsValueList& params) | 213 void FilterEngine::InitDone(JsValueList& params) |
| 157 { | 214 { |
| 158 jsEngine->RemoveEventCallback("init"); | 215 jsEngine->RemoveEventCallback("_init"); |
| 159 initialized = true; | 216 initialized = true; |
| 160 firstRun = params.size() && params[0]->AsBool(); | 217 firstRun = params.size() && params[0]->AsBool(); |
| 161 } | 218 } |
| 162 | 219 |
| 163 bool FilterEngine::IsFirstRun() const | 220 bool FilterEngine::IsFirstRun() const |
| 164 { | 221 { |
| 165 return firstRun; | 222 return firstRun; |
| 166 } | 223 } |
| 167 | 224 |
| 168 FilterPtr FilterEngine::GetFilter(const std::string& text) | 225 FilterPtr FilterEngine::GetFilter(const std::string& text) |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 std::vector<SubscriptionPtr> FilterEngine::FetchAvailableSubscriptions() const | 261 std::vector<SubscriptionPtr> FilterEngine::FetchAvailableSubscriptions() const |
| 205 { | 262 { |
| 206 JsValuePtr func = jsEngine->Evaluate("API.getRecommendedSubscriptions"); | 263 JsValuePtr func = jsEngine->Evaluate("API.getRecommendedSubscriptions"); |
| 207 JsValueList values = func->Call()->AsList(); | 264 JsValueList values = func->Call()->AsList(); |
| 208 std::vector<SubscriptionPtr> result; | 265 std::vector<SubscriptionPtr> result; |
| 209 for (JsValueList::iterator it = values.begin(); it != values.end(); it++) | 266 for (JsValueList::iterator it = values.begin(); it != values.end(); it++) |
| 210 result.push_back(SubscriptionPtr(new Subscription(*it))); | 267 result.push_back(SubscriptionPtr(new Subscription(*it))); |
| 211 return result; | 268 return result; |
| 212 } | 269 } |
| 213 | 270 |
| 271 void FilterEngine::ShowNextNotification(const std::string& url) |
| 272 { |
| 273 JsValuePtr func = jsEngine->Evaluate("API.showNextNotification"); |
| 274 JsValueList params; |
| 275 if (!url.empty()) |
| 276 { |
| 277 params.push_back(jsEngine->NewValue(url)); |
| 278 } |
| 279 func->Call(params); |
| 280 } |
| 281 |
| 282 void FilterEngine::SetShowNotificationCallback(const ShowNotificationCallback& v
alue) |
| 283 { |
| 284 if (!value) |
| 285 return; |
| 286 |
| 287 jsEngine->SetEventCallback("_showNotification", |
| 288 std::bind(&FilterEngine::ShowNotification, this, value, |
| 289 std::placeholders::_1)); |
| 290 } |
| 291 |
| 292 void FilterEngine::RemoveShowNotificationCallback() |
| 293 { |
| 294 jsEngine->RemoveEventCallback("_showNotification"); |
| 295 } |
| 296 |
| 214 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, | 297 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, |
| 215 const std::string& contentType, | 298 ContentType contentType, |
| 216 const std::string& documentUrl) const | 299 const std::string& documentUrl) const |
| 217 { | 300 { |
| 218 std::vector<std::string> documentUrls; | 301 std::vector<std::string> documentUrls; |
| 219 documentUrls.push_back(documentUrl); | 302 documentUrls.push_back(documentUrl); |
| 220 return Matches(url, contentType, documentUrls); | 303 return Matches(url, contentType, documentUrls); |
| 221 } | 304 } |
| 222 | 305 |
| 223 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, | 306 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, |
| 224 const std::string& contentType, | 307 ContentType contentType, |
| 225 const std::vector<std::string>& documentUrls) const | 308 const std::vector<std::string>& documentUrls) const |
| 226 { | 309 { |
| 227 if (documentUrls.empty()) | 310 if (documentUrls.empty()) |
| 228 return CheckFilterMatch(url, contentType, ""); | 311 return CheckFilterMatch(url, contentType, ""); |
| 229 | 312 |
| 230 std::string lastDocumentUrl = documentUrls.front(); | 313 std::string lastDocumentUrl = documentUrls.front(); |
| 231 for (std::vector<std::string>::const_iterator it = documentUrls.begin(); | 314 for (std::vector<std::string>::const_iterator it = documentUrls.begin(); |
| 232 it != documentUrls.end(); it++) { | 315 it != documentUrls.end(); it++) { |
| 233 const std::string documentUrl = *it; | 316 const std::string documentUrl = *it; |
| 234 AdblockPlus::FilterPtr match = CheckFilterMatch(documentUrl, "DOCUMENT", | 317 AdblockPlus::FilterPtr match = CheckFilterMatch(documentUrl, |
| 318 CONTENT_TYPE_DOCUMENT, |
| 235 lastDocumentUrl); | 319 lastDocumentUrl); |
| 236 if (match && match->GetType() == AdblockPlus::Filter::TYPE_EXCEPTION) | 320 if (match && match->GetType() == AdblockPlus::Filter::TYPE_EXCEPTION) |
| 237 return match; | 321 return match; |
| 238 lastDocumentUrl = documentUrl; | 322 lastDocumentUrl = documentUrl; |
| 239 } | 323 } |
| 240 | 324 |
| 241 return CheckFilterMatch(url, contentType, lastDocumentUrl); | 325 return CheckFilterMatch(url, contentType, lastDocumentUrl); |
| 242 } | 326 } |
| 243 | 327 |
| 244 AdblockPlus::FilterPtr FilterEngine::CheckFilterMatch(const std::string& url, | 328 AdblockPlus::FilterPtr FilterEngine::CheckFilterMatch(const std::string& url, |
| 245 const std::string& contentType, | 329 ContentType contentType, |
| 246 const std::string& documentUrl) const | 330 const std::string& documentUrl) const |
| 247 { | 331 { |
| 248 JsValuePtr func = jsEngine->Evaluate("API.checkFilterMatch"); | 332 JsValuePtr func = jsEngine->Evaluate("API.checkFilterMatch"); |
| 249 JsValueList params; | 333 JsValueList params; |
| 250 params.push_back(jsEngine->NewValue(url)); | 334 params.push_back(jsEngine->NewValue(url)); |
| 251 params.push_back(jsEngine->NewValue(contentType)); | 335 params.push_back(jsEngine->NewValue(ContentTypeToString(contentType))); |
| 252 params.push_back(jsEngine->NewValue(documentUrl)); | 336 params.push_back(jsEngine->NewValue(documentUrl)); |
| 253 JsValuePtr result = func->Call(params); | 337 JsValuePtr result = func->Call(params); |
| 254 if (!result->IsNull()) | 338 if (!result->IsNull()) |
| 255 return FilterPtr(new Filter(result)); | 339 return FilterPtr(new Filter(result)); |
| 256 else | 340 else |
| 257 return FilterPtr(); | 341 return FilterPtr(); |
| 258 } | 342 } |
| 259 | 343 |
| 260 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri
ng& domain) const | 344 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri
ng& domain) const |
| 261 { | 345 { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 287 } | 371 } |
| 288 | 372 |
| 289 std::string FilterEngine::GetHostFromURL(const std::string& url) | 373 std::string FilterEngine::GetHostFromURL(const std::string& url) |
| 290 { | 374 { |
| 291 JsValuePtr func = jsEngine->Evaluate("API.getHostFromUrl"); | 375 JsValuePtr func = jsEngine->Evaluate("API.getHostFromUrl"); |
| 292 JsValueList params; | 376 JsValueList params; |
| 293 params.push_back(jsEngine->NewValue(url)); | 377 params.push_back(jsEngine->NewValue(url)); |
| 294 return func->Call(params)->AsString(); | 378 return func->Call(params)->AsString(); |
| 295 } | 379 } |
| 296 | 380 |
| 297 | 381 void FilterEngine::SetUpdateAvailableCallback( |
| 298 void FilterEngine::ForceUpdateCheck(FilterEngine::UpdaterCallback callback) | 382 FilterEngine::UpdateAvailableCallback callback) |
| 299 { | 383 { |
| 300 std::string eventName = "updateCheckDone"; | 384 jsEngine->SetEventCallback("updateAvailable", |
| 385 std::bind(&FilterEngine::UpdateAvailable, this, callback, |
| 386 std::placeholders::_1)); |
| 387 } |
| 388 |
| 389 void FilterEngine::RemoveUpdateAvailableCallback() |
| 390 { |
| 391 jsEngine->RemoveEventCallback("updateAvailable"); |
| 392 } |
| 393 |
| 394 void FilterEngine::UpdateAvailable( |
| 395 FilterEngine::UpdateAvailableCallback callback, JsValueList& params) |
| 396 { |
| 397 if (params.size() >= 1 && !params[0]->IsNull()) |
| 398 callback(params[0]->AsString()); |
| 399 } |
| 400 |
| 401 void FilterEngine::ForceUpdateCheck( |
| 402 FilterEngine::UpdateCheckDoneCallback callback) |
| 403 { |
| 404 std::string eventName = "_updateCheckDone"; |
| 301 eventName += ++updateCheckId; | 405 eventName += ++updateCheckId; |
| 302 | 406 |
| 303 jsEngine->SetEventCallback(eventName, std::bind(&FilterEngine::UpdateCheckDone
, | 407 jsEngine->SetEventCallback(eventName, std::bind(&FilterEngine::UpdateCheckDone
, |
| 304 this, eventName, callback, std::placeholders::_1)); | 408 this, eventName, callback, std::placeholders::_1)); |
| 305 | 409 |
| 306 JsValuePtr func = jsEngine->Evaluate("API.forceUpdateCheck"); | 410 JsValuePtr func = jsEngine->Evaluate("API.forceUpdateCheck"); |
| 307 JsValueList params; | 411 JsValueList params; |
| 308 params.push_back(jsEngine->NewValue(eventName)); | 412 params.push_back(jsEngine->NewValue(eventName)); |
| 309 func->Call(params); | 413 func->Call(params); |
| 310 } | 414 } |
| 311 | 415 |
| 312 void FilterEngine::UpdateCheckDone(const std::string& eventName, FilterEngine::U
pdaterCallback callback, JsValueList& params) | 416 void FilterEngine::UpdateCheckDone(const std::string& eventName, |
| 417 FilterEngine::UpdateCheckDoneCallback callback, JsValueList& params) |
| 313 { | 418 { |
| 314 jsEngine->RemoveEventCallback(eventName); | 419 jsEngine->RemoveEventCallback(eventName); |
| 315 | 420 |
| 316 std::string error(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsSt
ring() : ""); | 421 std::string error(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsSt
ring() : ""); |
| 317 callback(error); | 422 callback(error); |
| 318 } | 423 } |
| 319 | 424 |
| 320 void FilterEngine::SetFilterChangeCallback(FilterEngine::FilterChangeCallback ca
llback) | 425 void FilterEngine::SetFilterChangeCallback(FilterEngine::FilterChangeCallback ca
llback) |
| 321 { | 426 { |
| 322 jsEngine->SetEventCallback("filterChange", std::bind(&FilterEngine::FilterChan
ged, | 427 jsEngine->SetEventCallback("filterChange", std::bind(&FilterEngine::FilterChan
ged, |
| 323 this, callback, std::placeholders::_1)); | 428 this, callback, std::placeholders::_1)); |
| 324 } | 429 } |
| 325 | 430 |
| 326 void FilterEngine::RemoveFilterChangeCallback() | 431 void FilterEngine::RemoveFilterChangeCallback() |
| 327 { | 432 { |
| 328 jsEngine->RemoveEventCallback("filterChange"); | 433 jsEngine->RemoveEventCallback("filterChange"); |
| 329 } | 434 } |
| 330 | 435 |
| 331 void FilterEngine::FilterChanged(FilterEngine::FilterChangeCallback callback, Js
ValueList& params) | 436 void FilterEngine::FilterChanged(FilterEngine::FilterChangeCallback callback, Js
ValueList& params) |
| 332 { | 437 { |
| 333 std::string action(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsS
tring() : ""); | 438 std::string action(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsS
tring() : ""); |
| 334 JsValuePtr item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false)); | 439 JsValuePtr item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false)); |
| 335 callback(action, item); | 440 callback(action, item); |
| 336 } | 441 } |
| 442 |
| 443 void FilterEngine::ShowNotification(const ShowNotificationCallback& callback, |
| 444 const JsValueList& params) |
| 445 { |
| 446 if (params.size() < 1) |
| 447 return; |
| 448 |
| 449 callback(Notification::JsValueToNotification(params[0])); |
| 450 } |
| 451 |
| 452 |
| 453 int FilterEngine::CompareVersions(const std::string& v1, const std::string& v2) |
| 454 { |
| 455 JsValueList params; |
| 456 params.push_back(jsEngine->NewValue(v1)); |
| 457 params.push_back(jsEngine->NewValue(v2)); |
| 458 JsValuePtr func = jsEngine->Evaluate("API.compareVersions"); |
| 459 return func->Call(params)->AsInt(); |
| 460 } |
| LEFT | RIGHT |