Commit f2c5d73c authored by Himanshu Jaju's avatar Himanshu Jaju Committed by Commit Bot

Update client name in OnSyncStarting

SyncMode is not available when GetDeviceName is called. So
we have to manually update the device name and merge it with
sync data.

Bug: 1009454
Change-Id: Icf0760eabf41171b9d7f222b620b0c41bc32bc59
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1866651
Commit-Queue: Himanshu Jaju <himanshujaju@chromium.org>
Reviewed-by: default avatarAlex Chau <alexchau@chromium.org>
Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Reviewed-by: default avatarMikel Astiz <mastiz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#707806}
parent e59ad318
...@@ -83,6 +83,11 @@ class BASE_EXPORT SysInfo { ...@@ -83,6 +83,11 @@ class BASE_EXPORT SysInfo {
// Note: validate any new usage with the privacy team. // Note: validate any new usage with the privacy team.
// TODO(crbug.com/907518): Implement support on other platforms. // TODO(crbug.com/907518): Implement support on other platforms.
std::string serial_number; std::string serial_number;
bool operator==(const HardwareInfo& rhs) const {
return manufacturer == rhs.manufacturer && model == rhs.model &&
serial_number == rhs.serial_number;
}
}; };
// Returns via |callback| a struct containing descriptive UTF-8 strings for // Returns via |callback| a struct containing descriptive UTF-8 strings for
// the current machine manufacturer and model, or empty strings if the // the current machine manufacturer and model, or empty strings if the
......
...@@ -52,7 +52,10 @@ DeviceInfo::DeviceInfo(const std::string& guid, ...@@ -52,7 +52,10 @@ DeviceInfo::DeviceInfo(const std::string& guid,
hardware_info_(hardware_info), hardware_info_(hardware_info),
last_updated_timestamp_(last_updated_timestamp), last_updated_timestamp_(last_updated_timestamp),
send_tab_to_self_receiving_enabled_(send_tab_to_self_receiving_enabled), send_tab_to_self_receiving_enabled_(send_tab_to_self_receiving_enabled),
sharing_info_(sharing_info) {} sharing_info_(sharing_info) {
// We do not store device's serial number in DeviceInfo.
hardware_info_.serial_number.clear();
}
DeviceInfo::~DeviceInfo() {} DeviceInfo::~DeviceInfo() {}
...@@ -144,6 +147,7 @@ bool DeviceInfo::Equals(const DeviceInfo& other) const { ...@@ -144,6 +147,7 @@ bool DeviceInfo::Equals(const DeviceInfo& other) const {
this->sync_user_agent() == other.sync_user_agent() && this->sync_user_agent() == other.sync_user_agent() &&
this->device_type() == other.device_type() && this->device_type() == other.device_type() &&
this->signin_scoped_device_id() == other.signin_scoped_device_id() && this->signin_scoped_device_id() == other.signin_scoped_device_id() &&
this->hardware_info() == other.hardware_info() &&
this->send_tab_to_self_receiving_enabled() == this->send_tab_to_self_receiving_enabled() ==
other.send_tab_to_self_receiving_enabled() && other.send_tab_to_self_receiving_enabled() &&
this->sharing_info() == other.sharing_info(); this->sharing_info() == other.sharing_info();
...@@ -176,4 +180,8 @@ void DeviceInfo::set_sharing_info( ...@@ -176,4 +180,8 @@ void DeviceInfo::set_sharing_info(
sharing_info_ = sharing_info; sharing_info_ = sharing_info;
} }
void DeviceInfo::set_client_name(const std::string& client_name) {
client_name_ = client_name;
}
} // namespace syncer } // namespace syncer
...@@ -120,6 +120,8 @@ class DeviceInfo { ...@@ -120,6 +120,8 @@ class DeviceInfo {
void set_sharing_info(const base::Optional<SharingInfo>& sharing_info); void set_sharing_info(const base::Optional<SharingInfo>& sharing_info);
void set_client_name(const std::string& client_name);
// Converts the |DeviceInfo| values to a JS friendly DictionaryValue, // Converts the |DeviceInfo| values to a JS friendly DictionaryValue,
// which extension APIs can expose to third party apps. // which extension APIs can expose to third party apps.
std::unique_ptr<base::DictionaryValue> ToValue(); std::unique_ptr<base::DictionaryValue> ToValue();
...@@ -127,7 +129,7 @@ class DeviceInfo { ...@@ -127,7 +129,7 @@ class DeviceInfo {
private: private:
const std::string guid_; const std::string guid_;
const std::string client_name_; std::string client_name_;
const std::string chrome_version_; const std::string chrome_version_;
......
...@@ -204,6 +204,16 @@ void DeviceInfoSyncBridge::OnSyncStarting( ...@@ -204,6 +204,16 @@ void DeviceInfoSyncBridge::OnSyncStarting(
device_info_prefs_->AddLocalCacheGuid(local_cache_guid_); device_info_prefs_->AddLocalCacheGuid(local_cache_guid_);
// SyncMode determines the client name in GetLocalClientName(). // SyncMode determines the client name in GetLocalClientName().
sync_mode_ = request.sync_mode; sync_mode_ = request.sync_mode;
if (!change_processor()->IsTrackingMetadata()) {
return;
}
// Local device's client name needs to updated if OnSyncStarting is called
// after local device has already been initialized since the client name
// depends on |sync_mode_|.
local_device_info_provider_->UpdateClientName(GetLocalClientName());
ReconcileLocalAndStored();
} }
std::unique_ptr<MetadataChangeList> std::unique_ptr<MetadataChangeList>
......
...@@ -213,41 +213,6 @@ DeviceInfoSpecifics CreateSpecifics( ...@@ -213,41 +213,6 @@ DeviceInfoSpecifics CreateSpecifics(
return specifics; return specifics;
} }
DeviceInfoSpecifics DeviceInfoToSpecifics(const DeviceInfo& info) {
auto hardware_info = info.hardware_info();
DeviceInfoSpecifics specifics;
specifics.set_cache_guid(info.guid());
specifics.set_client_name(info.client_name());
specifics.set_chrome_version(info.chrome_version());
specifics.set_sync_user_agent(info.sync_user_agent());
specifics.set_device_type(info.device_type());
specifics.set_signin_scoped_device_id(info.signin_scoped_device_id());
specifics.set_model(hardware_info.model);
specifics.set_manufacturer(hardware_info.manufacturer);
specifics.set_last_updated_timestamp(TimeToProtoTime(base::Time::Now()));
sync_pb::FeatureSpecificFields* feature_fields =
specifics.mutable_feature_fields();
feature_fields->set_send_tab_to_self_receiving_enabled(
info.send_tab_to_self_receiving_enabled());
const base::Optional<DeviceInfo::SharingInfo>& sharing_info =
info.sharing_info();
if (sharing_info) {
sync_pb::SharingSpecificFields* sharing_fields =
specifics.mutable_sharing_fields();
sharing_fields->set_fcm_token(sharing_info->fcm_token);
sharing_fields->set_p256dh(sharing_info->p256dh);
sharing_fields->set_auth_secret(sharing_info->auth_secret);
for (sync_pb::SharingSpecificFields::EnabledFeatures feature :
sharing_info->enabled_features) {
sharing_fields->add_enabled_features(feature);
}
}
return specifics;
}
ModelTypeState StateWithEncryption(const std::string& encryption_key_name) { ModelTypeState StateWithEncryption(const std::string& encryption_key_name) {
ModelTypeState state; ModelTypeState state;
state.set_initial_sync_done(true); state.set_initial_sync_done(true);
...@@ -317,6 +282,11 @@ class TestLocalDeviceInfoProvider : public MutableLocalDeviceInfoProvider { ...@@ -317,6 +282,11 @@ class TestLocalDeviceInfoProvider : public MutableLocalDeviceInfoProvider {
void Clear() override { local_device_info_.reset(); } void Clear() override { local_device_info_.reset(); }
void UpdateClientName(const std::string& client_name) override {
ASSERT_TRUE(local_device_info_);
local_device_info_->set_client_name(client_name);
}
version_info::Channel GetChannel() const override { version_info::Channel GetChannel() const override {
return version_info::Channel::UNKNOWN; return version_info::Channel::UNKNOWN;
} }
...@@ -344,7 +314,14 @@ class DeviceInfoSyncBridgeTest : public testing::Test, ...@@ -344,7 +314,14 @@ class DeviceInfoSyncBridgeTest : public testing::Test,
: store_(ModelTypeStoreTestUtil::CreateInMemoryStoreForTest()), : store_(ModelTypeStoreTestUtil::CreateInMemoryStoreForTest()),
local_hardware_info_(GetLocalHardwareInfoBlocking()) { local_hardware_info_(GetLocalHardwareInfoBlocking()) {
DeviceInfoPrefs::RegisterProfilePrefs(pref_service_.registry()); DeviceInfoPrefs::RegisterProfilePrefs(pref_service_.registry());
ON_CALL(*processor(), IsTrackingMetadata()).WillByDefault(Return(true));
// By default, mimic a real processor's behavior for IsTrackingMetadata().
ON_CALL(mock_processor_, ModelReadyToSync(_))
.WillByDefault([this](std::unique_ptr<MetadataBatch> batch) {
ON_CALL(mock_processor_, IsTrackingMetadata())
.WillByDefault(
Return(batch->GetModelTypeState().initial_sync_done()));
});
} }
~DeviceInfoSyncBridgeTest() override { ~DeviceInfoSyncBridgeTest() override {
...@@ -393,6 +370,7 @@ class DeviceInfoSyncBridgeTest : public testing::Test, ...@@ -393,6 +370,7 @@ class DeviceInfoSyncBridgeTest : public testing::Test,
bridge()->CreateMetadataChangeList(); bridge()->CreateMetadataChangeList();
metadata_change_list->UpdateModelTypeState(StateWithEncryption("")); metadata_change_list->UpdateModelTypeState(StateWithEncryption(""));
ON_CALL(*processor(), IsTrackingMetadata()).WillByDefault(Return(true));
bridge()->MergeSyncData(std::move(metadata_change_list), bridge()->MergeSyncData(std::move(metadata_change_list),
EntityChangeList()); EntityChangeList());
} }
...@@ -789,6 +767,7 @@ TEST_F(DeviceInfoSyncBridgeTest, MergeEmpty) { ...@@ -789,6 +767,7 @@ TEST_F(DeviceInfoSyncBridgeTest, MergeEmpty) {
EXPECT_CALL(*processor(), Delete(_, _)).Times(0); EXPECT_CALL(*processor(), Delete(_, _)).Times(0);
bridge()->OnSyncStarting(TestDataTypeActivationRequest(SyncMode::kFull)); bridge()->OnSyncStarting(TestDataTypeActivationRequest(SyncMode::kFull));
ON_CALL(*processor(), IsTrackingMetadata()).WillByDefault(Return(true));
auto error = bridge()->MergeSyncData(bridge()->CreateMetadataChangeList(), auto error = bridge()->MergeSyncData(bridge()->CreateMetadataChangeList(),
EntityChangeList()); EntityChangeList());
EXPECT_FALSE(error); EXPECT_FALSE(error);
...@@ -810,6 +789,7 @@ TEST_F(DeviceInfoSyncBridgeTest, MergeLocalGuid) { ...@@ -810,6 +789,7 @@ TEST_F(DeviceInfoSyncBridgeTest, MergeLocalGuid) {
EXPECT_CALL(*processor(), Delete(_, _)).Times(0); EXPECT_CALL(*processor(), Delete(_, _)).Times(0);
bridge()->OnSyncStarting(TestDataTypeActivationRequest(SyncMode::kFull)); bridge()->OnSyncStarting(TestDataTypeActivationRequest(SyncMode::kFull));
ON_CALL(*processor(), IsTrackingMetadata()).WillByDefault(Return(true));
auto error = auto error =
bridge()->MergeSyncData(bridge()->CreateMetadataChangeList(), bridge()->MergeSyncData(bridge()->CreateMetadataChangeList(),
EntityAddList({CreateLocalDeviceSpecifics()})); EntityAddList({CreateLocalDeviceSpecifics()}));
...@@ -1051,6 +1031,7 @@ TEST_F(DeviceInfoSyncBridgeTest, ApplyStopSyncChangesWithClearData) { ...@@ -1051,6 +1031,7 @@ TEST_F(DeviceInfoSyncBridgeTest, ApplyStopSyncChangesWithClearData) {
// If sync is re-enabled and the remote data is now empty, we shouldn't // If sync is re-enabled and the remote data is now empty, we shouldn't
// contain remote data. // contain remote data.
bridge()->OnSyncStarting(TestDataTypeActivationRequest(SyncMode::kFull)); bridge()->OnSyncStarting(TestDataTypeActivationRequest(SyncMode::kFull));
ON_CALL(*processor(), IsTrackingMetadata()).WillByDefault(Return(true));
bridge()->MergeSyncData(bridge()->CreateMetadataChangeList(), bridge()->MergeSyncData(bridge()->CreateMetadataChangeList(),
EntityChangeList()); EntityChangeList());
// Local device. // Local device.
...@@ -1200,27 +1181,17 @@ TEST_F(DeviceInfoSyncBridgeTest, RefreshLocalDeviceNameForSyncModeToggle) { ...@@ -1200,27 +1181,17 @@ TEST_F(DeviceInfoSyncBridgeTest, RefreshLocalDeviceNameForSyncModeToggle) {
ASSERT_EQ(expected_device_name_full_sync, device->client_name()); ASSERT_EQ(expected_device_name_full_sync, device->client_name());
// Toggle to transport only sync mode. // Toggle to transport only sync mode.
syncer::EntityChangeList entity_change_list; bridge()->ApplyStopSyncChanges(/*delete_metadata_change_list=*/nullptr);
entity_change_list.push_back(EntityChange::CreateUpdate(
device->guid(), SpecificsToEntity(DeviceInfoToSpecifics(*device))));
bridge()->ApplyStopSyncChanges(bridge()->CreateMetadataChangeList());
bridge()->OnSyncStarting( bridge()->OnSyncStarting(
TestDataTypeActivationRequest(SyncMode::kTransportOnly)); TestDataTypeActivationRequest(SyncMode::kTransportOnly));
bridge()->MergeSyncData(bridge()->CreateMetadataChangeList(),
std::move(entity_change_list));
device = local_device()->GetLocalDeviceInfo(); device = local_device()->GetLocalDeviceInfo();
ASSERT_TRUE(device); ASSERT_TRUE(device);
ASSERT_EQ(expected_device_name_transport_only, device->client_name()); ASSERT_EQ(expected_device_name_transport_only, device->client_name());
// Toggle to full sync mode. // Toggle to full sync mode.
ASSERT_TRUE(entity_change_list.empty()); bridge()->ApplyStopSyncChanges(/*delete_metadata_change_list=*/nullptr);
entity_change_list.push_back(EntityChange::CreateUpdate(
device->guid(), SpecificsToEntity(DeviceInfoToSpecifics(*device))));
bridge()->ApplyStopSyncChanges(bridge()->CreateMetadataChangeList());
bridge()->OnSyncStarting(TestDataTypeActivationRequest(SyncMode::kFull)); bridge()->OnSyncStarting(TestDataTypeActivationRequest(SyncMode::kFull));
bridge()->MergeSyncData(bridge()->CreateMetadataChangeList(),
std::move(entity_change_list));
device = local_device()->GetLocalDeviceInfo(); device = local_device()->GetLocalDeviceInfo();
ASSERT_TRUE(device); ASSERT_TRUE(device);
......
...@@ -46,6 +46,10 @@ class MutableLocalDeviceInfoProvider : public LocalDeviceInfoProvider { ...@@ -46,6 +46,10 @@ class MutableLocalDeviceInfoProvider : public LocalDeviceInfoProvider {
const std::string& client_name, const std::string& client_name,
const base::SysInfo::HardwareInfo& hardware_info) = 0; const base::SysInfo::HardwareInfo& hardware_info) = 0;
virtual void Clear() = 0; virtual void Clear() = 0;
// Updates the local device's client name. Initialize() must be called before
// calling this function.
virtual void UpdateClientName(const std::string& client_name) = 0;
}; };
} // namespace syncer } // namespace syncer
......
...@@ -76,4 +76,10 @@ void LocalDeviceInfoProviderImpl::Clear() { ...@@ -76,4 +76,10 @@ void LocalDeviceInfoProviderImpl::Clear() {
local_device_info_.reset(); local_device_info_.reset();
} }
void LocalDeviceInfoProviderImpl::UpdateClientName(
const std::string& client_name) {
DCHECK(local_device_info_);
local_device_info_->set_client_name(client_name);
}
} // namespace syncer } // namespace syncer
...@@ -33,6 +33,7 @@ class LocalDeviceInfoProviderImpl : public MutableLocalDeviceInfoProvider { ...@@ -33,6 +33,7 @@ class LocalDeviceInfoProviderImpl : public MutableLocalDeviceInfoProvider {
const std::string& client_name, const std::string& client_name,
const base::SysInfo::HardwareInfo& hardware_info) override; const base::SysInfo::HardwareInfo& hardware_info) override;
void Clear() override; void Clear() override;
void UpdateClientName(const std::string& client_name) override;
version_info::Channel GetChannel() const override; version_info::Channel GetChannel() const override;
const DeviceInfo* GetLocalDeviceInfo() const override; const DeviceInfo* GetLocalDeviceInfo() const override;
std::unique_ptr<Subscription> RegisterOnInitializedCallback( std::unique_ptr<Subscription> RegisterOnInitializedCallback(
......
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