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 #ifndef COMPONENTS_SUGGESTIONS_BLACKLIST_STORE_H_ | 5 #ifndef COMPONENTS_SUGGESTIONS_BLACKLIST_STORE_H_ |
6 #define COMPONENTS_SUGGESTIONS_BLACKLIST_STORE_H_ | 6 #define COMPONENTS_SUGGESTIONS_BLACKLIST_STORE_H_ |
7 | 7 |
8 #include <map> | |
9 #include <string> | |
10 | |
8 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/time/time.h" | |
9 #include "components/suggestions/proto/suggestions.pb.h" | 13 #include "components/suggestions/proto/suggestions.pb.h" |
10 #include "url/gurl.h" | 14 #include "url/gurl.h" |
11 | 15 |
12 class PrefService; | 16 class PrefService; |
13 | 17 |
14 namespace user_prefs { | 18 namespace user_prefs { |
15 class PrefRegistrySyncable; | 19 class PrefRegistrySyncable; |
16 } // namespace user_prefs | 20 } // namespace user_prefs |
17 | 21 |
18 namespace suggestions { | 22 namespace suggestions { |
19 | 23 |
20 // A helper class for reading, writing and modifying a small blacklist stored | 24 // A helper class for reading, writing, modifying and applying a small URL |
21 // in the Profile preferences. It also handles SuggestionsProfile | 25 // blacklist, pending upload to the server. The class has a concept of time |
22 // filtering based on the stored blacklist. | 26 // duration before which a blacklisted URL becomes candidate for upload to the |
27 // server. Keep in mind most of the operations involve interaction with the disk | |
28 // (the profile's preferences). | |
23 class BlacklistStore { | 29 class BlacklistStore { |
24 public: | 30 public: |
25 explicit BlacklistStore(PrefService* profile_prefs); | 31 BlacklistStore( |
32 PrefService* profile_prefs, | |
33 const base::TimeDelta& upload_delay = base::TimeDelta::FromSeconds(15)); | |
26 virtual ~BlacklistStore(); | 34 virtual ~BlacklistStore(); |
27 | 35 |
28 // Returns true if successful or |url| was already in the blacklist. | 36 // Returns true if successful or |url| was already in the blacklist. If |url| |
37 // was already in the blacklist, its blacklisting timestamp gets updated. | |
29 virtual bool BlacklistUrl(const GURL& url); | 38 virtual bool BlacklistUrl(const GURL& url); |
30 | 39 |
31 // Sets |url| to the first URL from the blacklist. Returns false if the | 40 // Returns true if the blacklist is empty. |
41 virtual bool IsEmpty(); | |
Mathieu
2014/12/04 18:53:32
does this need to be public? I can't really see ho
manzagop (departed)
2014/12/05 15:13:22
Unused. Got rid of the function.
| |
42 | |
43 // Gets the time until any URL is ready for upload. Returns false if the | |
32 // blacklist is empty. | 44 // blacklist is empty. |
33 virtual bool GetFirstUrlFromBlacklist(GURL* url); | 45 virtual bool GetTimeUntilReadyForUpload(base::TimeDelta* delta); |
34 | 46 |
35 // Removes |url| from the stored blacklist. Returns true if successful or if | 47 // Gets the time until |url| is ready for upload. Returns false if |url| is |
36 // |url| is not in the blacklist. | 48 // not part of the blacklist. |
49 virtual bool GetTimeUntilReadyForUpload(const GURL& url, | |
Mathieu
2014/12/04 18:53:32
I get easily confused by two methods with identica
manzagop (departed)
2014/12/05 15:13:22
Done.
| |
50 base::TimeDelta* delta); | |
51 | |
52 // Sets |url| to a URL from the blacklist that is candidate for upload. | |
53 // Returns false if there is no candidate for upload. | |
54 virtual bool GetCandidateForUpload(GURL* url); | |
55 | |
56 // Removes |url| from the stored blacklist. Returns true if successful, false | |
57 // on failure or if |url| was not in the blacklist. Note that this function | |
58 // does not enforce a minimum time since blacklist before removal. | |
37 virtual bool RemoveUrl(const GURL& url); | 59 virtual bool RemoveUrl(const GURL& url); |
38 | 60 |
39 // Applies the blacklist to |suggestions|. | 61 // Applies the blacklist to |suggestions|. |
40 virtual void FilterSuggestions(SuggestionsProfile* suggestions); | 62 virtual void FilterSuggestions(SuggestionsProfile* suggestions); |
41 | 63 |
42 // Register BlacklistStore related prefs in the Profile prefs. | 64 // Register BlacklistStore related prefs in the Profile prefs. |
43 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | 65 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); |
44 | 66 |
45 protected: | 67 protected: |
46 // Test seam. For simplicity of mock creation. | 68 // Test seam. For simplicity of mock creation. |
47 BlacklistStore() {} | 69 BlacklistStore() {} |
48 | 70 |
49 // Loads the blacklist data from the Profile preferences into | 71 // Loads the blacklist data from the Profile preferences into |
50 // |blacklist|. If there is a problem with loading, the pref value is | 72 // |blacklist|. If there is a problem with loading, the pref value is |
51 // cleared, false is returned and |blacklist| is cleared. If successful, | 73 // cleared, false is returned and |blacklist| is cleared. If successful, |
52 // |blacklist| will contain the loaded data and true is returned. | 74 // |blacklist| will contain the loaded data and true is returned. |
53 bool LoadBlacklist(SuggestionsBlacklist* blacklist); | 75 bool LoadBlacklist(SuggestionsBlacklist* blacklist); |
54 | 76 |
55 // Stores the provided |blacklist| to the Profile preferences, using | 77 // Stores the provided |blacklist| to the Profile preferences, using |
56 // a base64 encoding of its protobuf serialization. | 78 // a base64 encoding of its protobuf serialization. |
57 bool StoreBlacklist(const SuggestionsBlacklist& blacklist); | 79 bool StoreBlacklist(const SuggestionsBlacklist& blacklist); |
58 | 80 |
59 // Clears any blacklist data from the profile's preferences. | 81 // Clears any blacklist data from the profile's preferences. |
60 void ClearBlacklist(); | 82 void ClearBlacklist(); |
61 | 83 |
62 private: | 84 private: |
63 // The pref service used to persist the suggestions blacklist. | 85 // The pref service used to persist the suggestions blacklist. |
64 PrefService* pref_service_; | 86 PrefService* pref_service_; |
65 | 87 |
88 // Delay after which a URL becomes candidate for upload, measured from the | |
89 // last time the URL was added. | |
90 base::TimeDelta upload_delay_; | |
91 | |
92 // The times at which URLs were blacklisted. Used to determine when a URL is | |
93 // valid for server upload. Guaranteed to contain URLs that are not ready for | |
94 // upload. Might not contain URLs that are ready for upload. | |
95 std::map<std::string, base::TimeTicks> blacklist_times_; | |
96 | |
66 DISALLOW_COPY_AND_ASSIGN(BlacklistStore); | 97 DISALLOW_COPY_AND_ASSIGN(BlacklistStore); |
67 }; | 98 }; |
68 | 99 |
69 } // namespace suggestions | 100 } // namespace suggestions |
70 | 101 |
71 #endif // COMPONENTS_SUGGESTIONS_BLACKLIST_STORE_H_ | 102 #endif // COMPONENTS_SUGGESTIONS_BLACKLIST_STORE_H_ |
OLD | NEW |