File: network.h

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; 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 (108 lines) | stat: -rw-r--r-- 3,919 bytes parent folder | download | duplicates (2)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_UPDATE_CLIENT_NETWORK_H_
#define COMPONENTS_UPDATE_CLIENT_NETWORK_H_

#include <stdint.h>

#include <memory>
#include <optional>
#include <string>

#include "base/containers/flat_map.h"
#include "base/functional/callback_forward.h"
#include "base/memory/ref_counted.h"

class GURL;

namespace base {
class FilePath;
}  // namespace base

namespace update_client {

class NetworkFetcher {
 public:
  // If the request does not have an X-Retry-After header, implementations
  // should pass -1 for |xheader_retry_after_sec|.
  using PostRequestCompleteCallback =
      base::OnceCallback<void(std::optional<std::string> response_body,
                              int net_error,
                              const std::string& header_etag,
                              const std::string& header_x_cup_server_proof,
                              const std::string& header_cookie,
                              int64_t xheader_retry_after_sec)>;
  using DownloadToFileCompleteCallback =
      base::OnceCallback<void(int net_error, int64_t content_size)>;

  // `content_length` is -1 if the value is not known.
  using ResponseStartedCallback =
      base::RepeatingCallback<void(int response_code, int64_t content_length)>;

  // `current` is the number of bytes received thus far.
  using ProgressCallback = base::RepeatingCallback<void(int64_t current)>;

  // The following two headers carry the ECSDA signature of the POST response,
  // if signing has been used. Two headers are used for redundancy purposes.
  // The value of the `X-Cup-Server-Proof` is preferred.
  static constexpr char kHeaderEtag[] = "ETag";
  static constexpr char kHeaderXCupServerProof[] = "X-Cup-Server-Proof";

  // The server uses the optional X-Retry-After header to indicate that the
  // current request should not be attempted again.
  //
  // The value of the header is the number of seconds to wait before trying to
  // do a subsequent update check. Only the values retrieved over HTTPS are
  // trusted.
  static constexpr char kHeaderXRetryAfter[] = "X-Retry-After";

  // The HTTP 'Cookie' header to pass through to the
  // `PostRequestCompleteCallback`. This header isn't used by the Omaha update
  // protocol but is necessary for other uses of `NetworkFetcher`.
  static constexpr char kHeaderCookie[] = "Cookie";

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

  virtual ~NetworkFetcher() = default;

  virtual void PostRequest(
      const GURL& url,
      const std::string& post_data,
      const std::string& content_type,
      const base::flat_map<std::string, std::string>& post_additional_headers,
      ResponseStartedCallback response_started_callback,
      ProgressCallback progress_callback,
      PostRequestCompleteCallback post_request_complete_callback) = 0;

  // Returns a cancellation closure.
  virtual base::OnceClosure DownloadToFile(
      const GURL& url,
      const base::FilePath& file_path,
      ResponseStartedCallback response_started_callback,
      ProgressCallback progress_callback,
      DownloadToFileCompleteCallback download_to_file_complete_callback) = 0;

 protected:
  NetworkFetcher() = default;
};

class NetworkFetcherFactory
    : public base::RefCountedThreadSafe<NetworkFetcherFactory> {
 public:
  virtual std::unique_ptr<NetworkFetcher> Create() const = 0;

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

 protected:
  friend class base::RefCountedThreadSafe<NetworkFetcherFactory>;
  NetworkFetcherFactory() = default;
  virtual ~NetworkFetcherFactory() = default;
};

}  // namespace update_client

#endif  // COMPONENTS_UPDATE_CLIENT_NETWORK_H_