30
30
#include " components/bookmarks/browser/bookmark_model.h"
31
31
#include " components/signin/public/identity_manager/account_info.h"
32
32
#include " components/sync/engine_impl/syncer.h"
33
- #include " content/public/browser/browser_thread.h"
34
33
#include " net/base/network_interfaces.h"
35
34
#include " ui/base/models/tree_node_iterator.h"
36
35
@@ -78,22 +77,21 @@ std::string GetDeviceName() {
78
77
return hostname;
79
78
}
80
79
81
- RecordsListPtr CreateDeviceCreationRecordExtension (
82
- const std::string& deviceName,
83
- const std::string& objectId,
84
- const SyncRecord::Action& action,
85
- const std::string& deviceId) {
80
+ RecordsListPtr CreateDeviceRecord (const std::string& device_name,
81
+ const std::string& object_id,
82
+ const SyncRecord::Action& action,
83
+ const std::string& device_id) {
86
84
RecordsListPtr records = std::make_unique<RecordsList>();
87
85
88
86
SyncRecordPtr record = std::make_unique<SyncRecord>();
89
87
90
88
record->action = action;
91
- record->deviceId = deviceId ;
92
- record->objectId = objectId ;
89
+ record->deviceId = device_id ;
90
+ record->objectId = object_id ;
93
91
record->objectData = SyncObjectData_DEVICE; // "device"
94
92
95
93
std::unique_ptr<Device> device = std::make_unique<Device>();
96
- device->name = deviceName ;
94
+ device->name = device_name ;
97
95
record->SetDevice (std::move (device));
98
96
99
97
records->emplace_back (std::move (record));
@@ -167,11 +165,10 @@ BraveProfileSyncServiceImpl::BraveProfileSyncServiceImpl(Profile* profile,
167
165
InitParams init_params)
168
166
: BraveProfileSyncService(std::move(init_params)),
169
167
brave_sync_client_ (BraveSyncClient::Create(this , profile)) {
170
- brave_sync_words_ = std::string ();
171
168
brave_sync_prefs_ =
172
169
std::make_unique<prefs::Prefs>(sync_client_->GetPrefService ());
173
170
174
- // Moniter syncs prefs required in GetSettingsAndDevices
171
+ // Monitor syncs prefs required in GetSettingsAndDevices
175
172
brave_pref_change_registrar_.Init (sync_client_->GetPrefService ());
176
173
brave_pref_change_registrar_.Add (
177
174
prefs::kSyncEnabled ,
@@ -200,30 +197,18 @@ BraveProfileSyncServiceImpl::BraveProfileSyncServiceImpl(Profile* profile,
200
197
201
198
model_ = BookmarkModelFactory::GetForBrowserContext (profile);
202
199
203
- if (!brave_sync_prefs_->GetSeed ().empty () &&
204
- !brave_sync_prefs_->GetThisDeviceName ().empty ()) {
205
- brave_sync_configured_ = true ;
206
- }
207
200
network_connection_tracker_->AddNetworkConnectionObserver (this );
208
201
RecordSyncStateP3A ();
209
202
}
210
203
211
204
void BraveProfileSyncServiceImpl::OnNudgeSyncCycle (RecordsListPtr records) {
212
- if (!IsBraveSyncEnabled ())
205
+ if (!brave_sync_prefs_-> GetSyncEnabled ())
213
206
return ;
214
207
215
208
for (auto & record : *records) {
216
209
record->deviceId = brave_sync_prefs_->GetThisDeviceId ();
217
210
}
218
211
if (!records->empty ()) {
219
- if (((!brave_sync::tools::IsTimeEmpty (chain_created_time_) &&
220
- (base::Time::Now () - chain_created_time_).InSeconds () < 30u ) ||
221
- brave_sync_prefs_->GetSyncDevices ()->size () < 2 )) {
222
- // Store records for now
223
- pending_send_records_.push_back (std::move (records));
224
- return ;
225
- }
226
- SendAndPurgePendingRecords ();
227
212
SendSyncRecords (jslib_const::SyncRecordType_BOOKMARKS, std::move (records));
228
213
}
229
214
}
@@ -241,27 +226,31 @@ void BraveProfileSyncServiceImpl::OnSetupSyncHaveCode(
241
226
return ;
242
227
}
243
228
229
+ Uint8Array seed;
230
+ if (!crypto::PassphraseToBytes32 (sync_words, &seed)) {
231
+ OnSyncSetupError (" ERR_SYNC_WRONG_WORDS" );
232
+ return ;
233
+ }
234
+
244
235
if (brave_sync_initializing_) {
245
236
NotifyLogMessage (" currently initializing" );
246
237
return ;
247
238
}
248
239
249
- if (IsBraveSyncConfigured ()) {
240
+ if (!brave_sync_prefs_-> GetSeed (). empty ()) {
250
241
NotifyLogMessage (" already configured" );
251
242
return ;
252
243
}
253
244
254
- ForceCompleteReset ();
255
245
DCHECK (!brave_sync_prefs_->GetSyncEnabled ());
256
246
257
247
if (device_name.empty ())
258
248
brave_sync_prefs_->SetThisDeviceName (GetDeviceName ());
259
249
else
260
250
brave_sync_prefs_->SetThisDeviceName (device_name);
261
251
brave_sync_initializing_ = true ;
262
-
263
252
brave_sync_prefs_->SetSyncEnabled (true );
264
- brave_sync_words_ = sync_words ;
253
+ seed_ = seed ;
265
254
}
266
255
267
256
void BraveProfileSyncServiceImpl::OnSetupSyncNewToSync (
@@ -273,18 +262,13 @@ void BraveProfileSyncServiceImpl::OnSetupSyncNewToSync(
273
262
return ;
274
263
}
275
264
276
- if (IsBraveSyncConfigured ()) {
265
+ if (!brave_sync_prefs_-> GetSeed (). empty ()) {
277
266
NotifyLogMessage (" already configured" );
278
267
return ;
279
268
}
280
269
281
- ForceCompleteReset ();
282
270
DCHECK (!brave_sync_prefs_->GetSyncEnabled ());
283
271
284
- // If the previous attempt was connect to sync chain
285
- // and failed to receive save-init-data
286
- brave_sync_words_.clear ();
287
-
288
272
if (device_name.empty ())
289
273
brave_sync_prefs_->SetThisDeviceName (GetDeviceName ());
290
274
else
@@ -305,6 +289,7 @@ void BraveProfileSyncServiceImpl::OnDeleteDevice(const std::string& device_id) {
305
289
const std::string object_id = device->object_id_ ;
306
290
SendDeviceSyncRecord (SyncRecord::Action::A_DELETE, device_name, device_id,
307
291
object_id);
292
+ FetchDevices ();
308
293
}
309
294
}
310
295
@@ -321,7 +306,6 @@ void BraveProfileSyncServiceImpl::OnResetSync() {
321
306
// we can reset it by ResetSyncInternal()
322
307
const std::string device_id = brave_sync_prefs_->GetThisDeviceId ();
323
308
OnDeleteDevice (device_id);
324
- reseting_ = true ;
325
309
}
326
310
}
327
311
@@ -394,12 +378,8 @@ void BraveProfileSyncServiceImpl::OnGetInitData(
394
378
DCHECK_CURRENTLY_ON (content::BrowserThread::UI);
395
379
396
380
Uint8Array seed;
397
- if (!brave_sync_words_.empty ()) {
398
- VLOG (1 ) << " [Brave Sync] Init from sync words" ;
399
- if (!crypto::PassphraseToBytes32 (brave_sync_words_, &seed)) {
400
- OnSyncSetupError (" ERR_SYNC_WRONG_WORDS" );
401
- return ;
402
- }
381
+ if (!seed_.empty ()) {
382
+ seed = seed_;
403
383
} else if (!brave_sync_prefs_->GetSeed ().empty ()) {
404
384
seed = Uint8ArrayFromString (brave_sync_prefs_->GetSeed ());
405
385
VLOG (1 ) << " [Brave Sync] Init from prefs" ;
@@ -432,7 +412,7 @@ void BraveProfileSyncServiceImpl::OnGetInitData(
432
412
void BraveProfileSyncServiceImpl::OnSaveInitData (const Uint8Array& seed,
433
413
const Uint8Array& device_id) {
434
414
DCHECK_CURRENTLY_ON (content::BrowserThread::UI);
435
- DCHECK (!brave_sync_initialized_ );
415
+ DCHECK (!brave_sync_ready_ );
436
416
// If we are here and brave_sync_initializing_ is false, we have came
437
417
// not from OnSetupSyncNewToSync or OnSetupSyncHaveCode.
438
418
// One case is we put wrong code words and then restarted before cleared
@@ -444,27 +424,18 @@ void BraveProfileSyncServiceImpl::OnSaveInitData(const Uint8Array& seed,
444
424
445
425
std::string prev_seed_str = brave_sync_prefs_->GetPrevSeed ();
446
426
447
- brave_sync_words_ .clear ();
427
+ seed_ .clear ();
448
428
DCHECK (!seed_str.empty ());
449
429
450
430
if (prev_seed_str == seed_str) { // reconnecting to previous sync chain
451
431
brave_sync_prefs_->SetPrevSeed (std::string ());
452
432
} else if (!prev_seed_str.empty ()) { // connect/create to new sync chain
453
- // bookmark_change_processor_->Reset(true);
454
433
brave_sync_prefs_->SetPrevSeed (std::string ());
455
- } else {
456
- // This is not required, because when there is no previous seed, bookmarks
457
- // should not have a metadata. However, this is done by intention, to be
458
- // a remedy for cases when sync had been reset and prev_seed_str had been
459
- // cleared when it shouldn't (brave-browser#3188).
460
- // bookmark_change_processor_->Reset(true);
461
434
}
462
435
463
436
brave_sync_prefs_->SetSeed (seed_str);
464
437
brave_sync_prefs_->SetThisDeviceId (device_id_str);
465
438
466
- brave_sync_configured_ = true ;
467
-
468
439
brave_sync_initializing_ = false ;
469
440
}
470
441
@@ -480,8 +451,8 @@ void BraveProfileSyncServiceImpl::OnSyncReady() {
480
451
return ;
481
452
}
482
453
483
- DCHECK (false == brave_sync_initialized_ );
484
- brave_sync_initialized_ = true ;
454
+ DCHECK (false == brave_sync_ready_ );
455
+ brave_sync_ready_ = true ;
485
456
486
457
// For launching from legacy sync profile and also brand new profile
487
458
if (brave_sync_prefs_->GetMigratedBookmarksVersion () < 2 )
@@ -676,27 +647,16 @@ void BraveProfileSyncServiceImpl::NotifyHaveSyncWords(
676
647
void BraveProfileSyncServiceImpl::ResetSyncInternal () {
677
648
SignalWaitableEvent ();
678
649
brave_sync_prefs_->SetPrevSeed (brave_sync_prefs_->GetSeed ());
679
-
680
650
brave_sync_prefs_->Clear ();
681
651
682
- brave_sync_configured_ = false ;
683
- brave_sync_initialized_ = false ;
652
+ brave_sync_ready_ = false ;
684
653
685
- brave_sync_prefs_->SetSyncEnabled (false );
686
654
ProfileSyncService::GetUserSettings ()->SetSyncRequested (false );
687
655
688
656
// brave sync doesn't support pause sync so treating every new sync chain as
689
657
// first time setup
690
658
syncer::SyncPrefs sync_prefs (sync_client_->GetPrefService ());
691
659
sync_prefs.SetLastSyncedTime (base::Time ());
692
-
693
- reseting_ = false ;
694
- }
695
-
696
- void BraveProfileSyncServiceImpl::ForceCompleteReset () {
697
- if (reseting_) {
698
- ResetSyncInternal ();
699
- }
700
660
}
701
661
702
662
void BraveProfileSyncServiceImpl::SetPermanentNodesOrder (
@@ -881,7 +841,7 @@ void BraveProfileSyncServiceImpl::SendDeviceSyncRecord(
881
841
const std::string& device_id,
882
842
const std::string& object_id) {
883
843
DCHECK_CALLED_ON_VALID_SEQUENCE (sequence_checker_);
884
- RecordsListPtr records = CreateDeviceCreationRecordExtension (
844
+ RecordsListPtr records = CreateDeviceRecord (
885
845
device_name, object_id, static_cast <SyncRecord::Action>(action),
886
846
device_id);
887
847
SendSyncRecords (SyncRecordType_PREFERENCES, std::move (records));
@@ -891,10 +851,8 @@ void BraveProfileSyncServiceImpl::OnResolvedPreferences(
891
851
const RecordsList& records) {
892
852
const std::string this_device_id = brave_sync_prefs_->GetThisDeviceId ();
893
853
bool this_device_deleted = false ;
894
- bool contains_only_one_device = false ;
895
854
896
855
auto sync_devices = brave_sync_prefs_->GetSyncDevices ();
897
- auto old_devices_size = sync_devices->size ();
898
856
for (const auto & record : records) {
899
857
DCHECK (record->has_device () || record->has_sitesetting ());
900
858
if (record->has_device ()) {
@@ -907,30 +865,13 @@ void BraveProfileSyncServiceImpl::OnResolvedPreferences(
907
865
this_device_deleted ||
908
866
(record->deviceId == this_device_id &&
909
867
record->action == SyncRecord::Action::A_DELETE && actually_merged);
910
- contains_only_one_device =
911
- sync_devices->size () < 2 &&
912
- record->action == SyncRecord::Action::A_DELETE && actually_merged;
913
868
}
914
869
} // for each device
915
870
916
871
brave_sync_prefs_->SetSyncDevices (*sync_devices);
917
872
918
- if (old_devices_size < 2 && sync_devices->size () >= 2 ) {
919
- // Save chain creation time to send bookmarks 30 sec after
920
- chain_created_time_ = base::Time::Now ();
921
- }
922
- if (!tools::IsTimeEmpty (chain_created_time_) &&
923
- (base::Time::Now () - chain_created_time_).InSeconds () > 30u ) {
924
- SendAndPurgePendingRecords ();
925
- }
926
-
927
873
if (this_device_deleted) {
928
874
ResetSyncInternal ();
929
- } else if (contains_only_one_device) {
930
- // We see amount of devices had been decreased to 1 and it is not this
931
- // device had been deleted. So call OnResetSync which will send DELETE
932
- // record for this device
933
- OnResetSync ();
934
875
}
935
876
}
936
877
@@ -956,31 +897,19 @@ bool BraveProfileSyncServiceImpl::IsBraveSyncEnabled() const {
956
897
return brave_sync_prefs_->GetSyncEnabled ();
957
898
}
958
899
959
- bool BraveProfileSyncServiceImpl::IsBraveSyncInitialized () const {
960
- return brave_sync_initialized_;
961
- }
962
-
963
- bool BraveProfileSyncServiceImpl::IsBraveSyncConfigured () const {
964
- return brave_sync_configured_ &&
965
- // When there is 0 or 1 device, it means chain is not completely
966
- // created, so we should give a chance to make force reset in
967
- // |OnSetupSyncHaveCode| or in |OnSetupSyncNewToSync|
968
- (brave_sync_prefs_->GetSyncDevices ()->size () >= 2 );
969
- }
970
900
void BraveProfileSyncServiceImpl::FetchDevices () {
971
901
DCHECK (sync_client_);
972
902
brave_sync_prefs_->SetLastFetchTime (base::Time::Now ());
973
- // When the chain is not fully created or our device preferences queue is
974
- // not available, it may happened we will not find required device sync
975
- // record when we will be switched to use SQS instead of S3 in sync lib.
903
+
904
+ // When we create the device, we also create a set of SQS queues
905
+ // For other devices' S3 put lambda hook, some amount of time is required
906
+ // to see our queues in the result of listQueues
976
907
// Default Chromium fetch interval is 60 sec.
977
908
// So during 70 sec after the chain creation forcing use of S3
978
909
// by set start_at_time to 0.
979
-
980
910
base::Time start_at_time;
981
- if (brave_sync_prefs_->GetSyncDevices ()->size () <= 1 ||
982
- (!tools::IsTimeEmpty (chain_created_time_) &&
983
- (base::Time::Now () - chain_created_time_).InSeconds () < 70u )) {
911
+ if (!tools::IsTimeEmpty (this_device_created_time_) &&
912
+ (base::Time::Now () - this_device_created_time_).InSeconds () < 70u ) {
984
913
start_at_time = base::Time ();
985
914
} else {
986
915
start_at_time = brave_sync_prefs_->GetLatestDeviceRecordTime ();
@@ -991,15 +920,17 @@ void BraveProfileSyncServiceImpl::FetchDevices() {
991
920
}
992
921
void BraveProfileSyncServiceImpl::OnPollSyncCycle (GetRecordsCallback cb,
993
922
base::WaitableEvent* wevent) {
994
- if (!IsBraveSyncEnabled ())
923
+ if (!brave_sync_prefs_-> GetSyncEnabled ())
995
924
return ;
996
925
997
- if (IsTimeEmpty (brave_sync_prefs_->GetLastFetchTime ()))
926
+ if (IsTimeEmpty (brave_sync_prefs_->GetLastFetchTime ())) {
998
927
SendCreateDevice ();
928
+ this_device_created_time_ = base::Time::Now ();
929
+ }
999
930
1000
931
FetchDevices ();
1001
932
1002
- if (!brave_sync_initialized_ ) {
933
+ if (!brave_sync_ready_ ) {
1003
934
wevent->Signal ();
1004
935
return ;
1005
936
}
@@ -1027,14 +958,6 @@ BraveSyncService* BraveProfileSyncServiceImpl::GetSyncService() const {
1027
958
const_cast <BraveProfileSyncServiceImpl*>(this ));
1028
959
}
1029
960
1030
- void BraveProfileSyncServiceImpl::SendAndPurgePendingRecords () {
1031
- for (auto & records_to_send : pending_send_records_) {
1032
- SendSyncRecords (jslib_const::SyncRecordType_BOOKMARKS,
1033
- std::move (records_to_send));
1034
- }
1035
- pending_send_records_.clear ();
1036
- }
1037
-
1038
961
void BraveProfileSyncServiceImpl::SendSyncRecords (
1039
962
const std::string& category_name,
1040
963
RecordsListPtr records) {
0 commit comments