Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Fix #3113: Fetch adblock data every 6 hours.
Browse files Browse the repository at this point in the history
  • Loading branch information
iccub committed Dec 9, 2020
1 parent dd49e30 commit 74a0bd6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Client/Application/Delegates/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UIViewControllerRestorati
if let authInfo = KeychainWrapper.sharedAppContainerKeychain.authenticationInfo(), authInfo.isPasscodeRequiredImmediately {
authenticator?.willEnterForeground()
}

AdblockResourceDownloader.shared.startLoading()
}

func applicationWillResignActive(_ application: UIApplication) {
Expand Down
14 changes: 14 additions & 0 deletions Client/Frontend/Settings/AdblockDebugMenuTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class AdblockDebugMenuTableViewController: TableViewController {
if listNames.isEmpty { return }

self.dataSource.sections = [self.actionsSection,
self.fetchSection,
self.datesSection,
self.bundledListsSection(names: listNames),
self.downloadedListsSection(names: listNames)]
Expand All @@ -44,6 +45,19 @@ class AdblockDebugMenuTableViewController: TableViewController {
return section
}

private var fetchSection: Section {
var section = Section(footer: "Last time we pinged the server for new data. If adblock list hasn't changed `Last Time Updated` section does not update.")
let dateFormatter = DateFormatter().then {
$0.dateStyle = .short
$0.timeStyle = .short
}

section.rows = [.init(text: "Last fetch time", detailText:
dateFormatter.string(from: AdblockResourceDownloader.shared.lastFetchDate))]

return section
}

private var datesSection: Section {
var section = Section(header: "Last time updated",
footer: "When the lists were last time updated on the device")
Expand Down
17 changes: 15 additions & 2 deletions Client/WebFilters/AdblockResourceDownloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,22 @@ class AdblockResourceDownloader {
Preferences.Shields.useRegionAdBlock.observe(from: self)
}

/// Initialized with year 1970 to force adblock fetch at first launch.
private(set) var lastFetchDate = Date(timeIntervalSince1970: 0)

func startLoading() {
AdblockResourceDownloader.shared.regionalAdblockResourcesSetup()
AdblockResourceDownloader.shared.generalAdblockResourcesSetup()
let now = Date()
let fetchInterval = AppConstants.buildChannel.isPublic ? 6.hours : 10.minutes

if abs(now.timeIntervalSince(lastFetchDate)) >= fetchInterval {
log.debug("Checking if new adblock resources have been uploaded.")
lastFetchDate = now

AdblockResourceDownloader.shared.regionalAdblockResourcesSetup()
AdblockResourceDownloader.shared.generalAdblockResourcesSetup()
} else {
log.debug("Less than 1 hour passed, not checking for new adblock resources")
}
}

func regionalAdblockResourcesSetup() {
Expand Down

0 comments on commit 74a0bd6

Please sign in to comment.