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). Note that the class should be used as a |
| 29 // singleton for the upload candidacy to work properly. |
23 class BlacklistStore { | 30 class BlacklistStore { |
24 public: | 31 public: |
25 explicit BlacklistStore(PrefService* profile_prefs); | 32 BlacklistStore( |
| 33 PrefService* profile_prefs, |
| 34 const base::TimeDelta& upload_delay = base::TimeDelta::FromSeconds(15)); |
26 virtual ~BlacklistStore(); | 35 virtual ~BlacklistStore(); |
27 | 36 |
28 // Returns true if successful or |url| was already in the blacklist. | 37 // Returns true if successful or |url| was already in the blacklist. If |url| |
| 38 // was already in the blacklist, its blacklisting timestamp gets updated. |
29 virtual bool BlacklistUrl(const GURL& url); | 39 virtual bool BlacklistUrl(const GURL& url); |
30 | 40 |
31 // Sets |url| to the first URL from the blacklist. Returns false if the | 41 // Gets the time until any URL is ready for upload. Returns false if the |
32 // blacklist is empty. | 42 // blacklist is empty. |
33 virtual bool GetFirstUrlFromBlacklist(GURL* url); | 43 virtual bool GetTimeUntilReadyForUpload(base::TimeDelta* delta); |
34 | 44 |
35 // Removes |url| from the stored blacklist. Returns true if successful or if | 45 // Gets the time until |url| is ready for upload. Returns false if |url| is |
36 // |url| is not in the blacklist. | 46 // not part of the blacklist. |
| 47 virtual bool GetTimeUntilURLReadyForUpload(const GURL& url, |
| 48 base::TimeDelta* delta); |
| 49 |
| 50 // Sets |url| to a URL from the blacklist that is candidate for upload. |
| 51 // Returns false if there is no candidate for upload. |
| 52 virtual bool GetCandidateForUpload(GURL* url); |
| 53 |
| 54 // Removes |url| from the stored blacklist. Returns true if successful, false |
| 55 // on failure or if |url| was not in the blacklist. Note that this function |
| 56 // does not enforce a minimum time since blacklist before removal. |
37 virtual bool RemoveUrl(const GURL& url); | 57 virtual bool RemoveUrl(const GURL& url); |
38 | 58 |
39 // Applies the blacklist to |suggestions|. | 59 // Applies the blacklist to |suggestions|. |
40 virtual void FilterSuggestions(SuggestionsProfile* suggestions); | 60 virtual void FilterSuggestions(SuggestionsProfile* suggestions); |
41 | 61 |
42 // Register BlacklistStore related prefs in the Profile prefs. | 62 // Register BlacklistStore related prefs in the Profile prefs. |
43 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | 63 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); |
44 | 64 |
45 protected: | 65 protected: |
46 // Test seam. For simplicity of mock creation. | 66 // Test seam. For simplicity of mock creation. |
47 BlacklistStore() {} | 67 BlacklistStore() {} |
48 | 68 |
49 // Loads the blacklist data from the Profile preferences into | 69 // Loads the blacklist data from the Profile preferences into |
50 // |blacklist|. If there is a problem with loading, the pref value is | 70 // |blacklist|. If there is a problem with loading, the pref value is |
51 // cleared, false is returned and |blacklist| is cleared. If successful, | 71 // cleared, false is returned and |blacklist| is cleared. If successful, |
52 // |blacklist| will contain the loaded data and true is returned. | 72 // |blacklist| will contain the loaded data and true is returned. |
53 bool LoadBlacklist(SuggestionsBlacklist* blacklist); | 73 bool LoadBlacklist(SuggestionsBlacklist* blacklist); |
54 | 74 |
55 // Stores the provided |blacklist| to the Profile preferences, using | 75 // Stores the provided |blacklist| to the Profile preferences, using |
56 // a base64 encoding of its protobuf serialization. | 76 // a base64 encoding of its protobuf serialization. |
57 bool StoreBlacklist(const SuggestionsBlacklist& blacklist); | 77 bool StoreBlacklist(const SuggestionsBlacklist& blacklist); |
58 | 78 |
59 // Clears any blacklist data from the profile's preferences. | 79 // Clears any blacklist data from the profile's preferences. |
60 void ClearBlacklist(); | 80 void ClearBlacklist(); |
61 | 81 |
62 private: | 82 private: |
63 // The pref service used to persist the suggestions blacklist. | 83 // The pref service used to persist the suggestions blacklist. |
64 PrefService* pref_service_; | 84 PrefService* pref_service_; |
65 | 85 |
| 86 // Delay after which a URL becomes candidate for upload, measured from the |
| 87 // last time the URL was added. |
| 88 base::TimeDelta upload_delay_; |
| 89 |
| 90 // The times at which URLs were blacklisted. Used to determine when a URL is |
| 91 // valid for server upload. Guaranteed to contain URLs that are not ready for |
| 92 // upload. Might not contain URLs that are ready for upload. |
| 93 std::map<std::string, base::TimeTicks> blacklist_times_; |
| 94 |
66 DISALLOW_COPY_AND_ASSIGN(BlacklistStore); | 95 DISALLOW_COPY_AND_ASSIGN(BlacklistStore); |
67 }; | 96 }; |
68 | 97 |
69 } // namespace suggestions | 98 } // namespace suggestions |
70 | 99 |
71 #endif // COMPONENTS_SUGGESTIONS_BLACKLIST_STORE_H_ | 100 #endif // COMPONENTS_SUGGESTIONS_BLACKLIST_STORE_H_ |
OLD | NEW |