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

Side by Side 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, 9 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/ntp_snippets/ntp_snippets_service.h" 5 #include "components/ntp_snippets/ntp_snippets_service.h"
6 6
7 #include "base/files/file_path.h" 7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h" 8 #include "base/files/file_util.h"
9 #include "base/json/json_string_value_serializer.h" 9 #include "base/json/json_reader.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/path_service.h" 11 #include "base/path_service.h"
12 #include "base/task_runner_util.h" 12 #include "base/task_runner_util.h"
13 #include "base/values.h" 13 #include "base/values.h"
14 14
15 namespace { 15 namespace {
16 16
17 // TODO(crbug.com/587857): This is an extremely small value, for development. 17 // TODO(crbug.com/587857): This is an extremely small value, for development.
18 // Replace it by something sensible and add a command line param to control it. 18 // Replace it by something sensible and add a command line param to control it.
19 const int kDefaultFetchingIntervalSeconds = 60; 19 const int kDefaultFetchingIntervalSeconds = 60;
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 85
86 void NTPSnippetsService::OnFileReadDone(const std::string* json, bool success) { 86 void NTPSnippetsService::OnFileReadDone(const std::string* json, bool success) {
87 if (!success) 87 if (!success)
88 return; 88 return;
89 89
90 DCHECK(json); 90 DCHECK(json);
91 LoadFromJSONString(*json); 91 LoadFromJSONString(*json);
92 } 92 }
93 93
94 bool NTPSnippetsService::LoadFromJSONString(const std::string& str) { 94 bool NTPSnippetsService::LoadFromJSONString(const std::string& str) {
95 snippets_.clear(); 95 scoped_ptr<base::Value> deserialized = base::JSONReader::Read(str);
96
97 JSONStringValueDeserializer deserializer(str);
98 int error_code;
99 std::string error_message;
100
101 scoped_ptr<base::Value> deserialized =
102 deserializer.Deserialize(&error_code, &error_message);
103 if (!deserialized) 96 if (!deserialized)
104 return false; 97 return false;
105 98
106 const base::DictionaryValue* top_dict = NULL; 99 const base::DictionaryValue* top_dict = nullptr;
107 if (!deserialized->GetAsDictionary(&top_dict)) 100 if (!deserialized->GetAsDictionary(&top_dict))
108 return false; 101 return false;
109 102
110 const base::ListValue* list = NULL; 103 const base::ListValue* list = nullptr;
111 if (!top_dict->GetList("recos", &list)) 104 if (!top_dict->GetList("recos", &list))
112 return false; 105 return false;
113 106
114 for (base::Value* const value : *list) { 107 for (const base::Value* const value : *list) {
115 const base::DictionaryValue* dict = NULL; 108 const base::DictionaryValue* dict = nullptr;
116 if (!value->GetAsDictionary(&dict)) 109 if (!value->GetAsDictionary(&dict))
117 return false; 110 return false;
118 111
119 const base::DictionaryValue* content = NULL; 112 const base::DictionaryValue* content = nullptr;
120 if (!dict->GetDictionary("contentInfo", &content)) 113 if (!dict->GetDictionary("contentInfo", &content))
121 return false; 114 return false;
122 scoped_ptr<NTPSnippet> snippet = 115 scoped_ptr<NTPSnippet> snippet =
123 NTPSnippet::NTPSnippetFromDictionary(*content); 116 NTPSnippet::NTPSnippetFromDictionary(*content);
124 if (!snippet) 117 if (!snippet)
125 return false; 118 return false;
126 snippets_.push_back(std::move(snippet)); 119
120 // Check if we already have a snippet with the same URL. If so, replace it
121 // rather than adding a duplicate.
122 const GURL& url = snippet->url();
123 auto it = std::find_if(snippets_.begin(), snippets_.end(),
124 [&url](const scoped_ptr<NTPSnippet>& old_snippet) {
125 return old_snippet->url() == url;
126 });
127 if (it != snippets_.end())
128 *it = std::move(snippet);
129 else
130 snippets_.push_back(std::move(snippet));
127 } 131 }
128 loaded_ = true; 132 loaded_ = true;
129 133
130 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 134 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
131 NTPSnippetsServiceLoaded(this)); 135 NTPSnippetsServiceLoaded(this));
132 return true; 136 return true;
133 } 137 }
134 138
135 void NTPSnippetsService::OnSnippetsDownloaded( 139 void NTPSnippetsService::OnSnippetsDownloaded(
136 const base::FilePath& download_path) { 140 const base::FilePath& download_path) {
137 std::string* downloaded_data = new std::string; 141 std::string* downloaded_data = new std::string;
138 base::PostTaskAndReplyWithResult( 142 base::PostTaskAndReplyWithResult(
139 file_task_runner_.get(), FROM_HERE, 143 file_task_runner_.get(), FROM_HERE,
140 base::Bind(&ReadFileToString, download_path, downloaded_data), 144 base::Bind(&ReadFileToString, download_path, downloaded_data),
141 base::Bind(&NTPSnippetsService::OnFileReadDone, 145 base::Bind(&NTPSnippetsService::OnFileReadDone,
142 weak_ptr_factory_.GetWeakPtr(), base::Owned(downloaded_data))); 146 weak_ptr_factory_.GetWeakPtr(), base::Owned(downloaded_data)));
143 } 147 }
144 148
145 } // namespace ntp_snippets 149 } // namespace ntp_snippets
OLDNEW
« 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