1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use std::{
fmt::Write,
sync::atomic::{AtomicBool, Ordering},
time::Duration,
};
use dogear::{AbortSignal, Guid, ProblemCounts, StructureCounts, TelemetryEvent};
use log::{Level, LevelFilter, Log, Metadata, Record};
use moz_task::{Task, TaskRunnable, ThreadPtrHandle};
use nserror::nsresult;
use nsstring::{nsACString, nsCString, nsString};
use storage_variant::HashPropertyBag;
use xpcom::interfaces::{mozIServicesLogSink, mozISyncedBookmarksMirrorProgressListener};
extern "C" {
fn NS_GeneratePlacesGUID(guid: *mut nsACString) -> nsresult;
}
fn generate_guid() -> Result<nsCString, nsresult> {
let mut guid = nsCString::new();
let rv = unsafe { NS_GeneratePlacesGUID(&mut *guid) };
if rv.succeeded() {
Ok(guid)
} else {
Err(rv)
}
}
/// An abort controller is used to abort merges running on the storage thread
/// from the main thread. Its design is based on the DOM API of the same name.
pub struct AbortController {
aborted: AtomicBool,
}
impl AbortController {
/// Signals the store to stop merging as soon as it can.
pub fn abort(&self) {
self.aborted.store(true, Ordering::Release)
}
}
impl Default for AbortController {
fn default() -> AbortController {
AbortController {
aborted: AtomicBool::new(false),
}
}
}
impl AbortSignal for AbortController {
fn aborted(&self) -> bool {
self.aborted.load(Ordering::Acquire)
}
}
/// The merger driver, created and used on the storage thread.
pub struct Driver {
log: Logger,
progress: Option<ThreadPtrHandle<mozISyncedBookmarksMirrorProgressListener>>,
}
impl Driver {
#[inline]
pub fn new(
log: Logger,
progress: Option<ThreadPtrHandle<mozISyncedBookmarksMirrorProgressListener>>,
) -> Driver {
Driver { log, progress }
}
}
impl dogear::Driver for Driver {
fn generate_new_guid(&self, invalid_guid: &Guid) -> dogear::Result<Guid> {
generate_guid()
.map_err(|_| dogear::ErrorKind::InvalidGuid(invalid_guid.clone()).into())
.and_then(|s| Guid::from_utf8(s.as_ref()))
}
#[inline]
fn max_log_level(&self) -> LevelFilter {
self.log.max_level
}
#[inline]
fn logger(&self) -> &dyn Log {
&self.log
}
fn record_telemetry_event(&self, event: TelemetryEvent) {
if let Some(ref progress) = self.progress {
let task = RecordTelemetryEventTask {
progress: progress.clone(),
event,
};
let _ = TaskRunnable::new(
"bookmark_sync::Driver::record_telemetry_event",
Box::new(task),
)
.and_then(|r| TaskRunnable::dispatch(r, progress.owning_thread()));
}
}
}
pub struct Logger {
pub max_level: LevelFilter,
logger: Option<ThreadPtrHandle<mozIServicesLogSink>>,
}
impl Logger {
#[inline]
pub fn new(
max_level: LevelFilter,
logger: Option<ThreadPtrHandle<mozIServicesLogSink>>,
) -> Logger {
Logger { max_level, logger }
}
}
impl Log for Logger {
#[inline]
fn enabled(&self, meta: &Metadata) -> bool {
self.logger.is_some() && meta.level() <= self.max_level
}
fn log(&self, record: &Record) {
if !self.enabled(record.metadata()) {
return;
}
if let Some(logger) = &self.logger {
let mut message = nsString::new();
match write!(message, "{}", record.args()) {
Ok(_) => {
let task = LogTask {
logger: logger.clone(),
level: record.metadata().level(),
message,
};
let _ = TaskRunnable::new("bookmark_sync::Logger::log", Box::new(task))
.and_then(|r| TaskRunnable::dispatch(r, logger.owning_thread()));
}
Err(_) => {}
}
}
}
fn flush(&self) {}
}
/// Logs a message to the mirror logger. This task is created on the async
/// thread, and dispatched to the main thread.
struct LogTask {
logger: ThreadPtrHandle<mozIServicesLogSink>,
level: Level,
message: nsString,
}
impl Task for LogTask {
fn run(&self) {
let logger = self.logger.get().unwrap();
match self.level {
Level::Error => unsafe {
logger.Error(&*self.message);
},
Level::Warn => unsafe {
logger.Warn(&*self.message);
},
Level::Debug => unsafe {
logger.Debug(&*self.message);
},
Level::Trace => unsafe {
logger.Trace(&*self.message);
},
_ => {}
}
}
fn done(&self) -> Result<(), nsresult> {
Ok(())
}
}
/// Calls a progress listener callback for a merge telemetry event. This task is
/// created on the async thread, and dispatched to the main thread.
struct RecordTelemetryEventTask {
progress: ThreadPtrHandle<mozISyncedBookmarksMirrorProgressListener>,
event: TelemetryEvent,
}
impl Task for RecordTelemetryEventTask {
fn run(&self) {
let callback = self.progress.get().unwrap();
let _ = match &self.event {
TelemetryEvent::FetchLocalTree(stats) => unsafe {
callback.OnFetchLocalTree(
as_millis(stats.time),
stats.items as i64,
stats.deletions as i64,
problem_counts_to_bag(&stats.problems).bag().coerce(),
)
},
TelemetryEvent::FetchRemoteTree(stats) => unsafe {
callback.OnFetchRemoteTree(
as_millis(stats.time),
stats.items as i64,
stats.deletions as i64,
problem_counts_to_bag(&stats.problems).bag().coerce(),
)
},
TelemetryEvent::Merge(time, counts) => unsafe {
callback.OnMerge(
as_millis(*time),
structure_counts_to_bag(counts).bag().coerce(),
)
},
TelemetryEvent::Apply(time) => unsafe { callback.OnApply(as_millis(*time)) },
};
}
fn done(&self) -> std::result::Result<(), nsresult> {
Ok(())
}
}
fn as_millis(d: Duration) -> i64 {
d.as_secs() as i64 * 1000 + i64::from(d.subsec_millis())
}
fn problem_counts_to_bag(problems: &ProblemCounts) -> HashPropertyBag {
let mut bag = HashPropertyBag::new();
bag.set("orphans", problems.orphans as i64);
bag.set("misparentedRoots", problems.misparented_roots as i64);
bag.set(
"multipleParents",
problems.multiple_parents_by_children as i64,
);
bag.set("missingParents", problems.missing_parent_guids as i64);
bag.set("nonFolderParents", problems.non_folder_parent_guids as i64);
bag.set(
"parentChildDisagreements",
problems.parent_child_disagreements as i64,
);
bag.set("missingChildren", problems.missing_children as i64);
bag
}
fn structure_counts_to_bag(counts: &StructureCounts) -> HashPropertyBag {
let mut bag = HashPropertyBag::new();
bag.set("remoteRevives", counts.remote_revives as i64);
bag.set("localDeletes", counts.local_deletes as i64);
bag.set("localRevives", counts.local_revives as i64);
bag.set("remoteDeletes", counts.remote_deletes as i64);
bag.set("dupes", counts.dupes as i64);
bag.set("items", counts.merged_nodes as i64);
bag
}
|