File: download_response_handler.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 (128 lines) | stat: -rw-r--r-- 5,013 bytes parent folder | download | duplicates (8)
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
// Copyright 2017 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_DOWNLOAD_PUBLIC_COMMON_DOWNLOAD_RESPONSE_HANDLER_H_
#define COMPONENTS_DOWNLOAD_PUBLIC_COMMON_DOWNLOAD_RESPONSE_HANDLER_H_

#include <optional>
#include <string>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "components/download/public/common/download_create_info.h"
#include "components/download/public/common/download_export.h"
#include "components/download/public/common/download_source.h"
#include "components/download/public/common/download_stream.mojom.h"
#include "components/download/public/common/download_url_parameters.h"
#include "components/download/public/common/download_utils.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/cert/cert_status_flags.h"
#include "services/network/public/mojom/fetch_api.mojom-shared.h"
#include "services/network/public/mojom/url_loader.mojom.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "url/origin.h"

namespace download {

// This class is responsible for handling the server response for a download.
// It passes the DataPipeConsumerHandle and completion status to the download
// sink. The class is common to both navigation triggered downloads and
// context menu downloads
class COMPONENTS_DOWNLOAD_EXPORT DownloadResponseHandler
    : public network::mojom::URLLoaderClient {
 public:
  // Class for handling the stream response.
  class Delegate {
   public:
    virtual void OnResponseStarted(
        std::unique_ptr<DownloadCreateInfo> download_create_info,
        mojom::DownloadStreamHandlePtr stream_handle) = 0;
    virtual void OnReceiveRedirect() = 0;
    virtual void OnResponseCompleted() = 0;
    virtual bool CanRequestURL(const GURL& url) = 0;
    virtual void OnUploadProgress(uint64_t bytes_uploaded) = 0;
  };

  DownloadResponseHandler(
      network::ResourceRequest* resource_request,
      Delegate* delegate,
      std::unique_ptr<DownloadSaveInfo> save_info,
      bool is_parallel_request,
      bool is_transient,
      bool fetch_error_body,
      network::mojom::RedirectMode cross_origin_redirects,
      const DownloadUrlParameters::RequestHeadersType& request_headers,
      const std::string& request_origin,
      DownloadSource download_source,
      bool require_safety_checks,
      std::vector<GURL> url_chain,
      bool is_background_mode);

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

  ~DownloadResponseHandler() override;

  // network::mojom::URLLoaderClient
  void OnReceiveEarlyHints(network::mojom::EarlyHintsPtr early_hints) override;
  void OnReceiveResponse(
      network::mojom::URLResponseHeadPtr head,
      mojo::ScopedDataPipeConsumerHandle body,
      std::optional<mojo_base::BigBuffer> cached_metadata) override;
  void OnReceiveRedirect(const net::RedirectInfo& redirect_info,
                         network::mojom::URLResponseHeadPtr head) override;
  void OnUploadProgress(int64_t current_position,
                        int64_t total_size,
                        OnUploadProgressCallback callback) override;
  void OnTransferSizeUpdated(int32_t transfer_size_diff) override;
  void OnComplete(const network::URLLoaderCompletionStatus& status) override;

 private:
  std::unique_ptr<DownloadCreateInfo> CreateDownloadCreateInfo(
      const network::mojom::URLResponseHead& head);

  // Helper method that is called when response is received.
  void OnResponseStarted(mojom::DownloadStreamHandlePtr stream_handle);

  const raw_ptr<Delegate> delegate_;

  std::unique_ptr<DownloadCreateInfo> create_info_;

  bool started_;

  // Information needed to create DownloadCreateInfo when the time comes.
  std::unique_ptr<DownloadSaveInfo> save_info_;
  std::vector<GURL> url_chain_;
  std::string method_;
  GURL referrer_;
  net::ReferrerPolicy referrer_policy_;
  bool is_transient_;
  bool fetch_error_body_;
  network::mojom::RedirectMode cross_origin_redirects_;
  url::Origin first_origin_;
  DownloadUrlParameters::RequestHeadersType request_headers_;
  std::string request_origin_;
  DownloadSource download_source_;
  net::CertStatus cert_status_ = 0;
  bool has_strong_validators_;
  std::optional<url::Origin> request_initiator_;
  ::network::mojom::CredentialsMode credentials_mode_;
  std::optional<net::IsolationInfo> isolation_info_;
  bool is_partial_request_;
  bool completed_;
  bool require_safety_checks_;

  // The abort reason if this class decides to block the download.
  DownloadInterruptReason abort_reason_;

  // Mojo interface remote to send the completion status to the download sink.
  mojo::Remote<mojom::DownloadStreamClient> client_remote_;

  // Whether the download is running in background mode.
  bool is_background_mode_;
};

}  // namespace download

#endif  // COMPONENTS_DOWNLOAD_PUBLIC_COMMON_DOWNLOAD_RESPONSE_HANDLER_H_