Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(190)

Side by Side Diff: components/suggestions/suggestions_store.cc

Issue 423133003: [Suggestions Service] Add support for expiring the SuggestionsStore (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Coding style fixes and refactoring Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/time.h"
11 #include "components/pref_registry/pref_registry_syncable.h" 12 #include "components/pref_registry/pref_registry_syncable.h"
12 #include "components/suggestions/suggestions_pref_names.h" 13 #include "components/suggestions/suggestions_pref_names.h"
13 14
14 namespace suggestions { 15 namespace suggestions {
15 16
16 SuggestionsStore::SuggestionsStore(PrefService* profile_prefs) 17 SuggestionsStore::SuggestionsStore(PrefService* profile_prefs)
17 : pref_service_(profile_prefs) { 18 : pref_service_(profile_prefs) {
18 DCHECK(profile_prefs); 19 DCHECK(profile_prefs);
19 } 20 }
20 21
(...skipping 12 matching lines...) Expand all
33 // If the decode process fails, assume the pref value is corrupt and clear it. 34 // If the decode process fails, assume the pref value is corrupt and clear it.
34 std::string suggestions_data; 35 std::string suggestions_data;
35 if (!base::Base64Decode(base64_suggestions_data, &suggestions_data) || 36 if (!base::Base64Decode(base64_suggestions_data, &suggestions_data) ||
36 !suggestions->ParseFromString(suggestions_data)) { 37 !suggestions->ParseFromString(suggestions_data)) {
37 VLOG(1) << "Suggestions data in profile pref is corrupt, clearing it."; 38 VLOG(1) << "Suggestions data in profile pref is corrupt, clearing it.";
38 suggestions->Clear(); 39 suggestions->Clear();
39 ClearSuggestions(); 40 ClearSuggestions();
40 return false; 41 return false;
41 } 42 }
42 43
44 // Filter expired suggestions and update the stored suggestions if
45 // at least one was filtered. Return false if all suggestions are filtered.
Mathieu 2014/08/04 14:39:11 move as many words to previous line as you can.
gayane -on leave until 09-2017 2014/08/04 16:34:58 Done.
46 int unfiltered_size = suggestions->suggestions_size();
47 FilterExpiredSuggestions(suggestions);
48 if (suggestions->suggestions_size() != unfiltered_size){
49 StoreSuggestions(*suggestions);
50 }
51 if (suggestions->suggestions_size() == 0){
Mathieu 2014/08/04 14:39:11 if (!suggestions->suggesitons_size())
gayane -on leave until 09-2017 2014/08/04 16:34:59 Done.
52 suggestions->Clear();
53 return false;
54 }
55
43 return true; 56 return true;
44 } 57 }
45 58
59 void SuggestionsStore::FilterExpiredSuggestions(
60 SuggestionsProfile* suggestions) {
61 SuggestionsProfile* filteredSuggestions = new SuggestionsProfile();
Mathieu 2014/08/04 14:39:11 to construct easily: SuggestionsProfile filtered_
gayane -on leave until 09-2017 2014/08/04 16:34:58 Done.
62 int64 now_usec = (base::Time::NowFromSystemTime() - base::Time::UnixEpoch())
63 .ToInternalValue();
64
65 for (int i = 0; i < suggestions->suggestions_size(); ++i) {
66 ChromeSuggestion* suggestion = suggestions->mutable_suggestions(i);
67 if (!suggestion->has_expiry_ts() || suggestion->expiry_ts() > now_usec) {
68 ChromeSuggestion* tmp = filteredSuggestions->add_suggestions();
Mathieu 2014/08/04 14:39:10 Can probably do filteredSuggestions->add_suggestio
gayane -on leave until 09-2017 2014/08/04 16:34:58 Done.
69 tmp->Swap(suggestion);
70 }
71 }
72 suggestions->Swap(filteredSuggestions);
73 }
74
46 bool SuggestionsStore::StoreSuggestions(const SuggestionsProfile& suggestions) { 75 bool SuggestionsStore::StoreSuggestions(const SuggestionsProfile& suggestions) {
47 std::string suggestions_data; 76 std::string suggestions_data;
48 if (!suggestions.SerializeToString(&suggestions_data)) return false; 77 if (!suggestions.SerializeToString(&suggestions_data)) return false;
49 78
50 std::string base64_suggestions_data; 79 std::string base64_suggestions_data;
51 base::Base64Encode(suggestions_data, &base64_suggestions_data); 80 base::Base64Encode(suggestions_data, &base64_suggestions_data);
52 81
53 pref_service_->SetString(prefs::kSuggestionsData, base64_suggestions_data); 82 pref_service_->SetString(prefs::kSuggestionsData, base64_suggestions_data);
54 return true; 83 return true;
55 } 84 }
56 85
57 void SuggestionsStore::ClearSuggestions() { 86 void SuggestionsStore::ClearSuggestions() {
58 pref_service_->ClearPref(prefs::kSuggestionsData); 87 pref_service_->ClearPref(prefs::kSuggestionsData);
59 } 88 }
60 89
61 // static 90 // static
62 void SuggestionsStore::RegisterProfilePrefs( 91 void SuggestionsStore::RegisterProfilePrefs(
63 user_prefs::PrefRegistrySyncable* registry) { 92 user_prefs::PrefRegistrySyncable* registry) {
64 registry->RegisterStringPref( 93 registry->RegisterStringPref(
65 prefs::kSuggestionsData, std::string(), 94 prefs::kSuggestionsData, std::string(),
66 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 95 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
67 } 96 }
68 97
69 } // namespace suggestions 98 } // namespace suggestions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698