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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_SUGGESTIONS_BLACKLIST_STORE_H_
6 #define COMPONENTS_SUGGESTIONS_BLACKLIST_STORE_H_
11 #include "base/macros.h"
12 #include "base/time/time.h"
13 #include "components/suggestions/proto/suggestions.pb.h"
18 namespace user_prefs
{
19 class PrefRegistrySyncable
;
20 } // namespace user_prefs
22 namespace suggestions
{
24 // A helper class for reading, writing, modifying and applying a small URL
25 // blacklist, pending upload to the server. The class has a concept of time
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.
30 class BlacklistStore
{
33 PrefService
* profile_prefs
,
34 const base::TimeDelta
& upload_delay
= base::TimeDelta::FromSeconds(15));
35 virtual ~BlacklistStore();
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.
39 virtual bool BlacklistUrl(const GURL
& url
);
41 // Clears any blacklist data from the profile's preferences.
42 virtual void ClearBlacklist();
44 // Gets the time until any URL is ready for upload. Returns false if the
45 // blacklist is empty.
46 virtual bool GetTimeUntilReadyForUpload(base::TimeDelta
* delta
);
48 // Gets the time until |url| is ready for upload. Returns false if |url| is
49 // not part of the blacklist.
50 virtual bool GetTimeUntilURLReadyForUpload(const GURL
& url
,
51 base::TimeDelta
* delta
);
53 // Sets |url| to a URL from the blacklist that is candidate for upload.
54 // Returns false if there is no candidate for upload.
55 virtual bool GetCandidateForUpload(GURL
* url
);
57 // Removes |url| from the stored blacklist. Returns true if successful, false
58 // on failure or if |url| was not in the blacklist. Note that this function
59 // does not enforce a minimum time since blacklist before removal.
60 virtual bool RemoveUrl(const GURL
& url
);
62 // Applies the blacklist to |suggestions|.
63 virtual void FilterSuggestions(SuggestionsProfile
* suggestions
);
65 // Register BlacklistStore related prefs in the Profile prefs.
66 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
69 // Test seam. For simplicity of mock creation.
72 // Loads the blacklist data from the Profile preferences into
73 // |blacklist|. If there is a problem with loading, the pref value is
74 // cleared, false is returned and |blacklist| is cleared. If successful,
75 // |blacklist| will contain the loaded data and true is returned.
76 bool LoadBlacklist(SuggestionsBlacklist
* blacklist
);
78 // Stores the provided |blacklist| to the Profile preferences, using
79 // a base64 encoding of its protobuf serialization.
80 bool StoreBlacklist(const SuggestionsBlacklist
& blacklist
);
83 // The pref service used to persist the suggestions blacklist.
84 PrefService
* pref_service_
;
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_
;
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_
;
95 DISALLOW_COPY_AND_ASSIGN(BlacklistStore
);
98 } // namespace suggestions
100 #endif // COMPONENTS_SUGGESTIONS_BLACKLIST_STORE_H_