OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/suggestions/suggestions_store.h" | 5 #include "components/suggestions/suggestions_store.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64.h" |
10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
11 #include "base/time/default_clock.h" | |
11 #include "base/time/time.h" | 12 #include "base/time/time.h" |
12 #include "components/pref_registry/pref_registry_syncable.h" | 13 #include "components/pref_registry/pref_registry_syncable.h" |
13 #include "components/suggestions/suggestions_pref_names.h" | 14 #include "components/suggestions/suggestions_pref_names.h" |
14 | 15 |
15 namespace suggestions { | 16 namespace suggestions { |
16 | 17 |
17 SuggestionsStore::SuggestionsStore(PrefService* profile_prefs) | 18 SuggestionsStore::SuggestionsStore(PrefService* profile_prefs) |
18 : pref_service_(profile_prefs) { | 19 : pref_service_(profile_prefs), |
20 clock_(new base::DefaultClock()) { | |
Matt Giuca
2014/12/18 22:51:13
Nit: Indentation is wrong. (Run git cl format?)
gayane -on leave until 09-2017
2014/12/22 16:29:38
Done.
| |
19 DCHECK(profile_prefs); | 21 DCHECK(profile_prefs); |
20 } | 22 } |
21 | 23 |
24 SuggestionsStore::SuggestionsStore() {} | |
25 | |
22 SuggestionsStore::~SuggestionsStore() {} | 26 SuggestionsStore::~SuggestionsStore() {} |
23 | 27 |
28 void SuggestionsStore::SetClockForTesting(scoped_ptr<base::Clock> test_clock) { | |
29 this->clock_ = test_clock.Pass(); | |
30 } | |
31 | |
24 bool SuggestionsStore::LoadSuggestions(SuggestionsProfile* suggestions) { | 32 bool SuggestionsStore::LoadSuggestions(SuggestionsProfile* suggestions) { |
25 DCHECK(suggestions); | 33 DCHECK(suggestions); |
26 | 34 |
27 const std::string base64_suggestions_data = | 35 const std::string base64_suggestions_data = |
28 pref_service_->GetString(prefs::kSuggestionsData); | 36 pref_service_->GetString(prefs::kSuggestionsData); |
29 if (base64_suggestions_data.empty()) { | 37 if (base64_suggestions_data.empty()) { |
30 suggestions->Clear(); | 38 suggestions->Clear(); |
31 return false; | 39 return false; |
32 } | 40 } |
33 | 41 |
(...skipping 20 matching lines...) Expand all Loading... | |
54 StoreSuggestions(*suggestions); | 62 StoreSuggestions(*suggestions); |
55 } | 63 } |
56 } | 64 } |
57 | 65 |
58 return true; | 66 return true; |
59 } | 67 } |
60 | 68 |
61 void SuggestionsStore::FilterExpiredSuggestions( | 69 void SuggestionsStore::FilterExpiredSuggestions( |
62 SuggestionsProfile* suggestions) { | 70 SuggestionsProfile* suggestions) { |
63 SuggestionsProfile filtered_suggestions; | 71 SuggestionsProfile filtered_suggestions; |
64 int64 now_usec = (base::Time::NowFromSystemTime() - base::Time::UnixEpoch()) | 72 int64 now_usec = (this->clock_->Now() - base::Time::UnixEpoch()) |
65 .ToInternalValue(); | 73 .ToInternalValue(); |
66 | 74 |
67 for (int i = 0; i < suggestions->suggestions_size(); ++i) { | 75 for (int i = 0; i < suggestions->suggestions_size(); ++i) { |
68 ChromeSuggestion* suggestion = suggestions->mutable_suggestions(i); | 76 ChromeSuggestion* suggestion = suggestions->mutable_suggestions(i); |
69 if (!suggestion->has_expiry_ts() || suggestion->expiry_ts() > now_usec) { | 77 if (!suggestion->has_expiry_ts() || suggestion->expiry_ts() > now_usec) { |
70 filtered_suggestions.add_suggestions()->Swap(suggestion); | 78 filtered_suggestions.add_suggestions()->Swap(suggestion); |
71 } | 79 } |
72 } | 80 } |
73 suggestions->Swap(&filtered_suggestions); | 81 suggestions->Swap(&filtered_suggestions); |
74 } | 82 } |
(...skipping 15 matching lines...) Expand all Loading... | |
90 | 98 |
91 // static | 99 // static |
92 void SuggestionsStore::RegisterProfilePrefs( | 100 void SuggestionsStore::RegisterProfilePrefs( |
93 user_prefs::PrefRegistrySyncable* registry) { | 101 user_prefs::PrefRegistrySyncable* registry) { |
94 registry->RegisterStringPref( | 102 registry->RegisterStringPref( |
95 prefs::kSuggestionsData, std::string(), | 103 prefs::kSuggestionsData, std::string(), |
96 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 104 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
97 } | 105 } |
98 | 106 |
99 } // namespace suggestions | 107 } // namespace suggestions |
OLD | NEW |