Skip to content

Commit

Permalink
Additional removal of JSONReader::ReadDeprecated from chrome/browser/ash
Browse files Browse the repository at this point in the history
This CL removes additional occurrences of JSONReader::ReadDeprecated,
as well as updates the use of base::Value around the code changed to
remove any use of deprecated methods/classes from base/values.h.

Bug: 1339314, 1187001
Change-Id: I8b8613357c11682ecd3658a97a6eed56c9857ca9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3739068
Reviewed-by: Alexander Alekseev <alemate@chromium.org>
Commit-Queue: Claudio DeSouza <cdesouza@igalia.com>
Cr-Commit-Position: refs/heads/main@{#1021600}
  • Loading branch information
cdesouza-chromium authored and Chromium LUCI CQ committed Jul 7, 2022
1 parent 56fbd26 commit 0afbe8e
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ void EnhancedNetworkTtsImpl::OnResponseJsonParsed(
const bool is_last_request,
data_decoder::DataDecoder::ValueOrError result) {
// Extract results for the request.
if (result.value) {
SendResponse(
UnpackJsonResponse(*result.value, start_index, is_last_request));
if (result.value && result.value->is_list()) {
SendResponse(UnpackJsonResponse(result.value->GetList(), start_index,
is_last_request));
// Only start the next request after finishing the current one. This method
// will also reset the internal state if there is no more request.
ProcessNextServerRequest();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ std::string CreateServerResponse(const std::vector<uint8_t>& expected_output) {
}

bool AreRequestsEqual(const std::string& json_a, const std::string& json_b) {
const std::unique_ptr<base::Value> dict_a =
base::JSONReader::ReadDeprecated(json_a);
const std::unique_ptr<base::Value> dict_b =
base::JSONReader::ReadDeprecated(json_b);
absl::optional<base::Value> parsed_a = base::JSONReader::Read(json_a);
absl::optional<base::Value> parsed_b = base::JSONReader::Read(json_b);
base::Value::Dict& dict_a = parsed_a->GetDict();
base::Value::Dict& dict_b = parsed_b->GetDict();

const absl::optional<double> rate_a =
dict_a->FindDoublePath(kSpeechFactorPath);
dict_a.FindDoubleByDottedPath(kSpeechFactorPath);
const absl::optional<double> rate_b =
dict_b->FindDoublePath(kSpeechFactorPath);
dict_b.FindDoubleByDottedPath(kSpeechFactorPath);
// Speech rates should have only one decimal digit.
if (!HasOneDecimalDigit(rate_a) || !HasOneDecimalDigit(rate_b))
return false;
Expand All @@ -114,9 +114,9 @@ bool AreRequestsEqual(const std::string& json_a, const std::string& json_b) {
return false;

// Compare the dicts without the speech rates.
dict_a->RemovePath(kSpeechFactorPath);
dict_b->RemovePath(kSpeechFactorPath);
return *dict_a == *dict_b;
dict_a.RemoveByDottedPath(kSpeechFactorPath);
dict_b.RemoveByDottedPath(kSpeechFactorPath);
return dict_a == dict_b;
}

} // namespace enhanced_network_tts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,9 @@ mojom::TtsResponsePtr GetResultOnError(
return mojom::TtsResponse::NewErrorCode(error_code);
}

mojom::TtsResponsePtr UnpackJsonResponse(const base::Value& json_data,
mojom::TtsResponsePtr UnpackJsonResponse(const base::Value::List& list_data,
const int start_index,
const bool is_last_request) {
base::Value::ConstListView list_data = json_data.GetListDeprecated();

// Depending on the size of input text (n), the list size should be 1 + 2n.
// The first item in the list is "metadata", then each input text has one
// dictionary for "text" and another dictionary for "audio". Since we only
Expand All @@ -216,29 +214,27 @@ mojom::TtsResponsePtr UnpackJsonResponse(const base::Value& json_data,
// ...
// ]
std::vector<mojom::TimingInfoPtr> timing_infos;
const base::Value& text_dict = list_data[1];
const base::Value* timing_info_ptr =
text_dict.FindListPath("text.timingInfo");
if (timing_info_ptr == nullptr) {
const base::Value::Dict& text_dict = list_data[1].GetDict();
const base::Value::List* timing_info_list =
text_dict.FindListByDottedPath("text.timingInfo");
if (timing_info_list == nullptr) {
DVLOG(1) << "HTTP response for Enhance Network TTS has unexpected timing "
"info data.";
return GetResultOnError(mojom::TtsRequestError::kReceivedUnexpectedData);
}

base::Value::ConstListView timing_info_list =
timing_info_ptr->GetListDeprecated();
for (size_t i = 0; i < timing_info_list.size(); ++i) {
const base::Value& timing_info = timing_info_list[i];
const std::string* timing_info_text_ptr = timing_info.FindStringKey("text");
for (size_t i = 0; i < timing_info_list->size(); ++i) {
const base::Value::Dict& timing_info = (*timing_info_list)[i].GetDict();
const std::string* timing_info_text_ptr = timing_info.FindString("text");
const std::string* timing_info_timeoffset_ptr =
timing_info.FindStringPath("location.timeLocation.timeOffset");
timing_info.FindStringByDottedPath("location.timeLocation.timeOffset");
const std::string* timing_info_duration_ptr =
timing_info.FindStringPath("location.timeLocation.duration");
timing_info.FindStringByDottedPath("location.timeLocation.duration");
// If the first item in the timing_info_list does not have a text offset,
// we default that to 0. If the first item starts with whitespaces, the
// server will send back the text offset for the item.
absl::optional<int> timing_info_text_offset =
timing_info.FindIntPath("location.textLocation.offset");
timing_info.FindIntByDottedPath("location.textLocation.offset");
if (timing_info_text_offset == absl::nullopt && i == 0) {
timing_info_text_offset = 0;
}
Expand All @@ -255,8 +251,9 @@ mojom::TtsResponsePtr UnpackJsonResponse(const base::Value& json_data,
}

// Decode audio data.
const base::Value& audio_dict = list_data[2];
const std::string* audio_bytes_ptr = audio_dict.FindStringPath("audio.bytes");
const base::Value::Dict& audio_dict = list_data[2].GetDict();
const std::string* audio_bytes_ptr =
audio_dict.FindStringByDottedPath("audio.bytes");
if (audio_bytes_ptr == nullptr) {
DVLOG(1) << "HTTP response for Enhance Network TTS has unexpected audio "
"bytes data.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ mojom::TtsResponsePtr GetResultOnError(const mojom::TtsRequestError error_code);
// Unpack the JSON audio data from the server response. The data corresponds to
// the text piece that has the |start_index| in the original input utterance.
// |last_request| indicates if this is the last JSON data we expect.
mojom::TtsResponsePtr UnpackJsonResponse(const base::Value& json_data,
mojom::TtsResponsePtr UnpackJsonResponse(const base::Value::List& list_data,
const int start_index,
const bool last_request);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,26 +146,25 @@ TEST_F(EnhancedNetworkTtsUtilsTest, GetResultOnError) {
TEST_F(EnhancedNetworkTtsUtilsTest, UnpackJsonResponseSucceed) {
const std::vector<uint8_t> response_data = {1, 2, 5};
const std::string server_response = CreateServerResponse(response_data);
const std::unique_ptr<base::Value> json =
base::JSONReader::ReadDeprecated(server_response);
absl::optional<base::Value> json = base::JSONReader::Read(server_response);

mojom::TtsResponsePtr result = UnpackJsonResponse(*json, 0 /* start_index */,
true /* is_last_request */);
mojom::TtsResponsePtr result = UnpackJsonResponse(
json->GetList(), 0 /* start_index */, true /* is_last_request */);

EXPECT_TRUE(result->is_data());
EXPECT_EQ(result->get_data()->audio, std::vector<uint8_t>({1, 2, 5}));
EXPECT_TRUE(result->get_data()->last_data);
EXPECT_EQ(result->get_data()->time_info[0]->text_offset, 0u);

result = UnpackJsonResponse(*json, 4 /* start_index */,
result = UnpackJsonResponse(json->GetList(), 4 /* start_index */,
true /* is_last_request */);

EXPECT_TRUE(result->is_data());
EXPECT_EQ(result->get_data()->audio, std::vector<uint8_t>({1, 2, 5}));
EXPECT_TRUE(result->get_data()->last_data);
EXPECT_EQ(result->get_data()->time_info[0]->text_offset, 4u);

result = UnpackJsonResponse(*json, 4 /* start_index */,
result = UnpackJsonResponse(json->GetList(), 4 /* start_index */,
false /* is_last_request */);

EXPECT_TRUE(result->is_data());
Expand All @@ -177,11 +176,10 @@ TEST_F(EnhancedNetworkTtsUtilsTest, UnpackJsonResponseSucceed) {
TEST_F(EnhancedNetworkTtsUtilsTest,
UnpackJsonResponseFailsWithWrongResponseFormat) {
const std::string encoded_response = "[{}, {}, {}]";
const std::unique_ptr<base::Value> json =
base::JSONReader::ReadDeprecated(encoded_response);
absl::optional<base::Value> json = base::JSONReader::Read(encoded_response);

mojom::TtsResponsePtr result = UnpackJsonResponse(*json, 0 /* start_index */,
true /* is_last_request */);
mojom::TtsResponsePtr result = UnpackJsonResponse(
json->GetList(), 0 /* start_index */, true /* is_last_request */);

EXPECT_TRUE(result->is_error_code());
EXPECT_EQ(result->get_error_code(),
Expand All @@ -195,11 +193,10 @@ TEST_F(EnhancedNetworkTtsUtilsTest,
const std::string encoded_data(response_data.begin(), response_data.end());
const std::string encoded_response =
base::StringPrintf(kTemplateResponse, encoded_data.c_str());
const std::unique_ptr<base::Value> json =
base::JSONReader::ReadDeprecated(encoded_response);
absl::optional<base::Value> json = base::JSONReader::Read(encoded_response);

mojom::TtsResponsePtr result = UnpackJsonResponse(*json, 0 /* start_index */,
true /* is_last_request */);
mojom::TtsResponsePtr result = UnpackJsonResponse(
json->GetList(), 0 /* start_index */, true /* is_last_request */);

EXPECT_TRUE(result->is_error_code());
EXPECT_EQ(result->get_error_code(),
Expand Down
Loading

0 comments on commit 0afbe8e

Please sign in to comment.