diff options
-rw-r--r-- | base/values.cc | 22 | ||||
-rw-r--r-- | base/values.h | 9 | ||||
-rw-r--r-- | base/values_unittest.cc | 58 | ||||
-rw-r--r-- | chrome/browser/ui/webui/options/website_settings_handler.cc | 31 |
4 files changed, 80 insertions, 40 deletions
diff --git a/base/values.cc b/base/values.cc index 5d45ec3..b478b62 100644 --- a/base/values.cc +++ b/base/values.cc @@ -371,7 +371,7 @@ void DictionaryValue::Clear() { dictionary_.clear(); } -void DictionaryValue::Set(const std::string& path, Value* in_value) { +void DictionaryValue::Set(const std::string& path, scoped_ptr<Value> in_value) { DCHECK(IsStringUTF8(path)); DCHECK(in_value); @@ -392,7 +392,11 @@ void DictionaryValue::Set(const std::string& path, Value* in_value) { current_path.erase(0, delimiter_position + 1); } - current_dictionary->SetWithoutPathExpansion(current_path, in_value); + current_dictionary->SetWithoutPathExpansion(current_path, in_value.Pass()); +} + +void DictionaryValue::Set(const std::string& path, Value* in_value) { + Set(path, make_scoped_ptr(in_value)); } void DictionaryValue::SetBoolean(const std::string& path, bool in_value) { @@ -418,18 +422,24 @@ void DictionaryValue::SetString(const std::string& path, } void DictionaryValue::SetWithoutPathExpansion(const std::string& key, - Value* in_value) { + scoped_ptr<Value> in_value) { + Value* bare_ptr = in_value.release(); // If there's an existing value here, we need to delete it, because // we own all our children. std::pair<ValueMap::iterator, bool> ins_res = - dictionary_.insert(std::make_pair(key, in_value)); + dictionary_.insert(std::make_pair(key, bare_ptr)); if (!ins_res.second) { - DCHECK_NE(ins_res.first->second, in_value); // This would be bogus + DCHECK_NE(ins_res.first->second, bare_ptr); // This would be bogus delete ins_res.first->second; - ins_res.first->second = in_value; + ins_res.first->second = bare_ptr; } } +void DictionaryValue::SetWithoutPathExpansion(const std::string& key, + Value* in_value) { + SetWithoutPathExpansion(key, make_scoped_ptr(in_value)); +} + void DictionaryValue::SetBooleanWithoutPathExpansion( const std::string& path, bool in_value) { SetWithoutPathExpansion(path, new FundamentalValue(in_value)); diff --git a/base/values.h b/base/values.h index 04b2d26..4c22f3d 100644 --- a/base/values.h +++ b/base/values.h @@ -228,9 +228,9 @@ class BASE_EXPORT DictionaryValue : public Value { // within a key, but there are no other restrictions on keys. // 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. - // Note that the dictionary takes ownership of the value referenced by - // |in_value|, and therefore |in_value| must be non-NULL. + // to the path in that location. |in_value| must be non-null. + void Set(const std::string& path, scoped_ptr<Value> in_value); + // Deprecated version of the above. TODO(estade): remove. void Set(const std::string& path, Value* in_value); // Convenience forms of Set(). These methods will replace any existing @@ -243,6 +243,9 @@ class BASE_EXPORT DictionaryValue : public Value { // Like Set(), but without special treatment of '.'. This allows e.g. URLs to // be used as paths. + void SetWithoutPathExpansion(const std::string& key, + scoped_ptr<Value> in_value); + // Deprecated version of the above. TODO(estade): remove. void SetWithoutPathExpansion(const std::string& key, Value* in_value); // Convenience forms of SetWithoutPathExpansion(). diff --git a/base/values_unittest.cc b/base/values_unittest.cc index cbb07f3..296880a 100644 --- a/base/values_unittest.cc +++ b/base/values_unittest.cc @@ -312,6 +312,31 @@ TEST(ValuesTest, DictionaryRemoval) { TEST(ValuesTest, DictionaryWithoutPathExpansion) { DictionaryValue dict; + dict.Set("this.is.expanded", make_scoped_ptr(Value::CreateNullValue())); + dict.SetWithoutPathExpansion("this.isnt.expanded", + make_scoped_ptr(Value::CreateNullValue())); + + EXPECT_FALSE(dict.HasKey("this.is.expanded")); + EXPECT_TRUE(dict.HasKey("this")); + Value* value1; + EXPECT_TRUE(dict.Get("this", &value1)); + DictionaryValue* value2; + ASSERT_TRUE(dict.GetDictionaryWithoutPathExpansion("this", &value2)); + EXPECT_EQ(value1, value2); + EXPECT_EQ(1U, value2->size()); + + EXPECT_TRUE(dict.HasKey("this.isnt.expanded")); + Value* value3; + EXPECT_FALSE(dict.Get("this.isnt.expanded", &value3)); + Value* value4; + ASSERT_TRUE(dict.GetWithoutPathExpansion("this.isnt.expanded", &value4)); + EXPECT_EQ(Value::TYPE_NULL, value4->GetType()); +} + +// Tests the deprecated version of SetWithoutPathExpansion. +// TODO(estade): remove. +TEST(ValuesTest, DictionaryWithoutPathExpansionDeprecated) { + DictionaryValue dict; dict.Set("this.is.expanded", Value::CreateNullValue()); dict.SetWithoutPathExpansion("this.isnt.expanded", Value::CreateNullValue()); @@ -334,8 +359,8 @@ TEST(ValuesTest, DictionaryWithoutPathExpansion) { TEST(ValuesTest, DictionaryRemovePath) { DictionaryValue dict; - dict.Set("a.long.way.down", new FundamentalValue(1)); - dict.Set("a.long.key.path", new FundamentalValue(true)); + dict.SetInteger("a.long.way.down", 1); + dict.SetBoolean("a.long.key.path", true); scoped_ptr<Value> removed_item; EXPECT_TRUE(dict.RemovePath("a.long.way.down", &removed_item)); @@ -360,17 +385,17 @@ TEST(ValuesTest, DictionaryRemovePath) { TEST(ValuesTest, DeepCopy) { DictionaryValue original_dict; Value* original_null = Value::CreateNullValue(); - original_dict.Set("null", original_null); + original_dict.Set("null", make_scoped_ptr(original_null)); FundamentalValue* original_bool = new FundamentalValue(true); - original_dict.Set("bool", original_bool); + original_dict.Set("bool", make_scoped_ptr(original_bool)); FundamentalValue* original_int = new FundamentalValue(42); - original_dict.Set("int", original_int); + original_dict.Set("int", make_scoped_ptr(original_int)); FundamentalValue* original_double = new FundamentalValue(3.14); - original_dict.Set("double", original_double); + original_dict.Set("double", make_scoped_ptr(original_double)); StringValue* original_string = new StringValue("hello"); - original_dict.Set("string", original_string); + original_dict.Set("string", make_scoped_ptr(original_string)); StringValue* original_string16 = new StringValue(ASCIIToUTF16("hello16")); - original_dict.Set("string16", original_string16); + original_dict.Set("string16", make_scoped_ptr(original_string16)); scoped_ptr<char[]> original_buffer(new char[42]); memset(original_buffer.get(), '!', 42); @@ -382,11 +407,11 @@ TEST(ValuesTest, DeepCopy) { original_list->Append(original_list_element_0); FundamentalValue* original_list_element_1 = new FundamentalValue(1); original_list->Append(original_list_element_1); - original_dict.Set("list", original_list); + original_dict.Set("list", make_scoped_ptr(original_list)); DictionaryValue* original_nested_dictionary = new DictionaryValue(); - original_nested_dictionary->Set("key", new StringValue("value")); - original_dict.Set("dictionary", original_nested_dictionary); + original_nested_dictionary->SetString("key", "value"); + original_dict.Set("dictionary", make_scoped_ptr(original_nested_dictionary)); scoped_ptr<DictionaryValue> copy_dict(original_dict.DeepCopy()); ASSERT_TRUE(copy_dict.get()); @@ -515,7 +540,7 @@ TEST(ValuesTest, Equals) { dv.SetDouble("c", 2.5); dv.SetString("d1", "string"); dv.SetString("d2", ASCIIToUTF16("http://google.com")); - dv.Set("e", Value::CreateNullValue()); + dv.Set("e", make_scoped_ptr(Value::CreateNullValue())); scoped_ptr<DictionaryValue> copy; copy.reset(dv.DeepCopy()); @@ -524,7 +549,7 @@ TEST(ValuesTest, Equals) { ListValue* list = new ListValue; list->Append(Value::CreateNullValue()); list->Append(new DictionaryValue); - dv.Set("f", list); + dv.Set("f", make_scoped_ptr(list)); EXPECT_FALSE(dv.Equals(copy.get())); copy->Set("f", list->DeepCopy()); @@ -622,9 +647,10 @@ TEST(ValuesTest, DeepCopyCovariantReturnTypes) { TEST(ValuesTest, RemoveEmptyChildren) { scoped_ptr<DictionaryValue> root(new DictionaryValue); // Remove empty lists and dictionaries. - root->Set("empty_dict", new DictionaryValue); - root->Set("empty_list", new ListValue); - root->SetWithoutPathExpansion("a.b.c.d.e", new DictionaryValue); + root->Set("empty_dict", make_scoped_ptr(new DictionaryValue)); + root->Set("empty_list", make_scoped_ptr(new ListValue)); + root->SetWithoutPathExpansion("a.b.c.d.e", + make_scoped_ptr(new DictionaryValue)); root.reset(root->DeepCopyWithoutEmptyChildren()); EXPECT_TRUE(root->empty()); diff --git a/chrome/browser/ui/webui/options/website_settings_handler.cc b/chrome/browser/ui/webui/options/website_settings_handler.cc index 033f21c..da57296 100644 --- a/chrome/browser/ui/webui/options/website_settings_handler.cc +++ b/chrome/browser/ui/webui/options/website_settings_handler.cc @@ -382,7 +382,7 @@ void WebsiteSettingsHandler::UpdateOrigins() { base::Time last_usage = settings->GetLastUsageByPattern( it->primary_pattern, it->secondary_pattern, last_setting); - base::DictionaryValue* origin_entry = new base::DictionaryValue(); + scoped_ptr<base::DictionaryValue> origin_entry(new base::DictionaryValue()); origin_entry->SetDoubleWithoutPathExpansion("usage", last_usage.ToDoubleT()); base::string16 usage_string; @@ -396,9 +396,9 @@ void WebsiteSettingsHandler::UpdateOrigins() { GetReadableName(origin_url)); if (it->setting == CONTENT_SETTING_BLOCK) - blocked_origins.SetWithoutPathExpansion(origin, origin_entry); + blocked_origins.SetWithoutPathExpansion(origin, origin_entry.Pass()); else - allowed_origins.SetWithoutPathExpansion(origin, origin_entry); + allowed_origins.SetWithoutPathExpansion(origin, origin_entry.Pass()); } bool is_globally_allowed = settings->GetContentSettingOverride(last_setting); @@ -631,7 +631,7 @@ void WebsiteSettingsHandler::GetInfoForOrigin(const GURL& site_url, ContentSettingsType permission_type = kValidTypes[i]; // Append the possible settings. - base::ListValue* options = new base::ListValue; + scoped_ptr<base::ListValue> options(new base::ListValue()); ContentSetting default_value = map->GetDefaultContentSetting(permission_type, NULL); if (default_value != CONTENT_SETTING_ALLOW && @@ -675,14 +675,15 @@ void WebsiteSettingsHandler::GetInfoForOrigin(const GURL& site_url, permission = content_settings::ValueToContentSetting(v.get()); } - base::DictionaryValue* permission_info = new base::DictionaryValue; + scoped_ptr<base::DictionaryValue> + permission_info(new base::DictionaryValue()); permission_info->SetStringWithoutPathExpansion( "setting", content_settings::ContentSettingToString(permission)); - permission_info->SetWithoutPathExpansion("options", options); + permission_info->SetWithoutPathExpansion("options", options.Pass()); permission_info->SetBooleanWithoutPathExpansion( "editable", info.source == content_settings::SETTING_SOURCE_USER); permissions->SetWithoutPathExpansion( - content_settings::GetTypeName(permission_type), permission_info); + content_settings::GetTypeName(permission_type), permission_info.Pass()); } base::Value* storage_used = new base::StringValue(l10n_util::GetStringFUTF16( @@ -708,14 +709,14 @@ void WebsiteSettingsHandler::UpdateLocalStorage() { if (origin.find(last_filter_) == base::string16::npos) continue; - base::DictionaryValue* origin_entry = new base::DictionaryValue(); - origin_entry->SetWithoutPathExpansion( - "usage", new base::FundamentalValue(static_cast<double>(it->size))); - origin_entry->SetWithoutPathExpansion( - "usageString", new base::StringValue(ui::FormatBytes(it->size))); + scoped_ptr<base::DictionaryValue> origin_entry(new base::DictionaryValue()); + origin_entry->SetDoubleWithoutPathExpansion( + "usage", static_cast<double>(it->size)); + origin_entry->SetStringWithoutPathExpansion( + "usageString", ui::FormatBytes(it->size)); origin_entry->SetStringWithoutPathExpansion( "readableName", GetReadableName(it->origin_url)); - local_storage_map.SetWithoutPathExpansion(origin, origin_entry); + local_storage_map.SetWithoutPathExpansion(origin, origin_entry.Pass()); } web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins", local_storage_map); @@ -734,7 +735,7 @@ void WebsiteSettingsHandler::UpdateBatteryUsage() { if (origin.find(last_filter_) == base::string16::npos) continue; - base::DictionaryValue* origin_entry = new base::DictionaryValue(); + scoped_ptr<base::DictionaryValue> origin_entry(new base::DictionaryValue()); origin_entry->SetInteger("usage", it->second); if (it->second == 0) { origin_entry->SetString( @@ -748,7 +749,7 @@ void WebsiteSettingsHandler::UpdateBatteryUsage() { } origin_entry->SetStringWithoutPathExpansion("readableName", GetReadableName(it->first)); - power_map.SetWithoutPathExpansion(origin, origin_entry); + power_map.SetWithoutPathExpansion(origin, origin_entry.Pass()); } web_ui()->CallJavascriptFunction("WebsiteSettingsManager.populateOrigins", power_map); |