[AndroidWebViewShell] Replace rebaseline script with a new version using test_runner.py
[chromium-blink-merge.git] / components / suggestions / suggestions_store.cc
blob42d6dc5059df92278604464cedb5e19b3ce24c52
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/suggestions/suggestions_store.h"
7 #include <string>
9 #include "base/base64.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/time/default_clock.h"
12 #include "base/time/time.h"
13 #include "components/pref_registry/pref_registry_syncable.h"
14 #include "components/suggestions/suggestions_pref_names.h"
16 namespace suggestions {
18 SuggestionsStore::SuggestionsStore(PrefService* profile_prefs)
19 : pref_service_(profile_prefs), clock_(new base::DefaultClock()) {
20 DCHECK(profile_prefs);
23 SuggestionsStore::SuggestionsStore() {
26 SuggestionsStore::~SuggestionsStore() {}
28 void SuggestionsStore::SetClockForTesting(scoped_ptr<base::Clock> test_clock) {
29 this->clock_ = test_clock.Pass();
32 bool SuggestionsStore::LoadSuggestions(SuggestionsProfile* suggestions) {
33 DCHECK(suggestions);
35 const std::string base64_suggestions_data =
36 pref_service_->GetString(prefs::kSuggestionsData);
37 if (base64_suggestions_data.empty()) {
38 suggestions->Clear();
39 return false;
42 // If the decode process fails, assume the pref value is corrupt and clear it.
43 std::string suggestions_data;
44 if (!base::Base64Decode(base64_suggestions_data, &suggestions_data) ||
45 !suggestions->ParseFromString(suggestions_data)) {
46 VLOG(1) << "Suggestions data in profile pref is corrupt, clearing it.";
47 suggestions->Clear();
48 ClearSuggestions();
49 return false;
52 // Filter expired suggestions and update the stored suggestions if at least
53 // one was filtered. Return false if all suggestions are filtered.
54 int unfiltered_size = suggestions->suggestions_size();
55 FilterExpiredSuggestions(suggestions);
56 if (suggestions->suggestions_size() != unfiltered_size) {
57 if (!suggestions->suggestions_size()) {
58 suggestions->Clear();
59 ClearSuggestions();
60 return false;
61 } else {
62 StoreSuggestions(*suggestions);
66 return true;
69 void SuggestionsStore::FilterExpiredSuggestions(
70 SuggestionsProfile* suggestions) {
71 SuggestionsProfile filtered_suggestions;
72 int64 now_usec =
73 (this->clock_->Now() - base::Time::UnixEpoch()).ToInternalValue();
75 for (int i = 0; i < suggestions->suggestions_size(); ++i) {
76 ChromeSuggestion* suggestion = suggestions->mutable_suggestions(i);
77 if (!suggestion->has_expiry_ts() || suggestion->expiry_ts() > now_usec) {
78 filtered_suggestions.add_suggestions()->Swap(suggestion);
81 suggestions->Swap(&filtered_suggestions);
84 bool SuggestionsStore::StoreSuggestions(const SuggestionsProfile& suggestions) {
85 std::string suggestions_data;
86 if (!suggestions.SerializeToString(&suggestions_data)) return false;
88 std::string base64_suggestions_data;
89 base::Base64Encode(suggestions_data, &base64_suggestions_data);
91 pref_service_->SetString(prefs::kSuggestionsData, base64_suggestions_data);
92 return true;
95 void SuggestionsStore::ClearSuggestions() {
96 pref_service_->ClearPref(prefs::kSuggestionsData);
99 // static
100 void SuggestionsStore::RegisterProfilePrefs(
101 user_prefs::PrefRegistrySyncable* registry) {
102 registry->RegisterStringPref(prefs::kSuggestionsData, std::string());
105 } // namespace suggestions