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