Commit 61709246 authored by thestig's avatar thestig Committed by Commit bot

Add base::Value::GetTypeName().

Review-Url: https://codereview.chromium.org/2151793002
Cr-Commit-Position: refs/heads/master@{#406161}
parent 6f6e8feb
...@@ -21,6 +21,11 @@ namespace base { ...@@ -21,6 +21,11 @@ namespace base {
namespace { namespace {
const char* const kTypeNames[] = {"null", "boolean", "integer", "double",
"string", "binary", "dictionary", "list"};
static_assert(arraysize(kTypeNames) == Value::TYPE_LIST + 1,
"kTypeNames Has Wrong Size");
std::unique_ptr<Value> CopyWithoutEmptyChildren(const Value& node); std::unique_ptr<Value> CopyWithoutEmptyChildren(const Value& node);
// Make a deep copy of |node|, but don't include empty lists or dictionaries // Make a deep copy of |node|, but don't include empty lists or dictionaries
...@@ -77,6 +82,13 @@ std::unique_ptr<Value> Value::CreateNullValue() { ...@@ -77,6 +82,13 @@ std::unique_ptr<Value> Value::CreateNullValue() {
return WrapUnique(new Value(TYPE_NULL)); return WrapUnique(new Value(TYPE_NULL));
} }
// static
const char* Value::GetTypeName(Value::Type type) {
DCHECK_GE(type, 0);
DCHECK_LT(static_cast<size_t>(type), arraysize(kTypeNames));
return kTypeNames[type];
}
bool Value::GetAsBinary(const BinaryValue** out_value) const { bool Value::GetAsBinary(const BinaryValue** out_value) const {
return false; return false;
} }
......
...@@ -65,6 +65,9 @@ class BASE_EXPORT Value { ...@@ -65,6 +65,9 @@ class BASE_EXPORT Value {
static std::unique_ptr<Value> CreateNullValue(); static std::unique_ptr<Value> CreateNullValue();
// Returns the name for a given |type|.
static const char* GetTypeName(Type type);
// Returns the type of the value stored by the current Value object. // Returns the type of the value stored by the current Value object.
// Each type will be implemented by only one subclass of Value, so it's // Each type will be implemented by only one subclass of Value, so it's
// safe to use the Type to determine whether you can cast from // safe to use the Type to determine whether you can cast from
......
...@@ -30,15 +30,6 @@ std::vector<T> toVector(T const (&array)[N]) { ...@@ -30,15 +30,6 @@ std::vector<T> toVector(T const (&array)[N]) {
return std::vector<T>(array, array + N); return std::vector<T>(array, array + N);
} }
// Copied from policy/configuration_policy_handler.cc.
// TODO(pneubeck): move to a common place like base/.
std::string ValueTypeToString(base::Value::Type type) {
const char* const strings[] = {"null", "boolean", "integer", "double",
"string", "binary", "dictionary", "list"};
CHECK(static_cast<size_t>(type) < arraysize(strings));
return strings[type];
}
} // namespace } // namespace
Validator::Validator(bool error_on_unknown_field, Validator::Validator(bool error_on_unknown_field,
...@@ -70,15 +61,8 @@ std::unique_ptr<base::DictionaryValue> Validator::ValidateAndRepairObject( ...@@ -70,15 +61,8 @@ std::unique_ptr<base::DictionaryValue> Validator::ValidateAndRepairObject(
*result = VALID_WITH_WARNINGS; *result = VALID_WITH_WARNINGS;
} }
// The return value should be NULL if, and only if, |result| equals INVALID. // The return value should be NULL if, and only if, |result| equals INVALID.
DCHECK_EQ(result_value.get() == NULL, *result == INVALID); DCHECK_EQ(!result_value, *result == INVALID);
return base::DictionaryValue::From(std::move(result_value));
base::DictionaryValue* result_dict = NULL;
if (result_value) {
result_value.release()->GetAsDictionary(&result_dict);
CHECK(result_dict);
}
return base::WrapUnique(result_dict);
} }
std::unique_ptr<base::Value> Validator::MapValue( std::unique_ptr<base::Value> Validator::MapValue(
...@@ -87,8 +71,9 @@ std::unique_ptr<base::Value> Validator::MapValue( ...@@ -87,8 +71,9 @@ std::unique_ptr<base::Value> Validator::MapValue(
bool* error) { bool* error) {
if (onc_value.GetType() != signature.onc_type) { if (onc_value.GetType() != signature.onc_type) {
LOG(ERROR) << MessageHeader() << "Found value '" << onc_value LOG(ERROR) << MessageHeader() << "Found value '" << onc_value
<< "' of type '" << ValueTypeToString(onc_value.GetType()) << "' of type '" << base::Value::GetTypeName(onc_value.GetType())
<< "', but type '" << ValueTypeToString(signature.onc_type) << "', but type '"
<< base::Value::GetTypeName(signature.onc_type)
<< "' is required."; << "' is required.";
error_or_warning_found_ = *error = true; error_or_warning_found_ = *error = true;
return std::unique_ptr<base::Value>(); return std::unique_ptr<base::Value>();
...@@ -146,13 +131,12 @@ std::unique_ptr<base::DictionaryValue> Validator::MapObject( ...@@ -146,13 +131,12 @@ std::unique_ptr<base::DictionaryValue> Validator::MapObject(
} }
} }
if (valid) { if (valid)
return repaired; return repaired;
} else {
DCHECK(error_or_warning_found_); DCHECK(error_or_warning_found_);
error_or_warning_found_ = *error = true; error_or_warning_found_ = *error = true;
return std::unique_ptr<base::DictionaryValue>(); return std::unique_ptr<base::DictionaryValue>();
}
} }
std::unique_ptr<base::Value> Validator::MapField( std::unique_ptr<base::Value> Validator::MapField(
...@@ -288,10 +272,10 @@ bool Validator::ValidateRecommendedField( ...@@ -288,10 +272,10 @@ bool Validator::ValidateRecommendedField(
if (error_on_wrong_recommended_) { if (error_on_wrong_recommended_) {
LOG(ERROR) << message; LOG(ERROR) << message;
return false; return false;
} else {
LOG(WARNING) << message;
continue;
} }
LOG(WARNING) << message;
continue;
} }
repaired_recommended->AppendString(field_name); repaired_recommended->AppendString(field_name);
...@@ -446,12 +430,11 @@ bool Validator::ValidateSSIDAndHexSSID(base::DictionaryValue* object) { ...@@ -446,12 +430,11 @@ bool Validator::ValidateSSIDAndHexSSID(base::DictionaryValue* object) {
// If the HexSSID field is present, ignore errors in SSID because these // If the HexSSID field is present, ignore errors in SSID because these
// might be caused by the usage of a non-UTF-8 encoding when the SSID // might be caused by the usage of a non-UTF-8 encoding when the SSID
// field was automatically added (see FillInHexSSIDField). // field was automatically added (see FillInHexSSIDField).
if (object->HasKey(::onc::wifi::kHexSSID)) { if (!object->HasKey(::onc::wifi::kHexSSID)) {
LOG(WARNING) << msg;
} else {
LOG(ERROR) << msg; LOG(ERROR) << msg;
return false; return false;
} }
LOG(WARNING) << msg;
} }
// Check HexSSID validity. // Check HexSSID validity.
......
...@@ -30,18 +30,7 @@ namespace policy { ...@@ -30,18 +30,7 @@ namespace policy {
// static // static
std::string ConfigurationPolicyHandler::ValueTypeToString( std::string ConfigurationPolicyHandler::ValueTypeToString(
base::Value::Type type) { base::Value::Type type) {
static const char* strings[] = { return std::string(base::Value::GetTypeName(type));
"null",
"boolean",
"integer",
"double",
"string",
"binary",
"dictionary",
"list"
};
CHECK(static_cast<size_t>(type) < arraysize(strings));
return std::string(strings[type]);
} }
ConfigurationPolicyHandler::ConfigurationPolicyHandler() { ConfigurationPolicyHandler::ConfigurationPolicyHandler() {
......
...@@ -36,6 +36,7 @@ struct POLICY_EXPORT PolicyToPreferenceMapEntry { ...@@ -36,6 +36,7 @@ struct POLICY_EXPORT PolicyToPreferenceMapEntry {
// their corresponding preferences, and to check whether the policies are valid. // their corresponding preferences, and to check whether the policies are valid.
class POLICY_EXPORT ConfigurationPolicyHandler { class POLICY_EXPORT ConfigurationPolicyHandler {
public: public:
// TODO(thestig): Replace with base::Value::GetTypeName().
static std::string ValueTypeToString(base::Value::Type type); static std::string ValueTypeToString(base::Value::Type type);
ConfigurationPolicyHandler(); ConfigurationPolicyHandler();
......
...@@ -7,49 +7,33 @@ ...@@ -7,49 +7,33 @@
#include <stddef.h> #include <stddef.h>
#include "base/macros.h" #include "base/macros.h"
#include "base/strings/string_piece.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "extensions/common/manifest_constants.h" #include "extensions/common/manifest_constants.h"
using base::UTF8ToUTF16;
using base::StringPrintf;
namespace extensions { namespace extensions {
namespace { namespace {
const char* ValueTypeToString(const base::Value* value) {
const base::Value::Type type = value->GetType();
static const char* strings[] = {"null",
"boolean",
"integer",
"double",
"string",
"binary",
"dictionary",
"list"};
CHECK(static_cast<size_t>(type) < arraysize(strings));
return strings[type];
}
class ErrorBuilder { class ErrorBuilder {
public: public:
explicit ErrorBuilder(base::string16* error) : error_(error) {} explicit ErrorBuilder(base::string16* error) : error_(error) {}
// Appends a literal string |error|. // Appends a literal string |error|.
void Append(const char* error) { void Append(base::StringPiece error) {
if (error_->length()) if (!error_->empty())
error_->append(UTF8ToUTF16("; ")); error_->append(base::ASCIIToUTF16("; "));
error_->append(UTF8ToUTF16(error)); error_->append(base::UTF8ToUTF16(error));
} }
// Appends a string |error| with the first %s replaced by |sub|. // Appends a string |error| with the first %s replaced by |sub|.
void Append(const char* error, const char* sub) { void Append(base::StringPiece error, base::StringPiece sub) {
Append(base::StringPrintf(error, sub).c_str()); Append(base::StringPrintf(error.data(), sub.data()));
} }
private: private:
base::string16* error_; base::string16* const error_;
DISALLOW_COPY_AND_ASSIGN(ErrorBuilder); DISALLOW_COPY_AND_ASSIGN(ErrorBuilder);
}; };
...@@ -61,12 +45,12 @@ class ErrorBuilder { ...@@ -61,12 +45,12 @@ class ErrorBuilder {
bool ConvertManifestRule(const linked_ptr<DeclarativeManifestData::Rule>& rule, bool ConvertManifestRule(const linked_ptr<DeclarativeManifestData::Rule>& rule,
ErrorBuilder* error_builder) { ErrorBuilder* error_builder) {
auto convert_list = auto convert_list =
[error_builder](std::vector<std::unique_ptr<base::Value>>& list) { [error_builder](const std::vector<std::unique_ptr<base::Value>>& list) {
for (const std::unique_ptr<base::Value>& value : list) { for (const std::unique_ptr<base::Value>& value : list) {
base::DictionaryValue* dictionary = nullptr; base::DictionaryValue* dictionary = nullptr;
if (!value->GetAsDictionary(&dictionary)) { if (!value->GetAsDictionary(&dictionary)) {
error_builder->Append("expected dictionary, got %s", error_builder->Append("expected dictionary, got %s",
ValueTypeToString(value.get())); base::Value::GetTypeName(value->GetType()));
return false; return false;
} }
std::string type; std::string type;
...@@ -139,19 +123,15 @@ std::unique_ptr<DeclarativeManifestData> DeclarativeManifestData::FromValue( ...@@ -139,19 +123,15 @@ std::unique_ptr<DeclarativeManifestData> DeclarativeManifestData::FromValue(
const base::ListValue* list = nullptr; const base::ListValue* list = nullptr;
if (!value.GetAsList(&list)) { if (!value.GetAsList(&list)) {
error_builder.Append("'event_rules' expected list, got %s", error_builder.Append("'event_rules' expected list, got %s",
ValueTypeToString(&value)); base::Value::GetTypeName(value.GetType()));
return std::unique_ptr<DeclarativeManifestData>(); return std::unique_ptr<DeclarativeManifestData>();
} }
for (size_t i = 0; i < list->GetSize(); ++i) { for (const auto& element : *list) {
const base::DictionaryValue* dict = nullptr; const base::DictionaryValue* dict = nullptr;
if (!list->GetDictionary(i, &dict)) { if (!element->GetAsDictionary(&dict)) {
const base::Value* value = nullptr; error_builder.Append("expected dictionary, got %s",
if (list->Get(i, &value)) base::Value::GetTypeName(element->GetType()));
error_builder.Append("expected dictionary, got %s",
ValueTypeToString(value));
else
error_builder.Append("expected dictionary");
return std::unique_ptr<DeclarativeManifestData>(); return std::unique_ptr<DeclarativeManifestData>();
} }
std::string event; std::string event;
......
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