summaryrefslogtreecommitdiff
path: root/chromium/content/common/service_worker/service_worker_loader_helpers.h
blob: 3654e07fcfe1d7d9282798a6f26e8b6853118b4f (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
// Copyright 2017 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 CONTENT_COMMON_SERVICE_WORKER_SERVICE_WORKER_LOADER_HELPERS_H_
#define CONTENT_COMMON_SERVICE_WORKER_SERVICE_WORKER_LOADER_HELPERS_H_

#include "base/optional.h"
#include "content/common/service_worker/service_worker_types.h"
#include "net/http/http_request_headers.h"
#include "net/url_request/redirect_info.h"
#include "third_party/blink/public/mojom/blob/blob.mojom.h"

namespace network {
class ResourceRequestBody;
struct ResourceRequest;
struct ResourceResponseHead;
}

namespace content {

// Helper functions for service worker classes that use URLLoader
//(e.g., ServiceWorkerNavigationLoader and ServiceWorkerSubresourceLoader).
class ServiceWorkerLoaderHelpers {
 public:
  // Populates |out_head->headers| with the given |status_code|, |status_text|,
  // and |headers|.
  static void SaveResponseHeaders(const int status_code,
                                  const std::string& status_text,
                                  const ServiceWorkerHeaderMap& headers,
                                  network::ResourceResponseHead* out_head);
  // Populates |out_head| (except for headers) with given |response|.
  static void SaveResponseInfo(const ServiceWorkerResponse& response,
                               network::ResourceResponseHead* out_head);

  // Returns a redirect info if |response_head| is an redirect response.
  // Otherwise returns base::nullopt.
  static base::Optional<net::RedirectInfo> ComputeRedirectInfo(
      const network::ResourceRequest& original_request,
      const network::ResourceResponseHead& response_head,
      bool token_binding_negotiated);

  // Reads |blob| using the range in |headers| (if any), writing into
  // |handle_out|. Calls |on_blob_read_complete| when done or if an error
  // occurred. Returns a net error code if the inputs were invalid and reading
  // couldn't start. In that case |on_blob_read_complete| isn't called.
  static int ReadBlobResponseBody(
      blink::mojom::BlobPtr* blob,
      const net::HttpRequestHeaders& headers,
      base::OnceCallback<void(int net_error)> on_blob_read_complete,
      mojo::ScopedDataPipeConsumerHandle* handle_out);

  // Returns a new copy of the given body. This is useful for service worker
  // with NetworkService because it sends the ResourceRequestBody over Mojo IPC,
  // which moves out the DataPipeGetter elements in the Pickle code in
  // resources_messages.cc. We can't change the Pickle code to call
  // DataPipeGetter's Clone method because that code can run on different thread
  // than the DataPipeGetter.
  static scoped_refptr<network::ResourceRequestBody> CloneResourceRequestBody(
      const network::ResourceRequestBody* body);
};

}  // namespace content

#endif  // CONTENT_COMMON_SERVICE_WORKER_SERVICE_WORKER_LOADER_HELPERS_H_