diff options
author | hscham <hscham@chromium.org> | 2020-06-23 17:12:32 +0900 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2020-06-26 11:11:27 +0000 |
commit | f7b80a7e9bd61d67d3ebcf34b577d5388b01c105 (patch) | |
tree | b315f5543373bef26be27f6a4e2f461010fde0bb | |
parent | ebe9930f0ba708d4030aec396b8254c8af4845da (diff) | |
download | platform_external_libbrillo-f7b80a7e9bd61d67d3ebcf34b577d5388b01c105.tar.gz platform_external_libbrillo-f7b80a7e9bd61d67d3ebcf34b577d5388b01c105.tar.bz2 platform_external_libbrillo-f7b80a7e9bd61d67d3ebcf34b577d5388b01c105.zip |
libbrillo: remove base::JSONReader::.*Deprecated methods
This is a libchrome post r680000 uprev cleanup.
BUG=chromium:1054279
TEST=cros_run_unit_tests --board=eve --packages libbrillo
Change-Id: I35639610217eb64e77dcc8fcec1e152a584758eb
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform2/+/2259738
Tested-by: Grace Cham <hscham@chromium.org>
Commit-Queue: Qijiang Fan <fqj@google.com>
Reviewed-by: Hidehiko Abe <hidehiko@chromium.org>
Cr-Mirrored-From: https://chromium.googlesource.com/chromiumos/platform2
Cr-Mirrored-Commit: 0d2a1e7e390557357f2968b0ab4b4e42bc926239
-rw-r--r-- | brillo/http/http_transport_fake.cc | 14 | ||||
-rw-r--r-- | brillo/http/http_transport_fake.h | 2 | ||||
-rw-r--r-- | brillo/http/http_utils.cc | 14 | ||||
-rw-r--r-- | brillo/http/http_utils.h | 1 | ||||
-rw-r--r-- | brillo/value_conversion_test.cc | 14 | ||||
-rw-r--r-- | policy/device_policy_impl.cc | 36 |
6 files changed, 35 insertions, 46 deletions
diff --git a/brillo/http/http_transport_fake.cc b/brillo/http/http_transport_fake.cc index deef1f8..8652216 100644 --- a/brillo/http/http_transport_fake.cc +++ b/brillo/http/http_transport_fake.cc @@ -173,18 +173,16 @@ std::string ServerRequestResponseBase::GetDataAsString() const { return std::string(chars, data_.size()); } -std::unique_ptr<base::DictionaryValue> -ServerRequestResponseBase::GetDataAsJson() const { - std::unique_ptr<base::DictionaryValue> result; +base::Optional<base::Value> ServerRequestResponseBase::GetDataAsJson() const { if (brillo::mime::RemoveParameters( GetHeader(request_header::kContentType)) == brillo::mime::application::kJson) { - // TODO(crbug.com/1054279): use base::JSONReader::Read after uprev to - // r680000. - auto value = base::JSONReader::ReadDeprecated(GetDataAsString()); - result = base::DictionaryValue::From(std::move(value)); + auto value = base::JSONReader::Read(GetDataAsString()); + if (!value->is_dict()) + return base::nullopt; + return value; } - return result; + return base::nullopt; } std::string ServerRequestResponseBase::GetDataAsNormalizedJsonString() const { diff --git a/brillo/http/http_transport_fake.h b/brillo/http/http_transport_fake.h index 896a597..31f23af 100644 --- a/brillo/http/http_transport_fake.h +++ b/brillo/http/http_transport_fake.h @@ -142,7 +142,7 @@ class ServerRequestResponseBase { void SetData(StreamPtr stream); const std::vector<uint8_t>& GetData() const { return data_; } std::string GetDataAsString() const; - std::unique_ptr<base::DictionaryValue> GetDataAsJson() const; + base::Optional<base::Value> GetDataAsJson() const; // Parses the data into a JSON object and writes it back to JSON to normalize // its string representation (no pretty print, extra spaces, etc). std::string GetDataAsNormalizedJsonString() const; diff --git a/brillo/http/http_utils.cc b/brillo/http/http_utils.cc index 55e0bf3..39cca31 100644 --- a/brillo/http/http_utils.cc +++ b/brillo/http/http_utils.cc @@ -395,19 +395,17 @@ std::unique_ptr<base::DictionaryValue> ParseJsonResponse( } std::string json = response->ExtractDataAsString(); - std::string error_message; - // TODO(crbug.com/1054279): use base::JSONReader::ReadAndReturnValueWithError - // after uprev to r680000. - auto value = base::JSONReader::ReadAndReturnErrorDeprecated( - json, base::JSON_PARSE_RFC, nullptr, &error_message); - if (!value) { + auto json_result = + base::JSONReader::ReadAndReturnValueWithError(json, base::JSON_PARSE_RFC); + if (json_result.error_code != base::JSONReader::JSON_NO_ERROR) { brillo::Error::AddToPrintf(error, FROM_HERE, brillo::errors::json::kDomain, brillo::errors::json::kParseError, "Error '%s' occurred parsing JSON string '%s'", - error_message.c_str(), json.c_str()); + json_result.error_message.c_str(), json.c_str()); return result; } - result = base::DictionaryValue::From(std::move(value)); + result = base::DictionaryValue::From( + std::make_unique<base::Value>(std::move(*json_result.value))); if (!result) { brillo::Error::AddToPrintf(error, FROM_HERE, brillo::errors::json::kDomain, brillo::errors::json::kObjectExpected, diff --git a/brillo/http/http_utils.h b/brillo/http/http_utils.h index 0d4d109..56789c0 100644 --- a/brillo/http/http_utils.h +++ b/brillo/http/http_utils.h @@ -304,6 +304,7 @@ BRILLO_EXPORT RequestID PatchJson( // Given an http::Response object, parse the body data into Json object. // Returns null if failed. Optional |error| can be passed in to // get the extended error information as to why the parse failed. +// TODO(crbug.com/1099111): change return type to base::Optional<base::Value> BRILLO_EXPORT std::unique_ptr<base::DictionaryValue> ParseJsonResponse( Response* response, int* status_code, diff --git a/brillo/value_conversion_test.cc b/brillo/value_conversion_test.cc index 2cf6d00..8e6b449 100644 --- a/brillo/value_conversion_test.cc +++ b/brillo/value_conversion_test.cc @@ -28,15 +28,13 @@ namespace brillo { namespace { -std::unique_ptr<base::Value> ParseValue(std::string json) { +base::Optional<base::Value> ParseValue(std::string json) { std::replace(json.begin(), json.end(), '\'', '"'); - std::string message; - // TODO(crbug.com/1054279): use base::JSONReader::ReadAndReturnValueWithError - // after uprev to r680000. - auto value = base::JSONReader::ReadAndReturnErrorDeprecated( - json, base::JSON_PARSE_RFC, nullptr, &message); - CHECK(value) << "Failed to load JSON: " << message << ", " << json; - return value; + auto result = + base::JSONReader::ReadAndReturnValueWithError(json, base::JSON_PARSE_RFC); + CHECK_EQ(result.error_code, base::JSONReader::JSON_NO_ERROR) + << "Failed to load JSON: " << result.error_message << ", " << json; + return std::move(result.value); } inline bool IsEqualValue(const base::Value& val1, const base::Value& val2) { diff --git a/policy/device_policy_impl.cc b/policy/device_policy_impl.cc index 7ef9c4c..7e8c40b 100644 --- a/policy/device_policy_impl.cc +++ b/policy/device_policy_impl.cc @@ -151,27 +151,21 @@ bool DecodeWeeklyTimeFromValue(const base::DictionaryValue& dict_value, return true; } -std::unique_ptr<base::ListValue> DecodeListValueFromJSON( +base::Optional<base::Value> DecodeListValueFromJSON( const std::string& json_string) { - std::string error; - // TODO(crbug.com/1054279): use base::JSONReader::ReadAndReturnValueWithError - // after uprev to r680000. - std::unique_ptr<base::Value> decoded_json = - base::JSONReader::ReadAndReturnErrorDeprecated( - json_string, base::JSON_ALLOW_TRAILING_COMMAS, nullptr, &error); - if (!decoded_json) { - LOG(ERROR) << "Invalid JSON string: " << error; - return nullptr; - } - - std::unique_ptr<base::ListValue> list_val = - base::ListValue::From(std::move(decoded_json)); - if (!list_val) { + auto decoded_json = base::JSONReader::ReadAndReturnValueWithError( + json_string, base::JSON_ALLOW_TRAILING_COMMAS); + if (decoded_json.error_code != base::JSONReader::JSON_NO_ERROR) { + LOG(ERROR) << "Invalid JSON string: " << decoded_json.error_message; + return base::nullopt; + } + + if (!decoded_json.value->is_list()) { LOG(ERROR) << "JSON string is not a list"; - return nullptr; + return base::nullopt; } - return list_val; + return std::move(decoded_json.value); } } // namespace @@ -557,12 +551,12 @@ bool DevicePolicyImpl::GetDeviceUpdateStagingSchedule( if (!proto.has_staging_schedule()) return false; - std::unique_ptr<base::ListValue> list_val = + base::Optional<base::Value> list_val = DecodeListValueFromJSON(proto.staging_schedule()); if (!list_val) return false; - for (const auto& pair_value : *list_val) { + for (const auto& pair_value : list_val->GetList()) { const base::DictionaryValue* day_percentage_pair; if (!pair_value.GetAsDictionary(&day_percentage_pair)) return false; @@ -651,12 +645,12 @@ bool DevicePolicyImpl::GetDisallowedTimeIntervals( return false; } - std::unique_ptr<base::ListValue> list_val = + base::Optional<base::Value> list_val = DecodeListValueFromJSON(proto.disallowed_time_intervals()); if (!list_val) return false; - for (const auto& interval_value : *list_val) { + for (const auto& interval_value : list_val->GetList()) { const base::DictionaryValue* interval_dict; if (!interval_value.GetAsDictionary(&interval_dict)) { LOG(ERROR) << "Invalid JSON string given. Interval is not a dict."; |