Commit 49b99c4e authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Avoid some base::JSONReader instantiations.

base::JSONReader::Read() is a static method, so there is no need to
instantiate a base::JSONReader object. While making this change, also
stop using ReadDeprecated(), and make the code lint error free.

Change-Id: I4ba79d61af6c736e762cb926a04e28ef13fb63fe
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1476413Reviewed-by: default avatarEric Seckler <eseckler@chromium.org>
Reviewed-by: default avatarDavid Benjamin <davidben@chromium.org>
Reviewed-by: default avatarMax Moroz <mmoroz@chromium.org>
Reviewed-by: default avatarIstiaque Ahmed <lazyboy@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
Cr-Commit-Position: refs/heads/master@{#723439}
parent cf117092
...@@ -108,14 +108,12 @@ class NodeList : public std::vector<std::unique_ptr<Node>> { ...@@ -108,14 +108,12 @@ class NodeList : public std::vector<std::unique_ptr<Node>> {
static std::unique_ptr<NodeList> ParseJsonString(const uint8_t* data, static std::unique_ptr<NodeList> ParseJsonString(const uint8_t* data,
size_t size) { size_t size) {
std::unique_ptr<NodeList> nodes(new NodeList()); auto nodes = std::make_unique<NodeList>();
base::JSONReader reader; base::Optional<base::Value> value(base::JSONReader::Read(
std::unique_ptr<base::Value> value(reader.ReadDeprecated(
std::string(reinterpret_cast<const char*>(data), size))); std::string(reinterpret_cast<const char*>(data), size)));
if (value) { if (value)
nodes->ParseJson(*value); nodes->ParseJson(*value);
}
return nodes; return nodes;
} }
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "extensions/browser/value_store/leveldb_scoped_database.h" #include "extensions/browser/value_store/leveldb_scoped_database.h"
#include <utility>
#include "base/json/json_reader.h" #include "base/json/json_reader.h"
#include "base/json/json_writer.h" #include "base/json/json_writer.h"
#include "third_party/leveldatabase/src/include/leveldb/write_batch.h" #include "third_party/leveldatabase/src/include/leveldb/write_batch.h"
......
...@@ -5,7 +5,9 @@ ...@@ -5,7 +5,9 @@
#ifndef EXTENSIONS_BROWSER_VALUE_STORE_LEVELDB_SCOPED_DATABASE_H_ #ifndef EXTENSIONS_BROWSER_VALUE_STORE_LEVELDB_SCOPED_DATABASE_H_
#define EXTENSIONS_BROWSER_VALUE_STORE_LEVELDB_SCOPED_DATABASE_H_ #define EXTENSIONS_BROWSER_VALUE_STORE_LEVELDB_SCOPED_DATABASE_H_
#include <memory>
#include <string> #include <string>
#include <vector>
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
......
...@@ -20,18 +20,11 @@ namespace net { ...@@ -20,18 +20,11 @@ namespace net {
namespace ct { namespace ct {
namespace {
std::unique_ptr<base::Value> ParseJson(const std::string& json) {
base::JSONReader json_reader;
return json_reader.ReadDeprecated(json);
}
}
TEST(CTLogResponseParserTest, ParsesValidJsonSTH) { TEST(CTLogResponseParserTest, ParsesValidJsonSTH) {
std::unique_ptr<base::Value> sample_sth_json = base::Optional<base::Value> sample_sth_json =
ParseJson(GetSampleSTHAsJson()); base::JSONReader::Read(GetSampleSTHAsJson());
SignedTreeHead tree_head; SignedTreeHead tree_head;
EXPECT_TRUE(FillSignedTreeHead(*sample_sth_json.get(), &tree_head)); EXPECT_TRUE(FillSignedTreeHead(*sample_sth_json, &tree_head));
SignedTreeHead sample_sth; SignedTreeHead sample_sth;
ASSERT_TRUE(GetSampleSignedTreeHead(&sample_sth)); ASSERT_TRUE(GetSampleSignedTreeHead(&sample_sth));
...@@ -59,17 +52,17 @@ TEST(CTLogResponseParserTest, ParsesValidJsonSTH) { ...@@ -59,17 +52,17 @@ TEST(CTLogResponseParserTest, ParsesValidJsonSTH) {
} }
TEST(CTLogResponseParserTest, FailsToParseMissingFields) { TEST(CTLogResponseParserTest, FailsToParseMissingFields) {
std::unique_ptr<base::Value> missing_signature_sth = ParseJson( base::Optional<base::Value> missing_signature_sth = base::JSONReader::Read(
CreateSignedTreeHeadJsonString(1 /* tree_size */, 123456u /* timestamp */, CreateSignedTreeHeadJsonString(1 /* tree_size */, 123456u /* timestamp */,
GetSampleSTHSHA256RootHash(), "")); GetSampleSTHSHA256RootHash(), ""));
SignedTreeHead tree_head; SignedTreeHead tree_head;
ASSERT_FALSE(FillSignedTreeHead(*missing_signature_sth.get(), &tree_head)); ASSERT_FALSE(FillSignedTreeHead(*missing_signature_sth, &tree_head));
std::unique_ptr<base::Value> missing_root_hash_sth = ParseJson( base::Optional<base::Value> missing_root_hash_sth = base::JSONReader::Read(
CreateSignedTreeHeadJsonString(1 /* tree_size */, 123456u /* timestamp */, CreateSignedTreeHeadJsonString(1 /* tree_size */, 123456u /* timestamp */,
"", GetSampleSTHTreeHeadSignature())); "", GetSampleSTHTreeHeadSignature()));
ASSERT_FALSE(FillSignedTreeHead(*missing_root_hash_sth.get(), &tree_head)); ASSERT_FALSE(FillSignedTreeHead(*missing_root_hash_sth, &tree_head));
} }
TEST(CTLogResponseParserTest, FailsToParseIncorrectLengthRootHash) { TEST(CTLogResponseParserTest, FailsToParseIncorrectLengthRootHash) {
...@@ -79,32 +72,32 @@ TEST(CTLogResponseParserTest, FailsToParseIncorrectLengthRootHash) { ...@@ -79,32 +72,32 @@ TEST(CTLogResponseParserTest, FailsToParseIncorrectLengthRootHash) {
base::Base64Decode( base::Base64Decode(
base::StringPiece("/WHFMgXtI/umKKuACJIN0Bb73TcILm9WkeU6qszvoArK\n"), base::StringPiece("/WHFMgXtI/umKKuACJIN0Bb73TcILm9WkeU6qszvoArK\n"),
&too_long_hash); &too_long_hash);
std::unique_ptr<base::Value> too_long_hash_json = base::Optional<base::Value> too_long_hash_json =
ParseJson(CreateSignedTreeHeadJsonString( base::JSONReader::Read(CreateSignedTreeHeadJsonString(
1 /* tree_size */, 123456u /* timestamp */, 1 /* tree_size */, 123456u /* timestamp */,
GetSampleSTHSHA256RootHash(), too_long_hash)); GetSampleSTHSHA256RootHash(), too_long_hash));
ASSERT_FALSE(FillSignedTreeHead(*too_long_hash_json.get(), &tree_head)); ASSERT_FALSE(FillSignedTreeHead(*too_long_hash_json, &tree_head));
std::string too_short_hash; std::string too_short_hash;
base::Base64Decode( base::Base64Decode(
base::StringPiece("/WHFMgXtI/umKKuACJIN0Bb73TcILm9WkeU6qszvoA==\n"), base::StringPiece("/WHFMgXtI/umKKuACJIN0Bb73TcILm9WkeU6qszvoA==\n"),
&too_short_hash); &too_short_hash);
std::unique_ptr<base::Value> too_short_hash_json = base::Optional<base::Value> too_short_hash_json =
ParseJson(CreateSignedTreeHeadJsonString( base::JSONReader::Read(CreateSignedTreeHeadJsonString(
1 /* tree_size */, 123456u /* timestamp */, 1 /* tree_size */, 123456u /* timestamp */,
GetSampleSTHSHA256RootHash(), too_short_hash)); GetSampleSTHSHA256RootHash(), too_short_hash));
ASSERT_FALSE(FillSignedTreeHead(*too_short_hash_json.get(), &tree_head)); ASSERT_FALSE(FillSignedTreeHead(*too_short_hash_json, &tree_head));
} }
TEST(CTLogResponseParserTest, ParsesJsonSTHWithLargeTimestamp) { TEST(CTLogResponseParserTest, ParsesJsonSTHWithLargeTimestamp) {
SignedTreeHead tree_head; SignedTreeHead tree_head;
std::unique_ptr<base::Value> large_timestamp_json = base::Optional<base::Value> large_timestamp_json =
ParseJson(CreateSignedTreeHeadJsonString( base::JSONReader::Read(CreateSignedTreeHeadJsonString(
100, INT64_C(1) << 34, GetSampleSTHSHA256RootHash(), 100, INT64_C(1) << 34, GetSampleSTHSHA256RootHash(),
GetSampleSTHTreeHeadSignature())); GetSampleSTHTreeHeadSignature()));
ASSERT_TRUE(FillSignedTreeHead(*large_timestamp_json.get(), &tree_head)); ASSERT_TRUE(FillSignedTreeHead(*large_timestamp_json, &tree_head));
base::Time expected_time = base::Time expected_time =
base::Time::UnixEpoch() + base::Time::UnixEpoch() +
...@@ -122,12 +115,12 @@ TEST(CTLogResponseParserTest, ParsesConsistencyProofSuccessfully) { ...@@ -122,12 +115,12 @@ TEST(CTLogResponseParserTest, ParsesConsistencyProofSuccessfully) {
raw_nodes.push_back(first); raw_nodes.push_back(first);
raw_nodes.push_back(second); raw_nodes.push_back(second);
raw_nodes.push_back(third); raw_nodes.push_back(third);
std::unique_ptr<base::Value> sample_consistency_proof = base::Optional<base::Value> sample_consistency_proof =
ParseJson(CreateConsistencyProofJsonString(raw_nodes)); base::JSONReader::Read(CreateConsistencyProofJsonString(raw_nodes));
std::vector<std::string> output; std::vector<std::string> output;
ASSERT_TRUE(FillConsistencyProof(*sample_consistency_proof.get(), &output)); ASSERT_TRUE(FillConsistencyProof(*sample_consistency_proof, &output));
EXPECT_EQ(output[0], first); EXPECT_EQ(output[0], first);
EXPECT_EQ(output[1], second); EXPECT_EQ(output[1], second);
...@@ -137,28 +130,29 @@ TEST(CTLogResponseParserTest, ParsesConsistencyProofSuccessfully) { ...@@ -137,28 +130,29 @@ TEST(CTLogResponseParserTest, ParsesConsistencyProofSuccessfully) {
TEST(CTLogResponseParserTest, FailsOnInvalidProofJson) { TEST(CTLogResponseParserTest, FailsOnInvalidProofJson) {
std::vector<std::string> output; std::vector<std::string> output;
std::unique_ptr<base::Value> badly_encoded = base::Optional<base::Value> badly_encoded =
ParseJson(std::string("{\"consistency\": [\"notbase64\"]}")); base::JSONReader::Read(std::string("{\"consistency\": [\"notbase64\"]}"));
EXPECT_FALSE(FillConsistencyProof(*badly_encoded.get(), &output)); EXPECT_FALSE(FillConsistencyProof(*badly_encoded, &output));
std::unique_ptr<base::Value> not_a_string = base::Optional<base::Value> not_a_string =
ParseJson(std::string("{\"consistency\": [42, 16]}")); base::JSONReader::Read(std::string("{\"consistency\": [42, 16]}"));
EXPECT_FALSE(FillConsistencyProof(*badly_encoded.get(), &output)); EXPECT_FALSE(FillConsistencyProof(*badly_encoded, &output));
std::unique_ptr<base::Value> missing_consistency = base::Optional<base::Value> missing_consistency =
ParseJson(std::string("{}")); base::JSONReader::Read(std::string("{}"));
EXPECT_FALSE(FillConsistencyProof(*missing_consistency.get(), &output)); EXPECT_FALSE(FillConsistencyProof(*missing_consistency, &output));
std::unique_ptr<base::Value> not_a_dict = ParseJson(std::string("[]")); base::Optional<base::Value> not_a_dict =
EXPECT_FALSE(FillConsistencyProof(*not_a_dict.get(), &output)); base::JSONReader::Read(std::string("[]"));
EXPECT_FALSE(FillConsistencyProof(*not_a_dict, &output));
} }
TEST(CTLogResponseParserTest, ParsesProofJsonWithExtraFields) { TEST(CTLogResponseParserTest, ParsesProofJsonWithExtraFields) {
std::vector<std::string> output; std::vector<std::string> output;
std::unique_ptr<base::Value> badly_encoded = base::Optional<base::Value> badly_encoded = base::JSONReader::Read(
ParseJson(std::string("{\"consistency\": [], \"somethingelse\": 3}")); std::string("{\"consistency\": [], \"somethingelse\": 3}"));
EXPECT_TRUE(FillConsistencyProof(*badly_encoded.get(), &output)); EXPECT_TRUE(FillConsistencyProof(*badly_encoded, &output));
} }
} // namespace ct } // namespace ct
......
...@@ -5,6 +5,9 @@ ...@@ -5,6 +5,9 @@
#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_INSTRUMENTATION_TRACING_TRACED_VALUE_H_ #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_INSTRUMENTATION_TRACING_TRACED_VALUE_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_INSTRUMENTATION_TRACING_TRACED_VALUE_H_ #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_INSTRUMENTATION_TRACING_TRACED_VALUE_H_
#include <memory>
#include <string>
#include "base/macros.h" #include "base/macros.h"
#include "base/trace_event/traced_value.h" #include "base/trace_event/traced_value.h"
#include "third_party/blink/renderer/platform/platform_export.h" #include "third_party/blink/renderer/platform/platform_export.h"
......
...@@ -4,17 +4,17 @@ ...@@ -4,17 +4,17 @@
#include "third_party/blink/renderer/platform/instrumentation/tracing/traced_value.h" #include "third_party/blink/renderer/platform/instrumentation/tracing/traced_value.h"
#include <utility>
#include "base/json/json_reader.h" #include "base/json/json_reader.h"
#include "base/values.h" #include "base/values.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include <memory>
namespace blink { namespace blink {
std::unique_ptr<base::Value> ParseTracedValue( base::Optional<base::Value> ParseTracedValue(
std::unique_ptr<TracedValue> value) { std::unique_ptr<TracedValue> value) {
base::JSONReader reader; return base::JSONReader::Read(value->ToString().Utf8());
return reader.ReadDeprecated(value->ToString().Utf8());
} }
TEST(TracedValueTest, FlatDictionary) { TEST(TracedValueTest, FlatDictionary) {
...@@ -24,7 +24,7 @@ TEST(TracedValueTest, FlatDictionary) { ...@@ -24,7 +24,7 @@ TEST(TracedValueTest, FlatDictionary) {
value->SetBooleanWithCopiedName("bool", true); value->SetBooleanWithCopiedName("bool", true);
value->SetStringWithCopiedName("string", "string"); value->SetStringWithCopiedName("string", "string");
std::unique_ptr<base::Value> parsed = ParseTracedValue(std::move(value)); base::Optional<base::Value> parsed = ParseTracedValue(std::move(value));
base::DictionaryValue* dictionary; base::DictionaryValue* dictionary;
ASSERT_TRUE(parsed->GetAsDictionary(&dictionary)); ASSERT_TRUE(parsed->GetAsDictionary(&dictionary));
int int_value; int int_value;
...@@ -59,7 +59,7 @@ TEST(TracedValueTest, Hierarchy) { ...@@ -59,7 +59,7 @@ TEST(TracedValueTest, Hierarchy) {
value->EndArray(); value->EndArray();
value->SetStringWithCopiedName("s0", "foo"); value->SetStringWithCopiedName("s0", "foo");
std::unique_ptr<base::Value> parsed = ParseTracedValue(std::move(value)); base::Optional<base::Value> parsed = ParseTracedValue(std::move(value));
base::DictionaryValue* dictionary; base::DictionaryValue* dictionary;
ASSERT_TRUE(parsed->GetAsDictionary(&dictionary)); ASSERT_TRUE(parsed->GetAsDictionary(&dictionary));
int i0; int i0;
...@@ -106,7 +106,7 @@ TEST(TracedValueTest, Escape) { ...@@ -106,7 +106,7 @@ TEST(TracedValueTest, Escape) {
value->SetStringWithCopiedName("s3\\", "value3"); value->SetStringWithCopiedName("s3\\", "value3");
value->SetStringWithCopiedName("\"s4\"", "value4"); value->SetStringWithCopiedName("\"s4\"", "value4");
std::unique_ptr<base::Value> parsed = ParseTracedValue(std::move(value)); base::Optional<base::Value> parsed = ParseTracedValue(std::move(value));
base::DictionaryValue* dictionary; base::DictionaryValue* dictionary;
ASSERT_TRUE(parsed->GetAsDictionary(&dictionary)); ASSERT_TRUE(parsed->GetAsDictionary(&dictionary));
std::string s0; std::string s0;
...@@ -142,7 +142,7 @@ TEST(TracedValueTest, NonCopiedNames) { ...@@ -142,7 +142,7 @@ TEST(TracedValueTest, NonCopiedNames) {
value->PushInteger(2); value->PushInteger(2);
value->EndArray(); value->EndArray();
std::unique_ptr<base::Value> parsed = ParseTracedValue(std::move(value)); base::Optional<base::Value> parsed = ParseTracedValue(std::move(value));
base::DictionaryValue* dictionary; base::DictionaryValue* dictionary;
ASSERT_TRUE(parsed->GetAsDictionary(&dictionary)); ASSERT_TRUE(parsed->GetAsDictionary(&dictionary));
int int_value; int int_value;
......
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