Skip to content

Commit

Permalink
Merge pull request #32 from brave/use-inout-params
Browse files Browse the repository at this point in the history
Use inout params to simplify matches API
  • Loading branch information
antonok-edm authored Feb 1, 2021
2 parents 96fa135 + 90059d8 commit dee5183
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 24 deletions.
11 changes: 7 additions & 4 deletions src/lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 13 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
Expand All @@ -78,19 +81,21 @@ 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(),
_ => ptr::null_mut(),
},
None => ptr::null_mut(),
};
blocker_result.matched
}

/// Adds a tag to the engine for consideration
Expand Down
15 changes: 7 additions & 8 deletions src/wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions src/wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit dee5183

Please sign in to comment.