Skip to content

Commit

Permalink
expose resource assembly from neon bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
antonok-edm committed Sep 26, 2019
1 parent de6bf7b commit 3460597
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ extern crate neon_serde;
extern crate adblock;

use neon::prelude::*;
use std::path::Path;
use adblock::engine::Engine;
use adblock::filter_lists;
use adblock::resources::Resource;
use adblock::resource_assembler::{assemble_web_accessible_resources, assemble_scriptlet_resources};


declare_types! {
pub class JsEngine for Engine {
Expand Down Expand Up @@ -111,7 +115,8 @@ declare_types! {
}

method updateResources(mut cx) {
let resources: String = cx.argument::<JsString>(0)?.value();
let resources_arg = cx.argument::<JsValue>(0)?;
let resources: Vec<Resource> = neon_serde::from_value(&mut cx, resources_arg)?;

let mut this = cx.this();
let guard = cx.lock();
Expand Down Expand Up @@ -158,15 +163,14 @@ declare_types! {
}

method addResource(mut cx) {
let name: String = cx.argument::<JsString>(0)?.value();
let content_type: String = cx.argument::<JsString>(1)?.value();
let data: String = cx.argument::<JsString>(2)?.value();
let resource_arg = cx.argument::<JsValue>(0)?;
let resource: Resource = neon_serde::from_value(&mut cx, resource_arg)?;

let mut this = cx.this();
let guard = cx.lock();
{
let mut engine = this.borrow_mut(&guard);
engine.resource_add(&name, &content_type, &data);
engine.resource_add(resource);
}
Ok(JsNull::new().upcast())
}
Expand Down Expand Up @@ -200,9 +204,23 @@ fn lists(mut cx: FunctionContext) -> JsResult<JsValue> {
Ok(js_list)
}

fn ublock_resources(mut cx: FunctionContext) -> JsResult<JsValue> {
let web_accessible_resource_dir: String = cx.argument::<JsString>(0)?.value();
let redirect_engine_path: String = cx.argument::<JsString>(1)?.value();
let scriptlets_path: String = cx.argument::<JsString>(2)?.value();

let mut resources = assemble_web_accessible_resources(&Path::new(&web_accessible_resource_dir), &Path::new(&redirect_engine_path));
resources.append(&mut assemble_scriptlet_resources(&Path::new(&scriptlets_path)));

let js_resources = neon_serde::to_value(&mut cx, &resources)?;

Ok(js_resources)
}

register_module!(mut m, {
// Export the `JsEngine` class
m.export_class::<JsEngine>("Engine")?;
m.export_function("lists", lists)?;
m.export_function("uBlockResources", ublock_resources)?;
Ok(())
});

0 comments on commit 3460597

Please sign in to comment.