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 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.

Copy link
Contributor

@AndriusA AndriusA left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Copy link
Contributor

@AndriusA AndriusA left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@AndriusA AndriusA merged commit b1310df into master Oct 28, 2019
@AndriusA AndriusA deleted the web_accessible_resources branch October 28, 2019 12:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use uBO's injections to address some webcompat / perf issues
2 participants