summaryrefslogtreecommitdiff
path: root/chromium/content/browser/web_package/bundled_exchanges_handle.h
blob: 632ed251ad40b85d13f2e53ad721c0108c564d9c (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
// Copyright 2019 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_BROWSER_WEB_PACKAGE_BUNDLED_EXCHANGES_HANDLE_H_
#define CONTENT_BROWSER_WEB_PACKAGE_BUNDLED_EXCHANGES_HANDLE_H_

#include <memory>

#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/network/public/mojom/url_loader_factory.mojom.h"
#include "url/gurl.h"

namespace content {

class BundledExchangesSource;
class BundledExchangesURLLoaderFactory;
class BundledExchangesHandleTracker;
class BundledExchangesNavigationInfo;
class BundledExchangesReader;
class NavigationLoaderInterceptor;

// A class to provide interfaces to communicate with a BundledExchanges for
// loading. Running on the UI thread.
class BundledExchangesHandle {
 public:
  static std::unique_ptr<BundledExchangesHandle> CreateForFile(
      int frame_tree_node_id);
  static std::unique_ptr<BundledExchangesHandle> CreateForTrustableFile(
      std::unique_ptr<BundledExchangesSource> source,
      int frame_tree_node_id);
  static std::unique_ptr<BundledExchangesHandle> CreateForTrackedNavigation(
      scoped_refptr<BundledExchangesReader> reader,
      int frame_tree_node_id);
  static std::unique_ptr<BundledExchangesHandle> CreateForNavigationInfo(
      std::unique_ptr<BundledExchangesNavigationInfo> navigation_info,
      int frame_tree_node_id);

  ~BundledExchangesHandle();

  // Takes a NavigationLoaderInterceptor instance to handle the request for
  // a BundledExchanges, to redirect to the entry URL of the BundledExchanges,
  // and to load the main exchange from the BundledExchanges.
  std::unique_ptr<NavigationLoaderInterceptor> TakeInterceptor();

  // Creates a URLLoaderFactory to load resources from the BundledExchanges.
  void CreateURLLoaderFactory(
      mojo::PendingReceiver<network::mojom::URLLoaderFactory> receiver,
      mojo::Remote<network::mojom::URLLoaderFactory> fallback_factory);

  // Creates a BundledExchangesHandleTracker to track navigations within the
  // bundled exchanges file. Returns null if not yet succeeded to load the
  // exchanges file.
  std::unique_ptr<BundledExchangesHandleTracker> MaybeCreateTracker();

  // Checks if a valid BundledExchanges is attached, opened, and ready for use.
  bool IsReadyForLoading();

  // The base URL which will be set for the document to support relative path
  // subresource loading in unsigned bundled exchanges file.
  const GURL& base_url_override() const { return base_url_override_; }

  const BundledExchangesNavigationInfo* navigation_info() const {
    return navigation_info_.get();
  }

 private:
  BundledExchangesHandle();

  void SetInterceptor(std::unique_ptr<NavigationLoaderInterceptor> interceptor);

  // Called when succeeded to load the bundled exchanges file.
  // |target_inner_url| is the URL of the resource in the bundled exchanges
  // file, which are used for the navigation.
  void OnBundledExchangesFileLoaded(
      const GURL& target_inner_url,
      std::unique_ptr<BundledExchangesURLLoaderFactory> url_loader_factory);

  std::unique_ptr<NavigationLoaderInterceptor> interceptor_;

  GURL base_url_override_;
  std::unique_ptr<BundledExchangesNavigationInfo> navigation_info_;

  std::unique_ptr<BundledExchangesURLLoaderFactory> url_loader_factory_;

  base::WeakPtrFactory<BundledExchangesHandle> weak_factory_{this};

  DISALLOW_COPY_AND_ASSIGN(BundledExchangesHandle);
};

}  // namespace content

#endif  // CONTENT_BROWSER_WEB_PACKAGE_BUNDLED_EXCHANGES_HANDLE_H_