Commit 16d6f53c authored by dcheng's avatar dcheng Committed by Commit bot

Use StringPiece more in base::Value interfaces.

Many of these functions are called with string literals.
This leads to two problems:
- Implicit construction of std::string generates a
  surprising amount of code, since STL code is often
  inlined.
- We construct a temporary string, immediately copy it,
  and then throw away the original temporary.

Effect of this CL on an official Linux binary:
 before: 100.41 MiB
  after: 99.56 MiB

BUG=none

Review-Url: https://codereview.chromium.org/2278723003
Cr-Commit-Position: refs/heads/master@{#414450}
parent b8df2228
......@@ -61,7 +61,7 @@ class DictionaryHiddenRootValue : public DictionaryValue {
// Not overriding DictionaryValue::Remove because it just calls through to
// the method below.
bool RemoveWithoutPathExpansion(const std::string& key,
bool RemoveWithoutPathExpansion(StringPiece key,
std::unique_ptr<Value>* out) override {
// If the caller won't take ownership of the removed value, just call up.
if (!out)
......
This diff is collapsed.
......@@ -149,7 +149,7 @@ class BASE_EXPORT FundamentalValue : public Value {
class BASE_EXPORT StringValue : public Value {
public:
// Initializes a StringValue with a UTF-8 narrow character string.
explicit StringValue(const std::string& in_value);
explicit StringValue(StringPiece in_value);
// Initializes a StringValue with a string16.
explicit StringValue(const string16& in_value);
......@@ -223,7 +223,7 @@ class BASE_EXPORT DictionaryValue : public Value {
bool GetAsDictionary(const DictionaryValue** out_value) const override;
// Returns true if the current dictionary has a value for the given key.
bool HasKey(const std::string& key) const;
bool HasKey(StringPiece key) const;
// Returns the number of Values in this dictionary.
size_t size() const { return dictionary_.size(); }
......@@ -241,32 +241,31 @@ class BASE_EXPORT DictionaryValue : public Value {
// If the key at any step of the way doesn't exist, or exists but isn't
// a DictionaryValue, a new DictionaryValue will be created and attached
// to the path in that location. |in_value| must be non-null.
void Set(const std::string& path, std::unique_ptr<Value> in_value);
void Set(StringPiece path, std::unique_ptr<Value> in_value);
// Deprecated version of the above. TODO(estade): remove.
void Set(const std::string& path, Value* in_value);
void Set(StringPiece path, Value* in_value);
// Convenience forms of Set(). These methods will replace any existing
// value at that path, even if it has a different type.
void SetBoolean(const std::string& path, bool in_value);
void SetInteger(const std::string& path, int in_value);
void SetDouble(const std::string& path, double in_value);
void SetString(const std::string& path, const std::string& in_value);
void SetString(const std::string& path, const string16& in_value);
void SetBoolean(StringPiece path, bool in_value);
void SetInteger(StringPiece path, int in_value);
void SetDouble(StringPiece path, double in_value);
void SetString(StringPiece path, StringPiece in_value);
void SetString(StringPiece path, const string16& in_value);
// Like Set(), but without special treatment of '.'. This allows e.g. URLs to
// be used as paths.
void SetWithoutPathExpansion(const std::string& key,
void SetWithoutPathExpansion(StringPiece key,
std::unique_ptr<Value> in_value);
// Deprecated version of the above. TODO(estade): remove.
void SetWithoutPathExpansion(const std::string& key, Value* in_value);
void SetWithoutPathExpansion(StringPiece key, Value* in_value);
// Convenience forms of SetWithoutPathExpansion().
void SetBooleanWithoutPathExpansion(const std::string& path, bool in_value);
void SetIntegerWithoutPathExpansion(const std::string& path, int in_value);
void SetDoubleWithoutPathExpansion(const std::string& path, double in_value);
void SetStringWithoutPathExpansion(const std::string& path,
const std::string& in_value);
void SetStringWithoutPathExpansion(const std::string& path,
void SetBooleanWithoutPathExpansion(StringPiece path, bool in_value);
void SetIntegerWithoutPathExpansion(StringPiece path, int in_value);
void SetDoubleWithoutPathExpansion(StringPiece path, double in_value);
void SetStringWithoutPathExpansion(StringPiece path, StringPiece in_value);
void SetStringWithoutPathExpansion(StringPiece path,
const string16& in_value);
// Gets the Value associated with the given path starting from this object.
......@@ -284,46 +283,41 @@ class BASE_EXPORT DictionaryValue : public Value {
// and the return value will be true if the path is valid and the value at
// the end of the path can be returned in the form specified.
// |out_value| is optional and will only be set if non-NULL.
bool GetBoolean(const std::string& path, bool* out_value) const;
bool GetInteger(const std::string& path, int* out_value) const;
bool GetBoolean(StringPiece path, bool* out_value) const;
bool GetInteger(StringPiece path, int* out_value) const;
// Values of both type TYPE_INTEGER and TYPE_DOUBLE can be obtained as
// doubles.
bool GetDouble(const std::string& path, double* out_value) const;
bool GetString(const std::string& path, std::string* out_value) const;
bool GetString(const std::string& path, string16* out_value) const;
bool GetStringASCII(const std::string& path, std::string* out_value) const;
bool GetBinary(const std::string& path, const BinaryValue** out_value) const;
bool GetBinary(const std::string& path, BinaryValue** out_value);
bool GetDouble(StringPiece path, double* out_value) const;
bool GetString(StringPiece path, std::string* out_value) const;
bool GetString(StringPiece path, string16* out_value) const;
bool GetStringASCII(StringPiece path, std::string* out_value) const;
bool GetBinary(StringPiece path, const BinaryValue** out_value) const;
bool GetBinary(StringPiece path, BinaryValue** out_value);
bool GetDictionary(StringPiece path,
const DictionaryValue** out_value) const;
bool GetDictionary(StringPiece path, DictionaryValue** out_value);
bool GetList(const std::string& path, const ListValue** out_value) const;
bool GetList(const std::string& path, ListValue** out_value);
bool GetList(StringPiece path, const ListValue** out_value) const;
bool GetList(StringPiece path, ListValue** out_value);
// Like Get(), but without special treatment of '.'. This allows e.g. URLs to
// be used as paths.
bool GetWithoutPathExpansion(const std::string& key,
const Value** out_value) const;
bool GetWithoutPathExpansion(const std::string& key, Value** out_value);
bool GetBooleanWithoutPathExpansion(const std::string& key,
bool* out_value) const;
bool GetIntegerWithoutPathExpansion(const std::string& key,
int* out_value) const;
bool GetDoubleWithoutPathExpansion(const std::string& key,
double* out_value) const;
bool GetStringWithoutPathExpansion(const std::string& key,
bool GetWithoutPathExpansion(StringPiece key, const Value** out_value) const;
bool GetWithoutPathExpansion(StringPiece key, Value** out_value);
bool GetBooleanWithoutPathExpansion(StringPiece key, bool* out_value) const;
bool GetIntegerWithoutPathExpansion(StringPiece key, int* out_value) const;
bool GetDoubleWithoutPathExpansion(StringPiece key, double* out_value) const;
bool GetStringWithoutPathExpansion(StringPiece key,
std::string* out_value) const;
bool GetStringWithoutPathExpansion(const std::string& key,
bool GetStringWithoutPathExpansion(StringPiece key,
string16* out_value) const;
bool GetDictionaryWithoutPathExpansion(
const std::string& key,
StringPiece key,
const DictionaryValue** out_value) const;
bool GetDictionaryWithoutPathExpansion(const std::string& key,
bool GetDictionaryWithoutPathExpansion(StringPiece key,
DictionaryValue** out_value);
bool GetListWithoutPathExpansion(const std::string& key,
bool GetListWithoutPathExpansion(StringPiece key,
const ListValue** out_value) const;
bool GetListWithoutPathExpansion(const std::string& key,
ListValue** out_value);
bool GetListWithoutPathExpansion(StringPiece key, ListValue** out_value);
// Removes the Value with the specified path from this dictionary (or one
// of its child dictionaries, if the path is more than just a local key).
......@@ -331,18 +325,16 @@ class BASE_EXPORT DictionaryValue : public Value {
// |out_value|. If |out_value| is NULL, the removed value will be deleted.
// This method returns true if |path| is a valid path; otherwise it will
// return false and the DictionaryValue object will be unchanged.
virtual bool Remove(const std::string& path,
std::unique_ptr<Value>* out_value);
virtual bool Remove(StringPiece path, std::unique_ptr<Value>* out_value);
// Like Remove(), but without special treatment of '.'. This allows e.g. URLs
// to be used as paths.
virtual bool RemoveWithoutPathExpansion(const std::string& key,
virtual bool RemoveWithoutPathExpansion(StringPiece key,
std::unique_ptr<Value>* out_value);
// Removes a path, clearing out all dictionaries on |path| that remain empty
// after removing the value at |path|.
virtual bool RemovePath(const std::string& path,
std::unique_ptr<Value>* out_value);
virtual bool RemovePath(StringPiece path, std::unique_ptr<Value>* out_value);
// Makes a copy of |this| but doesn't include empty dictionaries and lists in
// the copy. This never returns NULL, even if |this| itself is empty.
......@@ -472,7 +464,7 @@ class BASE_EXPORT ListValue : public Value {
void AppendBoolean(bool in_value);
void AppendInteger(int in_value);
void AppendDouble(double in_value);
void AppendString(const std::string& in_value);
void AppendString(StringPiece in_value);
void AppendString(const string16& in_value);
void AppendStrings(const std::vector<std::string>& in_values);
void AppendStrings(const std::vector<string16>& in_values);
......
......@@ -171,7 +171,7 @@ void PluginsPageHandler::SetPluginAlwaysAllowed(const mojo::String& plugin,
// whitelisted by the user from automatically whitelisted ones.
DictionaryPrefUpdate update(profile->GetPrefs(),
prefs::kContentSettingsPluginWhitelist);
update->SetBoolean(plugin, allowed);
update->SetBoolean(plugin.get(), allowed);
}
void PluginsPageHandler::GetPluginsData(
......
......@@ -450,7 +450,8 @@ void ArcNetHostImpl::CreateNetwork(mojom::WifiConfigurationPtr cfg,
properties->SetStringWithoutPathExpansion(onc::network_config::kType,
onc::network_config::kWiFi);
wifi_dict->SetStringWithoutPathExpansion(onc::wifi::kHexSSID, cfg->hexssid);
wifi_dict->SetStringWithoutPathExpansion(onc::wifi::kHexSSID,
cfg->hexssid.get());
wifi_dict->SetBooleanWithoutPathExpansion(onc::wifi::kAutoConnect,
details->autoconnect);
if (cfg->security.get().empty()) {
......@@ -458,10 +459,10 @@ void ArcNetHostImpl::CreateNetwork(mojom::WifiConfigurationPtr cfg,
onc::wifi::kSecurityNone);
} else {
wifi_dict->SetStringWithoutPathExpansion(onc::wifi::kSecurity,
cfg->security);
cfg->security.get());
if (!details->passphrase.is_null()) {
wifi_dict->SetStringWithoutPathExpansion(onc::wifi::kPassphrase,
details->passphrase);
details->passphrase.get());
}
}
properties->SetWithoutPathExpansion(onc::network_config::kWiFi,
......
......@@ -13,6 +13,7 @@
#include "base/bind.h"
#include "base/json/json_reader.h"
#include "base/strings/string_piece.h"
#include "components/domain_reliability/beacon.h"
#include "components/domain_reliability/dispatcher.h"
#include "components/domain_reliability/scheduler.h"
......@@ -60,8 +61,8 @@ std::unique_ptr<DomainReliabilityBeacon> MakeBeacon(MockableTime* time) {
}
template <typename ValueType,
bool (DictionaryValue::* GetValueType)(const std::string&,
ValueType*) const>
bool (DictionaryValue::*GetValueType)(base::StringPiece, ValueType*)
const>
struct HasValue {
bool operator()(const DictionaryValue& dict,
const std::string& key,
......
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