Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync clear orders map #1174

Merged
merged 2 commits into from
Dec 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions browser/extensions/api/brave_sync_event_router.cc
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,13 @@ void BraveSyncEventRouter::LoadClient() {
event_router_->BroadcastEvent(std::move(event));
}

void BraveSyncEventRouter::ClearOrderMap() {
auto args = std::make_unique<base::ListValue>();
std::unique_ptr<Event> event(
new Event(extensions::events::FOR_TEST,
extensions::api::brave_sync::OnClearOrderMap::kEventName,
std::move(args)));
event_router_->BroadcastEvent(std::move(event));
}

} // namespace extensions
2 changes: 2 additions & 0 deletions browser/extensions/api/brave_sync_event_router.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class BraveSyncEventRouter {

void NeedSyncWords(const std::string& seed);

void ClearOrderMap();

void LoadClient();

private:
Expand Down
5 changes: 5 additions & 0 deletions common/extensions/api/brave_sync.json
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,11 @@
"type": "function",
"description": "Browser informs extension page to load js sync library.",
"parameters": []
},
{
"name": "onClearOrderMap",
"type": "function",
"description": "Browser informs extension page all portions of bookmarks are sent, so it is time to clear the order map"
}
],
"functions": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,7 @@ void BookmarkChangeProcessor::SendUnsynced(
jslib_const::SyncRecordType_BOOKMARKS, records);
records.clear();
}
sync_client_->ClearOrderMap();
}

void BookmarkChangeProcessor::InitialSync() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ TEST_F(BraveBookmarkChangeProcessorTest, ResetClearMeta) {
AddSimpleHierarchy(&folder1, &node_a, &node_b, &node_c);

EXPECT_CALL(*sync_client(), SendSyncRecords("BOOKMARKS", _)).Times(1);
EXPECT_CALL(*sync_client(), ClearOrderMap()).Times(1);
change_processor()->SendUnsynced(base::TimeDelta::FromMinutes(10));

EXPECT_TRUE(HasAnySyncMetaInfo(folder1));
Expand Down Expand Up @@ -291,6 +292,7 @@ TEST_F(BraveBookmarkChangeProcessorTest, ResetPreserveMeta) {
AddSimpleHierarchy(&folder1, &node_a, &node_b, &node_c);

EXPECT_CALL(*sync_client(), SendSyncRecords("BOOKMARKS", _)).Times(1);
EXPECT_CALL(*sync_client(), ClearOrderMap()).Times(1);
change_processor()->SendUnsynced(base::TimeDelta::FromMinutes(10));

EXPECT_TRUE(HasAnySyncMetaInfo(folder1));
Expand Down Expand Up @@ -333,6 +335,7 @@ void BraveBookmarkChangeProcessorTest::BookmarkAddedImpl() {
using brave_sync::jslib::SyncRecord;
EXPECT_CALL(*sync_client(), SendSyncRecords("BOOKMARKS",
ContainsRecord(SyncRecord::Action::A_CREATE, "https://a.com/"))).Times(1);
EXPECT_CALL(*sync_client(), ClearOrderMap()).Times(1);
change_processor()->SendUnsynced(base::TimeDelta::FromMinutes(10));
}

Expand All @@ -353,6 +356,7 @@ TEST_F(BraveBookmarkChangeProcessorTest, BookmarkDeleted) {
EXPECT_CALL(*sync_client(), SendSyncRecords("BOOKMARKS",
ContainsRecord(SyncRecord::Action::A_DELETE, "https://a.com/"))).Times(1);
model()->Remove(nodes.at(0));
EXPECT_CALL(*sync_client(), ClearOrderMap()).Times(1);
change_processor()->SendUnsynced(base::TimeDelta::FromMinutes(10));
EXPECT_FALSE(GetDeletedNodeRoot()->IsVisible());
}
Expand All @@ -370,6 +374,7 @@ TEST_F(BraveBookmarkChangeProcessorTest, BookmarkModified) {
EXPECT_CALL(*sync_client(), SendSyncRecords("BOOKMARKS",
ContainsRecord(SyncRecord::Action::A_UPDATE, "https://a-m.com/"))).Times(1);
model()->SetURL(nodes.at(0), GURL("https://a-m.com/"));
EXPECT_CALL(*sync_client(), ClearOrderMap()).Times(1);
change_processor()->SendUnsynced(base::TimeDelta::FromMinutes(10));
}

Expand All @@ -390,6 +395,7 @@ TEST_F(BraveBookmarkChangeProcessorTest, BookmarkMovedInFolder) {
EXPECT_EQ(intex_c, 2);

EXPECT_CALL(*sync_client(), SendSyncRecords("BOOKMARKS", _)).Times(1);
EXPECT_CALL(*sync_client(), ClearOrderMap()).Times(1);
change_processor()->SendUnsynced(base::TimeDelta::FromMinutes(10));

EXPECT_TRUE(HasAnySyncMetaInfo(folder1));
Expand All @@ -414,6 +420,7 @@ TEST_F(BraveBookmarkChangeProcessorTest, BookmarkMovedInFolder) {
// BookmarkNodeMoved does not reset "last_send_time" so SendUnsynced
// ignores order change untill unsynced_send_interval passes,
// so here below unsynced_send_interval is 0
EXPECT_CALL(*sync_client(), ClearOrderMap()).Times(1);
change_processor()->SendUnsynced(base::TimeDelta::FromMinutes(0));
}

Expand Down Expand Up @@ -444,6 +451,7 @@ TEST_F(BraveBookmarkChangeProcessorTest, DISABLED_MoveNodesBetweenDirs) {
// Send all created objects
EXPECT_CALL(*sync_client(), SendSyncRecords("BOOKMARKS",
RecordsNumber(4))).Times(1);
EXPECT_CALL(*sync_client(), ClearOrderMap()).Times(1);
change_processor()->SendUnsynced(base::TimeDelta::FromMinutes(10));
}

Expand All @@ -466,10 +474,12 @@ TEST_F(BraveBookmarkChangeProcessorTest, DeleteFolderWithNodes) {

EXPECT_CALL(*sync_client(), SendSyncRecords("BOOKMARKS",
RecordsNumber(4))).Times(1);
EXPECT_CALL(*sync_client(), ClearOrderMap()).Times(1);
change_processor()->SendUnsynced(base::TimeDelta::FromMinutes(10));

model()->Remove(folder1);
EXPECT_CALL(*sync_client(), SendSyncRecords("BOOKMARKS",_)).Times(1);
EXPECT_CALL(*sync_client(), ClearOrderMap()).Times(1);
change_processor()->SendUnsynced(base::TimeDelta::FromMinutes(10));
}

Expand Down
2 changes: 2 additions & 0 deletions components/brave_sync/client/brave_sync_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ class BraveSyncClient {
virtual void OnExtensionInitialized() = 0;

virtual void OnSyncEnabledChanged() = 0;

virtual void ClearOrderMap() = 0;
};

} // namespace brave_sync
Expand Down
5 changes: 5 additions & 0 deletions components/brave_sync/client/brave_sync_client_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,9 @@ void BraveSyncClientImpl::OnExtensionSystemReady() {
}
};

void BraveSyncClientImpl::ClearOrderMap() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
brave_sync_event_router_->ClearOrderMap();
}

} // namespace brave_sync
2 changes: 2 additions & 0 deletions components/brave_sync/client/brave_sync_client_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ class BraveSyncClientImpl : public BraveSyncClient,
void LoadOrUnloadExtension(bool load);
void OnExtensionSystemReady();

