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