| 1 |
// Copyright 2018-2019 Mozilla |
| 2 |
|
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
use std::{fmt::Arguments, time::Duration}; |
| 16 |
|
| 17 |
use log::{Level, LevelFilter, Log}; |
| 18 |
|
| 19 |
use crate::error::{ErrorKind, Result}; |
| 20 |
use crate::guid::Guid; |
| 21 |
use crate::merge::StructureCounts; |
| 22 |
use crate::tree::ProblemCounts; |
| 23 |
|
| 24 |
/// An abort signal is used to abort merging. Implementations of `AbortSignal` |
| 25 |
/// can store an aborted flag, usually as an atomic integer or Boolean, set |
| 26 |
/// the flag on abort, and have `AbortSignal::aborted` return the flag's value. |
| 27 |
/// |
| 28 |
/// Since merging is synchronous, it's not possible to interrupt a merge from |
| 29 |
/// the same thread that started it. In practice, this means a signal will |
| 30 |
/// implement `Send` and `Sync`, too, so that another thread can set the |
| 31 |
/// aborted flag. |
| 32 |
/// |
| 33 |
/// The name comes from the `AbortSignal` DOM API. |
| 34 |
pub trait AbortSignal { |
| 35 |
/// Indicates if the caller signaled to abort. |
| 36 |
fn aborted(&self) -> bool; |
| 37 |
|
| 38 |
/// Returns an error if the caller signaled to abort. This helper makes it |
| 39 |
/// easier to use the signal with the `?` operator. |
| 40 |
fn err_if_aborted(&self) -> Result<()> { |
| 41 |
if self.aborted() { |
| 42 |
Err(ErrorKind::Abort.into()) |
| 43 |
} else { |
| 44 |
Ok(()) |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/// A default signal that can't be aborted. |
| 50 |
pub struct DefaultAbortSignal; |
| 51 |
|
| 52 |
impl AbortSignal for DefaultAbortSignal { |
| 53 |
fn aborted(&self) -> bool { |
| 54 |
false |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
/// A merge telemetry event. |
| 59 |
pub enum TelemetryEvent { |
| 60 |
FetchLocalTree(TreeStats), |
| 61 |
FetchRemoteTree(TreeStats), |
| 62 |
Merge(Duration, StructureCounts), |
| 63 |
Apply(Duration), |
| 64 |
} |
| 65 |
|
| 66 |
/// Records the time taken to build a local or remote tree, number of items |
| 67 |
/// in the tree, and structure problem counts. |
| 68 |
pub struct TreeStats { |
| 69 |
pub time: Duration, |
| 70 |
pub items: usize, |
| 71 |
pub deletions: usize, |
| 72 |
pub problems: ProblemCounts, |
| 73 |
} |
| 74 |
|
| 75 |
/// A merge driver provides methods to customize merging behavior. |
| 76 |
pub trait Driver { |
| 77 |
/// Generates a new GUID for the given invalid GUID. This is used to fix up |
| 78 |
/// items with GUIDs that Places can't store (bug 1380606, bug 1313026). |
| 79 |
/// |
| 80 |
/// The default implementation returns an error, forbidding invalid GUIDs. |
| 81 |
/// |
| 82 |
/// Implementations of `Driver` can either use the `rand` and `base64` |
| 83 |
/// crates to generate a new, random GUID (9 bytes, Base64url-encoded |
| 84 |
/// without padding), or use an existing method like Desktop's |
| 85 |
/// `nsINavHistoryService::MakeGuid`. Dogear doesn't generate new GUIDs |
| 86 |
/// automatically to avoid depending on those crates. |
| 87 |
/// |
| 88 |
/// Implementations can also return `Ok(invalid_guid.clone())` to pass |
| 89 |
/// through all invalid GUIDs, as the tests do. |
| 90 |
fn generate_new_guid(&self, invalid_guid: &Guid) -> Result<Guid> { |
| 91 |
Err(ErrorKind::InvalidGuid(invalid_guid.clone()).into()) |
| 92 |
} |
| 93 |
|
| 94 |
/// Returns the maximum log level for merge messages. The default |
| 95 |
/// implementation returns the `log` crate's global maximum level. |
| 96 |
fn max_log_level(&self) -> LevelFilter { |
| 97 |
log::max_level() |
| 98 |
} |
| 99 |
|
| 100 |
/// Returns a logger for merge messages. |
| 101 |
/// |
| 102 |
/// The default implementation returns the `log` crate's global logger. |
| 103 |
/// |
| 104 |
/// Implementations can override this method to return a custom logger, |
| 105 |
/// where using the global logger won't work. For example, Firefox Desktop |
| 106 |
/// has an existing Sync logging setup outside of the `log` crate. |
| 107 |
fn logger(&self) -> &dyn Log { |
| 108 |
log::logger() |
| 109 |
} |
| 110 |
|
| 111 |
/// Records a merge telemetry event. |
| 112 |
/// |
| 113 |
/// The default implementation is a no-op that discards the event. |
| 114 |
/// Implementations can override this method to capture event and bookmark |
| 115 |
/// validation telemetry. |
| 116 |
fn record_telemetry_event(&self, _: TelemetryEvent) {} |
| 117 |
} |
| 118 |
|
| 119 |
/// A default implementation of the merge driver. |
| 120 |
pub struct DefaultDriver; |
| 121 |
|
| 122 |
impl Driver for DefaultDriver {} |
| 123 |
|
| 124 |
/// Logs a merge message. |
| 125 |
pub fn log<D: Driver>( |
| 126 |
driver: &D, |
| 127 |
level: Level, |
| 128 |
args: Arguments<'_>, |
| 129 |
module_path: &'static str, |
| 130 |
file: &'static str, |
| 131 |
line: u32, |
| 132 |
) { |
| 133 |
let meta = log::Metadata::builder() |
| 134 |
.level(level) |
| 135 |
.target(module_path) |
| 136 |
.build(); |
| 137 |
if driver.logger().enabled(&meta) { |
| 138 |
driver.logger().log( |
| 139 |
&log::Record::builder() |
| 140 |
.args(args) |
| 141 |
.metadata(meta) |
| 142 |
.module_path(Some(module_path)) |
| 143 |
.file(Some(file)) |
| 144 |
.line(Some(line)) |
| 145 |
.build(), |
| 146 |
); |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
#[macro_export] |
| 151 |
macro_rules! error { |
| 152 |
($driver:expr, $($args:tt)+) => { |
| 153 |
if log::Level::Error <= $crate::Driver::max_log_level($driver) { |
| 154 |
$crate::log( |
| 155 |
$driver, |
| 156 |
log::Level::Error, |
| 157 |
format_args!($($args)+), |
| 158 |
module_path!(), |
| 159 |
file!(), |
| 160 |
line!(), |
| 161 |
); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
#[macro_export] |
| 167 |
macro_rules! warn { |
| 168 |
($driver:expr, $($args:tt)+) => { |
| 169 |
if log::Level::Warn <= $crate::Driver::max_log_level($driver) { |
| 170 |
$crate::log( |
| 171 |
$driver, |
| 172 |
log::Level::Warn, |
| 173 |
format_args!($($args)+), |
| 174 |
module_path!(), |
| 175 |
file!(), |
| 176 |
line!(), |
| 177 |
); |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
#[macro_export] |
| 183 |
macro_rules! debug { |
| 184 |
($driver:expr, $($args:tt)+) => { |
| 185 |
if log::Level::Debug <= $crate::Driver::max_log_level($driver) { |
| 186 |
$crate::log( |
| 187 |
$driver, |
| 188 |
log::Level::Debug, |
| 189 |
format_args!($($args)+), |
| 190 |
module_path!(), |
| 191 |
file!(), |
| 192 |
line!(), |
| 193 |
); |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
#[macro_export] |
| 199 |
macro_rules! trace { |
| 200 |
($driver:expr, $($args:tt)+) => { |
| 201 |
if log::Level::Trace <= $crate::Driver::max_log_level($driver) { |
| 202 |
$crate::log( |
| 203 |
$driver, |
| 204 |
log::Level::Trace, |
| 205 |
format_args!($($args)+), |
| 206 |
module_path!(), |
| 207 |
file!(), |
| 208 |
line!(), |
| 209 |
); |
| 210 |
} |
| 211 |
} |
| 212 |
} |