12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)//
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// The Safe Browsing service is responsible for downloading anti-phishing and
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// anti-malware tables and checking urls against them.
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef CHROME_BROWSER_SAFE_BROWSING_DATABASE_MANAGER_H_
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define CHROME_BROWSER_SAFE_BROWSING_DATABASE_MANAGER_H_
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <deque>
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <map>
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <set>
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string>
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <vector>
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/callback.h"
187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/containers/hash_tables.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/ref_counted.h"
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/synchronization/lock.h"
22eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/safe_browsing/protocol_manager.h"
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/safe_browsing/safe_browsing_util.h"
25eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/gurl.h"
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class SafeBrowsingService;
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class SafeBrowsingDatabase;
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace base {
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class Thread;
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace net {
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class URLRequestContext;
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class URLRequestContextGetter;
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace safe_browsing {
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class ClientSideDetectionService;
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class DownloadProtectionService;
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Construction needs to happen on the main thread.
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class SafeBrowsingDatabaseManager
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    : public base::RefCountedThreadSafe<SafeBrowsingDatabaseManager>,
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      public SafeBrowsingProtocolManagerDelegate {
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  class Client;
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Bundle of SafeBrowsing state while performing a URL or hash prefix check.
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  struct SafeBrowsingCheck {
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // |check_type| should correspond to the type of item that is being
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // checked, either a URL or a binary hash/URL. We store this for two
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // purposes: to know which of Client's methods to call when a result is
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // known, and for logging purposes. It *isn't* used to predict the response
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // list type, that is information that the server gives us.
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    SafeBrowsingCheck(const std::vector<GURL>& urls,
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                      const std::vector<SBFullHash>& full_hashes,
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                      Client* client,
6158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                      safe_browsing_util::ListType check_type,
6258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                      const std::vector<SBThreatType>& expected_threats);
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    ~SafeBrowsingCheck();
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Either |urls| or |full_hashes| is used to lookup database. |*_results|
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // are parallel vectors containing the results. They are initialized to
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // contain SB_THREAT_TYPE_SAFE.
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    std::vector<GURL> urls;
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    std::vector<SBThreatType> url_results;
701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    std::vector<std::string> url_metadata;
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    std::vector<SBFullHash> full_hashes;
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    std::vector<SBThreatType> full_hash_results;
732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Client* client;
752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    bool need_get_hash;
762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::TimeTicks start;  // When check was sent to SB service.
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    safe_browsing_util::ListType check_type;  // See comment in constructor.
7858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    std::vector<SBThreatType> expected_threats;
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    std::vector<SBPrefix> prefix_hits;
80cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    std::vector<SBFullHashResult> cache_hits;
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Vends weak pointers for TimeoutCallback().  If the response is
832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // received before the timeout fires, factory is destructed and
842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // the timeout won't be fired.
852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // TODO(lzheng): We should consider to use this time out check
862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // for browsing too (instead of implementin in
872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // safe_browsing_resource_handler.cc).
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    scoped_ptr<base::WeakPtrFactory<
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        SafeBrowsingDatabaseManager> > timeout_factory_;
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   private:
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    DISALLOW_COPY_AND_ASSIGN(SafeBrowsingCheck);
932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  class Client {
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   public:
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    void OnSafeBrowsingResult(const SafeBrowsingCheck& check);
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)   protected:
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    virtual ~Client() {}
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Called when the result of checking a browse URL is known.
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    virtual void OnCheckBrowseUrlResult(const GURL& url,
1041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                        SBThreatType threat_type,
1051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                        const std::string& metadata) {}
1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Called when the result of checking a download URL is known.
1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    virtual void OnCheckDownloadUrlResult(const std::vector<GURL>& url_chain,
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                          SBThreatType threat_type) {}
1102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // Called when the result of checking a set of extensions is known.
1122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    virtual void OnCheckExtensionsResult(
1132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        const std::set<std::string>& threats) {}
1142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Creates the safe browsing service.  Need to initialize before using.
1172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  explicit SafeBrowsingDatabaseManager(
1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const scoped_refptr<SafeBrowsingService>& service);
1192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns true if the url's scheme can be checked.
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool CanCheckUrl(const GURL& url) const;
1222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns whether download protection is enabled.
1242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool download_protection_enabled() const {
1252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return enable_download_protection_;
1262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  }
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Called on the IO thread to check if the given url is safe or not.  If we
1292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // can synchronously determine that the url is safe, CheckUrl returns true.
1302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Otherwise it returns false, and "client" is called asynchronously with the
1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // result when it is ready.
1322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual bool CheckBrowseUrl(const GURL& url, Client* client);
1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Check if the prefix for |url| is in safebrowsing download add lists.
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Result will be passed to callback in |client|.
1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual bool CheckDownloadUrl(const std::vector<GURL>& url_chain,
1372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                Client* client);
1382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Check which prefixes in |extension_ids| are in the safebrowsing blacklist.
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns true if not, false if further checks need to be made in which case
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // the result will be passed to |client|.
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual bool CheckExtensionIDs(const std::set<std::string>& extension_ids,
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                 Client* client);
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
14590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Check if the given url is on the side-effect free whitelist.
14690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Can be called on any thread. Returns false if the check cannot be performed
14790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // (e.g. because we are disabled or because of an invalid scheme in the URL).
14890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Otherwise, returns true if the URL is on the whitelist based on matching
14990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // the hash prefix only (so there may be false positives).
15090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  virtual bool CheckSideEffectFreeWhitelistUrl(const GURL& url);
15190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Check if the |url| matches any of the full-length hashes from the
1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // client-side phishing detection whitelist.  Returns true if there was a
1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // match and false otherwise.  To make sure we are conservative we will return
1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // true if an error occurs. This method is expected to be called on the IO
1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // thread.
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual bool MatchCsdWhitelistUrl(const GURL& url);
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Check if the given IP address (either IPv4 or IPv6) matches the malware
1600f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // IP blacklist.
1610f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  virtual bool MatchMalwareIP(const std::string& ip_address);
1620f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Check if the |url| matches any of the full-length hashes from the
1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // download whitelist.  Returns true if there was a match and false otherwise.
1652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // To make sure we are conservative we will return true if an error occurs.
1662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This method is expected to be called on the IO thread.
1672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual bool MatchDownloadWhitelistUrl(const GURL& url);
1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Check if |str| matches any of the full-length hashes from the download
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // whitelist.  Returns true if there was a match and false otherwise.
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // To make sure we are conservative we will return true if an error occurs.
1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This method is expected to be called on the IO thread.
1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual bool MatchDownloadWhitelistString(const std::string& str);
1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
17558e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  // Check if the CSD malware IP matching kill switch is turned on.
17658e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch  virtual bool IsMalwareKillSwitchOn();
17758e6fbe4ee35d65e14b626c557d37565bf8ad179Ben Murdoch
178cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Check if the CSD whitelist kill switch is turned on.
179cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual bool IsCsdWhitelistKillSwitchOn();
180cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Called on the IO thread to cancel a pending check if the result is no
1822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // longer needed.
1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void CancelCheck(Client* client);
1842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Called on the IO thread when the SafeBrowsingProtocolManager has received
1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // the full hash results for prefix hits detected in the database.
187cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void HandleGetHashResults(SafeBrowsingCheck* check,
188cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                            const std::vector<SBFullHashResult>& full_hashes,
189cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                            const base::TimeDelta& cache_lifetime);
1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Log the user perceived delay caused by SafeBrowsing. This delay is the time
1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // delta starting from when we would have started reading data from the
1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // network, and ending when the SafeBrowsing check completes indicating that
1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // the current page is 'safe'.
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void LogPauseDelay(base::TimeDelta time);
1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Called to initialize objects that are used on the io_thread.  This may be
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // called multiple times during the life of the DatabaseManager. Should be
1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // called on IO thread.
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void StartOnIOThread();
2012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Called to stop or shutdown operations on the io_thread. This may be called
2032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // multiple times during the life of the DatabaseManager. Should be called
2042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // on IO thread. If shutdown is true, the manager is disabled permanently.
2052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void StopOnIOThread(bool shutdown);
2062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) protected:
2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~SafeBrowsingDatabaseManager();
2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
21068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // protected for tests.
21168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  void NotifyDatabaseUpdateFinished(bool update_succeeded);
21268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
2132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
2142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  friend class base::RefCountedThreadSafe<SafeBrowsingDatabaseManager>;
2152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  friend class SafeBrowsingServerTest;
2162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  friend class SafeBrowsingServiceTest;
2172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  friend class SafeBrowsingServiceTestHelper;
21858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  friend class SafeBrowsingDatabaseManagerTest;
219f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(SafeBrowsingDatabaseManagerTest, GetUrlThreatType);
2202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  typedef std::set<SafeBrowsingCheck*> CurrentChecks;
2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  typedef std::vector<SafeBrowsingCheck*> GetHashRequestors;
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  typedef base::hash_map<SBPrefix, GetHashRequestors> GetHashRequests;
2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Clients that we've queued up for checking later once the database is ready.
2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  struct QueuedCheck {
22758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    QueuedCheck(const safe_browsing_util::ListType check_type,
22858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                Client* client,
22958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                const GURL& url,
23058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                const std::vector<SBThreatType>& expected_threats,
23158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                const base::TimeTicks& start);
23258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    ~QueuedCheck();
2332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    safe_browsing_util::ListType check_type;
2342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    Client* client;
2352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    GURL url;
23658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    std::vector<SBThreatType> expected_threats;
2372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::TimeTicks start;  // When check was queued.
2382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
2392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
240f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Return the threat type from the first result in |full_hashes| which matches
241f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // |hash|, or SAFE if none match.
242f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  static SBThreatType GetHashThreatType(
243f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      const SBFullHash& hash,
244f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      const std::vector<SBFullHashResult>& full_hashes);
245f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
246f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Given a URL, compare all the possible host + path full hashes to the set of
247f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // provided full hashes.  Returns the threat type of the matching result from
248f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // |full_hashes|, or SAFE if none match.
249f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  static SBThreatType GetUrlThreatType(
250f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      const GURL& url,
2511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      const std::vector<SBFullHashResult>& full_hashes,
2521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      size_t* index);
253f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
2542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Called to stop operations on the io_thread. This may be called multiple
2552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // times during the life of the DatabaseManager. Should be called on IO
2562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // thread.
2572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void DoStopOnIOThread();
2582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns whether |database_| exists and is accessible.
2602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool DatabaseAvailable() const;
2612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Called on the IO thread.  If the database does not exist, queues up a call
2632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // on the db thread to create it.  Returns whether the database is available.
2642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
2652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Note that this is only needed outside the db thread, since functions on the
2662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // db thread can call GetDatabase() directly.
2672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool MakeDatabaseAvailable();
2682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Should only be called on db thread as SafeBrowsingDatabase is not
2702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // threadsafe.
2712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SafeBrowsingDatabase* GetDatabase();
2722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Called on the IO thread with the check result.
2742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void OnCheckDone(SafeBrowsingCheck* info);
2752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Called on the database thread to retrieve chunks.
2772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void GetAllChunksFromDatabase(GetChunksCallback callback);
2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Called on the IO thread with the results of all chunks.
2802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void OnGetAllChunksFromDatabase(const std::vector<SBListChunkRanges>& lists,
2812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                  bool database_error,
2822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                  GetChunksCallback callback);
2832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Called on the IO thread after the database reports that it added a chunk.
2852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void OnAddChunksComplete(AddChunksCallback callback);
2862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Notification that the database is done loading its bloom filter.  We may
2882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // have had to queue checks until the database is ready, and if so, this
2892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // checks them.
2902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void DatabaseLoadComplete();
2912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Called on the database thread to add/remove chunks and host keys.
293f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  void AddDatabaseChunks(const std::string& list,
294f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                         scoped_ptr<ScopedVector<SBChunkData> > chunks,
2952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                         AddChunksCallback callback);
2962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
297f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  void DeleteDatabaseChunks(
298f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      scoped_ptr<std::vector<SBChunkDelete> > chunk_deletes);
2992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void NotifyClientBlockingComplete(Client* client, bool proceed);
3012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void DatabaseUpdateFinished(bool update_succeeded);
3032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Called on the db thread to close the database.  See CloseDatabase().
3052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void OnCloseDatabase();
3062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Runs on the db thread to reset the database. We assume that resetting the
3082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // database is a synchronous operation.
3092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void OnResetDatabase();
3102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Internal worker function for processing full hashes.
3122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void OnHandleGetHashResults(SafeBrowsingCheck* check,
3132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                              const std::vector<SBFullHashResult>& full_hashes);
3142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Run one check against |full_hashes|.  Returns |true| if the check
3162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // finds a match in |full_hashes|.
3172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool HandleOneCheck(SafeBrowsingCheck* check,
3182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                      const std::vector<SBFullHashResult>& full_hashes);
3192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Invoked by CheckDownloadUrl. It checks the download URL on
3212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // safe_browsing_thread_.
3222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void CheckDownloadUrlOnSBThread(SafeBrowsingCheck* check);
3232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The callback function when a safebrowsing check is timed out. Client will
3252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // be notified that the safebrowsing check is SAFE when this happens.
3262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void TimeoutCallback(SafeBrowsingCheck* check);
3272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Calls the Client's callback on IO thread after CheckDownloadUrl finishes.
3292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void CheckDownloadUrlDone(SafeBrowsingCheck* check);
3302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Checks all extension ID hashes on safe_browsing_thread_.
3322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void CheckExtensionIDsOnSBThread(SafeBrowsingCheck* check);
3332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Helper function that calls safe browsing client and cleans up |checks_|.
3352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SafeBrowsingCheckDone(SafeBrowsingCheck* check);
3362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Helper function to set |check| with default values and start a safe
3382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // browsing check with timeout of |timeout|. |task| will be called on
3392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // success, otherwise TimeoutCallback will be called.
3402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void StartSafeBrowsingCheck(SafeBrowsingCheck* check,
3412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                              const base::Closure& task);
3422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // SafeBrowsingProtocolManageDelegate override
3442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void ResetDatabase() OVERRIDE;
3452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void UpdateStarted() OVERRIDE;
3462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void UpdateFinished(bool success) OVERRIDE;
3472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void GetChunks(GetChunksCallback callback) OVERRIDE;
348f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  virtual void AddChunks(const std::string& list,
349f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                         scoped_ptr<ScopedVector<SBChunkData> > chunks,
3502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                         AddChunksCallback callback) OVERRIDE;
3512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void DeleteChunks(
352f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      scoped_ptr<std::vector<SBChunkDelete> > chunk_deletes) OVERRIDE;
3532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_refptr<SafeBrowsingService> sb_service_;
3552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  CurrentChecks checks_;
3572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Used for issuing only one GetHash request for a given prefix.
3592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  GetHashRequests gethash_requests_;
3602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The persistent database.  We don't use a scoped_ptr because it
3622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // needs to be destroyed on a different thread than this object.
3632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SafeBrowsingDatabase* database_;
3642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Lock used to prevent possible data races due to compiler optimizations.
3662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  mutable base::Lock database_lock_;
3672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Whether the service is running. 'enabled_' is used by the
3692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // SafeBrowsingDatabaseManager on the IO thread during normal operations.
3702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool enabled_;
3712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Indicate if download_protection is enabled by command switch
3732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // so we allow this feature to be exersized.
3742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool enable_download_protection_;
3752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Indicate if client-side phishing detection whitelist should be enabled
3772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // or not.
3782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool enable_csd_whitelist_;
3792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Indicate if the download whitelist should be enabled or not.
3812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool enable_download_whitelist_;
3822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Indicate if the extension blacklist should be enabled.
3842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool enable_extension_blacklist_;
3852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
38690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Indicate if the side effect free whitelist should be enabled.
38790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool enable_side_effect_free_whitelist_;
38890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Indicate if the csd malware IP blacklist should be enabled.
3900f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  bool enable_ip_blacklist_;
3910f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
3922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The SafeBrowsing thread that runs database operations.
3932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
3942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Note: Functions that run on this thread should run synchronously and return
3952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // to the IO thread, not post additional tasks back to this thread, lest we
3962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // cause a race condition at shutdown time that leads to a database leak.
3972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<base::Thread> safe_browsing_thread_;
3982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Indicates if we're currently in an update cycle.
4002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool update_in_progress_;
4012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // When true, newly fetched chunks may not in the database yet since the
4032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // database is still updating.
4042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool database_update_in_progress_;
4052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Indicates if we're in the midst of trying to close the database.  If this
4072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // is true, nothing on the IO thread should access the database.
4082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool closing_database_;
4092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::deque<QueuedCheck> queued_checks_;
4112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Timeout to use for safe browsing checks.
4132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::TimeDelta check_timeout_;
4142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(SafeBrowsingDatabaseManager);
4162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
4172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif  // CHROME_BROWSER_SAFE_BROWSING_DATABASE_MANAGER_H_
419