Migrate usage of deprecated base::Value methods.
Methods provided by base::Value that consume or produce unmanaged raw
pointers have been deprecated and will be eventually removed from
libchrome. This CL migrates the usage of these deprecated methods in the
libweave code to the equivalent methods that use std::unique_ptr.
BUG=chromium:689697
TEST=Run unit tests.
Change-Id: I8cc5bb5d7a6ea742ad46e071db0887430bde5641
Reviewed-on: https://weave-review.googlesource.com/8458
Reviewed-by: Eric Caruso <ejcaruso@google.com>
diff --git a/src/commands/cloud_command_proxy.cc b/src/commands/cloud_command_proxy.cc
index 9ec3d3e..3348d52 100644
--- a/src/commands/cloud_command_proxy.cc
+++ b/src/commands/cloud_command_proxy.cc
@@ -4,6 +4,8 @@
#include "src/commands/cloud_command_proxy.h"
+#include <utility>
+
#include <base/bind.h>
#include <weave/enum_to_string.h>
#include <weave/provider/task_runner.h>
@@ -37,15 +39,15 @@
std::unique_ptr<base::DictionaryValue> patch{new base::DictionaryValue};
patch->Set(commands::attributes::kCommand_Error,
command_instance_->GetError()
- ? ErrorInfoToJson(*command_instance_->GetError()).release()
- : base::Value::CreateNullValue().release());
+ ? ErrorInfoToJson(*command_instance_->GetError())
+ : base::Value::CreateNullValue());
QueueCommandUpdate(std::move(patch));
}
void CloudCommandProxy::OnResultsChanged() {
std::unique_ptr<base::DictionaryValue> patch{new base::DictionaryValue};
patch->Set(commands::attributes::kCommand_Results,
- command_instance_->GetResults().release());
+ command_instance_->GetResults());
QueueCommandUpdate(std::move(patch));
}
@@ -59,7 +61,7 @@
void CloudCommandProxy::OnProgressChanged() {
std::unique_ptr<base::DictionaryValue> patch{new base::DictionaryValue};
patch->Set(commands::attributes::kCommand_Progress,
- command_instance_->GetProgress().release());
+ command_instance_->GetProgress());
QueueCommandUpdate(std::move(patch));
}
diff --git a/src/commands/command_dictionary.cc b/src/commands/command_dictionary.cc
index 053e7aa..3d2354a 100644
--- a/src/commands/command_dictionary.cc
+++ b/src/commands/command_dictionary.cc
@@ -4,6 +4,9 @@
#include "src/commands/command_dictionary.h"
+#include <utility>
+
+#include <base/memory/ptr_util.h>
#include <base/values.h>
#include <weave/enum_to_string.h>
@@ -210,15 +213,16 @@
if (!dict->GetDictionaryWithoutPathExpansion(package_name, &package)) {
// If this is the first time we encounter this package, create a JSON
// object for it.
- package = new base::DictionaryValue;
- dict->SetWithoutPathExpansion(package_name, package);
+ dict->SetWithoutPathExpansion(package_name,
+ base::MakeUnique<base::DictionaryValue>());
+ DCHECK(dict->GetDictionaryWithoutPathExpansion(package_name, &package));
}
- base::DictionaryValue* command_def = new base::DictionaryValue;
+ auto command_def = base::MakeUnique<base::DictionaryValue>();
command_def->Set(commands::attributes::kCommand_Parameters,
- parameters.release());
+ std::move(parameters));
command_def->SetString(commands::attributes::kCommand_Role,
EnumToString(pair.second->GetMinimalRole()));
- package->SetWithoutPathExpansion(command_name, command_def);
+ package->SetWithoutPathExpansion(command_name, std::move(command_def));
}
return dict;
}
diff --git a/src/commands/command_instance.cc b/src/commands/command_instance.cc
index f152d82..2b99113 100644
--- a/src/commands/command_instance.cc
+++ b/src/commands/command_instance.cc
@@ -269,15 +269,13 @@
json->SetString(commands::attributes::kCommand_Id, id_);
json->SetString(commands::attributes::kCommand_Name, name_);
json->Set(commands::attributes::kCommand_Parameters,
- TypedValueToJson(parameters_).release());
+ TypedValueToJson(parameters_));
json->Set(commands::attributes::kCommand_Progress,
- TypedValueToJson(progress_).release());
- json->Set(commands::attributes::kCommand_Results,
- TypedValueToJson(results_).release());
+ TypedValueToJson(progress_));
+ json->Set(commands::attributes::kCommand_Results, TypedValueToJson(results_));
json->SetString(commands::attributes::kCommand_State, EnumToString(state_));
if (error_) {
- json->Set(commands::attributes::kCommand_Error,
- ErrorInfoToJson(*error_).release());
+ json->Set(commands::attributes::kCommand_Error, ErrorInfoToJson(*error_));
}
return json;
diff --git a/src/commands/object_schema.cc b/src/commands/object_schema.cc
index b70beff..3ff5cee 100644
--- a/src/commands/object_schema.cc
+++ b/src/commands/object_schema.cc
@@ -6,6 +6,7 @@
#include <algorithm>
#include <limits>
+#include <utility>
#include <base/logging.h>
#include <base/values.h>
@@ -143,7 +144,7 @@
}
base::DictionaryValue array_object;
array_object.SetWithoutPathExpansion(commands::attributes::kOneOf_Enum,
- list->DeepCopy());
+ list->CreateDeepCopy());
prop = CreatePropType(type_name, error);
if (prop && !prop->FromJson(&array_object, base_schema, error))
prop.reset();
@@ -319,7 +320,7 @@
for (const auto& pair : properties_) {
auto prop_def = pair.second->ToJson(full_schema, in_command_def);
CHECK(prop_def);
- value->SetWithoutPathExpansion(pair.first, prop_def.release());
+ value->SetWithoutPathExpansion(pair.first, std::move(prop_def));
}
return value;
}
diff --git a/src/commands/prop_constraints.cc b/src/commands/prop_constraints.cc
index b7e9cf6..bc8e9ba 100644
--- a/src/commands/prop_constraints.cc
+++ b/src/commands/prop_constraints.cc
@@ -4,6 +4,8 @@
#include "src/commands/prop_constraints.h"
+#include <utility>
+
#include <base/json/json_writer.h>
#include <base/logging.h>
@@ -65,7 +67,7 @@
if (!overridden_only || HasOverriddenAttributes()) {
auto value = ToJson();
CHECK(value);
- dict->SetWithoutPathExpansion(GetDictKey(), value.release());
+ dict->SetWithoutPathExpansion(GetDictKey(), std::move(value));
}
}
diff --git a/src/commands/prop_types.cc b/src/commands/prop_types.cc
index 88a53bd..a1463a8 100644
--- a/src/commands/prop_types.cc
+++ b/src/commands/prop_types.cc
@@ -98,7 +98,7 @@
if (default_.value && (full_schema || !default_.is_inherited)) {
auto def_val = default_.value->ToJson();
CHECK(def_val);
- dict->Set(commands::attributes::kDefault, def_val.release());
+ dict->Set(commands::attributes::kDefault, std::move(def_val));
}
if (include_required)
@@ -472,7 +472,7 @@
CHECK(object_schema);
dict->SetWithoutPathExpansion(commands::attributes::kObject_Properties,
- object_schema.release());
+ std::move(object_schema));
dict->SetBooleanWithoutPathExpansion(
commands::attributes::kObject_AdditionalProperties,
object_schema_.value->GetExtraPropertiesAllowed());
@@ -482,7 +482,7 @@
required->AppendString(pair.first);
}
if (required->GetSize() > 0) {
- dict->Set(commands::attributes::kObject_Required, required.release());
+ dict->Set(commands::attributes::kObject_Required, std::move(required));
}
}
return value;
@@ -598,7 +598,8 @@
CHECK(value->GetAsDictionary(&dict)) << "Expecting a JSON object";
auto type = item_type_.value->ToJson(full_schema, false);
CHECK(type);
- dict->SetWithoutPathExpansion(commands::attributes::kItems, type.release());
+ dict->SetWithoutPathExpansion(commands::attributes::kItems,
+ std::move(type));
}
return value;
}
diff --git a/src/commands/schema_utils.cc b/src/commands/schema_utils.cc
index 3c3e949..05542b2 100644
--- a/src/commands/schema_utils.cc
+++ b/src/commands/schema_utils.cc
@@ -80,7 +80,7 @@
for (const auto& pair : value) {
auto prop_value = pair.second->ToJson();
CHECK(prop_value);
- dict->SetWithoutPathExpansion(pair.first, prop_value.release());
+ dict->SetWithoutPathExpansion(pair.first, std::move(prop_value));
}
return dict;
}
@@ -90,7 +90,7 @@
for (const auto& item : value) {
auto json = item->ToJson();
CHECK(json);
- list->Append(json.release());
+ list->Append(std::move(json));
}
return list;
}
diff --git a/src/commands/schema_utils.h b/src/commands/schema_utils.h
index 0c1d1b3..ee9ad41 100644
--- a/src/commands/schema_utils.h
+++ b/src/commands/schema_utils.h
@@ -11,6 +11,7 @@
#include <memory>
#include <string>
#include <type_traits>
+#include <utility>
#include <vector>
#include <base/logging.h>
@@ -69,7 +70,7 @@
for (const auto& v : values) {
auto json = TypedValueToJson(v);
CHECK(json);
- list->Append(json.release());
+ list->Append(std::move(json));
}
return list;
}
diff --git a/src/device_registration_info.cc b/src/device_registration_info.cc
index 3ee71ea..8055363 100644
--- a/src/device_registration_info.cc
+++ b/src/device_registration_info.cc
@@ -512,9 +512,9 @@
} else {
channel->SetString("supportedType", "pull");
}
- resource->Set("channel", channel.release());
- resource->Set("commandDefs", commands.release());
- resource->Set("state", state.release());
+ resource->Set("channel", std::move(channel));
+ resource->Set("commandDefs", std::move(commands));
+ resource->Set("state", std::move(state));
return resource;
}
@@ -544,7 +544,7 @@
base::DictionaryValue req_json;
req_json.SetString("id", ticket_id);
req_json.SetString("oauthClientId", GetSettings().client_id);
- req_json.Set("deviceDraft", device_draft.release());
+ req_json.Set("deviceDraft", std::move(device_draft));
auto url = GetServiceURL("registrationTickets/" + ticket_id,
{{"key", GetSettings().api_key}});
@@ -879,7 +879,7 @@
EnumToString(Command::State::kAborted));
if (error) {
command_patch.Set(commands::attributes::kCommand_Error,
- ErrorInfoToJson(*error).release());
+ ErrorInfoToJson(*error));
}
UpdateCommand(command_id, command_patch, base::Bind(&IgnoreCloudError));
}
@@ -1063,7 +1063,8 @@
continue;
}
- std::unique_ptr<base::DictionaryValue> cmd_copy{command_dict->DeepCopy()};
+ std::unique_ptr<base::DictionaryValue> cmd_copy =
+ command_dict->CreateDeepCopy();
cmd_copy->SetString("state", "aborted");
// TODO(wiley) We could consider handling this error case more gracefully.
DoCloudRequest(HttpClient::Method::kPut,
@@ -1147,17 +1148,17 @@
// "package.property_name", so must use DictionaryValue::Set() instead of
// DictionaryValue::SetWithoutPathExpansion to recreate the JSON
// property tree properly.
- changes->Set(pair.first, value.release());
+ changes->Set(pair.first, std::move(value));
}
- patch->Set("patch", changes.release());
+ patch->Set("patch", std::move(changes));
- patches->Append(patch.release());
+ patches->Append(std::move(patch));
}
base::DictionaryValue body;
body.SetString("requestTimeMs",
std::to_string(base::Time::Now().ToJavaTime()));
- body.Set("patches", patches.release());
+ body.Set("patches", std::move(patches));
device_state_update_pending_ = true;
DoCloudRequest(HttpClient::Method::kPost, GetDeviceURL("patchState"), &body,
diff --git a/src/device_registration_info_unittest.cc b/src/device_registration_info_unittest.cc
index df4a438..6ccf00e 100644
--- a/src/device_registration_info_unittest.cc
+++ b/src/device_registration_info_unittest.cc
@@ -433,10 +433,10 @@
json_resp.SetString("oauthClientId", test_data::kClientId);
base::DictionaryValue* device_draft = nullptr;
EXPECT_TRUE(json->GetDictionary("deviceDraft", &device_draft));
- device_draft = device_draft->DeepCopy();
- device_draft->SetString("id", test_data::kDeviceId);
- device_draft->SetString("kind", "weave#device");
- json_resp.Set("deviceDraft", device_draft);
+ auto device_draft_copy = device_draft->CreateDeepCopy();
+ device_draft_copy->SetString("id", test_data::kDeviceId);
+ device_draft_copy->SetString("kind", "weave#device");
+ json_resp.Set("deviceDraft", std::move(device_draft_copy));
callback.Run(ReplyWithJson(200, json_resp), nullptr);
})));
diff --git a/src/privet/cloud_delegate.cc b/src/privet/cloud_delegate.cc
index d612668..79c98cf 100644
--- a/src/privet/cloud_delegate.cc
+++ b/src/privet/cloud_delegate.cc
@@ -5,6 +5,7 @@
#include "src/privet/cloud_delegate.h"
#include <map>
+#include <utility>
#include <vector>
#include <base/bind.h>
@@ -199,13 +200,12 @@
for (const auto& it : command_owners_) {
if (CanAccessCommand(it.second, user_info, nullptr)) {
- list_value.Append(
- command_manager_->FindCommand(it.first)->ToJson().release());
+ list_value.Append(command_manager_->FindCommand(it.first)->ToJson());
}
}
base::DictionaryValue commands_json;
- commands_json.Set("commands", list_value.DeepCopy());
+ commands_json.Set("commands", list_value.CreateDeepCopy());
callback.Run(commands_json, nullptr);
}
diff --git a/src/privet/mock_delegates.h b/src/privet/mock_delegates.h
index 2186f2f..ef40eb8 100644
--- a/src/privet/mock_delegates.h
+++ b/src/privet/mock_delegates.h
@@ -9,6 +9,7 @@
#include <string>
#include <utility>
+#include <base/memory/ptr_util.h>
#include <base/values.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
@@ -184,7 +185,7 @@
.WillRepeatedly(ReturnRef(connection_state_));
EXPECT_CALL(*this, GetSetupState()).WillRepeatedly(ReturnRef(setup_state_));
EXPECT_CALL(*this, GetCloudId()).WillRepeatedly(Return("TestCloudId"));
- test_dict_.Set("test", new base::DictionaryValue);
+ test_dict_.Set("test", base::MakeUnique<base::DictionaryValue>());
EXPECT_CALL(*this, GetCommandDef()).WillRepeatedly(ReturnRef(test_dict_));
EXPECT_CALL(*this, GetState()).WillRepeatedly(ReturnRef(test_dict_));
}
diff --git a/src/privet/privet_handler.cc b/src/privet/privet_handler.cc
index 157c982..25b2e4c 100644
--- a/src/privet/privet_handler.cc
+++ b/src/privet/privet_handler.cc
@@ -12,6 +12,7 @@
#include <base/bind.h>
#include <base/location.h>
+#include <base/memory/ptr_util.h>
#include <base/strings/stringprintf.h>
#include <base/values.h>
#include <weave/enum_to_string.h>
@@ -173,9 +174,9 @@
it->GetLocation().file_name.c_str(),
it->GetLocation().line_number, nullptr};
inner->SetString(kErrorDebugInfoKey, location.ToString());
- errors->Append(inner.release());
+ errors->Append(std::move(inner));
}
- output->Set(kErrorDebugInfoKey, errors.release());
+ output->Set(kErrorDebugInfoKey, std::move(errors));
return output;
}
@@ -186,7 +187,7 @@
return;
}
parent->SetString(kStatusKey, kStatusErrorValue);
- parent->Set(kErrorKey, ErrorToJson(*state.error()).release());
+ parent->Set(kErrorKey, ErrorToJson(*state.error()));
}
void ReturnError(const Error& error,
@@ -199,7 +200,7 @@
}
}
std::unique_ptr<base::DictionaryValue> output{new base::DictionaryValue};
- output->Set(kErrorKey, ErrorToJson(error).release());
+ output->Set(kErrorKey, ErrorToJson(error));
callback.Run(code, *output);
}
@@ -258,7 +259,7 @@
std::unique_ptr<base::ListValue> pairing_types(new base::ListValue());
for (PairingType type : security.GetPairingTypes())
pairing_types->AppendString(EnumToString(type));
- auth->Set(kPairingKey, pairing_types.release());
+ auth->Set(kPairingKey, std::move(pairing_types));
std::unique_ptr<base::ListValue> auth_types(new base::ListValue());
auth_types->AppendString(kAuthTypeAnonymousValue);
@@ -268,12 +269,12 @@
// if (cloud.GetConnectionState().IsStatusEqual(ConnectionState::kOnline)) {
// auth_types->AppendString(kAuthTypeCloudValue);
// }
- auth->Set(kAuthModeKey, auth_types.release());
+ auth->Set(kAuthModeKey, std::move(auth_types));
std::unique_ptr<base::ListValue> crypto_types(new base::ListValue());
for (CryptoType type : security.GetCryptoTypes())
crypto_types->AppendString(EnumToString(type));
- auth->Set(kCryptoKey, crypto_types.release());
+ auth->Set(kCryptoKey, std::move(crypto_types));
return auth;
}
@@ -285,7 +286,7 @@
std::unique_ptr<base::ListValue> capabilities(new base::ListValue());
for (WifiType type : wifi.GetTypes())
capabilities->AppendString(EnumToString(type));
- result->Set(kInfoWifiCapabilitiesKey, capabilities.release());
+ result->Set(kInfoWifiCapabilitiesKey, std::move(capabilities));
result->SetString(kInfoWifiSsidKey, wifi.GetCurrentlyConnectedSsid());
@@ -503,23 +504,21 @@
output.SetString(kLocationKey, location);
output.SetString(kInfoModelIdKey, model_id);
- output.Set(kInfoModelManifestKey, CreateManifestSection(*cloud_).release());
+ output.Set(kInfoModelManifestKey, CreateManifestSection(*cloud_));
output.Set(
kInfoServicesKey,
- ToValue(std::vector<std::string>{GetDeviceUiKind(cloud_->GetModelId())})
- .release());
+ ToValue(std::vector<std::string>{GetDeviceUiKind(cloud_->GetModelId())}));
output.Set(
kInfoAuthenticationKey,
- CreateInfoAuthSection(*security_, GetAnonymousMaxScope(*cloud_, wifi_))
- .release());
+ CreateInfoAuthSection(*security_, GetAnonymousMaxScope(*cloud_, wifi_)));
- output.Set(kInfoEndpointsKey, CreateEndpointsSection(*device_).release());
+ output.Set(kInfoEndpointsKey, CreateEndpointsSection(*device_));
if (wifi_)
- output.Set(kWifiKey, CreateWifiSection(*wifi_).release());
+ output.Set(kWifiKey, CreateWifiSection(*wifi_));
- output.Set(kGcdKey, CreateGcdSection(*cloud_).release());
+ output.Set(kGcdKey, CreateGcdSection(*cloud_));
output.SetInteger(kInfoUptimeKey, device_->GetUptime().InSeconds());
@@ -737,21 +736,23 @@
const SetupState& state = cloud_->GetSetupState();
if (!state.IsStatusEqual(SetupState::kNone)) {
- base::DictionaryValue* gcd = new base::DictionaryValue;
- output.Set(kGcdKey, gcd);
- SetStateProperties(state, gcd);
+ auto gcd = base::MakeUnique<base::DictionaryValue>();
+ SetStateProperties(state, gcd.get());
if (state.IsStatusEqual(SetupState::kSuccess))
gcd->SetString(kInfoIdKey, cloud_->GetCloudId());
+
+ output.Set(kGcdKey, std::move(gcd));
}
if (wifi_) {
const SetupState& state = wifi_->GetSetupState();
if (!state.IsStatusEqual(SetupState::kNone)) {
- base::DictionaryValue* wifi = new base::DictionaryValue;
- output.Set(kWifiKey, wifi);
- SetStateProperties(state, wifi);
+ auto wifi = base::MakeUnique<base::DictionaryValue>();
+ SetStateProperties(state, wifi.get());
if (state.IsStatusEqual(SetupState::kSuccess))
wifi->SetString(kInfoWifiSsidKey, wifi_->GetCurrentlyConnectedSsid());
+
+ output.Set(kWifiKey, std::move(wifi));
}
}
@@ -762,8 +763,7 @@
const UserInfo& user_info,
const RequestCallback& callback) {
base::DictionaryValue output;
- base::DictionaryValue* defs = cloud_->GetState().DeepCopy();
- output.Set(kStateKey, defs);
+ output.Set(kStateKey, cloud_->GetState().CreateDeepCopy());
output.SetString(kFingerprintKey, std::to_string(state_fingerprint_));
callback.Run(http::kOk, output);
@@ -773,8 +773,7 @@
const UserInfo& user_info,
const RequestCallback& callback) {
base::DictionaryValue output;
- base::DictionaryValue* defs = cloud_->GetCommandDef().DeepCopy();
- output.Set(kCommandsKey, defs);
+ output.Set(kCommandsKey, cloud_->GetCommandDef().CreateDeepCopy());
output.SetString(kFingerprintKey, std::to_string(command_defs_fingerprint_));
callback.Run(http::kOk, output);
diff --git a/src/states/state_manager.cc b/src/states/state_manager.cc
index fe6e669..d430029 100644
--- a/src/states/state_manager.cc
+++ b/src/states/state_manager.cc
@@ -32,7 +32,7 @@
for (const auto& pair : packages_) {
auto pkg_value = pair.second->GetValuesAsJson();
CHECK(pkg_value);
- dict->SetWithoutPathExpansion(pair.first, pkg_value.release());
+ dict->SetWithoutPathExpansion(pair.first, std::move(pkg_value));
}
return dict;
}
diff --git a/src/states/state_package.cc b/src/states/state_package.cc
index 0b8e219..a23d068 100644
--- a/src/states/state_package.cc
+++ b/src/states/state_package.cc
@@ -4,6 +4,8 @@
#include "src/states/state_package.h"
+#include <utility>
+
#include <base/logging.h>
#include <base/values.h>
@@ -57,7 +59,7 @@
for (const auto& pair : values_) {
auto value = pair.second->ToJson();
CHECK(value);
- dict->SetWithoutPathExpansion(pair.first, value.release());
+ dict->SetWithoutPathExpansion(pair.first, std::move(value));
}
return dict;
}
diff --git a/src/weave_unittest.cc b/src/weave_unittest.cc
index 67c3c97..fba9a8a 100644
--- a/src/weave_unittest.cc
+++ b/src/weave_unittest.cc
@@ -4,6 +4,8 @@
#include <weave/device.h>
+#include <utility>
+
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <weave/provider/test/fake_task_runner.h>
@@ -347,14 +349,14 @@
auto draft = CreateDictionaryValue(kDeviceResource);
auto response = CreateDictionaryValue(kRegistrationResponse);
- response->Set("deviceDraft", draft->DeepCopy());
+ response->Set("deviceDraft", draft->CreateDeepCopy());
ExpectRequest(HttpClient::Method::kPatch,
"https://www.googleapis.com/weave/v1/registrationTickets/"
"TICKET_ID?key=TEST_API_KEY",
ValueToString(*response));
response = CreateDictionaryValue(kRegistrationFinalResponse);
- response->Set("deviceDraft", draft->DeepCopy());
+ response->Set("deviceDraft", draft->CreateDeepCopy());
ExpectRequest(HttpClient::Method::kPost,
"https://www.googleapis.com/weave/v1/registrationTickets/"
"TICKET_ID/finalize?key=TEST_API_KEY",