summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/loader/fetch/raw_resource.h
blob: 7e496f98be235a05dfac6061c7d85ae627445df7 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
    Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de)
    Copyright (C) 2001 Dirk Mueller <mueller@kde.org>
    Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
    Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_LOADER_FETCH_RAW_RESOURCE_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_LOADER_FETCH_RAW_RESOURCE_H_

#include <memory>

#include "base/optional.h"
#include "third_party/blink/public/platform/web_data_consumer_handle.h"
#include "third_party/blink/renderer/platform/loader/fetch/buffering_data_pipe_writer.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_client.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_loader_options.h"
#include "third_party/blink/renderer/platform/platform_export.h"

namespace blink {
class WebDataConsumerHandle;
class FetchParameters;
class RawResourceClient;
class ResourceFetcher;
class SubstituteData;
class SourceKeyedCachedMetadataHandler;

class PLATFORM_EXPORT RawResource final : public Resource {
 public:
  static RawResource* FetchSynchronously(FetchParameters&,
                                         ResourceFetcher*,
                                         RawResourceClient* = nullptr);
  static RawResource* Fetch(FetchParameters&,
                            ResourceFetcher*,
                            RawResourceClient*);
  static RawResource* FetchMainResource(FetchParameters&,
                                        ResourceFetcher*,
                                        RawResourceClient*,
                                        const SubstituteData&,
                                        unsigned long identifier);
  static RawResource* FetchImport(FetchParameters&,
                                  ResourceFetcher*,
                                  RawResourceClient*);
  static RawResource* FetchMedia(FetchParameters&,
                                 ResourceFetcher*,
                                 RawResourceClient*);
  static RawResource* FetchTextTrack(FetchParameters&,
                                     ResourceFetcher*,
                                     RawResourceClient*);
  static RawResource* FetchManifest(FetchParameters&,
                                    ResourceFetcher*,
                                    RawResourceClient*);

  // Exposed for testing
  static RawResource* CreateForTest(ResourceRequest request,
                                    ResourceType type) {
    ResourceLoaderOptions options;
    return MakeGarbageCollected<RawResource>(request, type, options);
  }
  static RawResource* CreateForTest(const KURL& url,
                                    scoped_refptr<const SecurityOrigin> origin,
                                    ResourceType type) {
    ResourceRequest request(url);
    request.SetRequestorOrigin(std::move(origin));
    return CreateForTest(request, type);
  }

  RawResource(const ResourceRequest&,
              ResourceType,
              const ResourceLoaderOptions&);

  // Resource implementation
  MatchStatus CanReuse(const FetchParameters&) const override;
  bool WillFollowRedirect(const ResourceRequest&,
                          const ResourceResponse&) override;

  void SetSerializedCachedMetadata(const uint8_t*, size_t) override;

  // Used for code caching of scripts with source code inline in the HTML.
  // Returns a cache handler which can store multiple cache metadata entries,
  // keyed by the source code of the script. This is valid only if type is
  // kMainResource.
  SourceKeyedCachedMetadataHandler* InlineScriptCacheHandler();

  // Used for code caching of fetched code resources. Returns a cache handler
  // which can only store a single cache metadata entry. This is valid only if
  // type is kRaw.
  SingleCachedMetadataHandler* ScriptCacheHandler();

  scoped_refptr<BlobDataHandle> DownloadedBlob() const {
    return downloaded_blob_;
  }

 protected:
  CachedMetadataHandler* CreateCachedMetadataHandler(
      std::unique_ptr<CachedMetadataSender> send_callback) override;

 private:
  class RawResourceFactory : public NonTextResourceFactory {
   public:
    explicit RawResourceFactory(ResourceType type)
        : NonTextResourceFactory(type) {}

    Resource* Create(const ResourceRequest& request,
                     const ResourceLoaderOptions& options) const override {
      return MakeGarbageCollected<RawResource>(request, type_, options);
    }
  };

  // Resource implementation
  void DidAddClient(ResourceClient*) override;
  void AppendData(const char*, size_t) override;
  bool ShouldIgnoreHTTPStatusCodeErrors() const override {
    return !IsLinkPreload();
  }
  void WillNotFollowRedirect() override;
  void ResponseReceived(const ResourceResponse&,
                        std::unique_ptr<WebDataConsumerHandle>) override;
  void DidSendData(unsigned long long bytes_sent,
                   unsigned long long total_bytes_to_be_sent) override;
  void DidDownloadData(unsigned long long) override;
  void DidDownloadToBlob(scoped_refptr<BlobDataHandle>) override;
  void ReportResourceTimingToClients(const ResourceTimingInfo&) override;
  bool MatchPreload(const FetchParameters&,
                    base::SingleThreadTaskRunner*) override;
  void NotifyFinished() override;

