Skip to content

Commit 3a998d9

Browse files
Merge pull request #4710 from brave/sync_fix_obj_id_dup
Sync fix bookmarks object id duplication
1 parent b281af4 commit 3a998d9

7 files changed

+98
-33
lines changed

browser/profiles/brave_bookmark_model_loaded_observer.cc

+3
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@ void BraveBookmarkModelLoadedObserver::BookmarkModelLoaded(
3131
// it is handled in BraveProfileSyncServiceImpl::OnSyncReady
3232
if (brave_profile_service && !brave_profile_service->IsBraveSyncEnabled())
3333
BraveMigrateOtherNode(model);
34+
35+
BraveProfileSyncServiceImpl::AddNonClonedBookmarkKeys(model);
3436
#else
3537
BraveMigrateOtherNode(model);
3638
#endif
39+
3740
BookmarkModelLoadedObserver::BookmarkModelLoaded(model, ids_reassigned);
3841
}

components/brave_sync/brave_profile_sync_service_impl.cc

+11
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,17 @@ void BraveProfileSyncServiceImpl::OnSyncReadyBookmarksModelLoaded() {
516516
BraveMigrateOtherNode(model_);
517517
}
518518

519+
// static
520+
void BraveProfileSyncServiceImpl::AddNonClonedBookmarkKeys(
521+
BookmarkModel* model) {
522+
DCHECK(model);
523+
DCHECK(model->loaded());
524+
model->AddNonClonedKey("object_id");
525+
model->AddNonClonedKey("order");
526+
model->AddNonClonedKey("sync_timestamp");
527+
model->AddNonClonedKey("version");
528+
}
529+
519530
syncer::ModelTypeSet BraveProfileSyncServiceImpl::GetPreferredDataTypes()
520531
const {
521532
// Force DEVICE_INFO type to have nudge cycle each time to fetch

components/brave_sync/brave_profile_sync_service_impl.h

+2
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ class BraveProfileSyncServiceImpl
170170
BraveSyncClient* GetBraveSyncClient() override;
171171
#endif
172172

173+
static void AddNonClonedBookmarkKeys(BookmarkModel* model);
174+
173175
bool IsBraveSyncEnabled() const override;
174176

175177
syncer::ModelTypeSet GetPreferredDataTypes() const override;

components/brave_sync/brave_sync_service_unittest.cc

+32
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ using brave_sync::RecordsList;
9999
using brave_sync::SimpleBookmarkSyncRecord;
100100
using brave_sync::SimpleDeviceRecord;
101101
using brave_sync::jslib::SyncRecord;
102+
using brave_sync::tools::AsMutable;
102103
using testing::_;
103104
using testing::AtLeast;
104105

@@ -1310,3 +1311,34 @@ TEST_F(BraveSyncServiceTest, DeviceIdV2MigrationDupDeviceId) {
13101311
base::BindOnce(&OnGetRecordsStub);
13111312
sync_service()->OnPollSyncCycle(std::move(on_get_records), &we);
13121313
}
1314+
1315+
TEST_F(BraveSyncServiceTest, AddNonClonedBookmarkKeys) {
1316+
sync_service()->AddNonClonedBookmarkKeys(model());
1317+
const bookmarks::BookmarkNode* bookmark_a1 =
1318+
model()->AddURL(model()->other_node(), 0, base::ASCIIToUTF16("A1"),
1319+
GURL("https://a1.com"));
1320+
1321+
AsMutable(bookmark_a1)->SetMetaInfo("object_id", "object_id_value");
1322+
AsMutable(bookmark_a1)->SetMetaInfo("order", "order_value");
1323+
AsMutable(bookmark_a1)->SetMetaInfo("sync_timestamp", "sync_timestamp_value");
1324+
AsMutable(bookmark_a1)->SetMetaInfo("version", "version_value");
1325+
1326+
model()->Copy(bookmark_a1, model()->other_node(), 1);
1327+
1328+
const bookmarks::BookmarkNode* bookmark_copy =
1329+
model()->other_node()->children().at(1).get();
1330+
1331+
std::string meta_object_id;
1332+
EXPECT_FALSE(bookmark_copy->GetMetaInfo("object_id", &meta_object_id));
1333+
EXPECT_TRUE(meta_object_id.empty());
1334+
std::string meta_order;
1335+
EXPECT_FALSE(bookmark_copy->GetMetaInfo("order", &meta_order));
1336+
EXPECT_TRUE(meta_order.empty());
1337+
std::string meta_sync_timestamp;
1338+
EXPECT_FALSE(
1339+
bookmark_copy->GetMetaInfo("sync_timestamp", &meta_sync_timestamp));
1340+
EXPECT_TRUE(meta_sync_timestamp.empty());
1341+
std::string meta_version;
1342+
EXPECT_FALSE(bookmark_copy->GetMetaInfo("version", &meta_version));
1343+
EXPECT_TRUE(meta_version.empty());
1344+
}

components/brave_sync/syncer_helper.cc

+4-9
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@
1313
namespace brave_sync {
1414
namespace {
1515

16-
// Get mutable node to prevent BookmarkMetaInfoChanged from being triggered
17-
bookmarks::BookmarkNode* AsMutable(const bookmarks::BookmarkNode* node) {
18-
return const_cast<bookmarks::BookmarkNode*>(node);
19-
}
20-
2116
void SetOrder(const bookmarks::BookmarkNode* node,
2217
const std::string& parent_order) {
2318
DCHECK(!parent_order.empty());
@@ -41,7 +36,7 @@ void SetOrder(const bookmarks::BookmarkNode* node,
4136

4237
std::string order =
4338
brave_sync::GetOrder(prev_order, next_order, parent_order);
44-
AsMutable(node)->SetMetaInfo("order", order);
39+
tools::AsMutable(node)->SetMetaInfo("order", order);
4540
}
4641

4742
} // namespace
@@ -92,17 +87,17 @@ void AddBraveMetaInfo(const bookmarks::BookmarkNode* node) {
9287
if (object_id.empty()) {
9388
object_id = tools::GenerateObjectId();
9489
}
95-
AsMutable(node)->SetMetaInfo("object_id", object_id);
90+
tools::AsMutable(node)->SetMetaInfo("object_id", object_id);
9691

9792
std::string parent_object_id;
9893
node->parent()->GetMetaInfo("object_id", &parent_object_id);
99-
AsMutable(node)->SetMetaInfo("parent_object_id", parent_object_id);
94+
tools::AsMutable(node)->SetMetaInfo("parent_object_id", parent_object_id);
10095

10196
std::string sync_timestamp;
10297
node->GetMetaInfo("sync_timestamp", &sync_timestamp);
10398
if (sync_timestamp.empty()) {
10499
sync_timestamp = std::to_string(base::Time::Now().ToJsTime());
105-
AsMutable(node)->SetMetaInfo("sync_timestamp", sync_timestamp);
100+
tools::AsMutable(node)->SetMetaInfo("sync_timestamp", sync_timestamp);
106101
}
107102
DCHECK(!sync_timestamp.empty());
108103
}

components/brave_sync/tools.cc

+28-14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
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 file,
3-
* You can obtain one at http://mozilla.org/MPL/2.0/. */
1+
/* Copyright 2020 The Brave Authors. All rights reserved.
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5+
46
#include "brave/components/brave_sync/tools.h"
57

68
#include <string>
@@ -15,21 +17,28 @@ namespace brave_sync {
1517

1618
namespace tools {
1719

18-
std::string GenerateObjectId() {
19-
//16 random 8-bit unsigned numbers
20-
const size_t length = 16;
21-
uint8_t bytes[length];
22-
crypto::RandBytes(bytes, sizeof(bytes));
20+
const size_t kIdSize = 16;
21+
22+
namespace {
23+
std::string PrintObjectId(uint8_t* bytes) {
2324
std::stringstream ss;
24-
for (size_t i = 0; i < length; ++i) {
25-
const uint8_t &byte = bytes[i];
26-
ss << std::dec << (int)byte;
27-
if (i != length - 1) {
25+
for (size_t i = 0; i < kIdSize; ++i) {
26+
const uint8_t& byte = bytes[i];
27+
ss << std::dec << static_cast<int>(byte);
28+
if (i != kIdSize - 1) {
2829
ss << ", ";
2930
}
3031
}
3132
return ss.str();
3233
}
34+
} // namespace
35+
36+
std::string GenerateObjectId() {
37+
// 16 random 8-bit unsigned numbers
38+
uint8_t bytes[kIdSize];
39+
crypto::RandBytes(bytes, sizeof(bytes));
40+
return PrintObjectId(bytes);
41+
}
3342

3443
std::string GetPlatformName() {
3544
#if defined(OS_ANDROID)
@@ -50,6 +59,11 @@ bool IsTimeEmpty(const base::Time &time) {
5059
return time.is_null() || base::checked_cast<int64_t>(time.ToJsTime()) == 0;
5160
}
5261

53-
} // namespace tools
62+
// Get mutable node to prevent BookmarkMetaInfoChanged from being triggered
63+
bookmarks::BookmarkNode* AsMutable(const bookmarks::BookmarkNode* node) {
64+
return const_cast<bookmarks::BookmarkNode*>(node);
65+
}
66+
67+
} // namespace tools
5468

55-
} // namespace brave_sync
69+
} // namespace brave_sync

components/brave_sync/tools.h

+18-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
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 file,
3-
* You can obtain one at http://mozilla.org/MPL/2.0/. */
4-
#ifndef BRAVE_COMPONENTS_BRAVE_SYNC_BRAVE_SYNC_TOOLS_H_
5-
#define BRAVE_COMPONENTS_BRAVE_SYNC_BRAVE_SYNC_TOOLS_H_
1+
/* Copyright 2020 The Brave Authors. All rights reserved.
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5+
6+
#ifndef BRAVE_COMPONENTS_BRAVE_SYNC_TOOLS_H_
7+
#define BRAVE_COMPONENTS_BRAVE_SYNC_TOOLS_H_
68

79
#include <string>
810

911
namespace base {
10-
class Time;
11-
} // namespace base
12+
class Time;
13+
} // namespace base
14+
15+
namespace bookmarks {
16+
class BookmarkNode;
17+
}
1218

1319
namespace brave_sync {
1420

@@ -19,8 +25,10 @@ std::string GetPlatformName();
1925

2026
bool IsTimeEmpty(const base::Time &time);
2127

22-
} // namespace tools
28+
bookmarks::BookmarkNode* AsMutable(const bookmarks::BookmarkNode* node);
29+
30+
} // namespace tools
2331

24-
} // namespace brave_sync
32+
} // namespace brave_sync
2533

26-
#endif // BRAVE_COMPONENTS_BRAVE_SYNC_BRAVE_SYNC_TOOLS_H_
34+
#endif // BRAVE_COMPONENTS_BRAVE_SYNC_TOOLS_H_

0 commit comments

Comments
 (0)