File: test_blocklist_state_fetcher.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (99 lines) | stat: -rw-r--r-- 3,042 bytes parent folder | download | duplicates (4)
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
// 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.

#include "chrome/browser/extensions/test_blocklist_state_fetcher.h"

#include "base/containers/contains.h"
#include "components/safe_browsing/core/browser/db/v4_test_util.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/mojom/url_loader.mojom.h"

namespace extensions {
namespace {

class DummySharedURLLoaderFactory : public network::SharedURLLoaderFactory {
 public:
  DummySharedURLLoaderFactory() = default;

  // network::URLLoaderFactory implementation:
  void CreateLoaderAndStart(
      mojo::PendingReceiver<network::mojom::URLLoader> loader,
      int32_t request_id,
      uint32_t options,
      const network::ResourceRequest& request,
      mojo::PendingRemote<network::mojom::URLLoaderClient> client,
      const net::MutableNetworkTrafficAnnotationTag& traffic_annotation)
      override {
    // Ensure the client pipe doesn't get closed to avoid SimpleURLLoader seeing
    // a connection error.
    clients_.push_back(std::move(client));
  }

  void Clone(mojo::PendingReceiver<network::mojom::URLLoaderFactory> receiver)
      override {
    NOTREACHED();
  }

  // network::PendingSharedURLLoaderFactory implementation
  std::unique_ptr<network::PendingSharedURLLoaderFactory> Clone() override {
    NOTREACHED();
  }

 private:
  friend class base::RefCounted<DummySharedURLLoaderFactory>;
  ~DummySharedURLLoaderFactory() override = default;

  std::vector<mojo::PendingRemote<network::mojom::URLLoaderClient>> clients_;
};

}  // namespace

TestBlocklistStateFetcher::TestBlocklistStateFetcher(
    BlocklistStateFetcher* fetcher)
    : fetcher_(fetcher) {
  fetcher_->SetSafeBrowsingConfig(safe_browsing::GetTestV4ProtocolConfig());

  url_loader_factory_ = base::MakeRefCounted<DummySharedURLLoaderFactory>();
  fetcher_->url_loader_factory_ = url_loader_factory_.get();
}

TestBlocklistStateFetcher::~TestBlocklistStateFetcher() = default;

void TestBlocklistStateFetcher::SetBlocklistVerdict(
    const std::string& id,
    ClientCRXListInfoResponse_Verdict state) {
  verdicts_[id] = state;
}

bool TestBlocklistStateFetcher::HandleFetcher(const std::string& id) {
  network::SimpleURLLoader* url_loader = nullptr;
  for (auto& it : fetcher_->requests_) {
    if (it.second.second == id) {
      url_loader = it.second.first.get();
      break;
    }
  }

  if (!url_loader) {
    return false;
  }

  ClientCRXListInfoResponse response;
  if (base::Contains(verdicts_, id)) {
    response.set_verdict(verdicts_[id]);
  } else {
    response.set_verdict(ClientCRXListInfoResponse::NOT_IN_BLOCKLIST);
  }

  std::string response_str;
  response.SerializeToString(&response_str);

  fetcher_->OnURLLoaderCompleteInternal(url_loader, response_str, 200, net::OK);

  return true;
}

}  // namespace extensions