void ClearOrderMap() override;

SyncMessageHandler* handler_; // not owned
Profile* profile_; // not owned
std::unique_ptr<brave_sync::prefs::Prefs> sync_prefs_;
Expand Down
6 changes: 5 additions & 1 deletion components/brave_sync/extension/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ chrome.braveSync.onSendSyncRecords.addListener(function(category_name, records)
if (category_name == 'BOOKMARKS') {
fixupSyncRecordsArrayExtensionToBrowser(records);
chrome.braveSync.resolvedSyncRecords(category_name, records);
orderMap = {};
}
});

Expand All @@ -82,6 +81,10 @@ chrome.braveSync.onLoadClient.addListener(function() {
LoadJsLibScript();
});

chrome.braveSync.onClearOrderMap.addListener(function() {
orderMap = {};
});

chrome.braveSync.extensionInitialized();
console.log("chrome.braveSync.extensionInitialized");

Expand All @@ -96,6 +99,7 @@ function getOrder(record) {
orderMap[record.objectId] = order;
getBookmarkOrderCallback = null;
}

var prevOrder = record.bookmark.prevOrder;
var parentOrder = record.bookmark.parentOrder;
if (!prevOrder && orderMap[record.bookmark.prevObjectId])
Expand Down
1 change: 1 addition & 0 deletions components/brave_sync/test_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class MockBraveSyncClient : public BraveSyncClient {
MOCK_METHOD1(NeedBytesFromSyncWords, void(const std::string& words));
MOCK_METHOD0(OnExtensionInitialized, void());
MOCK_METHOD0(OnSyncEnabledChanged, void());
MOCK_METHOD0(ClearOrderMap, void());
};

std::unique_ptr<Profile> CreateBraveSyncProfile(const base::FilePath& path);
Expand Down