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

Side by Side Diff: components/ntp_snippets/ntp_snippets_service.cc

Issue 1922083004: Allow fetching personalized snippets from ChromeReader. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: After code review #2 Created 4 years, 7 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
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 <algorithm> 7 #include <algorithm>
8 #include <iterator> 8 #include <iterator>
9 #include <utility> 9 #include <utility>
10 10
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 std::set<std::string> hosts; 123 std::set<std::string> hosts;
124 for (int i = 0; i < suggestions.suggestions_size(); ++i) { 124 for (int i = 0; i < suggestions.suggestions_size(); ++i) {
125 const ChromeSuggestion& suggestion = suggestions.suggestions(i); 125 const ChromeSuggestion& suggestion = suggestions.suggestions(i);
126 GURL url(suggestion.url()); 126 GURL url(suggestion.url());
127 if (url.is_valid()) 127 if (url.is_valid())
128 hosts.insert(url.host()); 128 hosts.insert(url.host());
129 } 129 }
130 return hosts; 130 return hosts;
131 } 131 }
132 132
133 const char kContentInfo[] = "contentInfo";
134
135 // Parses snippets from |list| and adds them to |snippets|. Returns true on 133 // Parses snippets from |list| and adds them to |snippets|. Returns true on
136 // success, false if anything went wrong. 134 // success, false if anything went wrong.
137 bool AddSnippetsFromListValue(const base::ListValue& list, 135 bool AddSnippetsFromListValue(const base::ListValue& list,
138 NTPSnippetsService::NTPSnippetStorage* snippets) { 136 NTPSnippetsService::NTPSnippetStorage* snippets) {
139 for (const base::Value* const value : list) { 137 for (const base::Value* const value : list) {
140 const base::DictionaryValue* dict = nullptr; 138 const base::DictionaryValue* dict = nullptr;
141 if (!value->GetAsDictionary(&dict)) 139 if (!value->GetAsDictionary(&dict))
142 return false; 140 return false;
143 141
144 const base::DictionaryValue* content = nullptr;
145 if (!dict->GetDictionary(kContentInfo, &content))
146 return false;
147 std::unique_ptr<NTPSnippet> snippet = 142 std::unique_ptr<NTPSnippet> snippet =
148 NTPSnippet::CreateFromDictionary(*content); 143 NTPSnippet::CreateFromDictionary(*dict);
149 if (!snippet) 144 if (!snippet)
150 return false; 145 return false;
151 146
152 snippets->push_back(std::move(snippet)); 147 snippets->push_back(std::move(snippet));
153 } 148 }
154 return true; 149 return true;
155 } 150 }
156 151
157 std::unique_ptr<base::ListValue> SnippetsToListValue( 152 std::unique_ptr<base::ListValue> SnippetsToListValue(
158 const NTPSnippetsService::NTPSnippetStorage& snippets) { 153 const NTPSnippetsService::NTPSnippetStorage& snippets) {
159 std::unique_ptr<base::ListValue> list(new base::ListValue); 154 std::unique_ptr<base::ListValue> list(new base::ListValue);
160 for (const auto& snippet : snippets) { 155 for (const auto& snippet : snippets) {
161 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue); 156 std::unique_ptr<base::DictionaryValue> dict = snippet->ToDictionary();
162 dict->Set(kContentInfo, snippet->ToDictionary());
163 list->Append(std::move(dict)); 157 list->Append(std::move(dict));
164 } 158 }
165 return list; 159 return list;
166 } 160 }
167 161
168 bool ContainsSnippet(const NTPSnippetsService::NTPSnippetStorage& haystack, 162 bool ContainsSnippet(const NTPSnippetsService::NTPSnippetStorage& haystack,
169 const std::unique_ptr<NTPSnippet>& needle) { 163 const std::unique_ptr<NTPSnippet>& needle) {
170 const GURL& url = needle->url(); 164 const GURL& url = needle->url();
171 return std::find_if(haystack.begin(), haystack.end(), 165 return std::find_if(haystack.begin(), haystack.end(),
172 [&url](const std::unique_ptr<NTPSnippet>& snippet) { 166 [&url](const std::unique_ptr<NTPSnippet>& snippet) {
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
235 } 229 }
236 230
237 void NTPSnippetsService::FetchSnippets() { 231 void NTPSnippetsService::FetchSnippets() {
238 FetchSnippetsFromHosts(GetSuggestionsHosts()); 232 FetchSnippetsFromHosts(GetSuggestionsHosts());
239 } 233 }
240 234
241 void NTPSnippetsService::FetchSnippetsFromHosts( 235 void NTPSnippetsService::FetchSnippetsFromHosts(
242 const std::set<std::string>& hosts) { 236 const std::set<std::string>& hosts) {
243 if (base::CommandLine::ForCurrentProcess()->HasSwitch( 237 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
244 switches::kDontRestrict)) { 238 switches::kDontRestrict)) {
245 snippets_fetcher_->FetchSnippets(std::set<std::string>(), kMaxSnippetCount); 239 snippets_fetcher_->FetchSnippets(std::set<std::string>(),
240 application_language_code_,
241 kMaxSnippetCount);
246 return; 242 return;
247 } 243 }
248 if (!hosts.empty()) { 244 if (!hosts.empty()) {
249 snippets_fetcher_->FetchSnippets(hosts, kMaxSnippetCount); 245 snippets_fetcher_->FetchSnippets(hosts,
246 application_language_code_,
247 kMaxSnippetCount);
250 } else { 248 } else {
251 last_fetch_status_ = kStatusMessageEmptyHosts; 249 last_fetch_status_ = kStatusMessageEmptyHosts;
252 LoadingSnippetsFinished(); 250 LoadingSnippetsFinished();
253 } 251 }
254 } 252 }
255 253
256 void NTPSnippetsService::RescheduleFetching() { 254 void NTPSnippetsService::RescheduleFetching() {
257 // The scheduler only exists on Android so far, it's null on other platforms. 255 // The scheduler only exists on Android so far, it's null on other platforms.
258 if (!scheduler_) 256 if (!scheduler_)
259 return; 257 return;
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after
533 if (snippet->expiry_date() < next_expiry) 531 if (snippet->expiry_date() < next_expiry)
534 next_expiry = snippet->expiry_date(); 532 next_expiry = snippet->expiry_date();
535 } 533 }
536 DCHECK_GT(next_expiry, expiry); 534 DCHECK_GT(next_expiry, expiry);
537 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, 535 expiry_timer_.Start(FROM_HERE, next_expiry - expiry,
538 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, 536 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished,
539 base::Unretained(this))); 537 base::Unretained(this)));
540 } 538 }
541 539
542 } // namespace ntp_snippets 540 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698