Commit bd515668 authored by Richard Knoll's avatar Richard Knoll Committed by Commit Bot

[Sharing] Filter invalid enum values from sharing features

We've deprecated an enum value which might still be set in some
profiles. This will make sure we're ignoring these invalid enum values
instead of hitting a DCHECK further down in sync code.

Bug: 1114108
Change-Id: I3fa0800052a0c7d84cdb30ceb3a1486bc7468264
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2395616Reviewed-by: default avatarMikel Astiz <mastiz@chromium.org>
Commit-Queue: Richard Knoll <knollr@chromium.org>
Cr-Commit-Position: refs/heads/master@{#804923}
parent ef264a96
......@@ -257,11 +257,14 @@ SharingSyncPreference::GetLocalSharingInfoForSync(PrefService* prefs) {
std::set<SharingSpecificFields::EnabledFeatures> enabled_features;
for (auto& value : enabled_features_value->GetList()) {
if (!value.is_int())
NOTREACHED();
DCHECK(value.is_int());
int feature_value = value.GetInt();
// Filter invalid enums from other browser versions.
if (!sync_pb::SharingSpecificFields::EnabledFeatures_IsValid(feature_value))
continue;
enabled_features.insert(
static_cast<SharingSpecificFields::EnabledFeatures>(value.GetInt()));
static_cast<SharingSpecificFields::EnabledFeatures>(feature_value));
}
return syncer::DeviceInfo::SharingInfo(std::move(*vapid_target_info),
......
......@@ -11,6 +11,7 @@
#include "base/values.h"
#include "chrome/browser/sharing/fake_device_info.h"
#include "chrome/browser/sharing/proto/sharing_message.pb.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/sync_device_info/device_info.h"
#include "components/sync_device_info/fake_device_info_sync_service.h"
......@@ -34,6 +35,8 @@ const char kDeviceSenderIdP256dh[] = "test_sender_id_p256dh";
const char kAuthorizedEntity[] = "authorized_entity";
const char kSharingInfoEnabledFeatures[] = "enabled_features";
} // namespace
class SharingSyncPreferenceTest : public testing::Test {
......@@ -52,6 +55,20 @@ class SharingSyncPreferenceTest : public testing::Test {
sync_pb::SharingSpecificFields::CLICK_TO_CALL_V2});
}
void AddEnabledFeature(int feature) {
const base::DictionaryValue* registration =
prefs_.GetDictionary(prefs::kSharingLocalSharingInfo);
base::Value enabled_features =
registration->FindListKey(kSharingInfoEnabledFeatures)->Clone();
enabled_features.Append(feature);
DictionaryPrefUpdate local_sharing_info_update(
&prefs_, prefs::kSharingLocalSharingInfo);
local_sharing_info_update->SetKey(kSharingInfoEnabledFeatures,
std::move(enabled_features));
}
sync_preferences::TestingPrefServiceSyncable prefs_;
syncer::FakeDeviceInfoSyncService fake_device_info_sync_service_;
SharingSyncPreference sharing_sync_preference_;
......@@ -121,3 +138,15 @@ TEST_F(SharingSyncPreferenceTest, GetLocalSharingInfoForSync) {
EXPECT_EQ(sharing_info,
SharingSyncPreference::GetLocalSharingInfoForSync(&prefs_));
}
TEST_F(SharingSyncPreferenceTest, GetLocalSharingInfoForSync_InvalidEnum) {
auto sharing_info = GetDefaultSharingInfo();
sharing_sync_preference_.SetLocalSharingInfo(sharing_info);
// Add invalid enabled feature enum value.
AddEnabledFeature(/*feature=*/-1);
// Expect invalid enum value to be filtered out.
EXPECT_EQ(sharing_info,
SharingSyncPreference::GetLocalSharingInfoForSync(&prefs_));
}
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