Skip to content

Commit 21461d7

Browse files
committed
Add the first version of the context_id shared component.
1 parent 729a20d commit 21461d7

File tree

9 files changed

+799
-0
lines changed

9 files changed

+799
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
### Android
66
- Added `RustComponentsInitializer.kt` to `init_rust_components`.
77

8+
### Context ID
9+
- Added the first version of a component for managing and rotating context IDs
10+
sent to MARS / Merino.
11+
812
#### BREAKING CHANGE
913
- Removed `Megazord.kt` and moved the contents to the new `RustComponentsInitializer.kt`.
1014
-

Cargo.lock

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ resolver = "2"
44
# Note: Any additions here should be repeated in default-members below.
55
members = [
66
"components/autofill",
7+
"components/context_id",
78
"components/crashtest",
89
"components/example",
910
#"components/example/cli",
@@ -94,6 +95,7 @@ exclude = [
9495
# use the full member set.
9596
default-members = [
9697
"components/autofill",
98+
"components/context_id",
9799
"components/crashtest",
98100
"components/fxa-client",
99101
"components/init_rust_components",

components/context_id/Cargo.toml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[package]
2+
name = "context_id"
3+
version = "0.1.0"
4+
edition = "2021"
5+
license = "MPL-2.0"
6+
7+
[build-dependencies]
8+
uniffi = { version = "0.29.0", features=["build"]}
9+
10+
[dependencies]
11+
chrono = "0.4"
12+
error-support = { path = "../support/error" }
13+
lazy_static = { version = "1.4" }
14+
log = "0.4"
15+
parking_lot = "0.12"
16+
serde = "1"
17+
serde_json = "1"
18+
thiserror = "1.0"
19+
uniffi = { version = "0.29.0" }
20+
url = "2"
21+
uuid = { version = "1.3", features = ["v4"]}
22+
viaduct = { path = "../viaduct" }
23+
24+
[dev-dependencies]
25+
mockito = "0.31"
26+
viaduct-reqwest = { path = "../support/viaduct-reqwest" }

components/context_id/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Context ID
2+
3+
A context ID is a UUID that is used when making requests to MARS or to Merino to help reduce click fraud.
4+
5+
The **Context ID Rust component** creates a shared mechanism for managing context IDs, and to allow them to be rotated after they have exceeded some age.
6+
7+
It is currently under construction and not yet used.
8+
9+
## Tests
10+
11+
Tests are run with
12+
13+
```shell
14+
cargo test -p context_id
15+
```
16+
17+
## Bugs
18+
19+
We use Bugzilla to track bugs and feature work. You can use [this link](bugzilla.mozilla.org/enter_bug.cgi?product=Firefox&component=New Tab Page) to file bugs in the `Firefox :: New Tab Page` bug component.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
use crate::error::ApiResult;
6+
7+
#[uniffi::export(callback_interface)]
8+
pub trait ContextIdCallback: Sync + Send {
9+
fn persist(&self, context_id: String, creation_date: i64) -> ApiResult<()>;
10+
fn rotated(&self, old_context_id: String) -> ApiResult<()>;
11+
}
12+
13+
pub struct DefaultContextIdCallback;
14+
impl ContextIdCallback for DefaultContextIdCallback {
15+
fn persist(&self, _context_id: String, _creation_date: i64) -> ApiResult<()> {
16+
Ok(())
17+
}
18+
fn rotated(&self, _old_context_id: String) -> ApiResult<()> {
19+
Ok(())
20+
}
21+
}

components/context_id/src/error.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
use error_support::{ErrorHandling, GetErrorHandling};
6+
7+
pub type Result<T> = std::result::Result<T, Error>;
8+
pub type ApiResult<T> = std::result::Result<T, ApiError>;
9+
10+
#[derive(Debug, thiserror::Error, uniffi::Error)]
11+
pub enum ApiError {
12+
#[error("Something unexpected occurred.")]
13+
Other { reason: String },
14+
}
15+
16+
#[derive(Debug, thiserror::Error)]
17+
pub enum Error {
18+
#[error("Timestamp was invalid")]
19+
InvalidTimestamp { timestamp: i64 },
20+
21+
#[error("URL parse error: {0}")]
22+
UrlParseError(#[from] url::ParseError),
23+
24+
#[error("Viaduct error: {0}")]
25+
ViaductError(#[from] viaduct::Error),
26+
27+
#[error("UniFFI callback error: {0}")]
28+
UniFFICallbackError(#[from] uniffi::UnexpectedUniFFICallbackError),
29+
}
30+
31+
// Define how our internal errors are handled and converted to external errors.
32+
impl GetErrorHandling for Error {
33+
type ExternalError = ApiError;
34+
35+
fn get_error_handling(&self) -> ErrorHandling<Self::ExternalError> {
36+
ErrorHandling::convert(ApiError::Other {
37+
reason: self.to_string(),
38+
})
39+
}
40+
}

0 commit comments

Comments
 (0)