| Left: | ||
| Right: |
| LEFT | RIGHT |
|---|---|
| 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-2017 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"); |
| 36 } | 38 } |
| 37 | 39 |
| 38 Filter::Type Filter::GetType() | 40 Filter::Type Filter::GetType() const |
| 39 { | 41 { |
| 40 std::string className = GetClass(); | 42 std::string className = GetClass(); |
| 41 if (className == "BlockingFilter") | 43 if (className == "BlockingFilter") |
| 42 return TYPE_BLOCKING; | 44 return TYPE_BLOCKING; |
| 43 else if (className == "WhitelistFilter") | 45 else if (className == "WhitelistFilter") |
| 44 return TYPE_EXCEPTION; | 46 return TYPE_EXCEPTION; |
| 45 else if (className == "ElemHideFilter") | 47 else if (className == "ElemHideFilter") |
| 46 return TYPE_ELEMHIDE; | 48 return TYPE_ELEMHIDE; |
| 47 else if (className == "ElemHideException") | 49 else if (className == "ElemHideException") |
| 48 return TYPE_ELEMHIDE_EXCEPTION; | 50 return TYPE_ELEMHIDE_EXCEPTION; |
| 49 else if (className == "CommentFilter") | 51 else if (className == "CommentFilter") |
| 50 return TYPE_COMMENT; | 52 return TYPE_COMMENT; |
| 51 else | 53 else |
| 52 return TYPE_INVALID; | 54 return TYPE_INVALID; |
| 53 } | 55 } |
| 54 | 56 |
| 55 bool Filter::IsListed() | 57 bool Filter::IsListed() const |
| 56 { | 58 { |
| 57 JsValuePtr func = jsEngine->Evaluate("API.isListedFilter"); | 59 JsValuePtr func = jsEngine->Evaluate("API.isListedFilter"); |
| 58 JsValueList params; | 60 return func->Call(*this).AsBool(); |
| 59 params.push_back(shared_from_this()); | |
| 60 return func->Call(params).AsBool(); | |
| 61 } | 61 } |
| 62 | 62 |
| 63 void Filter::AddToList() | 63 void Filter::AddToList() |
| 64 { | 64 { |
| 65 JsValuePtr func = jsEngine->Evaluate("API.addFilterToList"); | 65 JsValuePtr func = jsEngine->Evaluate("API.addFilterToList"); |
| 66 JsValueList params; | 66 func->Call(*this); |
| 67 params.push_back(shared_from_this()); | |
| 68 func->Call(params); | |
| 69 } | 67 } |
| 70 | 68 |
| 71 void Filter::RemoveFromList() | 69 void Filter::RemoveFromList() |
| 72 { | 70 { |
| 73 JsValuePtr func = jsEngine->Evaluate("API.removeFilterFromList"); | 71 JsValuePtr func = jsEngine->Evaluate("API.removeFilterFromList"); |
| 74 JsValueList params; | 72 func->Call(*this); |
| 75 params.push_back(shared_from_this()); | |
| 76 func->Call(params); | |
| 77 } | 73 } |
| 78 | 74 |
| 79 bool Filter::operator==(const Filter& filter) const | 75 bool Filter::operator==(const Filter& filter) const |
| 80 { | 76 { |
| 81 return GetProperty("text")->AsString() == filter.GetProperty("text")->AsString (); | 77 return GetProperty("text")->AsString() == filter.GetProperty("text")->AsString (); |
| 82 } | 78 } |
| 83 | 79 |
| 84 Subscription::Subscription(JsValue&& value) | 80 Subscription::Subscription(JsValue&& value) |
| 85 : JsValue(std::move(value)) | 81 : JsValue(std::move(value)) |
| 86 { | 82 { |
| 87 if (!IsObject()) | 83 if (!IsObject()) |
| 88 throw std::runtime_error("JavaScript value is not an object"); | 84 throw std::runtime_error("JavaScript value is not an object"); |
| 89 } | 85 } |
| 90 | 86 |
| 91 bool Subscription::IsListed() | 87 bool Subscription::IsListed() const |
| 92 { | 88 { |
| 93 JsValuePtr func = jsEngine->Evaluate("API.isListedSubscription"); | 89 JsValuePtr func = jsEngine->Evaluate("API.isListedSubscription"); |
| 94 JsValueList params; | 90 return func->Call(*this).AsBool(); |
| 95 params.push_back(shared_from_this()); | |
| 96 return func->Call(params).AsBool(); | |
| 97 } | 91 } |
| 98 | 92 |
| 99 void Subscription::AddToList() | 93 void Subscription::AddToList() |
| 100 { | 94 { |
| 101 JsValuePtr func = jsEngine->Evaluate("API.addSubscriptionToList"); | 95 JsValuePtr func = jsEngine->Evaluate("API.addSubscriptionToList"); |
| 102 JsValueList params; | 96 func->Call(*this); |
| 103 params.push_back(shared_from_this()); | |
| 104 func->Call(params); | |
| 105 } | 97 } |
| 106 | 98 |
| 107 void Subscription::RemoveFromList() | 99 void Subscription::RemoveFromList() |
| 108 { | 100 { |
| 109 JsValuePtr func = jsEngine->Evaluate("API.removeSubscriptionFromList"); | 101 JsValuePtr func = jsEngine->Evaluate("API.removeSubscriptionFromList"); |
| 110 JsValueList params; | 102 func->Call(*this); |
| 111 params.push_back(shared_from_this()); | |
| 112 func->Call(params); | |
| 113 } | 103 } |
| 114 | 104 |
| 115 void Subscription::UpdateFilters() | 105 void Subscription::UpdateFilters() |
| 116 { | 106 { |
| 117 JsValuePtr func = jsEngine->Evaluate("API.updateSubscription"); | 107 JsValuePtr func = jsEngine->Evaluate("API.updateSubscription"); |
| 118 JsValueList params; | 108 func->Call(*this); |
| 119 params.push_back(shared_from_this()); | 109 } |
| 120 func->Call(params); | 110 |
| 121 } | 111 bool Subscription::IsUpdating() const |
| 122 | |
| 123 bool Subscription::IsUpdating() | |
| 124 { | 112 { |
| 125 JsValuePtr func = jsEngine->Evaluate("API.isSubscriptionUpdating"); | 113 JsValuePtr func = jsEngine->Evaluate("API.isSubscriptionUpdating"); |
| 126 JsValueList params; | 114 return func->Call(*this).AsBool(); |
| 127 params.push_back(shared_from_this()); | |
| 128 JsValue result = func->Call(params); | |
| 129 return result.AsBool(); | |
| 130 } | 115 } |
| 131 | 116 |
| 132 bool Subscription::operator==(const Subscription& subscription) const | 117 bool Subscription::operator==(const Subscription& subscription) const |
| 133 { | 118 { |
| 134 return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsSt ring(); | 119 return GetProperty("url")->AsString() == subscription.GetProperty("url")->AsSt ring(); |
| 135 } | 120 } |
| 136 | 121 |
| 137 FilterEngine::FilterEngine(JsEnginePtr jsEngine, | 122 namespace |
| 138 const FilterEngine::Prefs& preconfiguredPrefs) | 123 { |
| 139 : jsEngine(jsEngine), initialized(false), firstRun(false), updateCheckId(0) | 124 class Sync |
| 140 { | 125 { |
| 141 jsEngine->SetEventCallback("_init", std::bind(&FilterEngine::InitDone, | 126 public: |
| 142 this, std::placeholders::_1)); | 127 Sync() |
| 143 | 128 :initialized(false) |
| 144 { | |
| 145 // Lock the JS engine while we are loading scripts, no timeouts should fire | |
| 146 // until we are done. | |
| 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 { | 129 { |
| 154 preconfiguredPrefsObject->SetProperty(it->first, it->second); | 130 |
| 155 } | 131 } |
| 156 jsEngine->SetGlobalProperty("_preconfiguredPrefs", preconfiguredPrefsObject) ; | 132 void Wait() |
| 157 // Load adblockplus scripts | 133 { |
| 158 for (int i = 0; !jsSources[i].empty(); i += 2) | 134 std::unique_lock<std::mutex> lock(mutex); |
| 159 jsEngine->Evaluate(jsSources[i + 1], jsSources[i]); | 135 while (!initialized) |
| 160 } | 136 cv.wait(lock); |
| 161 | 137 } |
| 162 // TODO: This should really be implemented via a conditional variable | 138 void Set() |
| 163 while (!initialized) | 139 { |
| 164 ::Sleep(10); | 140 { |
| 141 std::unique_lock<std::mutex> lock(mutex); | |
| 142 initialized = true; | |
| 143 } | |
| 144 cv.notify_all(); | |
| 145 } | |
| 146 private: | |
| 147 std::mutex mutex; | |
| 148 std::condition_variable cv; | |
| 149 bool initialized; | |
| 150 }; | |
| 151 } | |
| 152 | |
| 153 FilterEngine::FilterEngine(const JsEnginePtr& jsEngine) | |
| 154 : jsEngine(jsEngine), firstRun(false), updateCheckId(0) | |
| 155 { | |
| 156 } | |
| 157 | |
| 158 void FilterEngine::CreateAsync(const JsEnginePtr& jsEngine, | |
| 159 const FilterEngine::OnCreatedCallback& onCreated, | |
| 160 const FilterEngine::CreationParameters& params) | |
| 161 { | |
| 162 FilterEnginePtr filterEngine(new FilterEngine(jsEngine)); | |
| 163 auto sync = std::make_shared<Sync>(); | |
| 164 auto isConnectionAllowedCallback = params.isConnectionAllowedCallback; | |
| 165 if (isConnectionAllowedCallback) | |
| 166 jsEngine->SetIsConnectionAllowedCallback([sync, jsEngine]()->bool | |
| 167 { | |
| 168 sync->Wait(); | |
| 169 return jsEngine->IsConnectionAllowed(); | |
| 170 }); | |
| 171 jsEngine->SetEventCallback("_init", [jsEngine, filterEngine, onCreated, sync, isConnectionAllowedCallback](JsValueList& params) | |
| 172 { | |
| 173 filterEngine->firstRun = params.size() && params[0]->AsBool(); | |
| 174 if (isConnectionAllowedCallback) | |
| 175 { | |
| 176 std::weak_ptr<FilterEngine> weakFilterEngine = filterEngine; | |
| 177 jsEngine->SetIsConnectionAllowedCallback([weakFilterEngine, isConnectionAl lowedCallback]()->bool | |
| 178 { | |
| 179 auto filterEngine = weakFilterEngine.lock(); | |
| 180 if (!filterEngine) | |
| 181 return false; | |
| 182 return isConnectionAllowedCallback(filterEngine->GetAllowedConnectionTyp e().get()); | |
| 183 }); | |
| 184 } | |
| 185 sync->Set(); | |
| 186 onCreated(filterEngine); | |
| 187 jsEngine->RemoveEventCallback("_init"); | |
| 188 }); | |
| 189 | |
| 190 // Lock the JS engine while we are loading scripts, no timeouts should fire | |
| 191 // until we are done. | |
| 192 const JsContext context(jsEngine); | |
| 193 | |
| 194 // Set the preconfigured prefs | |
| 195 JsValuePtr preconfiguredPrefsObject = jsEngine->NewObject(); | |
| 196 for (FilterEngine::Prefs::const_iterator it = params.preconfiguredPrefs.begin( ); | |
| 197 it != params.preconfiguredPrefs.end(); it++) | |
| 198 { | |
| 199 preconfiguredPrefsObject->SetProperty(it->first, it->second); | |
| 200 } | |
| 201 jsEngine->SetGlobalProperty("_preconfiguredPrefs", preconfiguredPrefsObject); | |
| 202 // Load adblockplus scripts | |
| 203 for (int i = 0; !jsSources[i].empty(); i += 2) | |
| 204 jsEngine->Evaluate(jsSources[i + 1], jsSources[i]); | |
| 205 } | |
| 206 | |
| 207 FilterEnginePtr FilterEngine::Create(const JsEnginePtr& jsEngine, | |
| 208 const FilterEngine::CreationParameters& params) | |
| 209 { | |
| 210 FilterEnginePtr retValue; | |
| 211 Sync sync; | |
| 212 CreateAsync(jsEngine, [&retValue, &sync](const FilterEnginePtr& filterEngine) | |
| 213 { | |
| 214 retValue = filterEngine; | |
| 215 sync.Set(); | |
| 216 }, params); | |
| 217 sync.Wait(); | |
| 218 return retValue; | |
| 165 } | 219 } |
| 166 | 220 |
| 167 namespace | 221 namespace |
| 168 { | 222 { |
| 169 typedef std::map<FilterEngine::ContentType, std::string> ContentTypeMap; | 223 typedef std::map<FilterEngine::ContentType, std::string> ContentTypeMap; |
| 170 | 224 |
| 171 ContentTypeMap CreateContentTypeMap() | 225 ContentTypeMap CreateContentTypeMap() |
| 172 { | 226 { |
| 173 ContentTypeMap contentTypes; | 227 ContentTypeMap contentTypes; |
| 174 contentTypes[FilterEngine::CONTENT_TYPE_OTHER] = "OTHER"; | 228 contentTypes[FilterEngine::CONTENT_TYPE_OTHER] = "OTHER"; |
| 175 contentTypes[FilterEngine::CONTENT_TYPE_SCRIPT] = "SCRIPT"; | 229 contentTypes[FilterEngine::CONTENT_TYPE_SCRIPT] = "SCRIPT"; |
| 176 contentTypes[FilterEngine::CONTENT_TYPE_IMAGE] = "IMAGE"; | 230 contentTypes[FilterEngine::CONTENT_TYPE_IMAGE] = "IMAGE"; |
| 177 contentTypes[FilterEngine::CONTENT_TYPE_STYLESHEET] = "STYLESHEET"; | 231 contentTypes[FilterEngine::CONTENT_TYPE_STYLESHEET] = "STYLESHEET"; |
| 178 contentTypes[FilterEngine::CONTENT_TYPE_OBJECT] = "OBJECT"; | 232 contentTypes[FilterEngine::CONTENT_TYPE_OBJECT] = "OBJECT"; |
| 179 contentTypes[FilterEngine::CONTENT_TYPE_SUBDOCUMENT] = "SUBDOCUMENT"; | 233 contentTypes[FilterEngine::CONTENT_TYPE_SUBDOCUMENT] = "SUBDOCUMENT"; |
| 180 contentTypes[FilterEngine::CONTENT_TYPE_DOCUMENT] = "DOCUMENT"; | 234 contentTypes[FilterEngine::CONTENT_TYPE_DOCUMENT] = "DOCUMENT"; |
| 235 contentTypes[FilterEngine::CONTENT_TYPE_PING] = "PING"; | |
| 181 contentTypes[FilterEngine::CONTENT_TYPE_XMLHTTPREQUEST] = "XMLHTTPREQUEST"; | 236 contentTypes[FilterEngine::CONTENT_TYPE_XMLHTTPREQUEST] = "XMLHTTPREQUEST"; |
| 182 contentTypes[FilterEngine::CONTENT_TYPE_OBJECT_SUBREQUEST] = "OBJECT_SUBREQU EST"; | 237 contentTypes[FilterEngine::CONTENT_TYPE_OBJECT_SUBREQUEST] = "OBJECT_SUBREQU EST"; |
| 183 contentTypes[FilterEngine::CONTENT_TYPE_FONT] = "FONT"; | 238 contentTypes[FilterEngine::CONTENT_TYPE_FONT] = "FONT"; |
| 184 contentTypes[FilterEngine::CONTENT_TYPE_MEDIA] = "MEDIA"; | 239 contentTypes[FilterEngine::CONTENT_TYPE_MEDIA] = "MEDIA"; |
| 185 contentTypes[FilterEngine::CONTENT_TYPE_ELEMHIDE] = "ELEMHIDE"; | 240 contentTypes[FilterEngine::CONTENT_TYPE_ELEMHIDE] = "ELEMHIDE"; |
| 241 contentTypes[FilterEngine::CONTENT_TYPE_GENERICBLOCK] = "GENERICBLOCK"; | |
| 242 contentTypes[FilterEngine::CONTENT_TYPE_GENERICHIDE] = "GENERICHIDE"; | |
| 186 return contentTypes; | 243 return contentTypes; |
| 187 } | 244 } |
| 188 } | 245 } |
| 189 | 246 |
| 190 const ContentTypeMap FilterEngine::contentTypes = CreateContentTypeMap(); | 247 const ContentTypeMap FilterEngine::contentTypes = CreateContentTypeMap(); |
| 191 | 248 |
| 192 std::string FilterEngine::ContentTypeToString(ContentType contentType) | 249 std::string FilterEngine::ContentTypeToString(ContentType contentType) |
| 193 { | 250 { |
| 194 ContentTypeMap::const_iterator it = contentTypes.find(contentType); | 251 ContentTypeMap::const_iterator it = contentTypes.find(contentType); |
| 195 if (it != contentTypes.end()) | 252 if (it != contentTypes.end()) |
| 196 return it->second; | 253 return it->second; |
| 197 throw std::invalid_argument("Argument is not a valid ContentType"); | 254 throw std::invalid_argument("Argument is not a valid ContentType"); |
| 198 } | 255 } |
| 199 | 256 |
| 200 FilterEngine::ContentType FilterEngine::StringToContentType(const std::string& c ontentType) | 257 FilterEngine::ContentType FilterEngine::StringToContentType(const std::string& c ontentType) |
| 201 { | 258 { |
| 202 std::string contentTypeUpper = contentType; | 259 std::string contentTypeUpper = contentType; |
| 203 std::transform(contentType.begin(), contentType.end(), contentTypeUpper.begin( ), ::toupper); | 260 std::transform(contentType.begin(), contentType.end(), contentTypeUpper.begin( ), ::toupper); |
| 204 for (ContentTypeMap::const_iterator it = contentTypes.begin(); | 261 for (const auto& contentType : contentTypes) |
| 205 it != contentTypes.end(); it++) | 262 { |
| 206 { | 263 if (contentType.second == contentTypeUpper) |
| 207 if (it->second == contentTypeUpper) | 264 return contentType.first; |
| 208 return it->first; | |
| 209 } | 265 } |
| 210 throw std::invalid_argument("Cannot convert argument to ContentType"); | 266 throw std::invalid_argument("Cannot convert argument to ContentType"); |
| 211 } | 267 } |
| 212 | 268 |
| 213 void FilterEngine::InitDone(JsValueList& params) | |
| 214 { | |
| 215 jsEngine->RemoveEventCallback("_init"); | |
| 216 initialized = true; | |
| 217 firstRun = params.size() && params[0]->AsBool(); | |
| 218 } | |
| 219 | |
| 220 bool FilterEngine::IsFirstRun() const | 269 bool FilterEngine::IsFirstRun() const |
| 221 { | 270 { |
| 222 return firstRun; | 271 return firstRun; |
| 223 } | 272 } |
| 224 | 273 |
| 225 FilterPtr FilterEngine::GetFilter(const std::string& text) | 274 FilterPtr FilterEngine::GetFilter(const std::string& text) const |
| 226 { | 275 { |
| 227 JsValuePtr func = jsEngine->Evaluate("API.getFilterFromText"); | 276 JsValuePtr func = jsEngine->Evaluate("API.getFilterFromText"); |
| 228 JsValueList params; | 277 return FilterPtr(new Filter(func->Call(*jsEngine->NewValue(text)))); |
| 229 params.push_back(jsEngine->NewValue(text)); | 278 } |
| 230 return FilterPtr(new Filter(func->Call(params))); | 279 |
| 231 } | 280 SubscriptionPtr FilterEngine::GetSubscription(const std::string& url) const |
| 232 | |
| 233 SubscriptionPtr FilterEngine::GetSubscription(const std::string& url) | |
| 234 { | 281 { |
| 235 JsValuePtr func = jsEngine->Evaluate("API.getSubscriptionFromUrl"); | 282 JsValuePtr func = jsEngine->Evaluate("API.getSubscriptionFromUrl"); |
| 236 JsValueList params; | 283 return SubscriptionPtr(new Subscription(func->Call(*jsEngine->NewValue(url)))) ; |
| 237 params.push_back(jsEngine->NewValue(url)); | |
| 238 return SubscriptionPtr(new Subscription(func->Call(params))); | |
| 239 } | 284 } |
| 240 | 285 |
| 241 std::vector<FilterPtr> FilterEngine::GetListedFilters() const | 286 std::vector<FilterPtr> FilterEngine::GetListedFilters() const |
| 242 { | 287 { |
| 243 JsValuePtr func = jsEngine->Evaluate("API.getListedFilters"); | 288 JsValuePtr func = jsEngine->Evaluate("API.getListedFilters"); |
| 244 JsValueList values = func->Call().AsList(); | 289 JsValueList values = func->Call().AsList(); |
| 245 std::vector<FilterPtr> result; | 290 std::vector<FilterPtr> result; |
| 246 for (JsValueList::iterator it = values.begin(); it != values.end(); it++) | 291 for (JsValueList::iterator it = values.begin(); it != values.end(); it++) |
| 247 result.push_back(FilterPtr(new Filter(std::move(**it)))); | 292 result.push_back(FilterPtr(new Filter(std::move(**it)))); |
| 248 return result; | 293 return result; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 264 JsValueList values = func->Call().AsList(); | 309 JsValueList values = func->Call().AsList(); |
| 265 std::vector<SubscriptionPtr> result; | 310 std::vector<SubscriptionPtr> result; |
| 266 for (JsValueList::iterator it = values.begin(); it != values.end(); it++) | 311 for (JsValueList::iterator it = values.begin(); it != values.end(); it++) |
| 267 result.push_back(SubscriptionPtr(new Subscription(std::move(**it)))); | 312 result.push_back(SubscriptionPtr(new Subscription(std::move(**it)))); |
| 268 return result; | 313 return result; |
| 269 } | 314 } |
| 270 | 315 |
| 271 void FilterEngine::ShowNextNotification(const std::string& url) | 316 void FilterEngine::ShowNextNotification(const std::string& url) |
| 272 { | 317 { |
| 273 JsValuePtr func = jsEngine->Evaluate("API.showNextNotification"); | 318 JsValuePtr func = jsEngine->Evaluate("API.showNextNotification"); |
| 274 JsValueList params; | 319 JsConstValueList params; |
| 275 if (!url.empty()) | 320 if (!url.empty()) |
| 276 { | 321 { |
| 277 params.push_back(jsEngine->NewValue(url)); | 322 params.push_back(jsEngine->NewValue(url)); |
| 278 } | 323 } |
| 279 func->Call(params); | 324 func->Call(params); |
| 280 } | 325 } |
| 281 | 326 |
| 282 void FilterEngine::SetShowNotificationCallback(const ShowNotificationCallback& v alue) | 327 void FilterEngine::SetShowNotificationCallback(const ShowNotificationCallback& v alue) |
| 283 { | 328 { |
| 284 if (!value) | 329 if (!value) |
| 285 return; | 330 return; |
| 286 | 331 |
| 287 jsEngine->SetEventCallback("_showNotification", | 332 jsEngine->SetEventCallback("_showNotification", |
| 288 std::bind(&FilterEngine::ShowNotification, this, value, | 333 std::bind(&FilterEngine::ShowNotification, this, value, |
| 289 std::placeholders::_1)); | 334 std::placeholders::_1)); |
| 290 } | 335 } |
| 291 | 336 |
| 292 void FilterEngine::RemoveShowNotificationCallback() | 337 void FilterEngine::RemoveShowNotificationCallback() |
| 293 { | 338 { |
| 294 jsEngine->RemoveEventCallback("_showNotification"); | 339 jsEngine->RemoveEventCallback("_showNotification"); |
| 295 } | 340 } |
| 296 | 341 |
| 297 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, | 342 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, |
| 298 ContentType contentType, | 343 ContentTypeMask contentTypeMask, |
| 299 const std::string& documentUrl) const | 344 const std::string& documentUrl) const |
| 300 { | 345 { |
| 301 std::vector<std::string> documentUrls; | 346 std::vector<std::string> documentUrls; |
| 302 documentUrls.push_back(documentUrl); | 347 documentUrls.push_back(documentUrl); |
| 303 return Matches(url, contentType, documentUrls); | 348 return Matches(url, contentTypeMask, documentUrls); |
| 304 } | 349 } |
| 305 | 350 |
| 306 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, | 351 AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url, |
| 307 ContentType contentType, | 352 ContentTypeMask contentTypeMask, |
| 308 const std::vector<std::string>& documentUrls) const | 353 const std::vector<std::string>& documentUrls) const |
| 309 { | 354 { |
| 310 if (documentUrls.empty()) | 355 if (documentUrls.empty()) |
| 311 return CheckFilterMatch(url, contentType, ""); | 356 return CheckFilterMatch(url, contentTypeMask, ""); |
| 312 | 357 |
| 313 std::string lastDocumentUrl = documentUrls.front(); | 358 std::string lastDocumentUrl = documentUrls.front(); |
| 314 for (std::vector<std::string>::const_iterator it = documentUrls.begin(); | 359 for (const auto& documentUrl : documentUrls) { |
| 315 it != documentUrls.end(); it++) { | |
| 316 const std::string documentUrl = *it; | |
| 317 AdblockPlus::FilterPtr match = CheckFilterMatch(documentUrl, | 360 AdblockPlus::FilterPtr match = CheckFilterMatch(documentUrl, |
| 318 CONTENT_TYPE_DOCUMENT, | 361 CONTENT_TYPE_DOCUMENT, |
| 319 lastDocumentUrl); | 362 lastDocumentUrl); |
| 320 if (match && match->GetType() == AdblockPlus::Filter::TYPE_EXCEPTION) | 363 if (match && match->GetType() == AdblockPlus::Filter::TYPE_EXCEPTION) |
| 321 return match; | 364 return match; |
| 322 lastDocumentUrl = documentUrl; | 365 lastDocumentUrl = documentUrl; |
| 323 } | 366 } |
| 324 | 367 |
| 325 return CheckFilterMatch(url, contentType, lastDocumentUrl); | 368 return CheckFilterMatch(url, contentTypeMask, lastDocumentUrl); |
| 326 } | 369 } |
| 327 | 370 |
| 328 bool FilterEngine::IsDocumentWhitelisted(const std::string& url, | 371 bool FilterEngine::IsDocumentWhitelisted(const std::string& url, |
| 329 const std::vector<std::string>& documentUrls) const | 372 const std::vector<std::string>& documentUrls) const |
| 330 { | 373 { |
| 331 return !!GetWhitelistingFilter(url, CONTENT_TYPE_DOCUMENT, documentUrls); | 374 return !!GetWhitelistingFilter(url, CONTENT_TYPE_DOCUMENT, documentUrls); |
| 332 } | 375 } |
| 333 | 376 |
| 334 bool FilterEngine::IsElemhideWhitelisted(const std::string& url, | 377 bool FilterEngine::IsElemhideWhitelisted(const std::string& url, |
| 335 const std::vector<std::string>& documentUrls) const | 378 const std::vector<std::string>& documentUrls) const |
| 336 { | 379 { |
| 337 return !!GetWhitelistingFilter(url, CONTENT_TYPE_ELEMHIDE, documentUrls); | 380 return !!GetWhitelistingFilter(url, CONTENT_TYPE_ELEMHIDE, documentUrls); |
| 338 } | 381 } |
| 339 | 382 |
| 340 AdblockPlus::FilterPtr FilterEngine::CheckFilterMatch(const std::string& url, | 383 AdblockPlus::FilterPtr FilterEngine::CheckFilterMatch(const std::string& url, |
| 341 ContentType contentType, | 384 ContentTypeMask contentTypeMask, |
| 342 const std::string& documentUrl) const | 385 const std::string& documentUrl) const |
| 343 { | 386 { |
| 344 JsValuePtr func = jsEngine->Evaluate("API.checkFilterMatch"); | 387 JsValuePtr func = jsEngine->Evaluate("API.checkFilterMatch"); |
| 345 JsValueList params; | 388 JsConstValueList params; |
| 346 params.push_back(jsEngine->NewValue(url)); | 389 params.push_back(jsEngine->NewValue(url)); |
| 347 params.push_back(jsEngine->NewValue(ContentTypeToString(contentType))); | 390 params.push_back(jsEngine->NewValue(contentTypeMask)); |
| 348 params.push_back(jsEngine->NewValue(documentUrl)); | 391 params.push_back(jsEngine->NewValue(documentUrl)); |
| 349 JsValue result = func->Call(params); | 392 JsValue result = func->Call(params); |
| 350 if (!result.IsNull()) | 393 if (!result.IsNull()) |
| 351 return FilterPtr(new Filter(std::move(result))); | 394 return FilterPtr(new Filter(std::move(result))); |
| 352 else | 395 else |
| 353 return FilterPtr(); | 396 return FilterPtr(); |
| 354 } | 397 } |
| 355 | 398 |
| 356 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri ng& domain) const | 399 std::vector<std::string> FilterEngine::GetElementHidingSelectors(const std::stri ng& domain) const |
| 357 { | 400 { |
| 358 JsValuePtr func = jsEngine->Evaluate("API.getElementHidingSelectors"); | 401 JsValuePtr func = jsEngine->Evaluate("API.getElementHidingSelectors"); |
| 359 JsValueList params; | 402 JsValueList result = func->Call(*jsEngine->NewValue(domain)).AsList(); |
| 360 params.push_back(jsEngine->NewValue(domain)); | |
| 361 JsValueList result = func->Call(params).AsList(); | |
| 362 std::vector<std::string> selectors; | 403 std::vector<std::string> selectors; |
| 363 for (JsValueList::iterator it = result.begin(); it != result.end(); ++it) | 404 for (const auto& r: result) |
| 364 selectors.push_back((*it)->AsString()); | 405 selectors.push_back(r->AsString()); |
| 365 return selectors; | 406 return selectors; |
| 366 } | 407 } |
| 367 | 408 |
| 368 JsValuePtr FilterEngine::GetPref(const std::string& pref) const | 409 JsValuePtr FilterEngine::GetPref(const std::string& pref) const |
| 369 { | 410 { |
| 370 JsValuePtr func = jsEngine->Evaluate("API.getPref"); | 411 JsValuePtr func = jsEngine->Evaluate("API.getPref"); |
| 371 JsValueList params; | 412 return std::make_shared<JsValue>(func->Call(*jsEngine->NewValue(pref))); |
| 413 } | |
| 414 | |
| 415 void FilterEngine::SetPref(const std::string& pref, JsValuePtr value) | |
| 416 { | |
| 417 JsValuePtr func = jsEngine->Evaluate("API.setPref"); | |
| 418 JsConstValueList params; | |
| 372 params.push_back(jsEngine->NewValue(pref)); | 419 params.push_back(jsEngine->NewValue(pref)); |
| 373 return std::make_shared<JsValue>(func->Call(params)); | 420 if (value) |
|
Oleksandr
2016/01/31 21:23:24
Nit: This reminds me - isn't it time to drop use o
Eric
2016/02/08 19:54:50
It's already been done in the libadblockplus repos
| |
| 374 } | 421 params.push_back(value); |
| 375 | |
| 376 void FilterEngine::SetPref(const std::string& pref, JsValuePtr value) | |
| 377 { | |
| 378 JsValuePtr func = jsEngine->Evaluate("API.setPref"); | |
| 379 JsValueList params; | |
| 380 params.push_back(jsEngine->NewValue(pref)); | |
| 381 params.push_back(value); | |
| 382 func->Call(params); | 422 func->Call(params); |
| 383 } | 423 } |
| 384 | 424 |
| 385 std::string FilterEngine::GetHostFromURL(const std::string& url) | 425 std::string FilterEngine::GetHostFromURL(const std::string& url) const |
| 386 { | 426 { |
| 387 JsValuePtr func = jsEngine->Evaluate("API.getHostFromUrl"); | 427 JsValuePtr func = jsEngine->Evaluate("API.getHostFromUrl"); |
| 388 JsValueList params; | 428 return func->Call(*jsEngine->NewValue(url)).AsString(); |
| 389 params.push_back(jsEngine->NewValue(url)); | |
| 390 return func->Call(params).AsString(); | |
| 391 } | 429 } |
| 392 | 430 |
| 393 void FilterEngine::SetUpdateAvailableCallback( | 431 void FilterEngine::SetUpdateAvailableCallback( |
| 394 FilterEngine::UpdateAvailableCallback callback) | 432 FilterEngine::UpdateAvailableCallback callback) |
| 395 { | 433 { |
| 396 jsEngine->SetEventCallback("updateAvailable", | 434 jsEngine->SetEventCallback("updateAvailable", |
| 397 std::bind(&FilterEngine::UpdateAvailable, this, callback, | 435 std::bind(&FilterEngine::UpdateAvailable, this, callback, |
| 398 std::placeholders::_1)); | 436 std::placeholders::_1)); |
| 399 } | 437 } |
| 400 | 438 |
| 401 void FilterEngine::RemoveUpdateAvailableCallback() | 439 void FilterEngine::RemoveUpdateAvailableCallback() |
| 402 { | 440 { |
| 403 jsEngine->RemoveEventCallback("updateAvailable"); | 441 jsEngine->RemoveEventCallback("updateAvailable"); |
| 404 } | 442 } |
| 405 | 443 |
| 406 void FilterEngine::UpdateAvailable( | 444 void FilterEngine::UpdateAvailable( |
| 407 FilterEngine::UpdateAvailableCallback callback, JsValueList& params) | 445 FilterEngine::UpdateAvailableCallback callback, JsValueList& params) |
| 408 { | 446 { |
| 409 if (params.size() >= 1 && !params[0]->IsNull()) | 447 if (params.size() >= 1 && !params[0]->IsNull()) |
| 410 callback(params[0]->AsString()); | 448 callback(params[0]->AsString()); |
| 411 } | 449 } |
| 412 | 450 |
| 413 void FilterEngine::ForceUpdateCheck( | 451 void FilterEngine::ForceUpdateCheck( |
| 414 FilterEngine::UpdateCheckDoneCallback callback) | 452 const FilterEngine::UpdateCheckDoneCallback& callback) |
| 415 { | 453 { |
| 416 std::string eventName = "_updateCheckDone"; | 454 JsValuePtr func = jsEngine->Evaluate("API.forceUpdateCheck"); |
| 417 eventName += ++updateCheckId; | 455 JsConstValueList params; |
| 418 | 456 if (callback) |
| 419 jsEngine->SetEventCallback(eventName, std::bind(&FilterEngine::UpdateCheckDone , | 457 { |
| 458 std::string eventName = "_updateCheckDone" + std::to_string(++updateCheckId) ; | |
| 459 jsEngine->SetEventCallback(eventName, std::bind(&FilterEngine::UpdateCheckDo ne, | |
| 420 this, eventName, callback, std::placeholders::_1)); | 460 this, eventName, callback, std::placeholders::_1)); |
| 421 | 461 params.push_back(jsEngine->NewValue(eventName)); |
| 422 JsValuePtr func = jsEngine->Evaluate("API.forceUpdateCheck"); | 462 } |
| 423 JsValueList params; | |
| 424 params.push_back(jsEngine->NewValue(eventName)); | |
| 425 func->Call(params); | 463 func->Call(params); |
| 426 } | 464 } |
| 427 | 465 |
| 428 void FilterEngine::UpdateCheckDone(const std::string& eventName, | 466 void FilterEngine::UpdateCheckDone(const std::string& eventName, |
| 429 FilterEngine::UpdateCheckDoneCallback callback, JsValueList& params) | 467 FilterEngine::UpdateCheckDoneCallback callback, JsValueList& params) |
| 430 { | 468 { |
| 431 jsEngine->RemoveEventCallback(eventName); | 469 jsEngine->RemoveEventCallback(eventName); |
| 432 | 470 |
| 433 std::string error(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsSt ring() : ""); | 471 std::string error(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsSt ring() : ""); |
| 434 callback(error); | 472 callback(error); |
| 435 } | 473 } |
| 436 | 474 |
| 437 void FilterEngine::SetFilterChangeCallback(FilterEngine::FilterChangeCallback ca llback) | 475 void FilterEngine::SetFilterChangeCallback(FilterEngine::FilterChangeCallback ca llback) |
| 438 { | 476 { |
| 439 jsEngine->SetEventCallback("filterChange", std::bind(&FilterEngine::FilterChan ged, | 477 jsEngine->SetEventCallback("filterChange", std::bind(&FilterEngine::FilterChan ged, |
| 440 this, callback, std::placeholders::_1)); | 478 this, callback, std::placeholders::_1)); |
| 441 } | 479 } |
| 442 | 480 |
| 443 void FilterEngine::RemoveFilterChangeCallback() | 481 void FilterEngine::RemoveFilterChangeCallback() |
| 444 { | 482 { |
| 445 jsEngine->RemoveEventCallback("filterChange"); | 483 jsEngine->RemoveEventCallback("filterChange"); |
| 484 } | |
| 485 | |
| 486 void FilterEngine::SetAllowedConnectionType(const std::string* value) | |
| 487 { | |
| 488 SetPref("allowed_connection_type", value ? jsEngine->NewValue(*value) : nullpt r); | |
| 489 } | |
| 490 | |
| 491 std::unique_ptr<std::string> FilterEngine::GetAllowedConnectionType() const | |
| 492 { | |
| 493 auto prefValue = GetPref("allowed_connection_type"); | |
| 494 if (prefValue->IsUndefined()) | |
| 495 return nullptr; | |
| 496 return std::unique_ptr<std::string>(new std::string(prefValue->AsString())); | |
| 446 } | 497 } |
| 447 | 498 |
| 448 void FilterEngine::FilterChanged(FilterEngine::FilterChangeCallback callback, Js ValueList& params) | 499 void FilterEngine::FilterChanged(FilterEngine::FilterChangeCallback callback, Js ValueList& params) |
| 449 { | 500 { |
| 450 std::string action(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsS tring() : ""); | 501 std::string action(params.size() >= 1 && !params[0]->IsNull() ? params[0]->AsS tring() : ""); |
| 451 JsValuePtr item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false)); | 502 JsValuePtr item(params.size() >= 2 ? params[1] : jsEngine->NewValue(false)); |
| 452 callback(action, item); | 503 callback(action, item); |
| 453 } | 504 } |
| 454 | 505 |
| 455 void FilterEngine::ShowNotification(const ShowNotificationCallback& callback, | 506 void FilterEngine::ShowNotification(const ShowNotificationCallback& callback, |
| 456 const JsValueList& params) | 507 const JsValueList& params) |
| 457 { | 508 { |
| 458 if (params.size() < 1) | 509 if (params.size() < 1) |
| 459 return; | 510 return; |
| 460 | 511 |
| 461 if (!params[0]->IsObject()) | 512 if (!params[0]->IsObject()) |
| 462 { | 513 { |
| 463 return; | 514 return; |
| 464 } | 515 } |
| 465 callback(NotificationPtr(new Notification(std::move(*params[0])))); | 516 callback(NotificationPtr(new Notification(std::move(*params[0])))); |
| 466 } | 517 } |
| 467 | 518 |
| 468 | 519 |
| 469 int FilterEngine::CompareVersions(const std::string& v1, const std::string& v2) | 520 int FilterEngine::CompareVersions(const std::string& v1, const std::string& v2) const |
| 470 { | 521 { |
| 471 JsValueList params; | 522 JsConstValueList params; |
| 472 params.push_back(jsEngine->NewValue(v1)); | 523 params.push_back(jsEngine->NewValue(v1)); |
| 473 params.push_back(jsEngine->NewValue(v2)); | 524 params.push_back(jsEngine->NewValue(v2)); |
| 474 JsValuePtr func = jsEngine->Evaluate("API.compareVersions"); | 525 JsValuePtr func = jsEngine->Evaluate("API.compareVersions"); |
| 475 return func->Call(params).AsInt(); | 526 return func->Call(params).AsInt(); |
| 476 } | 527 } |
| 477 | 528 |
| 478 FilterPtr FilterEngine::GetWhitelistingFilter(const std::string& url, | 529 FilterPtr FilterEngine::GetWhitelistingFilter(const std::string& url, |
| 479 ContentType contentType, const std::string& documentUrl) const | 530 ContentTypeMask contentTypeMask, const std::string& documentUrl) const |
| 480 { | 531 { |
| 481 FilterPtr match = Matches(url, contentType, documentUrl); | 532 FilterPtr match = Matches(url, contentTypeMask, documentUrl); |
| 482 if (match && match->GetType() == Filter::TYPE_EXCEPTION) | 533 if (match && match->GetType() == Filter::TYPE_EXCEPTION) |
| 483 { | 534 { |
| 484 return match; | 535 return match; |
| 485 } | 536 } |
| 486 return FilterPtr(); | 537 return FilterPtr(); |
| 487 } | 538 } |
| 488 | 539 |
| 489 FilterPtr FilterEngine::GetWhitelistingFilter(const std::string& url, | 540 FilterPtr FilterEngine::GetWhitelistingFilter(const std::string& url, |
| 490 ContentType contentType, | 541 ContentTypeMask contentTypeMask, |
| 491 const std::vector<std::string>& documentUrls) const | 542 const std::vector<std::string>& documentUrls) const |
| 492 { | 543 { |
| 493 if (documentUrls.empty()) | 544 if (documentUrls.empty()) |
| 494 { | 545 { |
| 495 return GetWhitelistingFilter(url, contentType, ""); | 546 return GetWhitelistingFilter(url, contentTypeMask, ""); |
| 496 } | 547 } |
| 497 | 548 |
| 498 std::vector<std::string>::const_iterator urlIterator = documentUrls.begin(); | 549 std::vector<std::string>::const_iterator urlIterator = documentUrls.begin(); |
| 499 std::string currentUrl = url; | 550 std::string currentUrl = url; |
| 500 do | 551 do |
| 501 { | 552 { |
| 502 std::string parentUrl = *urlIterator++; | 553 std::string parentUrl = *urlIterator++; |
| 503 FilterPtr filter = GetWhitelistingFilter( | 554 FilterPtr filter = GetWhitelistingFilter(currentUrl, contentTypeMask, parent Url); |
| 504 currentUrl, contentType, parentUrl); | |
| 505 if (filter) | 555 if (filter) |
| 506 { | 556 { |
| 507 return filter; | 557 return filter; |
| 508 } | 558 } |
| 509 currentUrl = parentUrl; | 559 currentUrl = parentUrl; |
| 510 } | 560 } |
| 511 while (urlIterator != documentUrls.end()); | 561 while (urlIterator != documentUrls.end()); |
| 512 return FilterPtr(); | 562 return FilterPtr(); |
| 513 } | 563 } |
| LEFT | RIGHT |