summaryrefslogtreecommitdiffstats
path: root/chromium/third_party/blink/renderer/core/fetch/fetch_response_data.h
blob: 31bc68df135a30d9acc0a045d583a6b68b795019 (plain)
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
173
174
175
176
177
178
179
180
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_FETCH_FETCH_RESPONSE_DATA_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_FETCH_FETCH_RESPONSE_DATA_H_

#include <memory>

#include "base/macros.h"
#include "base/memory/scoped_refptr.h"
#include "net/http/http_response_info.h"
#include "services/network/public/mojom/fetch_api.mojom-blink-forward.h"
#include "third_party/blink/public/mojom/fetch/fetch_api_request.mojom-blink-forward.h"
#include "third_party/blink/public/mojom/fetch/fetch_api_response.mojom-blink-forward.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/core/fetch/body_stream_buffer.h"
#include "third_party/blink/renderer/core/fetch/fetch_request_data.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_response.h"
#include "third_party/blink/renderer/platform/network/http_header_set.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"
#include "third_party/blink/renderer/platform/wtf/vector.h"

namespace blink {

class ExceptionState;
class FetchHeaderList;
class ScriptState;

class CORE_EXPORT FetchResponseData final
    : public GarbageCollected<FetchResponseData> {
 public:
  // "A response can have an associated termination reason which is one of
  // end-user abort, fatal, and timeout."
  enum TerminationReason {
    kEndUserAbortTermination,
    kFatalTermination,
    kTimeoutTermination
  };

  static FetchResponseData* Create();
  static FetchResponseData* CreateNetworkErrorResponse();
  static FetchResponseData* CreateWithBuffer(BodyStreamBuffer*);

  FetchResponseData(network::mojom::FetchResponseType,
                    network::mojom::FetchResponseSource,
                    uint16_t,
                    AtomicString);

  FetchResponseData* CreateBasicFilteredResponse() const;
  FetchResponseData* CreateCorsFilteredResponse(
      const HTTPHeaderSet& exposed_headers) const;
  FetchResponseData* CreateOpaqueFilteredResponse() const;
  FetchResponseData* CreateOpaqueRedirectFilteredResponse() const;

  FetchResponseData* InternalResponse() { return internal_response_; }
  const FetchResponseData* InternalResponse() const {
    return internal_response_;
  }

  FetchResponseData* Clone(ScriptState*, ExceptionState& exception_state);

  network::mojom::FetchResponseType GetType() const { return type_; }
  network::mojom::FetchResponseSource ResponseSource() const {
    return response_source_;
  }
  const KURL* Url() const;
  uint16_t Status() const { return status_; }
  uint16_t InternalStatus() const;
  AtomicString StatusMessage() const { return status_message_; }
  FetchHeaderList* HeaderList() const { return header_list_.Get(); }
  FetchHeaderList* InternalHeaderList() const;
  BodyStreamBuffer* Buffer() const { return buffer_; }
  String MimeType() const;
  // Returns the BodyStreamBuffer of |m_internalResponse| if any. Otherwise,
  // returns |m_buffer|.
  BodyStreamBuffer* InternalBuffer() const;
  String InternalMIMEType() const;
  base::Time ResponseTime() const { return response_time_; }
  String CacheStorageCacheName() const { return cache_storage_cache_name_; }
  const HTTPHeaderSet& CorsExposedHeaderNames() const {
    return cors_exposed_header_names_;
  }
  bool HasRangeRequested() const { return has_range_requested_; }

  int64_t GetPadding() const { return padding_; }
  void SetPadding(int64_t padding) { padding_ = padding; }
  void SetResponseSource(network::mojom::FetchResponseSource response_source) {
    response_source_ = response_source;
  }
  void SetURLList(const Vector<KURL>&);
  const Vector<KURL>& UrlList() const { return url_list_; }
  const Vector<KURL>& InternalURLList() const;

  void SetStatus(uint16_t status) { status_ = status; }
  void SetStatusMessage(AtomicString status_message) {
    status_message_ = status_message;
  }
  void SetMimeType(const String& type) { mime_type_ = type; }
  void SetRequestMethod(const AtomicString& method) {
    request_method_ = method;
  }
  void SetResponseTime(base::Time response_time) {
    response_time_ = response_time;
  }
  void SetCacheStorageCacheName(const String& cache_storage_cache_name) {
    cache_storage_cache_name_ = cache_storage_cache_name;
  }
  void SetCorsExposedHeaderNames(const HTTPHeaderSet& header_names) {
    cors_exposed_header_names_ = header_names;
  }
  void SetConnectionInfo(
      net::HttpResponseInfo::ConnectionInfo connection_info) {
    connection_info_ = connection_info;
  }
  void SetAlpnNegotiatedProtocol(AtomicString alpn_negotiated_protocol) {
    alpn_negotiated_protocol_ = alpn_negotiated_protocol;
  }
  void SetLoadedWithCredentials(bool loaded_with_credentials) {
    loaded_with_credentials_ = loaded_with_credentials;
  }
  void SetWasFetchedViaSpdy(bool was_fetched_via_spdy) {
    was_fetched_via_spdy_ = was_fetched_via_spdy;
  }
  void SetHasRangeRequested(bool has_range_requested) {
    has_range_requested_ = has_range_requested;
  }

  // If the type is Default, replaces |buffer_|.
  // If the type is Basic or CORS, replaces |buffer_| and
  // |internal_response_->buffer_|.
  // If the type is Error or Opaque, does nothing.
  void ReplaceBodyStreamBuffer(BodyStreamBuffer*);

  // Does not contain the blob response body.
  mojom::blink::FetchAPIResponsePtr PopulateFetchAPIResponse(
      const KURL& request_url);

  // Initialize non-body data from the given |response|.
  void InitFromResourceResponse(
      ExecutionContext* context,
      network::mojom::FetchResponseType response_type,
      const Vector<KURL>& request_url_list,
      const AtomicString& request_method,
      network::mojom::CredentialsMode request_credentials,
      FetchRequestData::Tainting tainting,
      const ResourceResponse& response);

  void Trace(Visitor*) const;

 private:
  network::mojom::FetchResponseType type_;
  int64_t padding_;
  network::mojom::FetchResponseSource response_source_;
  std::unique_ptr<TerminationReason> termination_reason_;
  Vector<KURL> url_list_;
  uint16_t status_;
  AtomicString status_message_;
  Member<FetchHeaderList> header_list_;
  Member<FetchResponseData> internal_response_;
  Member<BodyStreamBuffer> buffer_;
  String mime_type_;
  AtomicString request_method_;
  base::Time response_time_;
  String cache_storage_cache_name_;
  HTTPHeaderSet cors_exposed_header_names_;
  net::HttpResponseInfo::ConnectionInfo connection_info_;
  AtomicString alpn_negotiated_protocol_;
  bool loaded_with_credentials_;
  bool was_fetched_via_spdy_;
  bool has_range_requested_;

  DISALLOW_COPY_AND_ASSIGN(FetchResponseData);
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_FETCH_FETCH_RESPONSE_DATA_H_