File: blocklist.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (172 lines) | stat: -rw-r--r-- 5,817 bytes parent folder | download | duplicates (5)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_EXTENSIONS_BLOCKLIST_H_
#define CHROME_BROWSER_EXTENSIONS_BLOCKLIST_H_

#include <list>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>

#include "base/callback_list.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "components/keyed_service/core/keyed_service.h"
#include "extensions/browser/blocklist_state.h"
#include "extensions/buildflags/buildflags.h"
#include "extensions/common/extension_id.h"

static_assert(BUILDFLAG(ENABLE_EXTENSIONS_CORE));

namespace content {
class BrowserContext;
}

namespace safe_browsing {
class SafeBrowsingDatabaseManager;
}

namespace extensions {

class BlocklistStateFetcher;

// The blocklist of extensions backed by safe browsing.
class Blocklist : public KeyedService {
 public:
  class Observer {
   public:
    // Observes `blocklist` on construction and unobserves on destruction.
    explicit Observer(Blocklist* blocklist);

    virtual void OnBlocklistUpdated() = 0;

   protected:
    virtual ~Observer();

   private:
    raw_ptr<Blocklist> blocklist_;
  };

  using BlocklistStateMap = std::map<ExtensionId, BlocklistState>;

  using GetBlocklistedIDsCallback =
      base::OnceCallback<void(const BlocklistStateMap&)>;

  using GetMalwareIDsCallback =
      base::OnceCallback<void(const std::set<ExtensionId>&)>;

  using IsBlocklistedCallback = base::OnceCallback<void(BlocklistState)>;

  using DatabaseReadyCallback = base::OnceCallback<void(bool)>;

  Blocklist();

  Blocklist(const Blocklist&) = delete;
  Blocklist& operator=(const Blocklist&) = delete;

  ~Blocklist() override;

  static Blocklist* Get(content::BrowserContext* context);

  // From the set of extension IDs passed in via `ids`, asynchronously checks
  // which are blocklisted and includes them in the resulting map passed
  // via `callback`, which will be sent on the caller's message loop. The values
  // of the map are the blocklist state for each extension. Extensions with
  // a BlocklistState of NOT_BLOCKLISTED are not included in the result.
  //
  // For a synchronous version which ONLY CHECKS CURRENTLY INSTALLED EXTENSIONS
  // see ExtensionPrefs::IsExtensionBlocklisted.
  void GetBlocklistedIDs(const std::set<ExtensionId>& ids,
                         GetBlocklistedIDsCallback callback);

  // From the subset of extension IDs passed in via `ids`, select the ones
  // marked in the blocklist as BLOCKLISTED_MALWARE and asynchronously pass
  // to `callback`. Basically, will call GetBlocklistedIDs and filter its
  // results.
  void GetMalwareIDs(const std::set<ExtensionId>& ids,
                     GetMalwareIDsCallback callback);

  // More convenient form of GetBlocklistedIDs for checking a single extension.
  void IsBlocklisted(const ExtensionId& extension_id,
                     IsBlocklistedCallback callback);

  // Used to mock BlocklistStateFetcher in unit tests. Blocklist owns the
  // `fetcher`.
  void SetBlocklistStateFetcherForTest(BlocklistStateFetcher* fetcher);

  // Reset the owned BlocklistStateFetcher to null and return the current
  // BlocklistStateFetcher.
  BlocklistStateFetcher* ResetBlocklistStateFetcherForTest();

  // Reset the listening for an updated database.
  void ResetDatabaseUpdatedListenerForTest();

  // Reset blocklist state cache to make sure the blocklist state is
  // fetched from the blocklist state fetcher.
  void ResetBlocklistStateCacheForTest();

  // Adds/removes an observer to the blocklist.
  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  // Invokes the callback method with a boolean indicating
  // whether the database is ready.
  void IsDatabaseReady(DatabaseReadyCallback callback);

 private:
  friend class ScopedDatabaseManagerForTest;

  // Use via ScopedDatabaseManagerForTest.
  static void SetDatabaseManager(
      scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager>
          database_manager);
  static scoped_refptr<safe_browsing::SafeBrowsingDatabaseManager>
  GetDatabaseManager();

  void ObserveNewDatabase();

  void NotifyObservers();

  void GetBlocklistStateForIDs(GetBlocklistedIDsCallback callback,
                               const std::set<ExtensionId>& blocklisted_ids);

  void RequestExtensionsBlocklistState(const std::set<ExtensionId>& ids,
                                       base::OnceClosure callback);

  void OnBlocklistStateReceived(const ExtensionId& id, BlocklistState state);

  void ReturnBlocklistStateMap(GetBlocklistedIDsCallback callback,
                               const std::set<ExtensionId>& blocklisted_ids);

  base::ObserverList<Observer>::Unchecked observers_;

  base::CallbackListSubscription database_updated_subscription_;
  base::CallbackListSubscription database_changed_subscription_;

  // The cached BlocklistState's, received from BlocklistStateFetcher.
  BlocklistStateMap blocklist_state_cache_;

  std::unique_ptr<BlocklistStateFetcher> state_fetcher_;

  // The list of ongoing requests for blocklist states that couldn't be
  // served directly from the cache. A new request is created in
  // GetBlocklistedIDs and deleted when the callback is called from
  // OnBlocklistStateReceived.
  //
  // This is a list of requests. Each item in the list is a request. A request
  // is a pair of [vector of string ids to check, response closure].
  std::list<std::pair<std::vector<std::string>, base::OnceClosure>>
      state_requests_;

  base::WeakPtrFactory<Blocklist> weak_ptr_factory_{this};
};

}  // namespace extensions

#endif  // CHROME_BROWSER_EXTENSIONS_BLOCKLIST_H_