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

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

Issue 1918083002: Convert //components/[f-n]* from scoped_ptr to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: … Created 4 years, 8 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 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
133 bool AddSnippetsFromListValue(const base::ListValue& list, 133 bool AddSnippetsFromListValue(const base::ListValue& list,
134 NTPSnippetsService::NTPSnippetStorage* snippets) { 134 NTPSnippetsService::NTPSnippetStorage* snippets) {
135 for (const base::Value* const value : list) { 135 for (const base::Value* const value : list) {
136 const base::DictionaryValue* dict = nullptr; 136 const base::DictionaryValue* dict = nullptr;
137 if (!value->GetAsDictionary(&dict)) 137 if (!value->GetAsDictionary(&dict))
138 return false; 138 return false;
139 139
140 const base::DictionaryValue* content = nullptr; 140 const base::DictionaryValue* content = nullptr;
141 if (!dict->GetDictionary(kContentInfo, &content)) 141 if (!dict->GetDictionary(kContentInfo, &content))
142 return false; 142 return false;
143 scoped_ptr<NTPSnippet> snippet = NTPSnippet::CreateFromDictionary(*content); 143 std::unique_ptr<NTPSnippet> snippet =
144 NTPSnippet::CreateFromDictionary(*content);
144 if (!snippet) 145 if (!snippet)
145 return false; 146 return false;
146 147
147 snippets->push_back(std::move(snippet)); 148 snippets->push_back(std::move(snippet));
148 } 149 }
149 return true; 150 return true;
150 } 151 }
151 152
152 scoped_ptr<base::ListValue> SnippetsToListValue( 153 std::unique_ptr<base::ListValue> SnippetsToListValue(
153 const NTPSnippetsService::NTPSnippetStorage& snippets) { 154 const NTPSnippetsService::NTPSnippetStorage& snippets) {
154 scoped_ptr<base::ListValue> list(new base::ListValue); 155 std::unique_ptr<base::ListValue> list(new base::ListValue);
155 for (const auto& snippet : snippets) { 156 for (const auto& snippet : snippets) {
156 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue); 157 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue);
157 dict->Set(kContentInfo, snippet->ToDictionary()); 158 dict->Set(kContentInfo, snippet->ToDictionary());
158 list->Append(std::move(dict)); 159 list->Append(std::move(dict));
159 } 160 }
160 return list; 161 return list;
161 } 162 }
162 163
163 bool ContainsSnippet(const NTPSnippetsService::NTPSnippetStorage& haystack, 164 bool ContainsSnippet(const NTPSnippetsService::NTPSnippetStorage& haystack,
164 const scoped_ptr<NTPSnippet>& needle) { 165 const std::unique_ptr<NTPSnippet>& needle) {
165 const GURL& url = needle->url(); 166 const GURL& url = needle->url();
166 return std::find_if(haystack.begin(), haystack.end(), 167 return std::find_if(haystack.begin(), haystack.end(),
167 [&url](const scoped_ptr<NTPSnippet>& snippet) { 168 [&url](const std::unique_ptr<NTPSnippet>& snippet) {
168 return snippet->url() == url; 169 return snippet->url() == url;
169 }) != haystack.end(); 170 }) != haystack.end();
170 } 171 }
171 172
172 } // namespace 173 } // namespace
173 174
174 NTPSnippetsService::NTPSnippetsService( 175 NTPSnippetsService::NTPSnippetsService(
175 PrefService* pref_service, 176 PrefService* pref_service,
176 SuggestionsService* suggestions_service, 177 SuggestionsService* suggestions_service,
177 scoped_refptr<base::SequencedTaskRunner> file_task_runner, 178 scoped_refptr<base::SequencedTaskRunner> file_task_runner,
178 const std::string& application_language_code, 179 const std::string& application_language_code,
179 NTPSnippetsScheduler* scheduler, 180 NTPSnippetsScheduler* scheduler,
180 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher, 181 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher,
181 const ParseJSONCallback& parse_json_callback) 182 const ParseJSONCallback& parse_json_callback)
182 : enabled_(false), 183 : enabled_(false),
183 pref_service_(pref_service), 184 pref_service_(pref_service),
184 suggestions_service_(suggestions_service), 185 suggestions_service_(suggestions_service),
185 file_task_runner_(file_task_runner), 186 file_task_runner_(file_task_runner),
186 application_language_code_(application_language_code), 187 application_language_code_(application_language_code),
187 scheduler_(scheduler), 188 scheduler_(scheduler),
188 snippets_fetcher_(std::move(snippets_fetcher)), 189 snippets_fetcher_(std::move(snippets_fetcher)),
189 parse_json_callback_(parse_json_callback), 190 parse_json_callback_(parse_json_callback),
190 weak_ptr_factory_(this) { 191 weak_ptr_factory_(this) {
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 if (!suggestions_service_) 278 if (!suggestions_service_)
278 return std::set<std::string>(); 279 return std::set<std::string>();
279 280
280 // TODO(treib) this should just call GetSnippetHostsFromPrefs 281 // TODO(treib) this should just call GetSnippetHostsFromPrefs
281 return GetSuggestionsHostsImpl( 282 return GetSuggestionsHostsImpl(
282 suggestions_service_->GetSuggestionsDataFromCache()); 283 suggestions_service_->GetSuggestionsDataFromCache());
283 } 284 }
284 285
285 bool NTPSnippetsService::DiscardSnippet(const GURL& url) { 286 bool NTPSnippetsService::DiscardSnippet(const GURL& url) {
286 auto it = std::find_if(snippets_.begin(), snippets_.end(), 287 auto it = std::find_if(snippets_.begin(), snippets_.end(),
287 [&url](const scoped_ptr<NTPSnippet>& snippet) { 288 [&url](const std::unique_ptr<NTPSnippet>& snippet) {
288 return snippet->url() == url; 289 return snippet->url() == url;
289 }); 290 });
290 if (it == snippets_.end()) 291 if (it == snippets_.end())
291 return false; 292 return false;
292 discarded_snippets_.push_back(std::move(*it)); 293 discarded_snippets_.push_back(std::move(*it));
293 snippets_.erase(it); 294 snippets_.erase(it);
294 StoreDiscardedSnippetsToPrefs(); 295 StoreDiscardedSnippetsToPrefs();
295 StoreSnippetsToPrefs(); 296 StoreSnippetsToPrefs();
296 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 297 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
297 NTPSnippetsServiceLoaded()); 298 NTPSnippetsServiceLoaded());
(...skipping 21 matching lines...) Expand all
319 320
320 void NTPSnippetsService::OnSuggestionsChanged( 321 void NTPSnippetsService::OnSuggestionsChanged(
321 const SuggestionsProfile& suggestions) { 322 const SuggestionsProfile& suggestions) {
322 std::set<std::string> hosts = GetSuggestionsHostsImpl(suggestions); 323 std::set<std::string> hosts = GetSuggestionsHostsImpl(suggestions);
323 if (hosts == GetSnippetHostsFromPrefs()) 324 if (hosts == GetSnippetHostsFromPrefs())
324 return; 325 return;
325 326
326 // Remove existing snippets that aren't in the suggestions anymore. 327 // Remove existing snippets that aren't in the suggestions anymore.
327 snippets_.erase( 328 snippets_.erase(
328 std::remove_if(snippets_.begin(), snippets_.end(), 329 std::remove_if(snippets_.begin(), snippets_.end(),
329 [&hosts](const scoped_ptr<NTPSnippet>& snippet) { 330 [&hosts](const std::unique_ptr<NTPSnippet>& snippet) {
330 return !hosts.count(snippet->url().host()); 331 return !hosts.count(snippet->url().host());
331 }), 332 }),
332 snippets_.end()); 333 snippets_.end());
333 334
334 StoreSnippetsToPrefs(); 335 StoreSnippetsToPrefs();
335 StoreSnippetHostsToPrefs(hosts); 336 StoreSnippetHostsToPrefs(hosts);
336 337
337 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 338 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
338 NTPSnippetsServiceLoaded()); 339 NTPSnippetsServiceLoaded());
339 340
(...skipping 12 matching lines...) Expand all
352 weak_ptr_factory_.GetWeakPtr(), snippets_json), 353 weak_ptr_factory_.GetWeakPtr(), snippets_json),
353 base::Bind(&NTPSnippetsService::OnJsonError, 354 base::Bind(&NTPSnippetsService::OnJsonError,
354 weak_ptr_factory_.GetWeakPtr(), snippets_json)); 355 weak_ptr_factory_.GetWeakPtr(), snippets_json));
355 } else { 356 } else {
356 last_fetch_status_ = status; 357 last_fetch_status_ = status;
357 LoadingSnippetsFinished(); 358 LoadingSnippetsFinished();
358 } 359 }
359 } 360 }
360 361
361 void NTPSnippetsService::OnJsonParsed(const std::string& snippets_json, 362 void NTPSnippetsService::OnJsonParsed(const std::string& snippets_json,
362 scoped_ptr<base::Value> parsed) { 363 std::unique_ptr<base::Value> parsed) {
363 if (!LoadFromValue(*parsed)) { 364 if (!LoadFromValue(*parsed)) {
364 LOG(WARNING) << "Received invalid snippets: " << snippets_json; 365 LOG(WARNING) << "Received invalid snippets: " << snippets_json;
365 last_fetch_status_ = kStatusMessageEmptyList; 366 last_fetch_status_ = kStatusMessageEmptyList;
366 } else { 367 } else {
367 last_fetch_status_ = kStatusMessageOK; 368 last_fetch_status_ = kStatusMessageOK;
368 } 369 }
369 370
370 LoadingSnippetsFinished(); 371 LoadingSnippetsFinished();
371 } 372 }
372 373
(...skipping 19 matching lines...) Expand all
392 } 393 }
393 394
394 bool NTPSnippetsService::LoadFromListValue(const base::ListValue& list) { 395 bool NTPSnippetsService::LoadFromListValue(const base::ListValue& list) {
395 NTPSnippetStorage new_snippets; 396 NTPSnippetStorage new_snippets;
396 if (!AddSnippetsFromListValue(list, &new_snippets)) 397 if (!AddSnippetsFromListValue(list, &new_snippets))
397 return false; 398 return false;
398 399
399 // Remove new snippets that we already have, or that have been discarded. 400 // Remove new snippets that we already have, or that have been discarded.
400 new_snippets.erase( 401 new_snippets.erase(
401 std::remove_if(new_snippets.begin(), new_snippets.end(), 402 std::remove_if(new_snippets.begin(), new_snippets.end(),
402 [this](const scoped_ptr<NTPSnippet>& snippet) { 403 [this](const std::unique_ptr<NTPSnippet>& snippet) {
403 return ContainsSnippet(discarded_snippets_, snippet) || 404 return ContainsSnippet(discarded_snippets_, snippet) ||
404 ContainsSnippet(snippets_, snippet); 405 ContainsSnippet(snippets_, snippet);
405 }), 406 }),
406 new_snippets.end()); 407 new_snippets.end());
407 408
408 // Fill in default publish/expiry dates where required. 409 // Fill in default publish/expiry dates where required.
409 for (scoped_ptr<NTPSnippet>& snippet : new_snippets) { 410 for (std::unique_ptr<NTPSnippet>& snippet : new_snippets) {
410 if (snippet->publish_date().is_null()) 411 if (snippet->publish_date().is_null())
411 snippet->set_publish_date(base::Time::Now()); 412 snippet->set_publish_date(base::Time::Now());
412 if (snippet->expiry_date().is_null()) { 413 if (snippet->expiry_date().is_null()) {
413 snippet->set_expiry_date( 414 snippet->set_expiry_date(
414 snippet->publish_date() + 415 snippet->publish_date() +
415 base::TimeDelta::FromMinutes(kDefaultExpiryTimeMins)); 416 base::TimeDelta::FromMinutes(kDefaultExpiryTimeMins));
416 } 417 }
417 } 418 }
418 419
419 // Insert the new snippets at the front. 420 // Insert the new snippets at the front.
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
472 list.AppendString(host); 473 list.AppendString(host);
473 pref_service_->Set(prefs::kSnippetHosts, list); 474 pref_service_->Set(prefs::kSnippetHosts, list);
474 } 475 }
475 476
476 void NTPSnippetsService::LoadingSnippetsFinished() { 477 void NTPSnippetsService::LoadingSnippetsFinished() {
477 // Remove expired snippets. 478 // Remove expired snippets.
478 base::Time expiry = base::Time::Now(); 479 base::Time expiry = base::Time::Now();
479 480
480 snippets_.erase( 481 snippets_.erase(
481 std::remove_if(snippets_.begin(), snippets_.end(), 482 std::remove_if(snippets_.begin(), snippets_.end(),
482 [&expiry](const scoped_ptr<NTPSnippet>& snippet) { 483 [&expiry](const std::unique_ptr<NTPSnippet>& snippet) {
483 return snippet->expiry_date() <= expiry; 484 return snippet->expiry_date() <= expiry;
484 }), 485 }),
485 snippets_.end()); 486 snippets_.end());
486 StoreSnippetsToPrefs(); 487 StoreSnippetsToPrefs();
487 488
488 discarded_snippets_.erase( 489 discarded_snippets_.erase(
489 std::remove_if(discarded_snippets_.begin(), discarded_snippets_.end(), 490 std::remove_if(discarded_snippets_.begin(), discarded_snippets_.end(),
490 [&expiry](const scoped_ptr<NTPSnippet>& snippet) { 491 [&expiry](const std::unique_ptr<NTPSnippet>& snippet) {
491 return snippet->expiry_date() <= expiry; 492 return snippet->expiry_date() <= expiry;
492 }), 493 }),
493 discarded_snippets_.end()); 494 discarded_snippets_.end());
494 StoreDiscardedSnippetsToPrefs(); 495 StoreDiscardedSnippetsToPrefs();
495 496
496 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 497 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
497 NTPSnippetsServiceLoaded()); 498 NTPSnippetsServiceLoaded());
498 499
499 // If there are any snippets left, schedule a timer for the next expiry. 500 // If there are any snippets left, schedule a timer for the next expiry.
500 if (snippets_.empty() && discarded_snippets_.empty()) 501 if (snippets_.empty() && discarded_snippets_.empty())
501 return; 502 return;
502 503
503 base::Time next_expiry = base::Time::Max(); 504 base::Time next_expiry = base::Time::Max();
504 for (const auto& snippet : snippets_) { 505 for (const auto& snippet : snippets_) {
505 if (snippet->expiry_date() < next_expiry) 506 if (snippet->expiry_date() < next_expiry)
506 next_expiry = snippet->expiry_date(); 507 next_expiry = snippet->expiry_date();
507 } 508 }
508 for (const auto& snippet : discarded_snippets_) { 509 for (const auto& snippet : discarded_snippets_) {
509 if (snippet->expiry_date() < next_expiry) 510 if (snippet->expiry_date() < next_expiry)
510 next_expiry = snippet->expiry_date(); 511 next_expiry = snippet->expiry_date();
511 } 512 }
512 DCHECK_GT(next_expiry, expiry); 513 DCHECK_GT(next_expiry, expiry);
513 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, 514 expiry_timer_.Start(FROM_HERE, next_expiry - expiry,
514 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, 515 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished,
515 base::Unretained(this))); 516 base::Unretained(this)));
516 } 517 }
517 518
518 } // namespace ntp_snippets 519 } // namespace ntp_snippets
OLDNEW
« no previous file with comments | « components/ntp_snippets/ntp_snippets_service.h ('k') | components/ntp_snippets/ntp_snippets_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698