From 259c0a3f44bdd8130c5d835e9f596624a079fbd1 Mon Sep 17 00:00:00 2001 From: reillyg Date: Thu, 10 Sep 2015 17:25:54 -0700 Subject: Add scoped_ptr-safe base::Value to Dictionary/List conversion functions. This change adds two static From() functions to the DictionaryValue and ListValue classes which take a scoped_ptr to a Value and either convert it to a scoped_ptr to a DictionaryValue or ListValue, or return nullptr. These are intended to replace the existing pattern, make_scoped_ptr(static_cast(value.release())) with the shorter and safer alternative, base::DictionaryValue::From(value.Pass()) Instances of this pattern in //extensions have been converted as examples. Review URL: https://codereview.chromium.org/1308013005 Cr-Commit-Position: refs/heads/master@{#348294} --- extensions/utility/unpacker.cc | 10 +++++----- extensions/utility/unpacker.h | 3 +-- 2 files changed, 6 insertions(+), 7 deletions(-) (limited to 'extensions/utility') diff --git a/extensions/utility/unpacker.cc b/extensions/utility/unpacker.cc index 2cd8f01..6e28a49b 100644 --- a/extensions/utility/unpacker.cc +++ b/extensions/utility/unpacker.cc @@ -112,7 +112,7 @@ Unpacker::Unpacker(const base::FilePath& working_dir, Unpacker::~Unpacker() { } -base::DictionaryValue* Unpacker::ReadManifest() { +scoped_ptr Unpacker::ReadManifest() { base::FilePath manifest_path = extension_dir_.Append(kManifestFilename); if (!base::PathExists(manifest_path)) { SetError(errors::kInvalidManifest); @@ -132,7 +132,7 @@ base::DictionaryValue* Unpacker::ReadManifest() { return NULL; } - return static_cast(root.release()); + return base::DictionaryValue::From(root.Pass()); } bool Unpacker::ReadAllMessageCatalogs(const std::string& default_locale) { @@ -161,7 +161,7 @@ bool Unpacker::ReadAllMessageCatalogs(const std::string& default_locale) { bool Unpacker::Run() { // Parse the manifest. - parsed_manifest_.reset(ReadManifest()); + parsed_manifest_ = ReadManifest(); if (!parsed_manifest_.get()) return false; // Error was already reported. @@ -253,8 +253,8 @@ bool Unpacker::AddDecodedImage(const base::FilePath& path) { bool Unpacker::ReadMessageCatalog(const base::FilePath& message_path) { std::string error; JSONFileValueDeserializer deserializer(message_path); - scoped_ptr root(static_cast( - deserializer.Deserialize(NULL, &error))); + scoped_ptr root = base::DictionaryValue::From( + make_scoped_ptr(deserializer.Deserialize(NULL, &error))); if (!root.get()) { base::string16 messages_file = message_path.LossyDisplayName(); if (error.empty()) { diff --git a/extensions/utility/unpacker.h b/extensions/utility/unpacker.h index 7e5a7e4..eaffc7b 100644 --- a/extensions/utility/unpacker.h +++ b/extensions/utility/unpacker.h @@ -55,8 +55,7 @@ class Unpacker { bool DumpMessageCatalogsToFile(); // Parse the manifest.json file inside the extension (not in the header). - // Caller takes ownership of return value. - base::DictionaryValue* ReadManifest(); + scoped_ptr ReadManifest(); // Parse all _locales/*/messages.json files inside the extension. bool ReadAllMessageCatalogs(const std::string& default_locale); -- cgit v1.1