summaryrefslogtreecommitdiffstats
path: root/chrome/common
diff options
context:
space:
mode:
authoraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-29 19:59:08 +0000
committeraa@chromium.org <aa@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2008-12-29 19:59:08 +0000
commitb4cebf87816cde49f6d4991ffa254e5ead97703b (patch)
treefb12e4b464a43af846f30bbd0dbe9484150373b0 /chrome/common
parent45ce59f19161b517c04a4e3bcd0890b938382b06 (diff)
downloadchromium_src-b4cebf87816cde49f6d4991ffa254e5ead97703b.zip
chromium_src-b4cebf87816cde49f6d4991ffa254e5ead97703b.tar.gz
chromium_src-b4cebf87816cde49f6d4991ffa254e5ead97703b.tar.bz2
Change the signature of JSONReader::Read() and related
methods to be more friendly to use with scoped_ptr. Change all the callsites. Review URL: http://codereview.chromium.org/16270 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@7486 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common')
-rw-r--r--chrome/common/json_value_serializer.cc11
-rw-r--r--chrome/common/json_value_serializer.h16
-rw-r--r--chrome/common/json_value_serializer_perftest.cc9
-rw-r--r--chrome/common/json_value_serializer_unittest.cc96
-rw-r--r--chrome/common/pref_service.cc42
-rw-r--r--chrome/common/pref_service_uitest.cc8
-rw-r--r--chrome/common/pref_service_unittest.cc6
7 files changed, 82 insertions, 106 deletions
diff --git a/chrome/common/json_value_serializer.cc b/chrome/common/json_value_serializer.cc
index 82938ec..54afe7a 100644
--- a/chrome/common/json_value_serializer.cc
+++ b/chrome/common/json_value_serializer.cc
@@ -21,13 +21,11 @@ bool JSONStringValueSerializer::Serialize(const Value& root) {
return true;
}
-bool JSONStringValueSerializer::Deserialize(Value** root,
- std::string* error_message) {
+Value* JSONStringValueSerializer::Deserialize(std::string* error_message) {
if (!json_string_)
return false;
- return JSONReader::ReadAndReturnError(*json_string_, root,
- allow_trailing_comma_,
+ return JSONReader::ReadAndReturnError(*json_string_, allow_trailing_comma_,
error_message);
}
@@ -50,13 +48,12 @@ bool JSONFileValueSerializer::Serialize(const Value& root) {
return true;
}
-bool JSONFileValueSerializer::Deserialize(Value** root,
- std::string* error_message) {
+Value* JSONFileValueSerializer::Deserialize(std::string* error_message) {
std::string json_string;
if (!file_util::ReadFileToString(json_file_path_, &json_string)) {
return false;
}
JSONStringValueSerializer serializer(json_string);
- return serializer.Deserialize(root, error_message);
+ return serializer.Deserialize(error_message);
}
diff --git a/chrome/common/json_value_serializer.h b/chrome/common/json_value_serializer.h
index 2e581ee..7a51528 100644
--- a/chrome/common/json_value_serializer.h
+++ b/chrome/common/json_value_serializer.h
@@ -39,12 +39,9 @@ class JSONStringValueSerializer : public ValueSerializer {
// Attempt to deserialize the data structure encoded in the string passed
// in to the constructor into a structure of Value objects. If the return
- // value is true, the |root| parameter will be set to point to a new Value
- // object that corresponds to the values represented in the string. The
- // caller takes ownership of the returned Value objects. Otherwise, the root
- // value will be changed and if |error_message| is non-null, it will contain
+ // value is NULL and |error_message| is non-null, |error-message| will contain
// a string describing the error.
- bool Deserialize(Value** root, std::string* error_message);
+ Value* Deserialize(std::string* error_message);
void set_pretty_print(bool new_value) { pretty_print_ = new_value; }
bool pretty_print() { return pretty_print_; }
@@ -86,12 +83,9 @@ class JSONFileValueSerializer : public ValueSerializer {
// Attempt to deserialize the data structure encoded in the file passed
// in to the constructor into a structure of Value objects. If the return
- // value is true, the |root| parameter will be set to point to a new Value
- // object that corresponds to the values represented in the file. The
- // caller takes ownership of the returned Value objects. Otherwise, the root
- // value will be changed and if |error_message| is non-null, it will contain
- // a string describing the error.
- bool Deserialize(Value** root, std::string* error_message);
+ // value is NULL, and if |error_message| is non-null, |error_message| will
+ // contain a string describing the error.
+ Value* Deserialize(std::string* error_message);
private:
std::wstring json_file_path_;
diff --git a/chrome/common/json_value_serializer_perftest.cc b/chrome/common/json_value_serializer_perftest.cc
index 6570d34..bef8fbe 100644
--- a/chrome/common/json_value_serializer_perftest.cc
+++ b/chrome/common/json_value_serializer_perftest.cc
@@ -52,10 +52,9 @@ TEST_F(JSONValueSerializerTests, Reading) {
PerfTimeLogger chrome_timer("chrome");
for (int i = 0; i < kIterations; ++i) {
for (size_t j = 0; j < test_cases_.size(); ++j) {
- Value* root = NULL;
JSONStringValueSerializer reader(test_cases_[j]);
- ASSERT_TRUE(reader.Deserialize(&root, NULL));
- delete root;
+ scoped_ptr<Value> root(reader.Deserialize(NULL));
+ ASSERT_TRUE(root.get());
}
}
chrome_timer.Done();
@@ -67,9 +66,9 @@ TEST_F(JSONValueSerializerTests, CompactWriting) {
// Convert test cases to Value objects.
std::vector<Value*> test_cases;
for (size_t i = 0; i < test_cases_.size(); ++i) {
- Value* root = NULL;
JSONStringValueSerializer reader(test_cases_[i]);
- ASSERT_TRUE(reader.Deserialize(&root, NULL));
+ Value* root = reader.Deserialize(NULL);
+ ASSERT_TRUE(root);
test_cases.push_back(root);
}
diff --git a/chrome/common/json_value_serializer_unittest.cc b/chrome/common/json_value_serializer_unittest.cc
index 3dd175b..30f6320 100644
--- a/chrome/common/json_value_serializer_unittest.cc
+++ b/chrome/common/json_value_serializer_unittest.cc
@@ -16,13 +16,12 @@
TEST(JSONValueSerializerTest, Roundtrip) {
const std::string original_serialization =
"{\"bool\":true,\"int\":42,\"list\":[1,2],\"null\":null,\"real\":3.14}";
- Value* root = NULL;
JSONStringValueSerializer serializer(original_serialization);
- ASSERT_TRUE(serializer.Deserialize(&root, NULL));
- ASSERT_TRUE(root);
+ scoped_ptr<Value> root(serializer.Deserialize(NULL));
+ ASSERT_TRUE(root.get());
ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
- DictionaryValue* root_dict = static_cast<DictionaryValue*>(root);
+ DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get());
Value* null_value = NULL;
ASSERT_TRUE(root_dict->Get(L"null", &null_value));
@@ -61,8 +60,6 @@ TEST(JSONValueSerializerTest, Roundtrip) {
" \"real\": 3.14\r\n"
"}\r\n";
ASSERT_EQ(pretty_serialization, test_serialization);
-
- delete root;
}
TEST(JSONValueSerializerTest, StringEscape) {
@@ -119,14 +116,14 @@ TEST(JSONValueSerializerTest, UnicodeStrings) {
ASSERT_EQ(expected, actual);
// escaped ascii text -> json
- Value* deserial_root = NULL;
JSONStringValueSerializer deserializer(expected);
- ASSERT_TRUE(deserializer.Deserialize(&deserial_root, NULL));
- DictionaryValue* dict_root = static_cast<DictionaryValue*>(deserial_root);
+ scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL));
+ ASSERT_TRUE(deserial_root.get());
+ DictionaryValue* dict_root =
+ static_cast<DictionaryValue*>(deserial_root.get());
std::wstring web_value;
ASSERT_TRUE(dict_root->GetString(L"web", &web_value));
ASSERT_EQ(test, web_value);
- delete deserial_root;
}
TEST(JSONValueSerializerTest, HexStrings) {
@@ -143,57 +140,53 @@ TEST(JSONValueSerializerTest, HexStrings) {
ASSERT_EQ(expected, actual);
// escaped ascii text -> json
- Value* deserial_root = NULL;
JSONStringValueSerializer deserializer(expected);
- ASSERT_TRUE(deserializer.Deserialize(&deserial_root, NULL));
- DictionaryValue* dict_root = static_cast<DictionaryValue*>(deserial_root);
+ scoped_ptr<Value> deserial_root(deserializer.Deserialize(NULL));
+ ASSERT_TRUE(deserial_root.get());
+ DictionaryValue* dict_root =
+ static_cast<DictionaryValue*>(deserial_root.get());
std::wstring test_value;
ASSERT_TRUE(dict_root->GetString(L"test", &test_value));
ASSERT_EQ(test, test_value);
- delete deserial_root;
// Test converting escaped regular chars
- deserial_root = NULL;
std::string escaped_chars = "{\"test\":\"\\x67\\x6f\"}";
JSONStringValueSerializer deserializer2(escaped_chars);
- ASSERT_TRUE(deserializer2.Deserialize(&deserial_root, NULL));
- dict_root = static_cast<DictionaryValue*>(deserial_root);
+ deserial_root.reset(deserializer2.Deserialize(NULL));
+ ASSERT_TRUE(deserial_root.get());
+ dict_root = static_cast<DictionaryValue*>(deserial_root.get());
ASSERT_TRUE(dict_root->GetString(L"test", &test_value));
ASSERT_EQ(std::wstring(L"go"), test_value);
- delete deserial_root;
}
TEST(JSONValueSerializerTest, AllowTrailingComma) {
- Value* root = NULL;
- Value* root_expected = NULL;
+ scoped_ptr<Value> root;
+ scoped_ptr<Value> root_expected;
std::string test_with_commas("{\"key\": [true,],}");
std::string test_no_commas("{\"key\": [true]}");
JSONStringValueSerializer serializer(test_with_commas);
serializer.set_allow_trailing_comma(true);
JSONStringValueSerializer serializer_expected(test_no_commas);
- ASSERT_TRUE(serializer.Deserialize(&root, NULL));
- ASSERT_TRUE(serializer_expected.Deserialize(&root_expected, NULL));
- ASSERT_TRUE(root->Equals(root_expected));
-
- delete root;
- delete root_expected;
+ root.reset(serializer.Deserialize(NULL));
+ ASSERT_TRUE(root.get());
+ root_expected.reset(serializer_expected.Deserialize(NULL));
+ ASSERT_TRUE(root_expected.get());
+ ASSERT_TRUE(root->Equals(root_expected.get()));
}
namespace {
void ValidateJsonList(const std::string& json) {
- Value* root = NULL;
- ASSERT_TRUE(JSONReader::Read(json, &root, false));
- ASSERT_TRUE(root && root->IsType(Value::TYPE_LIST));
- ListValue* list = static_cast<ListValue*>(root);
+ scoped_ptr<Value> root(JSONReader::Read(json, false));
+ ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST));
+ ListValue* list = static_cast<ListValue*>(root.get());
ASSERT_EQ(1U, list->GetSize());
Value* elt = NULL;
ASSERT_TRUE(list->Get(0, &elt));
int value = 0;
ASSERT_TRUE(elt && elt->GetAsInteger(&value));
ASSERT_EQ(1, value);
- delete root;
}
} // namespace
@@ -206,25 +199,26 @@ TEST(JSONValueSerializerTest, JSONReaderComments) {
ValidateJsonList("[ 1 /* one */ ] /* end */");
ValidateJsonList("[ 1 //// ,2\r\n ]");
- Value* root = NULL;
+ scoped_ptr<Value> root;
+
// It's ok to have a comment in a string.
- ASSERT_TRUE(JSONReader::Read("[\"// ok\\n /* foo */ \"]", &root, false));
- ASSERT_TRUE(root && root->IsType(Value::TYPE_LIST));
- ListValue* list = static_cast<ListValue*>(root);
+ root.reset(JSONReader::Read("[\"// ok\\n /* foo */ \"]", false));
+ ASSERT_TRUE(root.get() && root->IsType(Value::TYPE_LIST));
+ ListValue* list = static_cast<ListValue*>(root.get());
ASSERT_EQ(1U, list->GetSize());
Value* elt = NULL;
ASSERT_TRUE(list->Get(0, &elt));
std::wstring value;
ASSERT_TRUE(elt && elt->GetAsString(&value));
ASSERT_EQ(L"// ok\n /* foo */ ", value);
- delete root;
- root = NULL;
// You can't nest comments.
- ASSERT_FALSE(JSONReader::Read("/* /* inner */ outer */ [ 1 ]", &root, false));
+ root.reset(JSONReader::Read("/* /* inner */ outer */ [ 1 ]", false));
+ ASSERT_FALSE(root.get());
// Not a open comment token.
- ASSERT_FALSE(JSONReader::Read("/ * * / [1]", &root, false));
+ root.reset(JSONReader::Read("/ * * / [1]", false));
+ ASSERT_FALSE(root.get());
}
namespace {
@@ -261,13 +255,13 @@ TEST_F(JSONFileValueSerializerTest, Roundtrip) {
ASSERT_TRUE(file_util::PathExists(original_file_path));
JSONFileValueSerializer deserializer(original_file_path);
- Value* root;
- ASSERT_TRUE(deserializer.Deserialize(&root, NULL));
+ scoped_ptr<Value> root;
+ root.reset(deserializer.Deserialize(NULL));
- ASSERT_TRUE(root);
+ ASSERT_TRUE(root.get());
ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
- DictionaryValue* root_dict = static_cast<DictionaryValue*>(root);
+ DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get());
Value* null_value = NULL;
ASSERT_TRUE(root_dict->Get(L"null", &null_value));
@@ -298,8 +292,6 @@ TEST_F(JSONFileValueSerializerTest, Roundtrip) {
// Now compare file contents.
EXPECT_TRUE(file_util::ContentsEqual(original_file_path, written_file_path));
EXPECT_TRUE(file_util::Delete(written_file_path, false));
-
- delete root;
}
TEST_F(JSONFileValueSerializerTest, RoundtripNested) {
@@ -311,8 +303,9 @@ TEST_F(JSONFileValueSerializerTest, RoundtripNested) {
ASSERT_TRUE(file_util::PathExists(original_file_path));
JSONFileValueSerializer deserializer(original_file_path);
- Value* root;
- ASSERT_TRUE(deserializer.Deserialize(&root, NULL));
+ scoped_ptr<Value> root;
+ root.reset(deserializer.Deserialize(NULL));
+ ASSERT_TRUE(root.get());
// Now try writing.
std::wstring written_file_path = test_dir_;
@@ -326,8 +319,6 @@ TEST_F(JSONFileValueSerializerTest, RoundtripNested) {
// Now compare file contents.
EXPECT_TRUE(file_util::ContentsEqual(original_file_path, written_file_path));
EXPECT_TRUE(file_util::Delete(written_file_path, false));
-
- delete root;
}
TEST_F(JSONFileValueSerializerTest, NoWhitespace) {
@@ -337,9 +328,8 @@ TEST_F(JSONFileValueSerializerTest, NoWhitespace) {
L"serializer_test_nowhitespace.js");
ASSERT_TRUE(file_util::PathExists(source_file_path));
JSONFileValueSerializer serializer(source_file_path);
- Value* root;
- ASSERT_TRUE(serializer.Deserialize(&root, NULL));
- ASSERT_TRUE(root);
- delete root;
+ scoped_ptr<Value> root;
+ root.reset(serializer.Deserialize(NULL));
+ ASSERT_TRUE(root.get());
}
#endif // defined(OS_WIN)
diff --git a/chrome/common/pref_service.cc b/chrome/common/pref_service.cc
index 8f3322a..870e79e 100644
--- a/chrome/common/pref_service.cc
+++ b/chrome/common/pref_service.cc
@@ -136,38 +136,34 @@ bool PrefService::LoadPersistentPrefs(const std::wstring& file_path) {
DCHECK(CalledOnValidThread());
JSONFileValueSerializer serializer(file_path);
- Value* root = NULL;
- if (serializer.Deserialize(&root, NULL)) {
- // Preferences should always have a dictionary root.
- if (!root->IsType(Value::TYPE_DICTIONARY)) {
- delete root;
- return false;
- }
+ scoped_ptr<Value> root(serializer.Deserialize(NULL));
+ if (!root.get())
+ return false;
- persistent_.reset(static_cast<DictionaryValue*>(root));
- return true;
- }
+ // Preferences should always have a dictionary root.
+ if (!root->IsType(Value::TYPE_DICTIONARY))
+ return false;
- return false;
+ persistent_.reset(static_cast<DictionaryValue*>(root.release()));
+ return true;
}
void PrefService::ReloadPersistentPrefs() {
DCHECK(CalledOnValidThread());
JSONFileValueSerializer serializer(pref_filename_);
- Value* root;
- if (serializer.Deserialize(&root, NULL)) {
- // Preferences should always have a dictionary root.
- if (!root->IsType(Value::TYPE_DICTIONARY)) {
- delete root;
- return;
- }
+ scoped_ptr<Value> root(serializer.Deserialize(NULL));
+ if (!root.get())
+ return;
- persistent_.reset(static_cast<DictionaryValue*>(root));
- for (PreferenceSet::iterator it = prefs_.begin();
- it != prefs_.end(); ++it) {
- (*it)->root_pref_ = persistent_.get();
- }
+ // Preferences should always have a dictionary root.
+ if (!root->IsType(Value::TYPE_DICTIONARY))
+ return;
+
+ persistent_.reset(static_cast<DictionaryValue*>(root.release()));
+ for (PreferenceSet::iterator it = prefs_.begin();
+ it != prefs_.end(); ++it) {
+ (*it)->root_pref_ = persistent_.get();
}
}
diff --git a/chrome/common/pref_service_uitest.cc b/chrome/common/pref_service_uitest.cc
index b9f0774..0ecd114 100644
--- a/chrome/common/pref_service_uitest.cc
+++ b/chrome/common/pref_service_uitest.cc
@@ -82,13 +82,12 @@ TEST_F(PreferenceServiceTest, PreservedWindowPlacementIsLoaded) {
ASSERT_TRUE(file_util::PathExists(tmp_pref_file_));
JSONFileValueSerializer deserializer(tmp_pref_file_);
- Value* root = NULL;
- ASSERT_TRUE(deserializer.Deserialize(&root, NULL));
+ scoped_ptr<Value> root(deserializer.Deserialize(NULL));
- ASSERT_TRUE(root);
+ ASSERT_TRUE(root.get());
ASSERT_TRUE(root->IsType(Value::TYPE_DICTIONARY));
- DictionaryValue* root_dict = static_cast<DictionaryValue*>(root);
+ DictionaryValue* root_dict = static_cast<DictionaryValue*>(root.get());
// Retrieve the screen rect for the launched window
scoped_ptr<BrowserProxy> browser(automation()->GetBrowserWindow(0));
@@ -130,6 +129,5 @@ TEST_F(PreferenceServiceTest, PreservedWindowPlacementIsLoaded) {
ASSERT_TRUE(root_dict->GetBoolean(kBrowserWindowPlacement + L".maximized",
&is_maximized));
ASSERT_EQ(is_maximized, is_window_maximized);
- delete root;
}
diff --git a/chrome/common/pref_service_unittest.cc b/chrome/common/pref_service_unittest.cc
index 2baa908..e35e44a 100644
--- a/chrome/common/pref_service_unittest.cc
+++ b/chrome/common/pref_service_unittest.cc
@@ -155,14 +155,16 @@ TEST_F(PrefServiceTest, Overlay) {
Value* transient_value;
{
JSONStringValueSerializer serializer(transient);
- ASSERT_TRUE(serializer.Deserialize(&transient_value, NULL));
+ transient_value = serializer.Deserialize(NULL);
+ ASSERT_TRUE(transient_value);
}
prefs.transient()->Set(transient_string, transient_value);
Value* both_transient_value;
{
JSONStringValueSerializer serializer(transient);
- ASSERT_TRUE(serializer.Deserialize(&both_transient_value, NULL));
+ both_transient_value = serializer.Deserialize(NULL);
+ ASSERT_TRUE(both_transient_value);
}
prefs.transient()->Set(L"both", both_transient_value);