Ad blocking

Cefrium can cancel requests inside the engine, before they are sent, using filter lists in the EasyList format. Nothing is built in and nothing is blocked until your app loads a list. This guide covers 0.21.0.

Load a list

// Application.onCreate(), main process only.
Cefrium.downloadAndLoadEasyList(this, (rules, error) -> {
    // rules: how many the list has now; -1 if none could be loaded.
    // error: null, or why the download failed.
});

The list is downloaded at most once a day and kept in the app's files. The download goes through the engine's own network stack, so it uses the proxy set with setProxy on the default profile instead of a direct connection. If it fails, or returns something that is not a filter list (a Wi-Fi sign-in page, say), the last good copy is used.

Other lists work the same way: Cefrium.downloadAndLoadAdBlockList(context, url, callback), or Cefrium.loadAdBlockList(path) for a file you ship or fetch yourself. Loading the same list again replaces its rules. Parsing EasyList takes about 130 ms on our test device, so do it off the main thread.

What a list can do

RuleSupported
||ads.example.com^ (a host and its subdomains)Yes
||example.com/ads/ (a path prefix on a host)Yes
@@ exceptions of bothYes
@@||example.com^$document (no blocking on that site)Yes
Options third-party, domain=, script, image, stylesheet, xmlhttprequest, subdocument, media, font, object, ping, other, and their ~ negationsYes
Any other option (popup, csp, redirect, ...)The rule is skipped, not applied to every request
Wildcards, regular expressions, rules not anchored with ||Skipped
Cosmetic rules (##, element hiding)No: blocking is network only

With EasyList that is about 44,700 of its rules. A list never blocks the page itself: a main-frame navigation always goes through, and only the page's own requests are filtered.

Per browser and per site

browser.setAdBlockEnabled(false);   // this browser only; on by default
int blocked = browser.getBlockedCount();

getBlockedCount counts what this browser cancelled: requests a list matched plus any your RequestInterceptor blocked.

To let the user turn blocking off for a site, give the profile the sites they chose. On those sites a page loads everything, as with an @@||site^$document rule:

CefriumProfile.getDefault().setAdBlockExceptions(
        Arrays.asList("news.example", "https://shop.example"));

A host covers its subdomains; an origin covers only that scheme, host and port. Each profile has its own sites, incognito included, and the call replaces the previous set. They last for the run: keep the user's choices in your app and set them at start.

YouTube

Cefrium.loadYouTubeAdRules() adds Cefrium's heuristics for YouTube's ad requests. They are not part of any list and YouTube changes these endpoints, so treat them as best effort. Off unless you call it.

Your own rules in code

setRequestInterceptor decides per request in Java, after the lists. It runs on the network thread for every request, so keep it fast; a browser without an interceptor makes no call into Java for its requests.

Licences of the lists

Filter lists are not part of Cefrium and carry their own licence. EasyList is dual-licensed GPL-3.0 or CC BY-SA 3.0. Downloading it at run time, as above, keeps it out of your APK. If you ship a list inside your app, follow its licence for that file, and credit it where your app lists its open-source licences.