summaryrefslogtreecommitdiff
path: root/chromium/components/subresource_filter/content/browser/fake_safe_browsing_database_manager.h
blob: 764fbdf33056a977c2769035c7fd6345cff29ff6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_FAKE_SAFE_BROWSING_DATABASE_MANAGER_H_
#define COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_FAKE_SAFE_BROWSING_DATABASE_MANAGER_H_

#include <map>
#include <set>

#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "components/safe_browsing/db/test_database_manager.h"
#include "content/public/common/resource_type.h"

class GURL;

// Database manager that allows any URL to be configured as blacklisted for
// testing.
class FakeSafeBrowsingDatabaseManager
    : public safe_browsing::TestSafeBrowsingDatabaseManager {
 public:
  FakeSafeBrowsingDatabaseManager();

  void AddBlacklistedUrl(const GURL& url,
                         safe_browsing::SBThreatType threat_type,
                         const safe_browsing::ThreatMetadata& metadata);
  void AddBlacklistedUrl(const GURL& url,
                         safe_browsing::SBThreatType threat_type,
                         safe_browsing::ThreatPatternType pattern_type =
                             safe_browsing::ThreatPatternType::NONE);
  void RemoveBlacklistedUrl(const GURL& url);
  void RemoveAllBlacklistedUrls();

  void SimulateTimeout();

  // If set, will synchronously fail from CheckUrlForSubresourceFilter rather
  // than posting a task to fail if the URL does not match the blacklist.
  void set_synchronous_failure() { synchronous_failure_ = true; }

 protected:
  ~FakeSafeBrowsingDatabaseManager() override;

  // safe_browsing::TestSafeBrowsingDatabaseManager:
  bool CheckUrlForSubresourceFilter(const GURL& url, Client* client) override;
  bool CheckResourceUrl(const GURL& url, Client* client) override;
  bool IsSupported() const override;
  void CancelCheck(Client* client) override;
  bool ChecksAreAlwaysAsync() const override;
  bool CanCheckResourceType(
      content::ResourceType /* resource_type */) const override;
  bool CanCheckSubresourceFilter() const override;
  safe_browsing::ThreatSource GetThreatSource() const override;
  bool CheckExtensionIDs(const std::set<std::string>& extension_ids,
                         Client* client) override;

 private:
  void OnCheckUrlForSubresourceFilterComplete(Client* client, const GURL& url);

  std::set<Client*> checks_;
  std::map<
      GURL,
      std::pair<safe_browsing::SBThreatType, safe_browsing::ThreatMetadata>>
      url_to_threat_type_;
  bool simulate_timeout_ = false;
  bool synchronous_failure_ = false;

  base::WeakPtrFactory<FakeSafeBrowsingDatabaseManager> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(FakeSafeBrowsingDatabaseManager);
};

#endif  // COMPONENTS_SUBRESOURCE_FILTER_CONTENT_BROWSER_FAKE_SAFE_BROWSING_DATABASE_MANAGER_H_