Home | History | Annotate | Download | only in extensions
      1 // Copyright 2012 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 CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_
      6 #define CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_
      7 
      8 #include <set>
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "base/callback.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "base/observer_list.h"
     15 #include "chrome/browser/safe_browsing/database_manager.h"
     16 #include "content/public/browser/notification_observer.h"
     17 #include "content/public/browser/notification_registrar.h"
     18 
     19 namespace extensions {
     20 
     21 class Extension;
     22 class ExtensionPrefs;
     23 
     24 // A blacklist of extensions.
     25 class Blacklist : public content::NotificationObserver,
     26                   public base::SupportsWeakPtr<Blacklist> {
     27  public:
     28   class Observer {
     29    public:
     30     // Observes |blacklist| on construction and unobserves on destruction.
     31     explicit Observer(Blacklist* blacklist);
     32 
     33     virtual void OnBlacklistUpdated() = 0;
     34 
     35    protected:
     36     virtual ~Observer();
     37 
     38    private:
     39     Blacklist* blacklist_;
     40   };
     41 
     42   class ScopedDatabaseManagerForTest {
     43    public:
     44     explicit ScopedDatabaseManagerForTest(
     45         scoped_refptr<SafeBrowsingDatabaseManager> database_manager);
     46 
     47     ~ScopedDatabaseManagerForTest();
     48 
     49    private:
     50     scoped_refptr<SafeBrowsingDatabaseManager> original_;
     51 
     52     DISALLOW_COPY_AND_ASSIGN(ScopedDatabaseManagerForTest);
     53   };
     54 
     55   enum BlacklistState {
     56     NOT_BLACKLISTED,
     57     BLACKLISTED,
     58   };
     59 
     60   typedef base::Callback<void(const std::set<std::string>&)>
     61       GetBlacklistedIDsCallback;
     62 
     63   typedef base::Callback<void(BlacklistState)> IsBlacklistedCallback;
     64 
     65   // |prefs_| must outlive this.
     66   explicit Blacklist(ExtensionPrefs* prefs);
     67 
     68   virtual ~Blacklist();
     69 
     70   // From the set of extension IDs passed in via |ids|, asynchronously checks
     71   // which are blacklisted and includes them in the resulting set passed
     72   // via |callback|, which will be sent on the caller's message loop.
     73   //
     74   // For a synchronous version which ONLY CHECKS CURRENTLY INSTALLED EXTENSIONS
     75   // see ExtensionPrefs::IsExtensionBlacklisted.
     76   void GetBlacklistedIDs(const std::set<std::string>& ids,
     77                          const GetBlacklistedIDsCallback& callback);
     78 
     79   // More convenient form of GetBlacklistedIDs for checking a single extension.
     80   void IsBlacklisted(const std::string& extension_id,
     81                      const IsBlacklistedCallback& callback);
     82 
     83   // Sets the blacklist from the updater to contain the extension IDs in |ids|
     84   void SetFromUpdater(const std::vector<std::string>& ids,
     85                       const std::string& version);
     86 
     87   // Adds/removes an observer to the blacklist.
     88   void AddObserver(Observer* observer);
     89   void RemoveObserver(Observer* observer);
     90 
     91  private:
     92   // Use via ScopedDatabaseManagerForTest.
     93   static void SetDatabaseManager(
     94       scoped_refptr<SafeBrowsingDatabaseManager> database_manager);
     95   static scoped_refptr<SafeBrowsingDatabaseManager> GetDatabaseManager();
     96 
     97   // Handles the |safebrowsing_blacklisted_ids| response from querying the
     98   // safebrowsing blacklist, given that we know |pref_blacklisted_ids| are
     99   // already blacklisted. Responds to |callback| with the union.
    100   void OnSafeBrowsingResponse(
    101       const std::set<std::string>& pref_blacklisted_ids,
    102       const GetBlacklistedIDsCallback& callback,
    103       const std::set<std::string>& safebrowsing_blacklisted_ids);
    104 
    105   // content::NotificationObserver
    106   virtual void Observe(int type,
    107                        const content::NotificationSource& source,
    108                        const content::NotificationDetails& details) OVERRIDE;
    109 
    110   ObserverList<Observer> observers_;
    111 
    112   ExtensionPrefs* const prefs_;
    113 
    114   std::set<std::string> prefs_blacklist_;
    115 
    116   content::NotificationRegistrar registrar_;
    117 
    118   DISALLOW_COPY_AND_ASSIGN(Blacklist);
    119 };
    120 
    121 }  // namespace extensions
    122 
    123 #endif  // CHROME_BROWSER_EXTENSIONS_BLACKLIST_H_
    124