-
Notifications
You must be signed in to change notification settings - Fork 920
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Respect Expires
field for custom adblock subscriptions
#18836
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -343,6 +343,22 @@ pub unsafe extern "C" fn filter_list_metadata_title( | |
} | ||
} | ||
|
||
/// Returns the amount of time this filter list should be considered valid for, | ||
/// in hours. Defaults to 168 (i.e. 7 days) if unspecified by the | ||
/// `FilterListMetadata`. | ||
#[no_mangle] | ||
pub unsafe extern "C" fn filter_list_metadata_expires(metadata: *const FilterListMetadata) -> u16 { | ||
use adblock::lists::ExpiresInterval; | ||
|
||
const DEFAULT_EXPIRES_HOURS: u16 = 7 * 24; | ||
|
||
match (*metadata).expires.as_ref() { | ||
Some(ExpiresInterval::Days(d)) => 24 * *d as u16, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder do we really need to have the dedicated enum values for days and hours. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That API is intended as an AST-like representation - i.e. "just" parsing without applying any application logic. Could be used to construct a list header as well. |
||
Some(ExpiresInterval::Hours(h)) => *h, | ||
None => DEFAULT_EXPIRES_HOURS, | ||
} | ||
} | ||
|
||
/// Destroy a `FilterListMetadata` once you are done with it. | ||
#[no_mangle] | ||
pub unsafe extern "C" fn filter_list_metadata_destroy(metadata: *mut FilterListMetadata) { | ||
|
@@ -351,13 +367,16 @@ pub unsafe extern "C" fn filter_list_metadata_destroy(metadata: *mut FilterListM | |
} | ||
} | ||
|
||
/// Get EngineDebugInfo from the engine. Should be destoyed later by calling | ||
/// engine_debug_info_destroy(..). | ||
#[no_mangle] | ||
pub unsafe extern "C" fn get_engine_debug_info(engine: *mut Engine) -> *mut EngineDebugInfo { | ||
assert!(!engine.is_null()); | ||
let engine = Box::leak(Box::from_raw(engine)); | ||
Box::into_raw(Box::new(engine.get_debug_info())) | ||
} | ||
|
||
/// Returns the field of EngineDebugInfo structure. | ||
#[no_mangle] | ||
pub unsafe extern "C" fn engine_debug_info_get_attr( | ||
debug_info: *mut EngineDebugInfo, | ||
|
@@ -371,6 +390,11 @@ pub unsafe extern "C" fn engine_debug_info_get_attr( | |
*regex_data_size = info.blocker_debug_info.regex_data.len(); | ||
} | ||
|
||
/// Returns the fields of EngineDebugInfo->regex_data[index]. | ||
/// | ||
/// |regex| stay untouched if it ==None in the original structure. | ||
/// | ||
/// |index| must be in range [0, regex_data.len() - 1]. | ||
#[no_mangle] | ||
pub unsafe extern "C" fn engine_debug_info_get_regex_entry( | ||
debug_info: *mut EngineDebugInfo, | ||
|
@@ -394,6 +418,7 @@ pub unsafe extern "C" fn engine_debug_info_get_regex_entry( | |
*usage_count = entry.usage_count; | ||
} | ||
|
||
/// Destroy a `EngineDebugInfo` once you are done with it. | ||
#[no_mangle] | ||
pub unsafe extern "C" fn engine_debug_info_destroy(debug_info: *mut EngineDebugInfo) { | ||
if !debug_info.is_null() { | ||
|
@@ -408,6 +433,12 @@ pub unsafe extern "C" fn discard_regex(engine: *mut Engine, regex_id: u64) { | |
engine.discard_regex(regex_id); | ||
} | ||
|
||
/// Setup discard policy for adblock regexps. | ||
/// | ||
/// |cleanup_interval_sec| how ofter the engine should check the policy. | ||
/// | ||
/// |discard_unused_sec| time in sec after unused regex will be discarded. Zero | ||
/// means disable discarding completely. | ||
#[no_mangle] | ||
pub unsafe extern "C" fn setup_discard_policy( | ||
engine: *mut Engine, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,7 @@ typedef ADBLOCK_EXPORT struct FilterListMetadata { | |
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FilterListMetadata has a default ctor. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch - I added a default value in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added default value |
||
absl::optional<std::string> homepage; | ||
absl::optional<std::string> title; | ||
uint16_t expires; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just wonder, why 2 bytes integer is used here? Why not something like int64 which size matches to a word? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, could we change name to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maximum valid value is 14 days i.e. 336 hours, so it will definitely always fit in a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The names here correspond directly to the in-list representation, but I could add a doc comment at least There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added doc comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
So using uint16 instead uint64 is about saving a few bytes against perf loss. |
||
|
||
FilterListMetadata(FilterListMetadata&&); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,7 +60,22 @@ bool ParseOptionalStringField(const base::Value* value, | |
} | ||
} | ||
|
||
const base::TimeDelta kListUpdateInterval = base::Days(7); | ||
bool ParseExpiresWithFallback(const base::Value* value, uint16_t* field) { | ||
if (value == nullptr) { | ||
*field = kSubscriptionDefaultExpiresHours; | ||
return true; | ||
} else if (!value->is_int()) { | ||
return false; | ||
} else { | ||
int64_t i = value->GetInt(); | ||
if (i < 0 || i > 14 * 24) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets make another const or use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll make another const here; it's just a "maximum valid" value which doesn't necessarily have anything to do with the default There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added |
||
return false; | ||
} | ||
*field = (uint16_t)i; | ||
return true; | ||
} | ||
} | ||
|
||
const base::TimeDelta kListRetryInterval = base::Hours(1); | ||
const base::TimeDelta kListCheckInitialDelay = base::Minutes(1); | ||
|
||
|
@@ -101,6 +116,8 @@ void SubscriptionInfo::RegisterJSONConverter( | |
"homepage", &SubscriptionInfo::homepage, &ParseOptionalStringField); | ||
converter->RegisterCustomValueField<absl::optional<std::string>>( | ||
"title", &SubscriptionInfo::title, &ParseOptionalStringField); | ||
converter->RegisterCustomValueField<uint16_t>( | ||
"expires", &SubscriptionInfo::expires, &ParseExpiresWithFallback); | ||
} | ||
|
||
AdBlockSubscriptionServiceManager::AdBlockSubscriptionServiceManager( | ||
|
@@ -172,7 +189,8 @@ void AdBlockSubscriptionServiceManager::OnUpdateTimer( | |
info = BuildInfoFromDict(sub_url, *list_subscription_dict); | ||
|
||
base::TimeDelta until_next_refresh = | ||
kListUpdateInterval - (base::Time::Now() - info.last_update_attempt); | ||
base::Hours(info.expires) - | ||
(base::Time::Now() - info.last_update_attempt); | ||
|
||
if (info.enabled && | ||
((info.last_update_attempt != info.last_successful_update_attempt) || | ||
|
@@ -357,6 +375,8 @@ void AdBlockSubscriptionServiceManager::OnListMetadata( | |
info->homepage = absl::nullopt; | ||
} | ||
|
||
info->expires = metadata.expires; | ||
|
||
UpdateSubscriptionPrefs(sub_url, *info); | ||
|
||
NotifyObserversOfServiceEvent(); | ||
|
@@ -447,6 +467,7 @@ void AdBlockSubscriptionServiceManager::UpdateSubscriptionPrefs( | |
if (info.title) { | ||
subscription_dict.Set("title", *info.title); | ||
} | ||
subscription_dict.Set("expires", info.expires); | ||
subscriptions.Set(sub_url.spec(), std::move(subscription_dict)); | ||
|
||
// TODO(bridiver) - change to pref registrar | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
! Title: Test list | ||
! Homepage: https://example.com/list.txt | ||
! Expires: 3 days | ||
||b.com^*logo.png^ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the same as
kSubscriptionDefaultExpiresHours
.Could we avoid duplication of this const somehow?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose this could be exported as part of the FFI, I'll give it a try
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exporting worked without any issues; deduplicated everywhere