Chromium Code Reviews| 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 <algorithm> | |
| 8 #include <utility> | |
|
Marc Treib
2016/04/13 08:45:52
I think these two should stay, for std::find_if an
jkrcal
2016/04/14 15:27:00
Done.
| |
| 9 | |
| 10 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 11 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 12 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
| 13 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 14 #include "base/location.h" | 11 #include "base/location.h" |
| 15 #include "base/path_service.h" | 12 #include "base/path_service.h" |
| 16 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/task_runner_util.h" | 14 #include "base/task_runner_util.h" |
| 18 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 19 #include "base/values.h" | 16 #include "base/values.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 61 return GetFetchingInterval(switches::kFetchingIntervalWifiSeconds, | 58 return GetFetchingInterval(switches::kFetchingIntervalWifiSeconds, |
| 62 kFetchingIntervalWifiSeconds); | 59 kFetchingIntervalWifiSeconds); |
| 63 } | 60 } |
| 64 | 61 |
| 65 base::TimeDelta GetFetchingIntervalFallback() { | 62 base::TimeDelta GetFetchingIntervalFallback() { |
| 66 return GetFetchingInterval(switches::kFetchingIntervalFallbackSeconds, | 63 return GetFetchingInterval(switches::kFetchingIntervalFallbackSeconds, |
| 67 kFetchingIntervalFallbackSeconds); | 64 kFetchingIntervalFallbackSeconds); |
| 68 } | 65 } |
| 69 | 66 |
| 70 // Extracts the hosts from |suggestions| and returns them in a set. | 67 // Extracts the hosts from |suggestions| and returns them in a set. |
| 71 std::set<std::string> GetSuggestionsHosts( | 68 std::set<std::string> GetSuggestionsHostsImpl( |
| 72 const SuggestionsProfile& suggestions) { | 69 const SuggestionsProfile& suggestions) { |
| 73 std::set<std::string> hosts; | 70 std::set<std::string> hosts; |
| 74 for (int i = 0; i < suggestions.suggestions_size(); ++i) { | 71 for (int i = 0; i < suggestions.suggestions_size(); ++i) { |
| 75 const ChromeSuggestion& suggestion = suggestions.suggestions(i); | 72 const ChromeSuggestion& suggestion = suggestions.suggestions(i); |
| 76 GURL url(suggestion.url()); | 73 GURL url(suggestion.url()); |
| 77 if (url.is_valid()) | 74 if (url.is_valid()) |
| 78 hosts.insert(url.host()); | 75 hosts.insert(url.host()); |
| 79 } | 76 } |
| 80 return hosts; | 77 return hosts; |
| 81 } | 78 } |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 175 scheduler_->Unschedule(); | 172 scheduler_->Unschedule(); |
| 176 } | 173 } |
| 177 } | 174 } |
| 178 | 175 |
| 179 void NTPSnippetsService::Shutdown() { | 176 void NTPSnippetsService::Shutdown() { |
| 180 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 177 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
| 181 NTPSnippetsServiceShutdown()); | 178 NTPSnippetsServiceShutdown()); |
| 182 } | 179 } |
| 183 | 180 |
| 184 void NTPSnippetsService::FetchSnippets() { | 181 void NTPSnippetsService::FetchSnippets() { |
| 185 // |suggestions_service_| can be null in tests. | 182 // |suggestions_service_| can be null in tests. |
|
Marc Treib
2016/04/13 08:45:52
No point for this null check here anymore - please
jkrcal
2016/04/14 15:27:00
Done.
| |
| 186 if (!suggestions_service_) | 183 if (!suggestions_service_) |
| 187 return; | 184 return; |
| 188 | 185 |
| 189 FetchSnippetsImpl(GetSuggestionsHosts( | 186 FetchSnippetsFromHosts(GetSuggestionsHosts()); |
| 190 suggestions_service_->GetSuggestionsDataFromCache())); | 187 } |
| 188 | |
| 189 void NTPSnippetsService::FetchSnippetsFromHosts( | |
| 190 const std::set<std::string>& hosts) { | |
| 191 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 192 switches::kDontRestrict)) { | |
| 193 snippets_fetcher_->FetchSnippets(std::set<std::string>()); | |
| 194 return; | |
| 195 } | |
| 196 if (!hosts.empty()) | |
| 197 snippets_fetcher_->FetchSnippets(hosts); | |
| 198 } | |
| 199 | |
| 200 void NTPSnippetsService::ClearSnippets() { | |
| 201 snippets_.clear(); | |
| 202 | |
| 203 StoreSnippetsToPrefs(); | |
| 204 | |
| 205 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | |
| 206 NTPSnippetsServiceLoaded()); | |
| 207 } | |
| 208 | |
| 209 std::set<std::string> NTPSnippetsService::GetSuggestionsHosts() { | |
| 210 return GetSuggestionsHostsImpl( | |
| 211 suggestions_service_->GetSuggestionsDataFromCache()); | |
|
Marc Treib
2016/04/13 08:45:52
Can you please add a "TODO(treib)" saying that thi
jkrcal
2016/04/14 15:27:00
Done.
| |
| 191 } | 212 } |
| 192 | 213 |
| 193 bool NTPSnippetsService::DiscardSnippet(const GURL& url) { | 214 bool NTPSnippetsService::DiscardSnippet(const GURL& url) { |
| 194 auto it = std::find_if(snippets_.begin(), snippets_.end(), | 215 auto it = std::find_if(snippets_.begin(), snippets_.end(), |
| 195 [&url](const scoped_ptr<NTPSnippet>& snippet) { | 216 [&url](const scoped_ptr<NTPSnippet>& snippet) { |
| 196 return snippet->url() == url; | 217 return snippet->url() == url; |
| 197 }); | 218 }); |
| 198 if (it == snippets_.end()) | 219 if (it == snippets_.end()) |
| 199 return false; | 220 return false; |
| 200 discarded_snippets_.push_back(std::move(*it)); | 221 discarded_snippets_.push_back(std::move(*it)); |
| 201 snippets_.erase(it); | 222 snippets_.erase(it); |
| 202 StoreDiscardedSnippetsToPrefs(); | 223 StoreDiscardedSnippetsToPrefs(); |
| 203 StoreSnippetsToPrefs(); | 224 StoreSnippetsToPrefs(); |
| 225 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | |
| 226 NTPSnippetsServiceLoaded()); | |
|
Marc Treib
2016/04/13 08:45:52
misaligned
jkrcal
2016/04/14 15:27:00
Done.
| |
| 204 return true; | 227 return true; |
| 205 } | 228 } |
| 206 | 229 |
| 230 const NTPSnippetsService::NTPSnippetStorage& | |
| 231 NTPSnippetsService::GetDiscardedSnippets() { | |
| 232 return discarded_snippets_; | |
| 233 } | |
| 234 | |
| 235 void NTPSnippetsService::ClearDiscardedSnippets() { | |
| 236 discarded_snippets_.clear(); | |
| 237 StoreDiscardedSnippetsToPrefs(); | |
| 238 FetchSnippets(); | |
| 239 } | |
| 240 | |
| 207 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { | 241 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { |
| 208 observers_.AddObserver(observer); | 242 observers_.AddObserver(observer); |
| 209 observer->NTPSnippetsServiceLoaded(); | 243 observer->NTPSnippetsServiceLoaded(); |
| 210 } | 244 } |
| 211 | 245 |
| 212 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { | 246 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { |
| 213 observers_.RemoveObserver(observer); | 247 observers_.RemoveObserver(observer); |
| 214 } | 248 } |
| 215 | 249 |
| 216 void NTPSnippetsService::OnSuggestionsChanged( | 250 void NTPSnippetsService::OnSuggestionsChanged( |
| 217 const SuggestionsProfile& suggestions) { | 251 const SuggestionsProfile& suggestions) { |
| 218 std::set<std::string> hosts = GetSuggestionsHosts(suggestions); | 252 std::set<std::string> hosts = GetSuggestionsHostsImpl(suggestions); |
| 219 if (hosts == GetSnippetHostsFromPrefs()) | 253 if (hosts == GetSnippetHostsFromPrefs()) |
| 220 return; | 254 return; |
| 221 | 255 |
| 222 // Remove existing snippets that aren't in the suggestions anymore. | 256 // Remove existing snippets that aren't in the suggestions anymore. |
| 223 snippets_.erase( | 257 snippets_.erase( |
| 224 std::remove_if(snippets_.begin(), snippets_.end(), | 258 std::remove_if(snippets_.begin(), snippets_.end(), |
| 225 [&hosts](const scoped_ptr<NTPSnippet>& snippet) { | 259 [&hosts](const scoped_ptr<NTPSnippet>& snippet) { |
| 226 return !hosts.count(snippet->url().host()); | 260 return !hosts.count(snippet->url().host()); |
| 227 }), | 261 }), |
| 228 snippets_.end()); | 262 snippets_.end()); |
| 229 | 263 |
| 230 StoreSnippetsToPrefs(); | 264 StoreSnippetsToPrefs(); |
| 231 StoreSnippetHostsToPrefs(hosts); | 265 StoreSnippetHostsToPrefs(hosts); |
| 232 | 266 |
| 233 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 267 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
| 234 NTPSnippetsServiceLoaded()); | 268 NTPSnippetsServiceLoaded()); |
| 235 | 269 |
| 236 FetchSnippetsImpl(hosts); | 270 FetchSnippetsFromHosts(hosts); |
| 237 } | 271 } |
| 238 | 272 |
| 239 void NTPSnippetsService::OnSnippetsDownloaded( | 273 void NTPSnippetsService::OnSnippetsDownloaded( |
| 240 const std::string& snippets_json) { | 274 const std::string& snippets_json) { |
| 241 parse_json_callback_.Run( | 275 parse_json_callback_.Run( |
| 242 snippets_json, base::Bind(&NTPSnippetsService::OnJsonParsed, | 276 snippets_json, base::Bind(&NTPSnippetsService::OnJsonParsed, |
| 243 weak_ptr_factory_.GetWeakPtr(), snippets_json), | 277 weak_ptr_factory_.GetWeakPtr(), snippets_json), |
| 244 base::Bind(&NTPSnippetsService::OnJsonError, | 278 base::Bind(&NTPSnippetsService::OnJsonError, |
| 245 weak_ptr_factory_.GetWeakPtr(), snippets_json)); | 279 weak_ptr_factory_.GetWeakPtr(), snippets_json)); |
| 246 } | 280 } |
| 247 | 281 |
| 248 void NTPSnippetsService::OnJsonParsed(const std::string& snippets_json, | 282 void NTPSnippetsService::OnJsonParsed(const std::string& snippets_json, |
| 249 scoped_ptr<base::Value> parsed) { | 283 scoped_ptr<base::Value> parsed) { |
| 250 LOG_IF(WARNING, !LoadFromValue(*parsed)) << "Received invalid snippets: " | 284 LOG_IF(WARNING, !LoadFromValue(*parsed)) << "Received invalid snippets: " |
| 251 << snippets_json; | 285 << snippets_json; |
| 252 } | 286 } |
| 253 | 287 |
| 254 void NTPSnippetsService::OnJsonError(const std::string& snippets_json, | 288 void NTPSnippetsService::OnJsonError(const std::string& snippets_json, |
| 255 const std::string& error) { | 289 const std::string& error) { |
| 256 LOG(WARNING) << "Received invalid JSON (" << error << "): " << snippets_json; | 290 LOG(WARNING) << "Received invalid JSON (" << error << "): " << snippets_json; |
| 257 } | 291 } |
| 258 | 292 |
| 259 void NTPSnippetsService::FetchSnippetsImpl( | |
| 260 const std::set<std::string>& hosts) { | |
| 261 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 262 switches::kDontRestrict)) { | |
| 263 snippets_fetcher_->FetchSnippets(std::set<std::string>()); | |
| 264 return; | |
| 265 } | |
| 266 if (!hosts.empty()) | |
| 267 snippets_fetcher_->FetchSnippets(hosts); | |
| 268 } | |
| 269 | |
| 270 bool NTPSnippetsService::LoadFromValue(const base::Value& value) { | 293 bool NTPSnippetsService::LoadFromValue(const base::Value& value) { |
| 271 const base::DictionaryValue* top_dict = nullptr; | 294 const base::DictionaryValue* top_dict = nullptr; |
| 272 if (!value.GetAsDictionary(&top_dict)) | 295 if (!value.GetAsDictionary(&top_dict)) |
| 273 return false; | 296 return false; |
| 274 | 297 |
| 275 const base::ListValue* list = nullptr; | 298 const base::ListValue* list = nullptr; |
| 276 if (!top_dict->GetList("recos", &list)) | 299 if (!top_dict->GetList("recos", &list)) |
| 277 return false; | 300 return false; |
| 278 | 301 |
| 279 return LoadFromListValue(*list); | 302 return LoadFromListValue(*list); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 402 if (snippet->expiry_date() < next_expiry) | 425 if (snippet->expiry_date() < next_expiry) |
| 403 next_expiry = snippet->expiry_date(); | 426 next_expiry = snippet->expiry_date(); |
| 404 } | 427 } |
| 405 DCHECK_GT(next_expiry, expiry); | 428 DCHECK_GT(next_expiry, expiry); |
| 406 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, | 429 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, |
| 407 base::Bind(&NTPSnippetsService::RemoveExpiredSnippets, | 430 base::Bind(&NTPSnippetsService::RemoveExpiredSnippets, |
| 408 base::Unretained(this))); | 431 base::Unretained(this))); |
| 409 } | 432 } |
| 410 | 433 |
| 411 } // namespace ntp_snippets | 434 } // namespace ntp_snippets |
| OLD | NEW |