Commit f43786de authored by Sylvain Defresne's avatar Sylvain Defresne Committed by Commit Bot

Improve NTPCatalog::create API and implementation

Change the API to receive a base::Value instead of the deprecated
class base::DictionaryValue (the implementation already check the
type of the passed value so does not assume base::Dictionary type).

Change the API to receive the base::Value by reference instead of
pointer as there is only one caller and it already check that the
pointer is non-null before calling NTPCatalog::create.

Use the base::Value::Find{String,List}Key methods instead of the
base::FindKeyOfType method as it reduce the complexity of the
implementation.

Bug: 646113
Change-Id: Icc3a484619786d5afcf0733ef01c07904635d481
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1609780
Auto-Submit: Sylvain Defresne <sdefresne@chromium.org>
Reviewed-by: default avatarJustin DeWitt <dewittj@chromium.org>
Commit-Queue: Sylvain Defresne <sdefresne@chromium.org>
Cr-Commit-Position: refs/heads/master@{#659053}
parent 31df12e9
...@@ -7,20 +7,16 @@ ...@@ -7,20 +7,16 @@
#include <sstream> #include <sstream>
#include "base/logging.h" #include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/values.h" #include "base/values.h"
namespace explore_sites { namespace explore_sites {
// static // static
std::unique_ptr<NTPCatalog> NTPCatalog::create( std::unique_ptr<NTPCatalog> NTPCatalog::create(const base::Value& json) {
const base::DictionaryValue* json) { if (!json.is_dict())
if (!json || !json->is_dict())
return nullptr; return nullptr;
const base::ListValue* categories = static_cast<const base::ListValue*>( const base::Value* categories = json.FindListKey("categories");
json->FindKeyOfType("categories", base::Value::Type::LIST));
if (!categories) if (!categories)
return nullptr; return nullptr;
...@@ -29,27 +25,21 @@ std::unique_ptr<NTPCatalog> NTPCatalog::create( ...@@ -29,27 +25,21 @@ std::unique_ptr<NTPCatalog> NTPCatalog::create(
if (!category.is_dict()) { if (!category.is_dict()) {
return nullptr; return nullptr;
} }
const base::DictionaryValue* category_dict = const std::string* id = category.FindStringKey("id");
static_cast<const base::DictionaryValue*>(&category); const std::string* title = category.FindStringKey("title");
const base::Value* id = const std::string* icon_url_str = category.FindStringKey("icon_url");
category_dict->FindKeyOfType("id", base::Value::Type::STRING);
const base::Value* title =
category_dict->FindKeyOfType("title", base::Value::Type::STRING);
const base::Value* icon_url_str =
category_dict->FindKeyOfType("icon_url", base::Value::Type::STRING);
if (!id || !title || !icon_url_str) if (!id || !title || !icon_url_str)
continue; continue;
GURL icon_url(icon_url_str->GetString()); GURL icon_url(*icon_url_str);
if (icon_url.is_empty()) if (icon_url.is_empty())
continue; continue;
catalog_categories.push_back( catalog_categories.push_back({*id, *title, icon_url});
{id->GetString(), title->GetString(), icon_url});
} }
auto catalog = base::WrapUnique(new NTPCatalog(catalog_categories)); auto catalog = std::make_unique<NTPCatalog>(catalog_categories);
DVLOG(1) << "Catalog parsed: " << catalog->ToString(); DVLOG(1) << "Catalog parsed: " << catalog->ToString();
return catalog; return catalog;
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#ifndef CHROME_BROWSER_ANDROID_EXPLORE_SITES_CATALOG_H_ #ifndef CHROME_BROWSER_ANDROID_EXPLORE_SITES_CATALOG_H_
#define CHROME_BROWSER_ANDROID_EXPLORE_SITES_CATALOG_H_ #define CHROME_BROWSER_ANDROID_EXPLORE_SITES_CATALOG_H_
#include <memory>
#include <string> #include <string>
#include <utility> #include <utility>
#include <vector> #include <vector>
...@@ -12,7 +13,7 @@ ...@@ -12,7 +13,7 @@
#include "url/gurl.h" #include "url/gurl.h"
namespace base { namespace base {
class DictionaryValue; class Value;
} }
namespace explore_sites { namespace explore_sites {
...@@ -34,7 +35,7 @@ class NTPCatalog { ...@@ -34,7 +35,7 @@ class NTPCatalog {
}; };
// The NTPCatalog does not take ownership of |json|. // The NTPCatalog does not take ownership of |json|.
static std::unique_ptr<NTPCatalog> create(const base::DictionaryValue* json); static std::unique_ptr<NTPCatalog> create(const base::Value& json);
explicit NTPCatalog(const std::vector<Category>& category_list); explicit NTPCatalog(const std::vector<Category>& category_list);
~NTPCatalog(); ~NTPCatalog();
......
...@@ -113,8 +113,7 @@ void NTPJsonFetcher::OnJsonParseSuccess( ...@@ -113,8 +113,7 @@ void NTPJsonFetcher::OnJsonParseSuccess(
return; return;
} }
auto catalog = NTPCatalog::create( auto catalog = NTPCatalog::create(*parsed_json);
static_cast<base::DictionaryValue*>(parsed_json.get()));
std::move(callback_).Run(std::move(catalog)); std::move(callback_).Run(std::move(catalog));
} }
......
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