@@ -6,32 +6,19 @@ use crate::bookmark_sync::BookmarksSyncEngine;
66use crate :: db:: db:: { PlacesDb , SharedPlacesDb } ;
77use crate :: error:: * ;
88use crate :: history_sync:: HistorySyncEngine ;
9- use crate :: storage:: {
10- self , bookmarks:: bookmark_sync, delete_meta, get_meta, history:: history_sync, put_meta,
11- } ;
129use crate :: util:: normalize_path;
1310use error_support:: handle_error;
1411use interrupt_support:: register_interrupt;
1512use lazy_static:: lazy_static;
1613use parking_lot:: Mutex ;
1714use rusqlite:: OpenFlags ;
18- use std:: cell:: Cell ;
1915use std:: collections:: HashMap ;
2016use std:: path:: { Path , PathBuf } ;
2117use std:: sync:: {
2218 atomic:: { AtomicUsize , Ordering } ,
2319 Arc , Weak ,
2420} ;
25- use sync15:: client:: { sync_multiple, MemoryCachedState , Sync15StorageClientInit , SyncResult } ;
26- use sync15:: engine:: { EngineSyncAssociation , SyncEngine , SyncEngineId } ;
27- use sync15:: { telemetry, KeyBundle } ;
28-
29- // Not clear if this should be here, but this is the "global sync state"
30- // which is persisted to disk and reused for all engines.
31- // Note that this is only ever round-tripped, and never changed by, or impacted
32- // by a store or collection, so it's safe to storage globally rather than
33- // per collection.
34- pub const GLOBAL_STATE_META_KEY : & str = "global_sync_state_v2" ;
21+ use sync15:: engine:: { SyncEngine , SyncEngineId } ;
3522
3623// Our "sync manager" will use whatever is stashed here.
3724lazy_static:: lazy_static! {
@@ -121,11 +108,6 @@ lazy_static! {
121108
122109static ID_COUNTER : AtomicUsize = AtomicUsize :: new ( 0 ) ;
123110
124- pub struct SyncState {
125- pub mem_cached_state : Cell < MemoryCachedState > ,
126- pub disk_cached_state : Cell < Option < String > > ,
127- }
128-
129111/// For uniffi we need to expose our `Arc` returning constructor as a global function :(
130112/// https://github.com/mozilla/uniffi-rs/pull/1063 would fix this, but got some pushback
131113/// meaning we are forced into this unfortunate workaround.
@@ -140,7 +122,6 @@ pub fn places_api_new(db_name: impl AsRef<Path>) -> ApiResult<Arc<PlacesApi>> {
140122pub struct PlacesApi {
141123 db_name : PathBuf ,
142124 write_connection : Mutex < Option < PlacesDb > > ,
143- sync_state : Mutex < Option < SyncState > > ,
144125 coop_tx_lock : Arc < Mutex < ( ) > > ,
145126 // Used for get_sync_connection()
146127 // - The inner mutex synchronizes sync operation (for example one of the [SyncEngine] methods).
@@ -188,7 +169,6 @@ impl PlacesApi {
188169 let new = PlacesApi {
189170 db_name : db_name. clone ( ) ,
190171 write_connection : Mutex :: new ( Some ( connection) ) ,
191- sync_state : Mutex :: new ( None ) ,
192172 sync_connection : Mutex :: new ( Weak :: new ( ) ) ,
193173 id,
194174 coop_tx_lock,
@@ -275,17 +255,6 @@ impl PlacesApi {
275255 Ok ( ( ) )
276256 }
277257
278- fn get_disk_persisted_state ( & self , conn : & PlacesDb ) -> Result < Option < String > > {
279- get_meta :: < String > ( conn, GLOBAL_STATE_META_KEY )
280- }
281-
282- fn set_disk_persisted_state ( & self , conn : & PlacesDb , state : & Option < String > ) -> Result < ( ) > {
283- match state {
284- Some ( ref s) => put_meta ( conn, GLOBAL_STATE_META_KEY , s) ,
285- None => delete_meta ( conn, GLOBAL_STATE_META_KEY ) ,
286- }
287- }
288-
289258 // This allows the embedding app to say "make this instance available to
290259 // the sync manager". The implementation is more like "offer to sync mgr"
291260 // (thereby avoiding us needing to link with the sync manager) but
@@ -294,177 +263,6 @@ impl PlacesApi {
294263 pub fn register_with_sync_manager ( self : Arc < Self > ) {
295264 * PLACES_API_FOR_SYNC_MANAGER . lock ( ) = Arc :: downgrade ( & self ) ;
296265 }
297-
298- // NOTE: These should be deprecated as soon as possible - that will be once
299- // all consumers have been updated to use the .sync() method below, and/or
300- // we have implemented the sync manager and migrated consumers to that.
301- pub fn sync_history (
302- & self ,
303- client_init : & Sync15StorageClientInit ,
304- key_bundle : & KeyBundle ,
305- ) -> Result < telemetry:: SyncTelemetryPing > {
306- self . do_sync_one (
307- "history" ,
308- move |conn, mem_cached_state, disk_cached_state| {
309- let engine = HistorySyncEngine :: new ( conn) ?;
310- Ok ( sync_multiple (
311- & [ & engine] ,
312- disk_cached_state,
313- mem_cached_state,
314- client_init,
315- key_bundle,
316- & interrupt_support:: ShutdownInterruptee ,
317- None ,
318- ) )
319- } ,
320- )
321- }
322-
323- pub fn sync_bookmarks (
324- & self ,
325- client_init : & Sync15StorageClientInit ,
326- key_bundle : & KeyBundle ,
327- ) -> Result < telemetry:: SyncTelemetryPing > {
328- self . do_sync_one (
329- "bookmarks" ,
330- move |conn, mem_cached_state, disk_cached_state| {
331- let engine = BookmarksSyncEngine :: new ( conn) ?;
332- Ok ( sync_multiple (
333- & [ & engine] ,
334- disk_cached_state,
335- mem_cached_state,
336- client_init,
337- key_bundle,
338- & interrupt_support:: ShutdownInterruptee ,
339- None ,
340- ) )
341- } ,
342- )
343- }
344-
345- pub fn do_sync_one < F > (
346- & self ,
347- name : & ' static str ,
348- syncer : F ,
349- ) -> Result < telemetry:: SyncTelemetryPing >
350- where
351- F : FnOnce (
352- Arc < SharedPlacesDb > ,
353- & mut MemoryCachedState ,
354- & mut Option < String > ,
355- ) -> Result < SyncResult > ,
356- {
357- let mut guard = self . sync_state . lock ( ) ;
358- let conn = self . get_sync_connection ( ) ?;
359- if guard. is_none ( ) {
360- * guard = Some ( SyncState {
361- mem_cached_state : Cell :: default ( ) ,
362- disk_cached_state : Cell :: new ( self . get_disk_persisted_state ( & conn. lock ( ) ) ?) ,
363- } ) ;
364- }
365-
366- let sync_state = guard. as_ref ( ) . unwrap ( ) ;
367-
368- let mut mem_cached_state = sync_state. mem_cached_state . take ( ) ;
369- let mut disk_cached_state = sync_state. disk_cached_state . take ( ) ;
370- let mut result = syncer ( conn. clone ( ) , & mut mem_cached_state, & mut disk_cached_state) ?;
371- // even on failure we set the persisted state - sync itself takes care
372- // to ensure this has been None'd out if necessary.
373- self . set_disk_persisted_state ( & conn. lock ( ) , & disk_cached_state) ?;
374- sync_state. mem_cached_state . replace ( mem_cached_state) ;
375- sync_state. disk_cached_state . replace ( disk_cached_state) ;
376-
377- // for b/w compat reasons, we do some dances with the result.
378- if let Err ( e) = result. result {
379- return Err ( e. into ( ) ) ;
380- }
381- match result. engine_results . remove ( name) {
382- None | Some ( Ok ( ( ) ) ) => Ok ( result. telemetry ) ,
383- Some ( Err ( e) ) => Err ( e. into ( ) ) ,
384- }
385- }
386-
387- // This is the new sync API until the sync manager lands. It's currently
388- // not wired up via the FFI - it's possible we'll do declined engines too
389- // before we do.
390- // Note we've made a policy decision about the return value - even though
391- // it is Result<SyncResult>, we will only return an Err() if there's a
392- // fatal error that prevents us starting a sync, such as failure to open
393- // the DB. Any errors that happen *after* sync must not escape - ie, once
394- // we have a SyncResult, we must return it.
395- pub fn sync (
396- & self ,
397- client_init : & Sync15StorageClientInit ,
398- key_bundle : & KeyBundle ,
399- ) -> Result < SyncResult > {
400- let mut guard = self . sync_state . lock ( ) ;
401- let conn = self . get_sync_connection ( ) ?;
402- if guard. is_none ( ) {
403- * guard = Some ( SyncState {
404- mem_cached_state : Cell :: default ( ) ,
405- disk_cached_state : Cell :: new ( self . get_disk_persisted_state ( & conn. lock ( ) ) ?) ,
406- } ) ;
407- }
408-
409- let sync_state = guard. as_ref ( ) . unwrap ( ) ;
410-
411- let bm_engine = BookmarksSyncEngine :: new ( conn. clone ( ) ) ?;
412- let history_engine = HistorySyncEngine :: new ( conn. clone ( ) ) ?;
413- let mut mem_cached_state = sync_state. mem_cached_state . take ( ) ;
414- let mut disk_cached_state = sync_state. disk_cached_state . take ( ) ;
415-
416- // NOTE: After here we must never return Err()!
417- let result = sync_multiple (
418- & [ & history_engine, & bm_engine] ,
419- & mut disk_cached_state,
420- & mut mem_cached_state,
421- client_init,
422- key_bundle,
423- & interrupt_support:: ShutdownInterruptee ,
424- None ,
425- ) ;
426- // even on failure we set the persisted state - sync itself takes care
427- // to ensure this has been None'd out if necessary.
428- if let Err ( e) = self . set_disk_persisted_state ( & conn. lock ( ) , & disk_cached_state) {
429- error_support:: report_error!(
430- "places-sync-persist-failure" ,
431- "Failed to persist the sync state: {:?}" ,
432- e
433- ) ;
434- }
435- sync_state. mem_cached_state . replace ( mem_cached_state) ;
436- sync_state. disk_cached_state . replace ( disk_cached_state) ;
437-
438- Ok ( result)
439- }
440-
441- pub fn wipe_bookmarks ( & self ) -> Result < ( ) > {
442- // Take the lock to prevent syncing while we're doing this.
443- let _guard = self . sync_state . lock ( ) ;
444- let conn = self . get_sync_connection ( ) ?;
445-
446- storage:: bookmarks:: delete_everything ( & conn. lock ( ) ) ?;
447- Ok ( ( ) )
448- }
449-
450- pub fn reset_bookmarks ( & self ) -> Result < ( ) > {
451- // Take the lock to prevent syncing while we're doing this.
452- let _guard = self . sync_state . lock ( ) ;
453- let conn = self . get_sync_connection ( ) ?;
454-
455- bookmark_sync:: reset ( & conn. lock ( ) , & EngineSyncAssociation :: Disconnected ) ?;
456- Ok ( ( ) )
457- }
458-
459- #[ handle_error( crate :: Error ) ]
460- pub fn reset_history ( & self ) -> ApiResult < ( ) > {
461- // Take the lock to prevent syncing while we're doing this.
462- let _guard = self . sync_state . lock ( ) ;
463- let conn = self . get_sync_connection ( ) ?;
464-
465- history_sync:: reset ( & conn. lock ( ) , & EngineSyncAssociation :: Disconnected ) ?;
466- Ok ( ( ) )
467- }
468266}
469267
470268#[ cfg( test) ]
0 commit comments