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