  scoped_refptr<BlobDataHandle> downloaded_blob_;

  // Used for preload matching.
  std::unique_ptr<BufferingDataPipeWriter> data_pipe_writer_;
  std::unique_ptr<WebDataConsumerHandle> data_consumer_handle_;
};

// TODO(yhirano): Recover #if ENABLE_SECURITY_ASSERT when we stop adding
// RawResources to MemoryCache.
inline bool IsRawResource(ResourceType type) {
  return type == ResourceType::kMainResource || type == ResourceType::kRaw ||
         type == ResourceType::kTextTrack || type == ResourceType::kAudio ||
         type == ResourceType::kVideo || type == ResourceType::kManifest ||
         type == ResourceType::kImportResource;
}
inline bool IsRawResource(const Resource& resource) {
  return IsRawResource(resource.GetType());
}
inline RawResource* ToRawResource(Resource* resource) {
  SECURITY_DCHECK(!resource || IsRawResource(*resource));
  return static_cast<RawResource*>(resource);
}

class PLATFORM_EXPORT RawResourceClient : public ResourceClient {
 public:
  bool IsRawResourceClient() const final { return true; }

  // The order of the callbacks is as follows:
  // [Case 1] A successful load:
  // 0+  redirectReceived() and/or dataSent()
  // 1   responseReceived()
  // 0-1 setSerializedCachedMetadata()
  // 0+  dataReceived() or dataDownloaded(), but never both
  // 1   notifyFinished() with errorOccurred() = false
  // [Case 2] When redirect is blocked:
  // 0+  redirectReceived() and/or dataSent()
  // 1   redirectBlocked()
  // 1   notifyFinished() with errorOccurred() = true
  // [Case 3] Other failures:
  //     notifyFinished() with errorOccurred() = true is called at any time
  //     (unless notifyFinished() is already called).
  // In all cases:
  //     No callbacks are made after notifyFinished() or
  //     removeClient() is called.
  virtual void DataSent(Resource*,
                        unsigned long long /* bytesSent */,
                        unsigned long long /* totalBytesToBeSent */) {}
  virtual void ResponseReceived(Resource*,
                                const ResourceResponse&,
                                std::unique_ptr<WebDataConsumerHandle>) {}
  virtual void SetSerializedCachedMetadata(Resource*, const uint8_t*, size_t) {}
  virtual bool RedirectReceived(Resource*,
                                const ResourceRequest&,
                                const ResourceResponse&) {
    return true;
  }
  virtual void RedirectBlocked() {}
  virtual void DataDownloaded(Resource*, unsigned long long) {}
  virtual void DidReceiveResourceTiming(Resource*, const ResourceTimingInfo&) {}
  // Called for requests that had DownloadToBlob set to true. Can be called with
  // null if creating the blob failed for some reason (but the download itself
  // otherwise succeeded). Could also not be called at all if the downloaded
  // resource ended up being zero bytes.
  virtual void DidDownloadToBlob(Resource*, scoped_refptr<BlobDataHandle>) {}
};

// Checks the sequence of callbacks of RawResourceClient. This can be used only
// when a RawResourceClient is added as a client to at most one RawResource.
class PLATFORM_EXPORT RawResourceClientStateChecker final {
 public:
  RawResourceClientStateChecker();
  ~RawResourceClientStateChecker();

  // Call before addClient()/removeClient() is called.
  void WillAddClient();
  void WillRemoveClient();

  // Call RawResourceClientStateChecker::f() at the beginning of
  // RawResourceClient::f().
  void RedirectReceived();
  void RedirectBlocked();
  void DataSent();
  void ResponseReceived();
  void SetSerializedCachedMetadata();
  void DataReceived();
  void DataDownloaded();
  void DidDownloadToBlob();
  void NotifyFinished(Resource*);

 private:
  enum State {
    kNotAddedAsClient,
    kStarted,
    kRedirectBlocked,
    kResponseReceived,
    kSetSerializedCachedMetadata,
    kDataReceived,
    kDataDownloaded,
    kDidDownloadToBlob,
    kNotifyFinished
  };
  State state_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_LOADER_FETCH_RAW_RESOURCE_H_