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 <ctime> | |
manzagop (departed)
2014/07/31 15:31:51
Used?
gayane -on leave until 09-2017
2014/08/04 13:46:31
Removed
| |
7 #include <string> | 8 #include <string> |
8 | 9 |
9 #include "base/base64.h" | 10 #include "base/base64.h" |
10 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
12 #include "base/time/time.h" | |
11 #include "components/pref_registry/pref_registry_syncable.h" | 13 #include "components/pref_registry/pref_registry_syncable.h" |
12 #include "components/suggestions/suggestions_pref_names.h" | 14 #include "components/suggestions/suggestions_pref_names.h" |
13 | 15 |
16 | |
manzagop (departed)
2014/07/31 15:31:51
No need for this line.
gayane -on leave until 09-2017
2014/08/04 13:46:31
Done.
| |
14 namespace suggestions { | 17 namespace suggestions { |
15 | 18 |
16 SuggestionsStore::SuggestionsStore(PrefService* profile_prefs) | 19 SuggestionsStore::SuggestionsStore(PrefService* profile_prefs) |
17 : pref_service_(profile_prefs) { | 20 : pref_service_(profile_prefs) { |
18 DCHECK(profile_prefs); | 21 DCHECK(profile_prefs); |
19 } | 22 } |
20 | 23 |
21 SuggestionsStore::~SuggestionsStore() {} | 24 SuggestionsStore::~SuggestionsStore() {} |
22 | 25 |
23 bool SuggestionsStore::LoadSuggestions(SuggestionsProfile* suggestions) { | 26 bool SuggestionsStore::LoadSuggestions(SuggestionsProfile* suggestions) { |
24 DCHECK(suggestions); | 27 DCHECK(suggestions); |
25 | 28 |
26 const std::string base64_suggestions_data = | 29 const std::string base64_suggestions_data = |
27 pref_service_->GetString(prefs::kSuggestionsData); | 30 pref_service_->GetString(prefs::kSuggestionsData); |
28 if (base64_suggestions_data.empty()) { | 31 if (base64_suggestions_data.empty()) { |
29 suggestions->Clear(); | 32 suggestions->Clear(); |
30 return false; | 33 return false; |
31 } | 34 } |
32 | 35 |
33 // If the decode process fails, assume the pref value is corrupt and clear it. | 36 // If the decode process fails, assume the pref value is corrupt and clear it. |
34 std::string suggestions_data; | 37 std::string suggestions_data; |
35 if (!base::Base64Decode(base64_suggestions_data, &suggestions_data) || | 38 if (!base::Base64Decode(base64_suggestions_data, &suggestions_data) || |
36 !suggestions->ParseFromString(suggestions_data)) { | 39 !suggestions->ParseFromString(suggestions_data)) { |
37 VLOG(1) << "Suggestions data in profile pref is corrupt, clearing it."; | 40 VLOG(1) << "Suggestions data in profile pref is corrupt, clearing it."; |
38 suggestions->Clear(); | 41 suggestions->Clear(); |
39 ClearSuggestions(); | 42 ClearSuggestions(); |
40 return false; | 43 return false; |
41 } | 44 } |
42 | 45 |
46 // save number of suggestions before filtering expired suggestions | |
manzagop (departed)
2014/07/31 15:31:51
Capitalize Save
gayane -on leave until 09-2017
2014/08/04 13:46:30
Done.
| |
47 int unfiltered_size = suggestions->suggestions_size(); | |
48 FilterExpiredSuggestions(suggestions); | |
49 | |
50 // if any suggestion was expired update stored suggestions. | |
51 if (suggestions->suggestions_size() != unfiltered_size){ | |
52 StoreSuggestions(*suggestions); | |
53 } | |
54 | |
55 // if all suggestions were expired return false. | |
manzagop (departed)
2014/07/31 15:31:51
These 3 code blocks don't need to be seperated by
gayane -on leave until 09-2017
2014/08/04 13:46:31
Done.
| |
56 if (suggestions->suggestions_size() == 0){ | |
57 suggestions->Clear(); | |
58 return false; | |
59 } | |
60 | |
43 return true; | 61 return true; |
44 } | 62 } |
45 | 63 |
64 // private | |
manzagop (departed)
2014/07/31 15:31:51
I think I've only seen this kind of comment for st
gayane -on leave until 09-2017
2014/08/04 13:46:31
Done.
| |
65 void SuggestionsStore::FilterExpiredSuggestions( | |
66 SuggestionsProfile* suggestions) { | |
67 SuggestionsProfile* filteredSuggestions = new SuggestionsProfile(); | |
68 int64 now = (base::Time::NowFromSystemTime() | |
69 -base::Time::UnixEpoch()).ToInternalValue(); | |
manzagop (departed)
2014/07/31 15:31:50
Indentation. See suggestions_source for details.
gayane -on leave until 09-2017
2014/08/04 13:46:31
Done.
| |
70 | |
71 for (int i = 0; i < suggestions->suggestions_size(); i++) { | |
manzagop (departed)
2014/07/31 15:31:50
Even though not mandatory in this case, I believe
gayane -on leave until 09-2017
2014/08/04 13:46:31
Done.
| |
72 auto* suggestion = suggestions->mutable_suggestions(i); | |
manzagop (departed)
2014/07/31 15:31:50
AFAIK auto is not allowed yet since chromium hasn'
gayane -on leave until 09-2017
2014/08/04 13:46:31
Done.
| |
73 | |
74 // copy if valid | |
manzagop (departed)
2014/07/31 15:31:51
Comment does not add much so I'd remove it. You're
gayane -on leave until 09-2017
2014/08/04 13:46:30
Done.
| |
75 if (!suggestion->has_expiry_ts() || suggestion->expiry_ts() > now) { | |
76 ChromeSuggestion* tmp = filteredSuggestions->add_suggestions(); | |
77 tmp->Swap(suggestion); | |
78 } | |
79 } | |
80 | |
81 suggestions->Swap(filteredSuggestions); | |
82 } | |
83 | |
46 bool SuggestionsStore::StoreSuggestions(const SuggestionsProfile& suggestions) { | 84 bool SuggestionsStore::StoreSuggestions(const SuggestionsProfile& suggestions) { |
47 std::string suggestions_data; | 85 std::string suggestions_data; |
48 if (!suggestions.SerializeToString(&suggestions_data)) return false; | 86 if (!suggestions.SerializeToString(&suggestions_data)) return false; |
49 | 87 |
50 std::string base64_suggestions_data; | 88 std::string base64_suggestions_data; |
51 base::Base64Encode(suggestions_data, &base64_suggestions_data); | 89 base::Base64Encode(suggestions_data, &base64_suggestions_data); |
52 | 90 |
53 pref_service_->SetString(prefs::kSuggestionsData, base64_suggestions_data); | 91 pref_service_->SetString(prefs::kSuggestionsData, base64_suggestions_data); |
54 return true; | 92 return true; |
55 } | 93 } |
56 | 94 |
57 void SuggestionsStore::ClearSuggestions() { | 95 void SuggestionsStore::ClearSuggestions() { |
58 pref_service_->ClearPref(prefs::kSuggestionsData); | 96 pref_service_->ClearPref(prefs::kSuggestionsData); |
59 } | 97 } |
60 | 98 |
61 // static | 99 // static |
62 void SuggestionsStore::RegisterProfilePrefs( | 100 void SuggestionsStore::RegisterProfilePrefs( |
63 user_prefs::PrefRegistrySyncable* registry) { | 101 user_prefs::PrefRegistrySyncable* registry) { |
64 registry->RegisterStringPref( | 102 registry->RegisterStringPref( |
65 prefs::kSuggestionsData, std::string(), | 103 prefs::kSuggestionsData, std::string(), |
66 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 104 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
67 } | 105 } |
68 | 106 |
69 } // namespace suggestions | 107 } // namespace suggestions |
OLD | NEW |