| OLD | NEW |
| 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/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 NTPSnippetsService::NTPSnippetsService( | 115 NTPSnippetsService::NTPSnippetsService( |
| 116 PrefService* pref_service, | 116 PrefService* pref_service, |
| 117 SuggestionsService* suggestions_service, | 117 SuggestionsService* suggestions_service, |
| 118 scoped_refptr<base::SequencedTaskRunner> file_task_runner, | 118 scoped_refptr<base::SequencedTaskRunner> file_task_runner, |
| 119 const std::string& application_language_code, | 119 const std::string& application_language_code, |
| 120 NTPSnippetsScheduler* scheduler, | 120 NTPSnippetsScheduler* scheduler, |
| 121 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher, | 121 scoped_ptr<NTPSnippetsFetcher> snippets_fetcher, |
| 122 const ParseJSONCallback& parse_json_callback) | 122 const ParseJSONCallback& parse_json_callback) |
| 123 : pref_service_(pref_service), | 123 : pref_service_(pref_service), |
| 124 suggestions_service_(suggestions_service), | 124 suggestions_service_(suggestions_service), |
| 125 loaded_(false), | |
| 126 file_task_runner_(file_task_runner), | 125 file_task_runner_(file_task_runner), |
| 127 application_language_code_(application_language_code), | 126 application_language_code_(application_language_code), |
| 128 scheduler_(scheduler), | 127 scheduler_(scheduler), |
| 129 snippets_fetcher_(std::move(snippets_fetcher)), | 128 snippets_fetcher_(std::move(snippets_fetcher)), |
| 130 parse_json_callback_(parse_json_callback), | 129 parse_json_callback_(parse_json_callback), |
| 131 weak_ptr_factory_(this) { | 130 weak_ptr_factory_(this) { |
| 132 snippets_fetcher_subscription_ = snippets_fetcher_->AddCallback(base::Bind( | 131 snippets_fetcher_subscription_ = snippets_fetcher_->AddCallback(base::Bind( |
| 133 &NTPSnippetsService::OnSnippetsDownloaded, base::Unretained(this))); | 132 &NTPSnippetsService::OnSnippetsDownloaded, base::Unretained(this))); |
| 134 } | 133 } |
| 135 | 134 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 GetFetchingIntervalWifi(), | 167 GetFetchingIntervalWifi(), |
| 169 GetFetchingIntervalFallback()); | 168 GetFetchingIntervalFallback()); |
| 170 } else { | 169 } else { |
| 171 scheduler_->Unschedule(); | 170 scheduler_->Unschedule(); |
| 172 } | 171 } |
| 173 } | 172 } |
| 174 | 173 |
| 175 void NTPSnippetsService::Shutdown() { | 174 void NTPSnippetsService::Shutdown() { |
| 176 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 175 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
| 177 NTPSnippetsServiceShutdown(this)); | 176 NTPSnippetsServiceShutdown(this)); |
| 178 loaded_ = false; | |
| 179 } | 177 } |
| 180 | 178 |
| 181 void NTPSnippetsService::FetchSnippets() { | 179 void NTPSnippetsService::FetchSnippets() { |
| 182 // |suggestions_service_| can be null in tests. | 180 // |suggestions_service_| can be null in tests. |
| 183 if (!suggestions_service_) | 181 if (!suggestions_service_) |
| 184 return; | 182 return; |
| 185 | 183 |
| 186 FetchSnippetsImpl(GetSuggestionsHosts( | 184 FetchSnippetsImpl(GetSuggestionsHosts( |
| 187 suggestions_service_->GetSuggestionsDataFromCache())); | 185 suggestions_service_->GetSuggestionsDataFromCache())); |
| 188 } | 186 } |
| 189 | 187 |
| 190 bool NTPSnippetsService::DiscardSnippet(const GURL& url) { | 188 bool NTPSnippetsService::DiscardSnippet(const GURL& url) { |
| 191 auto it = std::find_if(snippets_.begin(), snippets_.end(), | 189 auto it = std::find_if(snippets_.begin(), snippets_.end(), |
| 192 [&url](const scoped_ptr<NTPSnippet>& snippet) { | 190 [&url](const scoped_ptr<NTPSnippet>& snippet) { |
| 193 return snippet->url() == url; | 191 return snippet->url() == url; |
| 194 }); | 192 }); |
| 195 if (it == snippets_.end()) | 193 if (it == snippets_.end()) |
| 196 return false; | 194 return false; |
| 197 discarded_snippets_.push_back(std::move(*it)); | 195 discarded_snippets_.push_back(std::move(*it)); |
| 198 snippets_.erase(it); | 196 snippets_.erase(it); |
| 199 StoreDiscardedSnippetsToPrefs(); | 197 StoreDiscardedSnippetsToPrefs(); |
| 200 StoreSnippetsToPrefs(); | 198 StoreSnippetsToPrefs(); |
| 201 return true; | 199 return true; |
| 202 } | 200 } |
| 203 | 201 |
| 204 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { | 202 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { |
| 205 observers_.AddObserver(observer); | 203 observers_.AddObserver(observer); |
| 206 if (loaded_) | 204 observer->NTPSnippetsServiceLoaded(this); |
| 207 observer->NTPSnippetsServiceLoaded(this); | |
| 208 } | 205 } |
| 209 | 206 |
| 210 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { | 207 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { |
| 211 observers_.RemoveObserver(observer); | 208 observers_.RemoveObserver(observer); |
| 212 } | 209 } |
| 213 | 210 |
| 214 void NTPSnippetsService::OnSuggestionsChanged( | 211 void NTPSnippetsService::OnSuggestionsChanged( |
| 215 const SuggestionsProfile& suggestions) { | 212 const SuggestionsProfile& suggestions) { |
| 216 std::vector<std::string> hosts = GetSuggestionsHosts(suggestions); | 213 std::vector<std::string> hosts = GetSuggestionsHosts(suggestions); |
| 217 | 214 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 const GURL& url = snippet->url(); | 295 const GURL& url = snippet->url(); |
| 299 auto it = std::find_if(snippets_.begin(), snippets_.end(), | 296 auto it = std::find_if(snippets_.begin(), snippets_.end(), |
| 300 [&url](const scoped_ptr<NTPSnippet>& old_snippet) { | 297 [&url](const scoped_ptr<NTPSnippet>& old_snippet) { |
| 301 return old_snippet->url() == url; | 298 return old_snippet->url() == url; |
| 302 }); | 299 }); |
| 303 if (it != snippets_.end()) | 300 if (it != snippets_.end()) |
| 304 *it = std::move(snippet); | 301 *it = std::move(snippet); |
| 305 else | 302 else |
| 306 snippets_.push_back(std::move(snippet)); | 303 snippets_.push_back(std::move(snippet)); |
| 307 } | 304 } |
| 308 loaded_ = true; | |
| 309 | 305 |
| 310 // Immediately remove any already-expired snippets. This will also notify our | 306 // Immediately remove any already-expired snippets. This will also notify our |
| 311 // observers and schedule the expiry timer. | 307 // observers and schedule the expiry timer. |
| 312 RemoveExpiredSnippets(); | 308 RemoveExpiredSnippets(); |
| 313 | 309 |
| 314 return true; | 310 return true; |
| 315 } | 311 } |
| 316 | 312 |
| 317 void NTPSnippetsService::LoadSnippetsFromPrefs() { | 313 void NTPSnippetsService::LoadSnippetsFromPrefs() { |
| 318 bool success = LoadFromListValue(*pref_service_->GetList(prefs::kSnippets)); | 314 bool success = LoadFromListValue(*pref_service_->GetList(prefs::kSnippets)); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 if (snippet->expiry_date() < next_expiry) | 375 if (snippet->expiry_date() < next_expiry) |
| 380 next_expiry = snippet->expiry_date(); | 376 next_expiry = snippet->expiry_date(); |
| 381 } | 377 } |
| 382 DCHECK_GT(next_expiry, expiry); | 378 DCHECK_GT(next_expiry, expiry); |
| 383 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, | 379 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, |
| 384 base::Bind(&NTPSnippetsService::RemoveExpiredSnippets, | 380 base::Bind(&NTPSnippetsService::RemoveExpiredSnippets, |
| 385 base::Unretained(this))); | 381 base::Unretained(this))); |
| 386 } | 382 } |
| 387 | 383 |
| 388 } // namespace ntp_snippets | 384 } // namespace ntp_snippets |
| OLD | NEW |