diff options
| author | Árni Dagur <agudmundsson@fc-md.umd.edu> | 2020-03-28 23:15:47 -0400 |
|---|---|---|
| committer | Árni Dagur <agudmundsson@fc-md.umd.edu> | 2020-03-28 23:15:47 -0400 |
| commit | 129bfc10146973ce8a33fcd383c56c4f8071a492 (patch) | |
| tree | 453a3a4f4776e29859df94c4a97354940e3dc919 /src | |
first commit
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib.rs | 181 |
1 files changed, 181 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..86512f7 --- /dev/null +++ b/src/lib.rs | |||
| @@ -0,0 +1,181 @@ | |||
| 1 | //! Python bindings for Brave's adblocking library, which is written in Rust. | ||
| 2 | #![deny( | ||
| 3 | future_incompatible, | ||
| 4 | nonstandard_style, | ||
| 5 | rust_2018_idioms, | ||
| 6 | missing_copy_implementations, | ||
| 7 | trivial_casts, | ||
| 8 | trivial_numeric_casts, | ||
| 9 | unsafe_code, | ||
| 10 | unused_qualifications | ||
| 11 | )] | ||
| 12 | |||
| 13 | use pyo3::class::PyObjectProtocol; | ||
| 14 | use pyo3::prelude::*; | ||
| 15 | |||
| 16 | use adblock::blocker::BlockerResult as RustBlockerResult; | ||
| 17 | use adblock::engine::Engine as RustEngine; | ||
| 18 | |||
| 19 | /// Brave's adblocking library in Python! | ||
| 20 | #[pymodule] | ||
| 21 | fn adblock(_py: Python<'_>, m: &PyModule) -> PyResult<()> { | ||
| 22 | m.add("__version__", env!("CARGO_PKG_VERSION"))?; | ||
| 23 | m.add_class::<Engine>()?; | ||
| 24 | Ok(()) | ||
| 25 | } | ||
| 26 | |||
| 27 | #[pyclass] | ||
| 28 | pub struct BlockerResult { | ||
| 29 | #[pyo3(get)] | ||
| 30 | pub matched: bool, | ||
| 31 | #[pyo3(get)] | ||
| 32 | pub explicit_cancel: bool, | ||
| 33 | #[pyo3(get)] | ||
| 34 | pub important: bool, | ||
| 35 | #[pyo3(get)] | ||
| 36 | pub redirect: Option<String>, | ||
| 37 | #[pyo3(get)] | ||
| 38 | pub exception: Option<String>, | ||
| 39 | #[pyo3(get)] | ||
| 40 | pub filter: Option<String>, | ||
| 41 | #[pyo3(get)] | ||
| 42 | pub error: Option<String>, | ||
| 43 | } | ||
| 44 | |||
| 45 | impl Into<BlockerResult> for RustBlockerResult { | ||
| 46 | fn into(self) -> BlockerResult { | ||
| 47 | BlockerResult { | ||
| 48 | matched: self.matched, | ||
| 49 | explicit_cancel: self.explicit_cancel, | ||
| 50 | important: self.important, | ||
| 51 | redirect: self.redirect, | ||
| 52 | exception: self.exception, | ||
| 53 | filter: self.filter, | ||
| 54 | error: self.error, | ||
| 55 | } | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | #[pyproto] | ||
| 60 | impl PyObjectProtocol for BlockerResult { | ||
| 61 | fn __repr__(&self) -> PyResult<String> { | ||
| 62 | Ok(format!( | ||
| 63 | "BlockerResult({}, {}, {}, {:?}, {:?}, {:?}, {:?})", | ||
| 64 | self.matched, | ||
| 65 | self.explicit_cancel, | ||
| 66 | self.important, | ||
| 67 | self.redirect, | ||
| 68 | self.exception, | ||
| 69 | self.filter, | ||
| 70 | self.error | ||
| 71 | )) | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | #[pyclass] | ||
| 76 | pub struct Engine { | ||
| 77 | engine: RustEngine, | ||
| 78 | } | ||
| 79 | |||
| 80 | #[pymethods] | ||
| 81 | impl Engine { | ||
| 82 | #[new] | ||
| 83 | pub fn from_rules(network_filters: Vec<String>) -> Self { | ||
| 84 | let engine = RustEngine::from_rules(&network_filters); | ||
| 85 | Self { engine } | ||
| 86 | } | ||
| 87 | |||
| 88 | /// ## Request types | ||
| 89 | /// Examples of valid `request_type` parameters include: | ||
| 90 | /// * `beacon` | ||
| 91 | /// * `csp_report` | ||
| 92 | /// * `document` | ||
| 93 | /// * `font` | ||
| 94 | /// * `media` | ||
| 95 | /// * `object` | ||
| 96 | /// * `script` | ||
| 97 | /// * `stylesheet` | ||
| 98 | /// * and et cetera... | ||
| 99 | pub fn check_network_urls( | ||
| 100 | &self, | ||
| 101 | url: &str, | ||
| 102 | source_url: &str, | ||
| 103 | request_type: &str, | ||
| 104 | ) -> BlockerResult { | ||
| 105 | let blocker_result = self | ||
| 106 | .engine | ||
| 107 | .check_network_urls(url, source_url, request_type); | ||
| 108 | blocker_result.into() | ||
| 109 | } | ||
| 110 | |||
| 111 | pub fn check_network_urls_with_hostnames( | ||
| 112 | &self, | ||
| 113 | url: &str, | ||
| 114 | hostname: &str, | ||
| 115 | source_hostname: &str, | ||
| 116 | request_type: &str, | ||
| 117 | third_party_request: Option<bool>, | ||
| 118 | ) -> BlockerResult { | ||
| 119 | let blocker_result = self.engine.check_network_urls_with_hostnames( | ||
| 120 | url, | ||
| 121 | hostname, | ||
| 122 | source_hostname, | ||
| 123 | request_type, | ||
| 124 | third_party_request, | ||
| 125 | ); | ||
| 126 | blocker_result.into() | ||
| 127 | } | ||
| 128 | |||
| 129 | pub fn check_network_urls_with_hostnames_subset( | ||
| 130 | &self, | ||
| 131 | url: &str, | ||
| 132 | hostname: &str, | ||
| 133 | source_hostname: &str, | ||
| 134 | request_type: &str, | ||
| 135 | third_party_request: Option<bool>, | ||
| 136 | previously_matched_rule: bool, | ||
| 137 | force_check_exceptions: bool, | ||
| 138 | ) -> BlockerResult { | ||
| 139 | let blocker_result = self.engine.check_network_urls_with_hostnames_subset( | ||
| 140 | url, | ||
| 141 | hostname, | ||
| 142 | source_hostname, | ||
| 143 | request_type, | ||
| 144 | third_party_request, | ||
| 145 | previously_matched_rule, | ||
| 146 | force_check_exceptions, | ||
| 147 | ); | ||
| 148 | blocker_result.into() | ||
| 149 | } | ||
| 150 | |||
| 151 | pub fn filter_exists(&self, filter: &str) -> bool { | ||
| 152 | self.engine.filter_exists(filter) | ||
| 153 | } | ||
| 154 | |||
| 155 | pub fn tags_enable(&mut self, tags: Vec<&str>) { | ||
| 156 | self.engine.tags_enable(&tags); | ||
| 157 | } | ||
| 158 | |||
| 159 | pub fn tags_disable(&mut self, tags: Vec<&str>) { | ||
| 160 | self.engine.tags_disable(&tags); | ||
| 161 | } | ||
| 162 | |||
| 163 | pub fn tag_exists(&self, tag: &str) -> bool { | ||
| 164 | self.engine.tag_exists(tag) | ||
| 165 | } | ||
| 166 | |||
| 167 | // pub fn hidden_class_id_selectors( | ||
| 168 | // &self, | ||
| 169 | // classes: Vec<String>, | ||
| 170 | // ids: Vec<String>, | ||
| 171 | // exceptions: &PySet, | ||
| 172 | // ) -> PyResult<Vec<String>> { | ||
| 173 | // let mut exception_hashset: HashSet<String> = HashSet::new(); | ||
| 174 | // for exception in exceptions.iter() { | ||
| 175 | // let exception_pystr = PyString::from(exception); | ||
| 176 | // exception_hashset.insert(exception.to_string()); | ||
| 177 | // } | ||
| 178 | // Ok(self.engine | ||
| 179 | // .hidden_class_id_selectors(&classes, &ids, &exception_hashset)) | ||
| 180 | // } | ||
| 181 | } | ||
