File: prefetch_url_loader_service_context.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 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 (111 lines) | stat: -rw-r--r-- 4,078 bytes parent folder | download | duplicates (7)
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_LOADER_PREFETCH_URL_LOADER_SERVICE_CONTEXT_H_
#define CONTENT_BROWSER_LOADER_PREFETCH_URL_LOADER_SERVICE_CONTEXT_H_

#include "base/functional/callback.h"
#include "content/browser/loader/subresource_proxying_url_loader_service.h"
#include "content/common/content_export.h"
#include "content/public/browser/frame_tree_node_id.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "net/base/load_flags.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/mojom/url_loader_factory.mojom.h"
#include "third_party/blink/public/mojom/renderer_preference_watcher.mojom.h"

namespace blink {
class URLLoaderThrottle;
}

namespace content {

class BrowserContext;

// Contains the data and functions to handle <link rel="prefetch"> requests.
class CONTENT_EXPORT PrefetchURLLoaderServiceContext final
    : public blink::mojom::RendererPreferenceWatcher {
 public:
  using BindContext = SubresourceProxyingURLLoaderService::BindContext;

  PrefetchURLLoaderServiceContext(
      BrowserContext* browser_context,
      mojo::ReceiverSet<network::mojom::URLLoaderFactory,
                        scoped_refptr<BindContext>>& loader_factory_receivers);
  ~PrefetchURLLoaderServiceContext() override;

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

  void CreatePrefetchLoaderAndStart(
      mojo::PendingReceiver<network::mojom::URLLoader> receiver,
      int32_t request_id,
      uint32_t options,
      const network::ResourceRequest& resource_request_in,
      mojo::PendingRemote<network::mojom::URLLoaderClient> client,
      const net::MutableNetworkTrafficAnnotationTag& traffic_annotation);

  // Register a callback that is fired right before a prefetch load is started
  // by this service.
  void RegisterPrefetchLoaderCallbackForTest(
      const base::RepeatingClosure& prefetch_load_callback) {
    prefetch_load_callback_for_testing_ = prefetch_load_callback;
  }

  void SetAcceptLanguages(const std::string& accept_langs) {
    accept_langs_ = accept_langs;
  }

  static bool IsPrefetchRequest(const network::ResourceRequest& request) {
    return request.load_flags & net::LOAD_PREFETCH;
  }

 private:
  // This ensures that the BindContext's |cross_origin_factory| member
  // exists by setting it to a special URLLoaderFactory created by the current
  // context's RenderFrameHost.
  void EnsureCrossOriginFactory();
  bool IsValidCrossOriginPrefetch(
      const network::ResourceRequest& resource_request);

  base::UnguessableToken GenerateRecursivePrefetchToken(
      base::WeakPtr<BindContext> bind_context,
      const network::ResourceRequest& request);

  // blink::mojom::RendererPreferenceWatcher.
  void NotifyUpdate(const blink::RendererPreferences& new_prefs) override;

  // For URLLoaderThrottlesGetter.
  std::vector<std::unique_ptr<blink::URLLoaderThrottle>>
  CreateURLLoaderThrottles(const network::ResourceRequest& request,
                           FrameTreeNodeId frame_tree_node_id);

  BindContext* current_bind_context() const {
    return loader_factory_receivers_->current_context().get();
  }

  raw_ptr<BrowserContext> browser_context_ = nullptr;

  raw_ref<mojo::ReceiverSet<network::mojom::URLLoaderFactory,
                            scoped_refptr<BindContext>>>
      loader_factory_receivers_;

  mojo::ReceiverSet<network::mojom::URLLoader,
                    std::unique_ptr<network::mojom::URLLoader>>
      prefetch_loader_receivers_;

  // Used in the IO thread.
  mojo::Receiver<blink::mojom::RendererPreferenceWatcher>
      preference_watcher_receiver_{this};

  base::RepeatingClosure prefetch_load_callback_for_testing_;

  std::string accept_langs_;
};

}  // namespace content

#endif  // CONTENT_BROWSER_LOADER_PREFETCH_URL_LOADER_SERVICE_CONTEXT_H_