File: local_resource_url_loader_factory.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 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 (113 lines) | stat: -rw-r--r-- 4,825 bytes parent folder | download | duplicates (6)
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
// Copyright 2025 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_RENDERER_LOCAL_RESOURCE_URL_LOADER_FACTORY_H_
#define CONTENT_RENDERER_LOCAL_RESOURCE_URL_LOADER_FACTORY_H_

#include <cstdint>
#include <memory>

#include "base/memory/ref_counted.h"
#include "base/memory/scoped_refptr.h"
#include "base/task/sequenced_task_runner.h"
#include "base/threading/sequence_bound.h"
#include "content/common/content_export.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/socket/socket.h"
#include "services/network/public/cpp/resource_request.h"
#include "services/network/public/mojom/url_loader.mojom.h"
#include "services/network/public/mojom/url_loader_factory.mojom.h"
#include "third_party/blink/public/mojom/loader/local_resource_loader_config.mojom.h"
#include "url/origin.h"

namespace content {

// LocalResourceURLLoaderFactory is a URLLoaderFactory that lives in the
// renderer process and fetches resources directly from the ResourceBundle,
// enabling renderers to load bundled resources entirely in-process. This can
// significantly reduce IPC overhead for WebUIs, whose resources come almost
// exclusively from the ResourceBundle.
class CONTENT_EXPORT LocalResourceURLLoaderFactory
    : public network::mojom::URLLoaderFactory {
 public:
  struct Source {
    Source(blink::mojom::LocalResourceSourcePtr source,
           std::map<std::string, std::string> replacement_strings);
    Source(Source&& other);
    Source& operator=(Source&& other);
    ~Source();
    blink::mojom::LocalResourceSourcePtr source;
    std::map<std::string, std::string> replacement_strings;
  };

  LocalResourceURLLoaderFactory(
      blink::mojom::LocalResourceLoaderConfigPtr config,
      mojo::PendingRemote<network::mojom::URLLoaderFactory> fallback);
  ~LocalResourceURLLoaderFactory() override;

  // network::mojom::URLLoaderFactory implementation.
  void CreateLoaderAndStart(
      mojo::PendingReceiver<network::mojom::URLLoader> loader,
      int32_t request_id,
      uint32_t options,
      const network::ResourceRequest& request,
      mojo::PendingRemote<network::mojom::URLLoaderClient> client,
      const net::MutableNetworkTrafficAnnotationTag& traffic_annotation)
      override;
  void Clone(mojo::PendingReceiver<network::mojom::URLLoaderFactory> receiver)
      override;

 private:
  // CanServe returns true if and only if all of the following are true:
  //
  // 1. The LocalResourceURLLoaderFactory has a resource ID for the given URL
  //    (which depends on the resource metadata received from the browser
  //    process), and
  // 2. The resource ID corresponds to a resource that exists in
  //    'resources.pak'.
  //
  // It should return false in the following cases:
  //
  // 1. For dynamic resources that are generated on-the-fly in the browser
  //    process (e.g. strings.m.js, chrome://theme/colors.css).
  // 2. For static resources that reside in a pak file that is not
  //    memory-mapped in the renderer process (e.g. browser_tests.pak).
  //
  // CanServe is called internally for every resource request to decide whether
  // the request can be serviced in-process or if it has to be serviced
  // out-of-process.
  bool CanServe(const network::ResourceRequest& request) const;

  // Fetches the resource from the ResourceBundle and sends it to the
  // URLLoaderClient. This is static because it is posted as a task which may
  // outlive |this|.
  static void GetResourceAndRespond(
      const scoped_refptr<base::RefCountedData<std::map<url::Origin, Source>>>
          sources,
      const network::ResourceRequest& request,
      mojo::PendingRemote<network::mojom::URLLoaderClient> client);

  // Map for resolving origins to their respective source metadata
  // (path-to-resource-ID mappings and string replacement maps).
  // It is ref-counted because it needs to be accessed in an async call to
  // GetResourceAndRespond, which may outlive |this|.
  const scoped_refptr<base::RefCountedData<std::map<url::Origin, Source>>>
      sources_;

  // Pipe to fallback factory, which should be the WebUIURLLoaderFactory in the
  // browser process. This is required because there are certain resources that
  // cannot be serviced in-process and must be serviced by the browser process.
  // See the CanServe method for more details.
  const mojo::Remote<network::mojom::URLLoaderFactory> fallback_;

  // Pipes to callers of the Clone method.
  mojo::ReceiverSet<network::mojom::URLLoaderFactory> receivers_;
};

}  // namespace content

#endif  // CONTENT_RENDERER_LOCAL_RESOURCE_URL_LOADER_FACTORY_H_