| 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 return std::set<std::string>(); |
| 188 | 212 |
| 189 FetchSnippetsImpl(GetSuggestionsHosts( | 213 // TODO(treib) this should just call GetSnippetHostsFromPrefs |
| 190 suggestions_service_->GetSuggestionsDataFromCache())); | 214 return GetSuggestionsHostsImpl( |
| 215 suggestions_service_->GetSuggestionsDataFromCache()); |
| 191 } | 216 } |
| 192 | 217 |
| 193 bool NTPSnippetsService::DiscardSnippet(const GURL& url) { | 218 bool NTPSnippetsService::DiscardSnippet(const GURL& url) { |
| 194 auto it = std::find_if(snippets_.begin(), snippets_.end(), | 219 auto it = std::find_if(snippets_.begin(), snippets_.end(), |
| 195 [&url](const scoped_ptr<NTPSnippet>& snippet) { | 220 [&url](const scoped_ptr<NTPSnippet>& snippet) { |
| 196 return snippet->url() == url; | 221 return snippet->url() == url; |
| 197 }); | 222 }); |
| 198 if (it == snippets_.end()) | 223 if (it == snippets_.end()) |
| 199 return false; | 224 return false; |
| 200 discarded_snippets_.push_back(std::move(*it)); | 225 discarded_snippets_.push_back(std::move(*it)); |
| 201 snippets_.erase(it); | 226 snippets_.erase(it); |
| 202 StoreDiscardedSnippetsToPrefs(); | 227 StoreDiscardedSnippetsToPrefs(); |
| 203 StoreSnippetsToPrefs(); | 228 StoreSnippetsToPrefs(); |
| 229 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
| 230 NTPSnippetsServiceLoaded()); |
| 204 return true; | 231 return true; |
| 205 } | 232 } |
| 206 | 233 |
| 234 void NTPSnippetsService::ClearDiscardedSnippets() { |
| 235 discarded_snippets_.clear(); |
| 236 StoreDiscardedSnippetsToPrefs(); |
| 237 FetchSnippets(); |
| 238 } |
| 239 |
| 207 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { | 240 void NTPSnippetsService::AddObserver(NTPSnippetsServiceObserver* observer) { |
| 208 observers_.AddObserver(observer); | 241 observers_.AddObserver(observer); |
| 209 observer->NTPSnippetsServiceLoaded(); | 242 observer->NTPSnippetsServiceLoaded(); |
| 210 } | 243 } |
| 211 | 244 |
| 212 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { | 245 void NTPSnippetsService::RemoveObserver(NTPSnippetsServiceObserver* observer) { |
| 213 observers_.RemoveObserver(observer); | 246 observers_.RemoveObserver(observer); |
| 214 } | 247 } |
| 215 | 248 |
| 216 void NTPSnippetsService::OnSuggestionsChanged( | 249 void NTPSnippetsService::OnSuggestionsChanged( |
| 217 const SuggestionsProfile& suggestions) { | 250 const SuggestionsProfile& suggestions) { |
| 218 std::set<std::string> hosts = GetSuggestionsHosts(suggestions); | 251 std::set<std::string> hosts = GetSuggestionsHostsImpl(suggestions); |
| 219 if (hosts == GetSnippetHostsFromPrefs()) | 252 if (hosts == GetSnippetHostsFromPrefs()) |
| 220 return; | 253 return; |
| 221 | 254 |
| 222 // Remove existing snippets that aren't in the suggestions anymore. | 255 // Remove existing snippets that aren't in the suggestions anymore. |
| 223 snippets_.erase( | 256 snippets_.erase( |
| 224 std::remove_if(snippets_.begin(), snippets_.end(), | 257 std::remove_if(snippets_.begin(), snippets_.end(), |
| 225 [&hosts](const scoped_ptr<NTPSnippet>& snippet) { | 258 [&hosts](const scoped_ptr<NTPSnippet>& snippet) { |
| 226 return !hosts.count(snippet->url().host()); | 259 return !hosts.count(snippet->url().host()); |
| 227 }), | 260 }), |
| 228 snippets_.end()); | 261 snippets_.end()); |
| 229 | 262 |
| 230 StoreSnippetsToPrefs(); | 263 StoreSnippetsToPrefs(); |
| 231 StoreSnippetHostsToPrefs(hosts); | 264 StoreSnippetHostsToPrefs(hosts); |
| 232 | 265 |
| 233 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, | 266 FOR_EACH_OBSERVER(NTPSnippetsServiceObserver, observers_, |
| 234 NTPSnippetsServiceLoaded()); | 267 NTPSnippetsServiceLoaded()); |
| 235 | 268 |
| 236 FetchSnippetsImpl(hosts); | 269 FetchSnippetsFromHosts(hosts); |
| 237 } | 270 } |
| 238 | 271 |
| 239 void NTPSnippetsService::OnSnippetsDownloaded( | 272 void NTPSnippetsService::OnSnippetsDownloaded( |
| 240 const std::string& snippets_json) { | 273 const std::string& snippets_json) { |
| 241 parse_json_callback_.Run( | 274 parse_json_callback_.Run( |
| 242 snippets_json, base::Bind(&NTPSnippetsService::OnJsonParsed, | 275 snippets_json, base::Bind(&NTPSnippetsService::OnJsonParsed, |
| 243 weak_ptr_factory_.GetWeakPtr(), snippets_json), | 276 weak_ptr_factory_.GetWeakPtr(), snippets_json), |
| 244 base::Bind(&NTPSnippetsService::OnJsonError, | 277 base::Bind(&NTPSnippetsService::OnJsonError, |
| 245 weak_ptr_factory_.GetWeakPtr(), snippets_json)); | 278 weak_ptr_factory_.GetWeakPtr(), snippets_json)); |
| 246 } | 279 } |
| 247 | 280 |
| 248 void NTPSnippetsService::OnJsonParsed(const std::string& snippets_json, | 281 void NTPSnippetsService::OnJsonParsed(const std::string& snippets_json, |
| 249 scoped_ptr<base::Value> parsed) { | 282 scoped_ptr<base::Value> parsed) { |
| 250 LOG_IF(WARNING, !LoadFromValue(*parsed)) << "Received invalid snippets: " | 283 LOG_IF(WARNING, !LoadFromValue(*parsed)) << "Received invalid snippets: " |
| 251 << snippets_json; | 284 << snippets_json; |
| 252 } | 285 } |
| 253 | 286 |
| 254 void NTPSnippetsService::OnJsonError(const std::string& snippets_json, | 287 void NTPSnippetsService::OnJsonError(const std::string& snippets_json, |
| 255 const std::string& error) { | 288 const std::string& error) { |
| 256 LOG(WARNING) << "Received invalid JSON (" << error << "): " << snippets_json; | 289 LOG(WARNING) << "Received invalid JSON (" << error << "): " << snippets_json; |
| 257 } | 290 } |
| 258 | 291 |
| 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) { | 292 bool NTPSnippetsService::LoadFromValue(const base::Value& value) { |
| 271 const base::DictionaryValue* top_dict = nullptr; | 293 const base::DictionaryValue* top_dict = nullptr; |
| 272 if (!value.GetAsDictionary(&top_dict)) | 294 if (!value.GetAsDictionary(&top_dict)) |
| 273 return false; | 295 return false; |
| 274 | 296 |
| 275 const base::ListValue* list = nullptr; | 297 const base::ListValue* list = nullptr; |
| 276 if (!top_dict->GetList("recos", &list)) | 298 if (!top_dict->GetList("recos", &list)) |
| 277 return false; | 299 return false; |
| 278 | 300 |
| 279 return LoadFromListValue(*list); | 301 return LoadFromListValue(*list); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 402 if (snippet->expiry_date() < next_expiry) | 424 if (snippet->expiry_date() < next_expiry) |
| 403 next_expiry = snippet->expiry_date(); | 425 next_expiry = snippet->expiry_date(); |
| 404 } | 426 } |
| 405 DCHECK_GT(next_expiry, expiry); | 427 DCHECK_GT(next_expiry, expiry); |
| 406 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, | 428 expiry_timer_.Start(FROM_HERE, next_expiry - expiry, |
| 407 base::Bind(&NTPSnippetsService::RemoveExpiredSnippets, | 429 base::Bind(&NTPSnippetsService::RemoveExpiredSnippets, |
| 408 base::Unretained(this))); | 430 base::Unretained(this))); |
| 409 } | 431 } |
| 410 | 432 |
| 411 } // namespace ntp_snippets | 433 } // namespace ntp_snippets |
| OLD | NEW |