Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(174)

Unified Diff: components/ntp_snippets/ntp_snippets_service.cc

Issue 1727573003: [NTP Snippets] Implement snippets merging (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@fetch_startup
Patch Set: rebase Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/ntp_snippets/ntp_snippets_service.cc
diff --git a/components/ntp_snippets/ntp_snippets_service.cc b/components/ntp_snippets/ntp_snippets_service.cc
index c696405d82fc58ec56c79ca21a2d7ced803a2938..e540f73b87e4975a19e02a392fbd943050282350 100644
--- a/components/ntp_snippets/ntp_snippets_service.cc
+++ b/components/ntp_snippets/ntp_snippets_service.cc
@@ -6,7 +6,7 @@
#include "base/files/file_path.h"
#include "base/files/file_util.h"
-#include "base/json/json_string_value_serializer.h"
+#include "base/json/json_reader.h"
#include "base/location.h"
#include "base/path_service.h"
#include "base/task_runner_util.h"
@@ -92,38 +92,42 @@ void NTPSnippetsService::OnFileReadDone(const std::string* json, bool success) {
}
bool NTPSnippetsService::LoadFromJSONString(const std::string& str) {
- snippets_.clear();
-
- JSONStringValueDeserializer deserializer(str);
- int error_code;
- std::string error_message;
-
- scoped_ptr<base::Value> deserialized =
- deserializer.Deserialize(&error_code, &error_message);
+ scoped_ptr<base::Value> deserialized = base::JSONReader::Read(str);
if (!deserialized)
return false;
- const base::DictionaryValue* top_dict = NULL;
+ const base::DictionaryValue* top_dict = nullptr;
if (!deserialized->GetAsDictionary(&top_dict))
return false;
- const base::ListValue* list = NULL;
+ const base::ListValue* list = nullptr;
if (!top_dict->GetList("recos", &list))
return false;
- for (base::Value* const value : *list) {
- const base::DictionaryValue* dict = NULL;
+ for (const base::Value* const value : *list) {
+ const base::DictionaryValue* dict = nullptr;
if (!value->GetAsDictionary(&dict))
return false;
- const base::DictionaryValue* content = NULL;
+ const base::DictionaryValue* content = nullptr;
if (!dict->GetDictionary("contentInfo", &content))
return false;
scoped_ptr<NTPSnippet> snippet =
NTPSnippet::NTPSnippetFromDictionary(*content);
if (!snippet)
return false;
- snippets_.push_back(std::move(snippet));
+
+ // Check if we already have a snippet with the same URL. If so, replace it
+ // rather than adding a duplicate.
+ const GURL& url = snippet->url();
+ auto it = std::find_if(snippets_.begin(), snippets_.end(),
+ [&url](const scoped_ptr<NTPSnippet>& old_snippet) {
+ return old_snippet->url() == url;
+ });
+ if (it != snippets_.end())
+ *it = std::move(snippet);
+ else
+ snippets_.push_back(std::move(snippet));
}
loaded_ = true;
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698