File: content_hash_fetcher.cc

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 (117 lines) | stat: -rw-r--r-- 4,783 bytes parent folder | download | duplicates (9)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/browser/content_hash_fetcher.h"

#include <stddef.h>

#include <algorithm>
#include <memory>
#include <vector>

#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/json/json_reader.h"
#include "base/task/sequenced_task_runner.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/browser/content_verifier/content_hash.h"
#include "extensions/browser/content_verifier/content_verifier_delegate.h"
#include "extensions/browser/extension_file_task_runner.h"
#include "extensions/browser/verified_contents.h"
#include "extensions/common/extension.h"
#include "extensions/common/file_util.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/base/load_flags.h"
#include "net/base/net_errors.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "services/network/public/mojom/url_response_head.mojom.h"

namespace extensions {

namespace internals {

ContentHashFetcher::ContentHashFetcher(ContentHash::FetchKey key)
    : fetch_key_(std::move(key)),
      response_task_runner_(base::SequencedTaskRunner::GetCurrentDefault()) {}

void ContentHashFetcher::OnSimpleLoaderComplete(
    std::unique_ptr<std::string> response_body) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  VLOG(1) << "URLFetchComplete for " << fetch_key_.extension_id
          << " is_success:" << !!response_body << " "
          << fetch_key_.fetch_url.possibly_invalid_spec();
  DCHECK(hash_fetcher_callback_);
  DCHECK(simple_loader_);
  bool report_http_response_code =
      (simple_loader_->NetError() == net::OK ||
       simple_loader_->NetError() == net::ERR_HTTP_RESPONSE_CODE_FAILURE) &&
      simple_loader_->ResponseInfo() && simple_loader_->ResponseInfo()->headers;
  response_task_runner_->PostTask(
      FROM_HERE,
      base::BindOnce(
          std::move(hash_fetcher_callback_), std::move(fetch_key_),
          std::move(response_body),
          report_http_response_code
              ? simple_loader_->ResponseInfo()->headers->response_code()
              : simple_loader_->NetError()));
  delete this;
}

void ContentHashFetcher::Start(HashFetcherCallback hash_fetcher_callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  hash_fetcher_callback_ = std::move(hash_fetcher_callback);

  net::NetworkTrafficAnnotationTag traffic_annotation =
      net::DefineNetworkTrafficAnnotation("content_hash_verification_job", R"(
        semantics {
          sender: "Web Store Content Verification"
          description:
            "The request sent to retrieve the file required for content "
            "verification for an extension from the Web Store."
          trigger:
            "An extension from the Web Store is missing the "
            "verified_contents.json file required for extension content "
            "verification."
          data: "The extension id and extension version."
          destination: GOOGLE_OWNED_SERVICE
        }
        policy {
          cookies_allowed: NO
          setting:
            "This feature cannot be directly disabled; it is enabled if any "
            "extension from the webstore is installed in the browser."
          policy_exception_justification:
            "Not implemented, not required. If the user has extensions from "
            "the Web Store, this feature is required to ensure the "
            "extensions match what is distributed by the store."
        })");
  auto resource_request = std::make_unique<network::ResourceRequest>();
  resource_request->url = fetch_key_.fetch_url;
  resource_request->load_flags = net::LOAD_DISABLE_CACHE;
  resource_request->credentials_mode = network::mojom::CredentialsMode::kOmit;

  mojo::Remote<network::mojom::URLLoaderFactory> url_loader_factory_remote(
      std::move(fetch_key_.url_loader_factory_remote));

  simple_loader_ = network::SimpleURLLoader::Create(std::move(resource_request),
                                                    traffic_annotation);
  const int kMaxRetries = 3;
  simple_loader_->SetRetryOptions(
      kMaxRetries,
      network::SimpleURLLoader::RetryMode::RETRY_ON_NETWORK_CHANGE);
  simple_loader_->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
      url_loader_factory_remote.get(),
      base::BindOnce(&ContentHashFetcher::OnSimpleLoaderComplete,
                     base::Unretained(this)));
}

ContentHashFetcher::~ContentHashFetcher() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}

}  // namespace internals
}  // namespace extensions