Revert of NaCl: Remove the IRT's Gyp dependency on libsrpc (patchset #1 id:1 of https...
[chromium-blink-merge.git] / components / suggestions / blacklist_store.h
blob6d1bc732311acbb91d3834878dbfc1837c1ab2b7
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_
8 #include <map>
9 #include <string>
11 #include "base/macros.h"
12 #include "base/time/time.h"
13 #include "components/suggestions/proto/suggestions.pb.h"
14 #include "url/gurl.h"
16 class PrefService;
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 {
31 public:
32 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 // Gets the time until any URL is ready for upload. Returns false if the
42 // blacklist is empty.
43 virtual bool GetTimeUntilReadyForUpload(base::TimeDelta* delta);
45 // Gets the time until |url| is ready for upload. Returns false if |url| is
46 // not part of the blacklist.
47 virtual bool GetTimeUntilURLReadyForUpload(const GURL& url,
48 base::TimeDelta* delta);
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);
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.
57 virtual bool RemoveUrl(const GURL& url);
59 // Applies the blacklist to |suggestions|.
60 virtual void FilterSuggestions(SuggestionsProfile* suggestions);
62 // Register BlacklistStore related prefs in the Profile prefs.
63 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
65 protected:
66 // Test seam. For simplicity of mock creation.
67 BlacklistStore();
69 // Loads the blacklist data from the Profile preferences into
70 // |blacklist|. If there is a problem with loading, the pref value is
71 // cleared, false is returned and |blacklist| is cleared. If successful,
72 // |blacklist| will contain the loaded data and true is returned.
73 bool LoadBlacklist(SuggestionsBlacklist* blacklist);
75 // Stores the provided |blacklist| to the Profile preferences, using
76 // a base64 encoding of its protobuf serialization.
77 bool StoreBlacklist(const SuggestionsBlacklist& blacklist);
79 // Clears any blacklist data from the profile's preferences.
80 void ClearBlacklist();
82 private:
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_