From 90059d882ca7f82d3cb80966c0ef65cf18f7a75d Mon Sep 17 00:00:00 2001 From: Anton Lazarev Date: Fri, 29 Jan 2021 16:02:55 -0800 Subject: [PATCH] use inout params to simplify matches API --- src/lib.h | 11 +++++++---- src/lib.rs | 21 +++++++++++++-------- src/wrapper.cc | 15 +++++++-------- src/wrapper.h | 8 ++++---- 4 files changed, 31 insertions(+), 24 deletions(-) diff --git a/src/lib.h b/src/lib.h index 74bdbc4fc201..4ed037c11444 100644 --- a/src/lib.h +++ b/src/lib.h @@ -34,18 +34,21 @@ struct C_Engine *engine_create(const char *rules); /** * Checks if a `url` matches for the specified `Engine` within the context. + * + * This API is designed for multi-engine use, so block results are used both as inputs and + * outputs. They will be updated to reflect additional checking within this engine, rather than + * being replaced with results just for this engine. */ -bool engine_match(struct C_Engine *engine, +void engine_match(struct C_Engine *engine, const char *url, const char *host, const char *tab_host, bool third_party, const char *resource_type, + bool *did_match_rule, bool *did_match_exception, bool *did_match_important, - char **redirect, - bool previously_matched_rule, - bool force_check_exceptions); + char **redirect); /** * Adds a tag to the engine for consideration diff --git a/src/lib.rs b/src/lib.rs index 82baefd917d9..ec1c5a0d6499 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -52,6 +52,10 @@ pub unsafe extern "C" fn engine_create(rules: *const c_char) -> *mut Engine { } /// Checks if a `url` matches for the specified `Engine` within the context. +/// +/// This API is designed for multi-engine use, so block results are used both as inputs and +/// outputs. They will be updated to reflect additional checking within this engine, rather than +/// being replaced with results just for this engine. #[no_mangle] pub unsafe extern "C" fn engine_match( engine: *mut Engine, @@ -60,12 +64,11 @@ pub unsafe extern "C" fn engine_match( tab_host: *const c_char, third_party: bool, resource_type: *const c_char, + did_match_rule: *mut bool, did_match_exception: *mut bool, did_match_important: *mut bool, redirect: *mut *mut c_char, - previously_matched_rule: bool, - force_check_exceptions: bool, -) -> bool { +) { let url = CStr::from_ptr(url).to_str().unwrap(); let host = CStr::from_ptr(host).to_str().unwrap(); let tab_host = CStr::from_ptr(tab_host).to_str().unwrap(); @@ -78,11 +81,14 @@ pub unsafe extern "C" fn engine_match( tab_host, resource_type, Some(third_party), - previously_matched_rule, - force_check_exceptions, + // Checking normal rules is skipped if a normal rule or exception rule was found previously + *did_match_rule || *did_match_exception, + // Always check exceptions unless one was found previously + !*did_match_exception, ); - *did_match_exception = blocker_result.exception.is_some(); - *did_match_important = blocker_result.important; + *did_match_rule |= blocker_result.matched; + *did_match_exception |= blocker_result.exception.is_some(); + *did_match_important |= blocker_result.important; *redirect = match blocker_result.redirect { Some(x) => match CString::new(x) { Ok(y) => y.into_raw(), @@ -90,7 +96,6 @@ pub unsafe extern "C" fn engine_match( }, None => ptr::null_mut(), }; - blocker_result.matched } /// Adds a tag to the engine for consideration diff --git a/src/wrapper.cc b/src/wrapper.cc index 253c9fcd9444..36ec31c252cc 100644 --- a/src/wrapper.cc +++ b/src/wrapper.cc @@ -43,22 +43,21 @@ Engine::Engine() : raw(engine_create("")) { Engine::Engine(const std::string& rules) : raw(engine_create(rules.c_str())) { } -bool Engine::matches(const std::string& url, const std::string& host, +void Engine::matches(const std::string& url, const std::string& host, const std::string& tab_host, bool is_third_party, - const std::string& resource_type, bool previously_matched_rule, - bool previously_matched_exception, std::string* redirect, - bool* did_match_exception, bool* did_match_important) { + const std::string& resource_type, bool* did_match_rule, + bool* did_match_exception, bool* did_match_important, + std::string* redirect) { char* redirect_char_ptr = nullptr; - bool result = engine_match(raw, url.c_str(), host.c_str(),tab_host.c_str(), - is_third_party, resource_type.c_str(), did_match_exception, did_match_important, - &redirect_char_ptr, previously_matched_rule, previously_matched_exception); + engine_match(raw, url.c_str(), host.c_str(), tab_host.c_str(), is_third_party, + resource_type.c_str(), did_match_rule, did_match_exception, + did_match_important, &redirect_char_ptr); if (redirect_char_ptr) { if (redirect) { *redirect = redirect_char_ptr; } c_char_buffer_destroy(redirect_char_ptr); } - return result; } bool Engine::deserialize(const char* data, size_t data_size) { diff --git a/src/wrapper.h b/src/wrapper.h index deb76b7be7b7..de353ae26695 100644 --- a/src/wrapper.h +++ b/src/wrapper.h @@ -63,11 +63,11 @@ class ADBLOCK_EXPORT Engine { public: Engine(); Engine(const std::string& rules); - bool matches(const std::string& url, const std::string& host, + void matches(const std::string& url, const std::string& host, const std::string& tab_host, bool is_third_party, - const std::string& resource_type, bool previously_matched_rule, - bool previously_matched_exception, std::string *redirect, - bool* did_match_exception, bool* did_match_important); + const std::string& resource_type, bool* did_match_rule, + bool* did_match_exception, bool* did_match_important, + std::string* redirect); bool deserialize(const char* data, size_t data_size); void addTag(const std::string& tag); void addResource(const std::string& key,