Skip to content

Commit

Permalink
Remove usage of deprecated unique_ptr<base::Value>
Browse files Browse the repository at this point in the history
Value::Set() is deprecated and Value::SetKey/Path() should be used
instead. base::ValueConversions returns unique_ptr<base::Value> which is
hard to use with the new SetKey/Path methods because they expect a
Value.
This CL converts CreateFilePathValue, CreateTimeDeltaValue and
CreateUnguessableTokenValue to return a Value instead of a unique_ptr.

Tbr: benwells@chromium.org, bauerb@chromium.org, pfeldman@chromium.org, qinmin@chromium.org, xhwang@chromium.org
Change-Id: Ia464bce2cdd74e0e1b51b57050cac3c0176893f8
Reviewed-on: https://chromium-review.googlesource.com/1150036
Commit-Queue: Christian Dullweber <dullweber@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Jan Wilken Dörrie <jdoerrie@chromium.org>
Cr-Commit-Position: refs/heads/master@{#578277}
  • Loading branch information
xchrdw authored and Commit Bot committed Jul 26, 2018
1 parent 7c6d591 commit 1374008
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 33 deletions.
2 changes: 1 addition & 1 deletion apps/saved_files_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void AddSavedFileEntry(ExtensionPrefs* prefs,

std::unique_ptr<base::DictionaryValue> file_entry_dict =
std::make_unique<base::DictionaryValue>();
file_entry_dict->Set(kFileEntryPath, CreateFilePathValue(file_entry.path));
file_entry_dict->SetKey(kFileEntryPath, CreateFilePathValue(file_entry.path));
file_entry_dict->SetBoolean(kFileEntryIsDirectory, file_entry.is_directory);
file_entry_dict->SetInteger(kFileEntrySequenceNumber,
file_entry.sequence_number);
Expand Down
4 changes: 2 additions & 2 deletions base/unguessable_token_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ TEST(UnguessableTokenTest, VerifySerialization) {

TEST(UnguessableTokenTest, VerifyValueSerialization) {
UnguessableToken token = UnguessableToken::Create();
std::unique_ptr<Value> value = CreateUnguessableTokenValue(token);
Value value = CreateUnguessableTokenValue(token);

UnguessableToken deserialized;
EXPECT_TRUE(GetValueAsUnguessableToken(*value, &deserialized));
EXPECT_TRUE(GetValueAsUnguessableToken(value, &deserialized));
EXPECT_EQ(token, deserialized);
}

Expand Down
14 changes: 6 additions & 8 deletions base/value_conversions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ union UnguessableTokenRepresentation {

// |Value| internally stores strings in UTF-8, so we have to convert from the
// system native code to UTF-8 and back.
std::unique_ptr<Value> CreateFilePathValue(const FilePath& in_value) {
return std::make_unique<Value>(in_value.AsUTF8Unsafe());
Value CreateFilePathValue(const FilePath& in_value) {
return Value(in_value.AsUTF8Unsafe());
}

bool GetValueAsFilePath(const Value& value, FilePath* file_path) {
Expand All @@ -47,9 +47,9 @@ bool GetValueAsFilePath(const Value& value, FilePath* file_path) {

// |Value| does not support 64-bit integers, and doubles do not have enough
// precision, so we store the 64-bit time value as a string instead.
std::unique_ptr<Value> CreateTimeDeltaValue(const TimeDelta& time) {
Value CreateTimeDeltaValue(const TimeDelta& time) {
std::string string_value = base::Int64ToString(time.ToInternalValue());
return std::make_unique<Value>(string_value);
return Value(string_value);
}

bool GetValueAsTimeDelta(const Value& value, TimeDelta* time) {
Expand All @@ -62,14 +62,12 @@ bool GetValueAsTimeDelta(const Value& value, TimeDelta* time) {
return true;
}

std::unique_ptr<Value> CreateUnguessableTokenValue(
const UnguessableToken& token) {
Value CreateUnguessableTokenValue(const UnguessableToken& token) {
UnguessableTokenRepresentation representation;
representation.field.high = token.GetHighForSerialization();
representation.field.low = token.GetLowForSerialization();

return std::make_unique<Value>(
HexEncode(representation.buffer, sizeof(representation.buffer)));
return Value(HexEncode(representation.buffer, sizeof(representation.buffer)));
}

bool GetValueAsUnguessableToken(const Value& value, UnguessableToken* token) {
Expand Down
8 changes: 3 additions & 5 deletions base/value_conversions.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ class UnguessableToken;
class Value;

// The caller takes ownership of the returned value.
BASE_EXPORT std::unique_ptr<Value> CreateFilePathValue(
const FilePath& in_value);
BASE_EXPORT Value CreateFilePathValue(const FilePath& in_value);
BASE_EXPORT bool GetValueAsFilePath(const Value& value, FilePath* file_path);

BASE_EXPORT std::unique_ptr<Value> CreateTimeDeltaValue(const TimeDelta& time);
BASE_EXPORT Value CreateTimeDeltaValue(const TimeDelta& time);
BASE_EXPORT bool GetValueAsTimeDelta(const Value& value, TimeDelta* time);

BASE_EXPORT std::unique_ptr<Value> CreateUnguessableTokenValue(
const UnguessableToken& token);
BASE_EXPORT Value CreateUnguessableTokenValue(const UnguessableToken& token);
BASE_EXPORT bool GetValueAsUnguessableToken(const Value& value,
UnguessableToken* token);

Expand Down
3 changes: 1 addition & 2 deletions chrome/browser/devtools/devtools_file_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,7 @@ void DevToolsFileHelper::SaveAsFileSelected(const std::string& url,
DictionaryPrefUpdate update(profile_->GetPrefs(),
prefs::kDevToolsEditedFiles);
base::DictionaryValue* files_map = update.Get();
files_map->SetWithoutPathExpansion(base::MD5String(url),
base::CreateFilePathValue(path));
files_map->SetKey(base::MD5String(url), base::CreateFilePathValue(path));
std::string file_system_path = path.AsUTF8Unsafe();
callback.Run(file_system_path);
file_task_runner_->PostTask(FROM_HERE, BindOnce(&WriteToFile, path, content));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,8 @@ void DownloadTargetDeterminerTest::EnableAutoOpenBasedOnExtension(
void DownloadTargetDeterminerTest::SetManagedDownloadPath(
const base::FilePath& path) {
profile()->GetTestingPrefService()->SetManagedPref(
prefs::kDownloadDefaultDirectory, base::CreateFilePathValue(path));
prefs::kDownloadDefaultDirectory,
base::Value::ToUniquePtrValue(base::CreateFilePathValue(path)));
}

void DownloadTargetDeterminerTest::SetPromptForDownload(bool prompt) {
Expand Down
3 changes: 1 addition & 2 deletions chrome/browser/profiles/profile_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,7 @@ void MarkProfileDirectoryForDeletion(const base::FilePath& path) {
// on shutdown. In case of a crash remaining files are removed on next start.
ListPrefUpdate deleted_profiles(g_browser_process->local_state(),
prefs::kProfilesDeleted);
std::unique_ptr<base::Value> value(CreateFilePathValue(path));
deleted_profiles->Append(std::move(value));
deleted_profiles->GetList().push_back(CreateFilePathValue(path));
}

// Cancel a scheduling deletion, so ScheduleProfileDirectoryForDeletion can be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ DownloadsListTracker::CreateDownloadItemValue(
file_value->SetString("id", base::NumberToString(download_item->GetId()));

base::FilePath download_path(download_item->GetTargetFilePath());
file_value->Set("file_path", base::CreateFilePathValue(download_path));
file_value->SetKey("file_path", base::CreateFilePathValue(download_path));
file_value->SetString("file_url",
net::FilePathToFileURL(download_path).spec());

Expand Down
4 changes: 2 additions & 2 deletions chrome/browser/ui/webui/signin/user_manager_screen_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -796,8 +796,8 @@ void UserManagerScreenHandler::SendUserList() {
profile_value->SetString(kKeyEmailAddress, entry->GetUserName());
profile_value->SetString(kKeyDisplayName,
profiles::GetAvatarNameForProfile(profile_path));
profile_value->Set(kKeyProfilePath,
base::CreateFilePathValue(profile_path));
profile_value->SetKey(kKeyProfilePath,
base::CreateFilePathValue(profile_path));
profile_value->SetBoolean(kKeyPublicAccount, false);
profile_value->SetBoolean(kKeyLegacySupervisedUser,
entry->IsLegacySupervised());
Expand Down
4 changes: 2 additions & 2 deletions chrome/common/custom_handlers/protocol_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ std::unique_ptr<base::DictionaryValue> ProtocolHandler::Encode() const {
auto d = std::make_unique<base::DictionaryValue>();
d->SetString("protocol", protocol_);
d->SetString("url", url_.spec());
d->Set("last_modified",
base::CreateTimeDeltaValue(last_modified_.ToDeltaSinceWindowsEpoch()));
d->SetKey("last_modified", base::CreateTimeDeltaValue(
last_modified_.ToDeltaSinceWindowsEpoch()));
return d;
}

Expand Down
3 changes: 1 addition & 2 deletions components/cdm/browser/media_drm_storage_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ class OriginData {
base::Value ToDictValue() const {
base::Value dict(base::Value::Type::DICTIONARY);

dict.SetKey(kOriginId, base::Value::FromUniquePtrValue(
base::CreateUnguessableTokenValue(origin_id_)));
dict.SetKey(kOriginId, base::CreateUnguessableTokenValue(origin_id_));
dict.SetKey(kCreationTime, base::Value(provision_time_.ToDoubleT()));

return dict;
Expand Down
3 changes: 2 additions & 1 deletion components/prefs/pref_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,8 @@ void PrefService::SetString(const std::string& path, const std::string& value) {

void PrefService::SetFilePath(const std::string& path,
const base::FilePath& value) {
SetUserPrefValue(path, base::CreateFilePathValue(value));
SetUserPrefValue(
path, base::Value::ToUniquePtrValue(base::CreateFilePathValue(value)));
}

void PrefService::SetInt64(const std::string& path, int64_t value) {
Expand Down
4 changes: 2 additions & 2 deletions extensions/browser/api/alarms/alarm_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ std::unique_ptr<base::ListValue> AlarmsToValue(
for (size_t i = 0; i < alarms.size(); ++i) {
std::unique_ptr<base::DictionaryValue> alarm =
alarms[i]->js_alarm->ToValue();
alarm->Set(kAlarmGranularity,
base::CreateTimeDeltaValue(alarms[i]->granularity));
alarm->SetKey(kAlarmGranularity,
base::CreateTimeDeltaValue(alarms[i]->granularity));
list->Append(std::move(alarm));
}
return list;
Expand Down
5 changes: 3 additions & 2 deletions extensions/browser/api/file_system/file_system_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,9 @@ base::FilePath GetLastChooseEntryDirectory(const ExtensionPrefs* prefs,
void SetLastChooseEntryDirectory(ExtensionPrefs* prefs,
const std::string& extension_id,
const base::FilePath& path) {
prefs->UpdateExtensionPref(extension_id, kLastChooseEntryDirectory,
base::CreateFilePathValue(path));
prefs->UpdateExtensionPref(
extension_id, kLastChooseEntryDirectory,
base::Value::ToUniquePtrValue(base::CreateFilePathValue(path)));
}

} // namespace file_system_api
Expand Down

0 comments on commit 1374008

Please sign in to comment.