Skip to content
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

Migrate to web_accessible_resources and scriptlets.js #50

Merged
merged 5 commits into from Oct 28, 2019

Conversation

@antonok-edm
Copy link
Collaborator

antonok-edm commented Sep 26, 2019

Closes brave/brave-browser#4564

With the deprecation of the resources.txt file, this allows adblock-rust to pull resources from uBlock Origin's newer web_accessible_resources directory and scriptlets.js file. Redirectable resources from web_accessible_resources and resources for cosmetic injection from scriptlets.js can both be represented as Resource structs.

Snippet from src/resources.rs:

/// Struct representing a resource that can be used by an adblocking engine.
///
/// - `name`: Represents the primary name of the resource, often a filename
/// - `aliases`: Represents secondary names that can be used to access the resource
/// - `kind`: How to interpret the resource data within `content`
/// - `content`: The resource data, encoded using standard base64 configuration
pub struct Resource {
    pub name: String,
    pub aliases: Vec<String>,
    pub kind: ResourceType,
    pub content: String,
}

/// Different ways that the data within the `content` field of a `Resource` can be interpreted.
///
/// - `Mime(type)` - interpret the data according to the MIME type represented by `type`
/// - `Template` - interpret the data as a Javascript scriptlet template, with embedded template parameters in the form of `{{1}}`, `{{2}}`, etc.
pub enum ResourceType {
    Mime(MimeType),
    Template,
}

pub enum MimeType {
    ImageGif,
    TextHtml,
    ApplicationJavascript,
    AudioMp3,
    VideoMp4,
    ImagePng,
    TextPlain,
    Unknown,
}

The resulting Resources can be loaded into the engine directly, or serialized to JSON for transfer to another instance. The equivalent data structures in JSON are as follows:

// Resource
{
    "name": String,
    "aliases": String[],
    "kind": ResourceType,
    "content": String,
}

// ResourceType
{ "mime": MimeType } | "template"

// MimeType
"image/gif" | "text/html" | "application/javascript" | ... | "application/octet-stream"

A new Javascript function uBlockResources is exposed through adblock-rust's neon bindings. This function takes 3 string arguments, namely:

  • The path to uBlock Origin's web_accessible_resources directory
  • The path to uBlock Origin's redirect-engine.js file
  • The path to uBlock Origin's scriptlets.js file

and will return a list of corresponding Resource JavaScript objects. In combination with a locally checked out version of the uBlock Origin source repo, this function can be used to produce a new set of resources for the adblocking engine.

@antonok-edm antonok-edm requested review from AndriusA and bbondy Sep 26, 2019
@antonok-edm antonok-edm self-assigned this Sep 26, 2019
@antonok-edm antonok-edm force-pushed the web_accessible_resources branch from 9a39e66 to 3460597 Sep 26, 2019
Copy link
Collaborator

AndriusA left a comment

looks good to me, one nit on the data structure for resource lookup

also given the complexity of resource handling has also grown we might want to separate it into a submodule instead of having a bunch of them at the top level

src/redirect_resources.rs Outdated Show resolved Hide resolved
@antonok-edm antonok-edm force-pushed the web_accessible_resources branch from 3460597 to f288830 Sep 27, 2019
Copy link
Collaborator

AndriusA left a comment

lgtm

@antonok-edm antonok-edm force-pushed the web_accessible_resources branch 5 times, most recently from 6b95651 to 2d91c04 Sep 28, 2019
@antonok-edm antonok-edm force-pushed the web_accessible_resources branch from 2d91c04 to c24a045 Oct 2, 2019
@antonok-edm antonok-edm force-pushed the web_accessible_resources branch from c24a045 to cfc4e3e Oct 18, 2019
@antonok-edm antonok-edm mentioned this pull request Oct 24, 2019
11 of 32 tasks complete
antonok-edm added 4 commits Sep 25, 2019
…rmat
@AndriusA AndriusA force-pushed the web_accessible_resources branch from cfc4e3e to 2e8d81e Oct 28, 2019
@AndriusA AndriusA force-pushed the web_accessible_resources branch from 2e8d81e to f0ef7fc Oct 28, 2019
@AndriusA AndriusA merged commit b1310df into master Oct 28, 2019
0 of 4 checks passed
0 of 4 checks passed
Travis CI - Branch Build Errored
Details
Travis CI - Pull Request Build Errored
Details
continuous-integration/travis-ci/pr The Travis CI build is in progress
Details
continuous-integration/travis-ci/push The Travis CI build is in progress
Details
@AndriusA AndriusA deleted the web_accessible_resources branch Oct 28, 2019
@bbondy bbondy mentioned this pull request Nov 12, 2019
5 of 32 tasks complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

2 participants
You can’t perform that action at this time.