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

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

Issue 1976153002: Various NTPSnippetsService cleanups: (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Shutdown() in tests 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 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 for (int i = 0; i < suggestions.suggestions_size(); ++i) { 119 for (int i = 0; i < suggestions.suggestions_size(); ++i) {
120 const ChromeSuggestion& suggestion = suggestions.suggestions(i); 120 const ChromeSuggestion& suggestion = suggestions.suggestions(i);
121 GURL url(suggestion.url()); 121 GURL url(suggestion.url());
122 if (url.is_valid()) 122 if (url.is_valid())
123 hosts.insert(url.host()); 123 hosts.insert(url.host());
124 } 124 }
125 return hosts; 125 return hosts;
126 } 126 }
127 127
128 std::unique_ptr<base::ListValue> SnippetsToListValue( 128 std::unique_ptr<base::ListValue> SnippetsToListValue(
129 const NTPSnippetsService::NTPSnippetStorage& snippets) { 129 const NTPSnippet::PtrVector& snippets) {
130 std::unique_ptr<base::ListValue> list(new base::ListValue); 130 std::unique_ptr<base::ListValue> list(new base::ListValue);
131 for (const auto& snippet : snippets) { 131 for (const auto& snippet : snippets) {
132 std::unique_ptr<base::DictionaryValue> dict = snippet->ToDictionary(); 132 std::unique_ptr<base::DictionaryValue> dict = snippet->ToDictionary();
133 list->Append(std::move(dict)); 133 list->Append(std::move(dict));
134 } 134 }
135 return list; 135 return list;
136 } 136 }
137 137
138 bool ContainsSnippet(const NTPSnippetsService::NTPSnippetStorage& haystack, 138 bool ContainsSnippet(const NTPSnippet::PtrVector& haystack,
139 const std::unique_ptr<NTPSnippet>& needle) { 139 const std::unique_ptr<NTPSnippet>& needle) {
140 const std::string& id = needle->id(); 140 const std::string& id = needle->id();
141 return std::find_if(haystack.begin(), haystack.end(), 141 return std::find_if(haystack.begin(), haystack.end(),
142 [&id](const std::unique_ptr<NTPSnippet>& snippet) { 142 [&id](const std::unique_ptr<NTPSnippet>& snippet) {
143 return snippet->id() == id; 143 return snippet->id() == id;
144 }) != haystack.end(); 144 }) != haystack.end();
145 } 145 }
146 146
147 void WrapImageFetchedCallback( 147 void WrapImageFetchedCallback(
148 const NTPSnippetsService::ImageFetchedCallback& callback, 148 const NTPSnippetsService::ImageFetchedCallback& callback,
149 const GURL& snippet_id_url, 149 const GURL& snippet_id_url,
150 const SkBitmap* bitmap) { 150 const SkBitmap* bitmap) {
151 callback.Run(snippet_id_url.spec(), bitmap); 151 callback.Run(snippet_id_url.spec(), bitmap);
152 } 152 }
153 153
154 } // namespace 154 } // namespace
155 155
156 NTPSnippetsService::NTPSnippetsService( 156 NTPSnippetsService::NTPSnippetsService(
157 PrefService* pref_service, 157 PrefService* pref_service,
158 SuggestionsService* suggestions_service, 158 SuggestionsService* suggestions_service,
159 scoped_refptr<base::SequencedTaskRunner> file_task_runner, 159 scoped_refptr<base::SequencedTaskRunner> file_task_runner,
160 const std::string& application_language_code, 160 const std::string& application_language_code,
161 NTPSnippetsScheduler* scheduler, 161 NTPSnippetsScheduler* scheduler,
162 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher, 162 std::unique_ptr<NTPSnippetsFetcher> snippets_fetcher,
163 std::unique_ptr<ImageFetcher> image_fetcher) 163 std::unique_ptr<ImageFetcher> image_fetcher)
164 : enabled_(false), 164 : did_init_(false),
165 did_shutdown_(false),
166 enabled_(false),
165 pref_service_(pref_service), 167 pref_service_(pref_service),
166 suggestions_service_(suggestions_service), 168 suggestions_service_(suggestions_service),
167 file_task_runner_(file_task_runner), 169 file_task_runner_(file_task_runner),
168 application_language_code_(application_language_code), 170 application_language_code_(application_language_code),
169 scheduler_(scheduler), 171 scheduler_(scheduler),
170 snippets_fetcher_(std::move(snippets_fetcher)), 172 snippets_fetcher_(std::move(snippets_fetcher)),
171 image_fetcher_(std::move(image_fetcher)) { 173 image_fetcher_(std::move(image_fetcher)) {
172 snippets_fetcher_->SetCallback(base::Bind( 174 snippets_fetcher_->SetCallback(base::Bind(
173 &NTPSnippetsService::OnFetchFinished, base::Unretained(this))); 175 &NTPSnippetsService::OnFetchFinished, base::Unretained(this)));
174 } 176 }
175 177
176 NTPSnippetsService::~NTPSnippetsService() {} 178 NTPSnippetsService::~NTPSnippetsService() {
179 DCHECK(!did_init_ || did_shutdown_);
180 }
177 181
178 // static 182 // static
179 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) { 183 void NTPSnippetsService::RegisterProfilePrefs(PrefRegistrySimple* registry) {
180 registry->RegisterListPref(prefs::kSnippets); 184 registry->RegisterListPref(prefs::kSnippets);
181 registry->RegisterListPref(prefs::kDiscardedSnippets); 185 registry->RegisterListPref(prefs::kDiscardedSnippets);
182 registry->RegisterListPref(prefs::kSnippetHosts); 186 registry->RegisterListPref(prefs::kSnippetHosts);
183 } 187 }
184 188
185 void NTPSnippetsService::Init(bool enabled) { 189 void NTPSnippetsService::Init(bool enabled) {
190 DCHECK(!did_init_);
191 did_init_ = true;
192
186 enabled_ = enabled; 193 enabled_ = enabled;
187 if (enabled_) { 194 if (enabled_) {
188 // |suggestions_service_| can be null in tests. 195 // |suggestions_service_| can be null in tests.
189 if (suggestions_service_) { 196 if (suggestions_service_) {
190 suggestions_service_subscription_ = suggestions_service_->AddCallback( 197 suggestions_service_subscription_ = suggestions_service_->AddCallback(
191 base::Bind(&NTPSnippetsService::OnSuggestionsChanged, 198 base::Bind(&NTPSnippetsService::OnSuggestionsChanged,
192 base::Unretained(this))); 199 base::Unretained(this)));
193 } 200 }
194 201
195 // Get any existing snippets immediately from prefs. 202 // Get any existing snippets immediately from prefs.
196 LoadDiscardedSnippetsFromPrefs(); 203 LoadDiscardedSnippetsFromPrefs();
197 LoadSnippetsFromPrefs(); 204 LoadSnippetsFromPrefs();
198 205
199 // If we don't have any snippets yet, start a fetch. 206 // If we don't have any snippets yet, start a fetch.
200 if (snippets_.empty()) 207 if (snippets_.empty())
201 FetchSnippets(); 208 FetchSnippets();
202 } 209 }
203 210
204 RescheduleFetching(); 211 RescheduleFetching();
205 } 212 }
206 213
207 void NTPSnippetsService::Shutdown() { 214 void NTPSnippetsService::Shutdown() {
215 if (!did_init_)
Bernhard Bauer 2016/05/13 16:04:05 Hm... under which circumstances would we shut down
Marc Treib 2016/05/17 08:38:14 Shutdown is overridden from BrowserContextKeyedSer
Bernhard Bauer 2016/05/17 09:23:40 Aww, I thought you would have learned by now 😉 Som
Marc Treib 2016/05/17 11:21:18 Alright, done. (Though really, we only care that I
Bernhard Bauer 2016/05/17 12:38:13 The default is to create services lazily when they
216 return;
217 DCHECK(!did_shutdown_);
218 did_shutdown_ = true;
219
208 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, 220 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_,
209 NTPSnippetsServiceShutdown()); 221 NTPSnippetsServiceShutdown());
222 suggestions_service_subscription_.reset();
210 enabled_ = false; 223 enabled_ = false;
211 } 224 }
212 225
213 void NTPSnippetsService::FetchSnippets() { 226 void NTPSnippetsService::FetchSnippets() {
214 FetchSnippetsFromHosts(GetSuggestionsHosts()); 227 FetchSnippetsFromHosts(GetSuggestionsHosts());
215 } 228 }
216 229
217 void NTPSnippetsService::FetchSnippetsFromHosts( 230 void NTPSnippetsService::FetchSnippetsFromHosts(
218 const std::set<std::string>& hosts) { 231 const std::set<std::string>& hosts) {
219 snippets_fetcher_->FetchSnippetsFromHosts(hosts, application_language_code_, 232 snippets_fetcher_->FetchSnippetsFromHosts(hosts, application_language_code_,
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 // Sparse histogram used because the number of snippets is small (bound by 356 // Sparse histogram used because the number of snippets is small (bound by
344 // kMaxSnippetCount). 357 // kMaxSnippetCount).
345 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount)); 358 DCHECK_LE(snippets->size(), static_cast<size_t>(kMaxSnippetCount));
346 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched", 359 UMA_HISTOGRAM_SPARSE_SLOWLY("NewTabPage.Snippets.NumArticlesFetched",
347 snippets->size()); 360 snippets->size());
348 MergeSnippets(std::move(*snippets)); 361 MergeSnippets(std::move(*snippets));
349 } 362 }
350 LoadingSnippetsFinished(); 363 LoadingSnippetsFinished();
351 } 364 }
352 365
353 void NTPSnippetsService::MergeSnippets(NTPSnippetStorage new_snippets) { 366 void NTPSnippetsService::MergeSnippets(NTPSnippet::PtrVector new_snippets) {
354 // Remove new snippets that we already have, or that have been discarded. 367 // Remove new snippets that we already have, or that have been discarded.
355 new_snippets.erase( 368 new_snippets.erase(
356 std::remove_if(new_snippets.begin(), new_snippets.end(), 369 std::remove_if(new_snippets.begin(), new_snippets.end(),
357 [this](const std::unique_ptr<NTPSnippet>& snippet) { 370 [this](const std::unique_ptr<NTPSnippet>& snippet) {
358 return ContainsSnippet(discarded_snippets_, snippet) || 371 return ContainsSnippet(discarded_snippets_, snippet) ||
359 ContainsSnippet(snippets_, snippet); 372 ContainsSnippet(snippets_, snippet);
360 }), 373 }),
361 new_snippets.end()); 374 new_snippets.end());
362 375
363 // Fill in default publish/expiry dates where required. 376 // Fill in default publish/expiry dates where required.
(...skipping 29 matching lines...) Expand all
393 } 406 }
394 } 407 }
395 408
396 // Insert the new snippets at the front. 409 // Insert the new snippets at the front.
397 snippets_.insert(snippets_.begin(), 410 snippets_.insert(snippets_.begin(),
398 std::make_move_iterator(new_snippets.begin()), 411 std::make_move_iterator(new_snippets.begin()),
399 std::make_move_iterator(new_snippets.end())); 412 std::make_move_iterator(new_snippets.end()));
400 } 413 }
401 414
402 void NTPSnippetsService::LoadSnippetsFromPrefs() { 415 void NTPSnippetsService::LoadSnippetsFromPrefs() {
403 NTPSnippetStorage prefs_snippets; 416 NTPSnippet::PtrVector prefs_snippets;
404 bool success = NTPSnippet::AddFromListValue( 417 bool success = NTPSnippet::AddFromListValue(
405 *pref_service_->GetList(prefs::kSnippets), &prefs_snippets); 418 *pref_service_->GetList(prefs::kSnippets), &prefs_snippets);
406 DCHECK(success) << "Failed to parse snippets from prefs"; 419 DCHECK(success) << "Failed to parse snippets from prefs";
407 MergeSnippets(std::move(prefs_snippets)); 420 MergeSnippets(std::move(prefs_snippets));
408 LoadingSnippetsFinished(); 421 LoadingSnippetsFinished();
409 } 422 }
410 423
411 void NTPSnippetsService::StoreSnippetsToPrefs() { 424 void NTPSnippetsService::StoreSnippetsToPrefs() {
412 pref_service_->Set(prefs::kSnippets, *SnippetsToListValue(snippets_)); 425 pref_service_->Set(prefs::kSnippets, *SnippetsToListValue(snippets_));
413 } 426 }
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
493 if (snippet->expiry_date() < next_expiry) 506 if (snippet->expiry_date() < next_expiry)
494 next_expiry = snippet->expiry_date(); 507 next_expiry = snippet->expiry_date();
495 } 508 }
496 DCHECK_GT(next_expiry, expiry); 509 DCHECK_GT(next_expiry, expiry);
497 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, 510 expiry_timer_.Start(FROM_HERE, next_expiry - expiry,
498 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished, 511 base::Bind(&NTPSnippetsService::LoadingSnippetsFinished,
499 base::Unretained(this))); 512 base::Unretained(this)));
500 } 513 }
501 514
502 } // namespace ntp_snippets 515 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698