• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef COMPONENTS_SUGGESTIONS_BLACKLIST_STORE_H_
6 #define COMPONENTS_SUGGESTIONS_BLACKLIST_STORE_H_
7 
8 #include "base/macros.h"
9 #include "components/suggestions/proto/suggestions.pb.h"
10 #include "url/gurl.h"
11 
12 class PrefService;
13 
14 namespace user_prefs {
15 class PrefRegistrySyncable;
16 }  // namespace user_prefs
17 
18 namespace suggestions {
19 
20 // A helper class for reading, writing and modifying a small blacklist stored
21 // in the Profile preferences. It also handles SuggestionsProfile
22 // filtering based on the stored blacklist.
23 class BlacklistStore {
24  public:
25   explicit BlacklistStore(PrefService* profile_prefs);
26   virtual ~BlacklistStore();
27 
28   // Returns true if successful or |url| was already in the blacklist.
29   virtual bool BlacklistUrl(const GURL& url);
30 
31   // Sets |url| to the first URL from the blacklist. Returns false if the
32   // blacklist is empty.
33   virtual bool GetFirstUrlFromBlacklist(GURL* url);
34 
35   // Removes |url| from the stored blacklist. Returns true if successful or if
36   // |url| is not in the blacklist.
37   virtual bool RemoveUrl(const GURL& url);
38 
39   // Applies the blacklist to |suggestions|.
40   virtual void FilterSuggestions(SuggestionsProfile* suggestions);
41 
42   // Register BlacklistStore related prefs in the Profile prefs.
43   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
44 
45  protected:
46   // Test seam. For simplicity of mock creation.
BlacklistStore()47   BlacklistStore() {}
48 
49   // Loads the blacklist data from the Profile preferences into
50   // |blacklist|. If there is a problem with loading, the pref value is
51   // cleared, false is returned and |blacklist| is cleared. If successful,
52   // |blacklist| will contain the loaded data and true is returned.
53   bool LoadBlacklist(SuggestionsBlacklist* blacklist);
54 
55   // Stores the provided |blacklist| to the Profile preferences, using
56   // a base64 encoding of its protobuf serialization.
57   bool StoreBlacklist(const SuggestionsBlacklist& blacklist);
58 
59   // Clears any blacklist data from the profile's preferences.
60   void ClearBlacklist();
61 
62  private:
63   // The pref service used to persist the suggestions blacklist.
64   PrefService* pref_service_;
65 
66   DISALLOW_COPY_AND_ASSIGN(BlacklistStore);
67 };
68 
69 }  // namespace suggestions
70 
71 #endif  // COMPONENTS_SUGGESTIONS_BLACKLIST_STORE_H_
72