Commit 8548fb8c authored by Jan Krcal's avatar Jan Krcal Committed by Commit Bot

[ModelTypeWorker] Clean up bookmarks-only code before refactoring

This CL prepares for a later refactoring by making semantically
non-equivalent changes first. This CL:
 - guards bookmark specific code by model_type == BOOKMARKS;
 - moving one line around to have all bookmark specific changes after
   the generic ones.

The follow-up CL will extract data type specific code into helper
functions to separate them from the main flow. This prepares for adding
further WALLET_DATA specific code into the worker.

Bug: 881289
Change-Id: Ia197e92294b56ecbe49b8f28f4581b4a8bb581a3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1818482
Auto-Submit: Jan Krcal <jkrcal@chromium.org>
Reviewed-by: default avatarMikel Astiz <mastiz@chromium.org>
Commit-Queue: Jan Krcal <jkrcal@chromium.org>
Cr-Commit-Position: refs/heads/master@{#699666}
parent 6d272367
...@@ -263,62 +263,63 @@ ModelTypeWorker::DecryptionStatus ModelTypeWorker::PopulateUpdateResponseData( ...@@ -263,62 +263,63 @@ ModelTypeWorker::DecryptionStatus ModelTypeWorker::PopulateUpdateResponseData(
data->name = update_entity.name(); data->name = update_entity.name();
data->is_folder = update_entity.folder(); data->is_folder = update_entity.folder();
data->parent_id = update_entity.parent_id_string(); data->parent_id = update_entity.parent_id_string();
data->server_defined_unique_tag = update_entity.server_defined_unique_tag();
// Handle deprecated positioning fields. Relevant only for bookmarks. // Populate |originator_cache_guid| and |originator_client_item_id|. This is
bool has_position_scheme = false; // currently relevant only for bookmarks.
SyncPositioningScheme sync_positioning_scheme; data->originator_cache_guid = update_entity.originator_cache_guid();
if (update_entity.has_unique_position()) { data->originator_client_item_id = update_entity.originator_client_item_id();
data->unique_position = update_entity.unique_position();
has_position_scheme = true;
sync_positioning_scheme = SyncPositioningScheme::UNIQUE_POSITION;
} else if (update_entity.has_position_in_parent() ||
update_entity.has_insert_after_item_id()) {
bool missing_originator_fields = false;
if (!update_entity.has_originator_cache_guid() ||
!update_entity.has_originator_client_item_id()) {
DLOG(ERROR) << "Update is missing requirements for bookmark position.";
missing_originator_fields = true;
}
std::string suffix = // Handle deprecated positioning fields. Relevant only for bookmarks.
missing_originator_fields if (model_type == syncer::BOOKMARKS) {
? UniquePosition::RandomSuffix() bool has_position_scheme = false;
: GenerateSyncableHash( SyncPositioningScheme sync_positioning_scheme;
syncer::GetModelType(update_entity), if (update_entity.has_unique_position()) {
/*client_tag=*/update_entity.originator_cache_guid() + data->unique_position = update_entity.unique_position();
update_entity.originator_client_item_id());
if (update_entity.has_position_in_parent()) {
data->unique_position =
UniquePosition::FromInt64(update_entity.position_in_parent(), suffix)
.ToProto();
has_position_scheme = true; has_position_scheme = true;
sync_positioning_scheme = SyncPositioningScheme::POSITION_IN_PARENT; sync_positioning_scheme = SyncPositioningScheme::UNIQUE_POSITION;
} else { } else if (update_entity.has_position_in_parent() ||
// If update_entity has insert_after_item_id, use 0 index. update_entity.has_insert_after_item_id()) {
DCHECK(update_entity.has_insert_after_item_id()); bool missing_originator_fields = false;
data->unique_position = UniquePosition::FromInt64(0, suffix).ToProto(); if (!update_entity.has_originator_cache_guid() ||
!update_entity.has_originator_client_item_id()) {
DLOG(ERROR) << "Update is missing requirements for bookmark position.";
missing_originator_fields = true;
}
std::string suffix =
missing_originator_fields
? UniquePosition::RandomSuffix()
: GenerateSyncableHash(
syncer::GetModelType(update_entity),
/*client_tag=*/update_entity.originator_cache_guid() +
update_entity.originator_client_item_id());
if (update_entity.has_position_in_parent()) {
data->unique_position = UniquePosition::FromInt64(
update_entity.position_in_parent(), suffix)
.ToProto();
has_position_scheme = true;
sync_positioning_scheme = SyncPositioningScheme::POSITION_IN_PARENT;
} else {
// If update_entity has insert_after_item_id, use 0 index.
DCHECK(update_entity.has_insert_after_item_id());
data->unique_position = UniquePosition::FromInt64(0, suffix).ToProto();
has_position_scheme = true;
sync_positioning_scheme = SyncPositioningScheme::INSERT_AFTER_ITEM_ID;
}
} else if (SyncerProtoUtil::ShouldMaintainPosition(update_entity) &&
!update_entity.deleted()) {
DLOG(ERROR) << "Missing required position information in update.";
has_position_scheme = true; has_position_scheme = true;
sync_positioning_scheme = SyncPositioningScheme::INSERT_AFTER_ITEM_ID; sync_positioning_scheme = SyncPositioningScheme::MISSING;
}
if (has_position_scheme) {
UMA_HISTOGRAM_ENUMERATION("Sync.Entities.PositioningScheme",
sync_positioning_scheme);
} }
} else if (SyncerProtoUtil::ShouldMaintainPosition(update_entity) &&
!update_entity.deleted()) {
DLOG(ERROR) << "Missing required position information in update.";
has_position_scheme = true;
sync_positioning_scheme = SyncPositioningScheme::MISSING;
}
if (has_position_scheme) {
UMA_HISTOGRAM_ENUMERATION("Sync.Entities.PositioningScheme",
sync_positioning_scheme);
} }
// Populate |originator_cache_guid| and |originator_client_item_id|. This is
// relevant only for bookmarks.
data->originator_cache_guid = update_entity.originator_cache_guid();
data->originator_client_item_id = update_entity.originator_client_item_id();
data->server_defined_unique_tag = update_entity.server_defined_unique_tag();
// Deleted entities must use the default instance of EntitySpecifics in // Deleted entities must use the default instance of EntitySpecifics in
// order for EntityData to correctly reflect that they are deleted. // order for EntityData to correctly reflect that they are deleted.
const sync_pb::EntitySpecifics& specifics = const sync_pb::EntitySpecifics& specifics =
......
...@@ -1333,8 +1333,6 @@ TEST_F(ModelTypeWorkerTest, PopulateUpdateResponseData) { ...@@ -1333,8 +1333,6 @@ TEST_F(ModelTypeWorkerTest, PopulateUpdateResponseData) {
entity.set_id_string("SomeID"); entity.set_id_string("SomeID");
entity.set_parent_id_string("ParentID"); entity.set_parent_id_string("ParentID");
entity.set_folder(false); entity.set_folder(false);
*entity.mutable_unique_position() =
UniquePosition::InitialPosition(UniquePosition::RandomSuffix()).ToProto();
entity.set_version(1); entity.set_version(1);
entity.set_client_defined_unique_tag("CLIENT_TAG"); entity.set_client_defined_unique_tag("CLIENT_TAG");
entity.set_server_defined_unique_tag("SERVER_TAG"); entity.set_server_defined_unique_tag("SERVER_TAG");
...@@ -1352,19 +1350,11 @@ TEST_F(ModelTypeWorkerTest, PopulateUpdateResponseData) { ...@@ -1352,19 +1350,11 @@ TEST_F(ModelTypeWorkerTest, PopulateUpdateResponseData) {
EXPECT_FALSE(data.id.empty()); EXPECT_FALSE(data.id.empty());
EXPECT_FALSE(data.parent_id.empty()); EXPECT_FALSE(data.parent_id.empty());
EXPECT_FALSE(data.is_folder); EXPECT_FALSE(data.is_folder);
EXPECT_TRUE(
syncer::UniquePosition::FromProto(data.unique_position).IsValid());
EXPECT_EQ("CLIENT_TAG", data.client_tag_hash); EXPECT_EQ("CLIENT_TAG", data.client_tag_hash);
EXPECT_EQ("SERVER_TAG", data.server_defined_unique_tag); EXPECT_EQ("SERVER_TAG", data.server_defined_unique_tag);
EXPECT_FALSE(data.is_deleted()); EXPECT_FALSE(data.is_deleted());
EXPECT_EQ(kTag1, data.specifics.preference().name()); EXPECT_EQ(kTag1, data.specifics.preference().name());
EXPECT_EQ(kValue1, data.specifics.preference().value()); EXPECT_EQ(kValue1, data.specifics.preference().value());
histogram_tester.ExpectUniqueSample(
"Sync.Entities.PositioningScheme",
/*sample=*/
ExpectedSyncPositioningScheme::UNIQUE_POSITION,
/*count=*/1);
} }
TEST_F(ModelTypeWorkerTest, PopulateUpdateResponseDataForBookmarkTombstone) { TEST_F(ModelTypeWorkerTest, PopulateUpdateResponseDataForBookmarkTombstone) {
...@@ -1395,7 +1385,8 @@ TEST_F(ModelTypeWorkerTest, PopulateUpdateResponseDataForBookmarkTombstone) { ...@@ -1395,7 +1385,8 @@ TEST_F(ModelTypeWorkerTest, PopulateUpdateResponseDataForBookmarkTombstone) {
EXPECT_TRUE(data.is_deleted()); EXPECT_TRUE(data.is_deleted());
} }
TEST_F(ModelTypeWorkerTest, PopulateUpdateResponseDataWithPositionInParent) { TEST_F(ModelTypeWorkerTest,
PopulateUpdateResponseDataForBookmarkWithPositionInParent) {
InitializeCommitOnly(); InitializeCommitOnly();
sync_pb::SyncEntity entity; sync_pb::SyncEntity entity;
...@@ -1410,7 +1401,7 @@ TEST_F(ModelTypeWorkerTest, PopulateUpdateResponseDataWithPositionInParent) { ...@@ -1410,7 +1401,7 @@ TEST_F(ModelTypeWorkerTest, PopulateUpdateResponseDataWithPositionInParent) {
EXPECT_EQ(ModelTypeWorker::SUCCESS, EXPECT_EQ(ModelTypeWorker::SUCCESS,
ModelTypeWorker::PopulateUpdateResponseData( ModelTypeWorker::PopulateUpdateResponseData(
&cryptographer, PREFERENCES, entity, &response_data)); &cryptographer, BOOKMARKS, entity, &response_data));
const EntityData& data = *response_data.entity; const EntityData& data = *response_data.entity;
EXPECT_TRUE( EXPECT_TRUE(
syncer::UniquePosition::FromProto(data.unique_position).IsValid()); syncer::UniquePosition::FromProto(data.unique_position).IsValid());
...@@ -1422,7 +1413,8 @@ TEST_F(ModelTypeWorkerTest, PopulateUpdateResponseDataWithPositionInParent) { ...@@ -1422,7 +1413,8 @@ TEST_F(ModelTypeWorkerTest, PopulateUpdateResponseDataWithPositionInParent) {
/*count=*/1); /*count=*/1);
} }
TEST_F(ModelTypeWorkerTest, PopulateUpdateResponseDataWithInsertAfterItemId) { TEST_F(ModelTypeWorkerTest,
PopulateUpdateResponseDataForBookmarkWithInsertAfterItemId) {
InitializeCommitOnly(); InitializeCommitOnly();
sync_pb::SyncEntity entity; sync_pb::SyncEntity entity;
...@@ -1437,7 +1429,7 @@ TEST_F(ModelTypeWorkerTest, PopulateUpdateResponseDataWithInsertAfterItemId) { ...@@ -1437,7 +1429,7 @@ TEST_F(ModelTypeWorkerTest, PopulateUpdateResponseDataWithInsertAfterItemId) {
EXPECT_EQ(ModelTypeWorker::SUCCESS, EXPECT_EQ(ModelTypeWorker::SUCCESS,
ModelTypeWorker::PopulateUpdateResponseData( ModelTypeWorker::PopulateUpdateResponseData(
&cryptographer, PREFERENCES, entity, &response_data)); &cryptographer, BOOKMARKS, entity, &response_data));
const EntityData& data = *response_data.entity; const EntityData& data = *response_data.entity;
EXPECT_TRUE( EXPECT_TRUE(
syncer::UniquePosition::FromProto(data.unique_position).IsValid()); syncer::UniquePosition::FromProto(data.unique_position).IsValid());
...@@ -1449,7 +1441,7 @@ TEST_F(ModelTypeWorkerTest, PopulateUpdateResponseDataWithInsertAfterItemId) { ...@@ -1449,7 +1441,7 @@ TEST_F(ModelTypeWorkerTest, PopulateUpdateResponseDataWithInsertAfterItemId) {
} }
TEST_F(ModelTypeWorkerTest, TEST_F(ModelTypeWorkerTest,
PopulateUpdateResponseDataWithBookmarkMissingPosition) { PopulateUpdateResponseDataForBookmarkWithMissingPosition) {
InitializeCommitOnly(); InitializeCommitOnly();
sync_pb::SyncEntity entity; sync_pb::SyncEntity entity;
...@@ -1466,7 +1458,7 @@ TEST_F(ModelTypeWorkerTest, ...@@ -1466,7 +1458,7 @@ TEST_F(ModelTypeWorkerTest,
EXPECT_EQ(ModelTypeWorker::SUCCESS, EXPECT_EQ(ModelTypeWorker::SUCCESS,
ModelTypeWorker::PopulateUpdateResponseData( ModelTypeWorker::PopulateUpdateResponseData(
&cryptographer, PREFERENCES, entity, &response_data)); &cryptographer, BOOKMARKS, entity, &response_data));
const EntityData& data = *response_data.entity; const EntityData& data = *response_data.entity;
EXPECT_FALSE( EXPECT_FALSE(
syncer::UniquePosition::FromProto(data.unique_position).IsValid()); syncer::UniquePosition::FromProto(data.unique_position).IsValid());
...@@ -1477,7 +1469,7 @@ TEST_F(ModelTypeWorkerTest, ...@@ -1477,7 +1469,7 @@ TEST_F(ModelTypeWorkerTest,
} }
TEST_F(ModelTypeWorkerTest, TEST_F(ModelTypeWorkerTest,
PopulateUpdateResponseDataWithNonBookmarkHasNoPosition) { PopulateUpdateResponseDataForNonBookmarkWithNoPosition) {
InitializeCommitOnly(); InitializeCommitOnly();
sync_pb::SyncEntity entity; sync_pb::SyncEntity entity;
...@@ -1498,7 +1490,7 @@ TEST_F(ModelTypeWorkerTest, ...@@ -1498,7 +1490,7 @@ TEST_F(ModelTypeWorkerTest,
/*count=*/0); /*count=*/0);
} }
TEST_F(ModelTypeWorkerTest, PopulateUpdateResponseDataWithBookmarkGUID) { TEST_F(ModelTypeWorkerTest, PopulateUpdateResponseDataForBookmarkWithGUID) {
const std::string kGuid1 = base::GenerateGUID(); const std::string kGuid1 = base::GenerateGUID();
const std::string kGuid2 = base::GenerateGUID(); const std::string kGuid2 = base::GenerateGUID();
...@@ -1524,7 +1516,8 @@ TEST_F(ModelTypeWorkerTest, PopulateUpdateResponseDataWithBookmarkGUID) { ...@@ -1524,7 +1516,8 @@ TEST_F(ModelTypeWorkerTest, PopulateUpdateResponseDataWithBookmarkGUID) {
EXPECT_EQ(kGuid2, data.originator_client_item_id); EXPECT_EQ(kGuid2, data.originator_client_item_id);
} }
TEST_F(ModelTypeWorkerTest, PopulateUpdateResponseDataWithMissingBookmarkGUID) { TEST_F(ModelTypeWorkerTest,
PopulateUpdateResponseDataForBookmarkWithMissingGUID) {
const std::string kGuid1 = base::GenerateGUID(); const std::string kGuid1 = base::GenerateGUID();
NormalInitialize(); NormalInitialize();
...@@ -1549,7 +1542,7 @@ TEST_F(ModelTypeWorkerTest, PopulateUpdateResponseDataWithMissingBookmarkGUID) { ...@@ -1549,7 +1542,7 @@ TEST_F(ModelTypeWorkerTest, PopulateUpdateResponseDataWithMissingBookmarkGUID) {
} }
TEST_F(ModelTypeWorkerTest, TEST_F(ModelTypeWorkerTest,
PopulateUpdateResponseDataWithMissingBookmarkGUIDAndInvalidOCII) { PopulateUpdateResponseDataForBookmarkWithMissingGUIDAndInvalidOCII) {
const std::string kInvalidOCII = "INVALID OCII"; const std::string kInvalidOCII = "INVALID OCII";
NormalInitialize(); NormalInitialize();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment