Alex Yang | f8e231903 | 2025-01-14 00:28:51 | [diff] [blame^] | 1 | // Copyright 2025 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CONTENT_RENDERER_LOCAL_RESOURCE_URL_LOADER_FACTORY_H_ |
| 6 | #define CONTENT_RENDERER_LOCAL_RESOURCE_URL_LOADER_FACTORY_H_ |
| 7 | |
| 8 | #include <cstdint> |
| 9 | #include <memory> |
| 10 | |
| 11 | #include "base/memory/ref_counted.h" |
| 12 | #include "base/memory/scoped_refptr.h" |
| 13 | #include "base/task/sequenced_task_runner.h" |
| 14 | #include "base/threading/sequence_bound.h" |
| 15 | #include "content/common/content_export.h" |
| 16 | #include "mojo/public/cpp/bindings/pending_receiver.h" |
| 17 | #include "mojo/public/cpp/bindings/pending_remote.h" |
| 18 | #include "mojo/public/cpp/bindings/receiver_set.h" |
| 19 | #include "mojo/public/cpp/bindings/remote.h" |
| 20 | #include "net/socket/socket.h" |
| 21 | #include "services/network/public/cpp/resource_request.h" |
| 22 | #include "services/network/public/mojom/url_loader.mojom.h" |
| 23 | #include "services/network/public/mojom/url_loader_factory.mojom.h" |
| 24 | #include "third_party/blink/public/mojom/loader/local_resource_loader_config.mojom.h" |
| 25 | #include "url/origin.h" |
| 26 | |
| 27 | namespace content { |
| 28 | |
| 29 | // LocalResourceURLLoaderFactory is a URLLoaderFactory that lives in the |
| 30 | // renderer process and fetches resources directly from the ResourceBundle, |
| 31 | // enabling renderers to load bundled resources entirely in-process. This can |
| 32 | // significantly reduce IPC overhead for WebUIs, whose resources come almost |
| 33 | // exclusively from the ResourceBundle. |
| 34 | class CONTENT_EXPORT LocalResourceURLLoaderFactory |
| 35 | : public network::mojom::URLLoaderFactory { |
| 36 | public: |
| 37 | struct Source { |
| 38 | Source(blink::mojom::LocalResourceSourcePtr source, |
| 39 | std::map<const std::string, std::string> replacement_strings); |
| 40 | Source(Source&& other); |
| 41 | Source& operator=(Source&& other); |
| 42 | ~Source(); |
| 43 | blink::mojom::LocalResourceSourcePtr source; |
| 44 | std::map<const std::string, std::string> replacement_strings; |
| 45 | }; |
| 46 | |
| 47 | LocalResourceURLLoaderFactory( |
| 48 | const blink::mojom::LocalResourceLoaderConfigPtr& config, |
| 49 | mojo::PendingRemote<network::mojom::URLLoaderFactory> fallback); |
| 50 | ~LocalResourceURLLoaderFactory() override; |
| 51 | |
| 52 | // network::mojom::URLLoaderFactory implementation. |
| 53 | void CreateLoaderAndStart( |
| 54 | mojo::PendingReceiver<network::mojom::URLLoader> loader, |
| 55 | int32_t request_id, |
| 56 | uint32_t options, |
| 57 | const network::ResourceRequest& request, |
| 58 | mojo::PendingRemote<network::mojom::URLLoaderClient> client, |
| 59 | const net::MutableNetworkTrafficAnnotationTag& traffic_annotation) |
| 60 | override; |
| 61 | void Clone(mojo::PendingReceiver<network::mojom::URLLoaderFactory> receiver) |
| 62 | override; |
| 63 | |
| 64 | private: |
| 65 | // CanServe returns true if and only if all of the following are true: |
| 66 | // |
| 67 | // 1. The LocalResourceURLLoaderFactory has a resource ID for the given URL |
| 68 | // (which depends on the resource metadata received from the browser |
| 69 | // process), and |
| 70 | // 2. The resource ID corresponds to a resource that exists in |
| 71 | // 'resources.pak'. |
| 72 | // |
| 73 | // It should return false in the following cases: |
| 74 | // |
| 75 | // 1. For dynamic resources that are generated on-the-fly in the browser |
| 76 | // process (e.g. strings.m.js, chrome://theme/colors.css). |
| 77 | // 2. For static resources that reside in a pak file that is not |
| 78 | // memory-mapped in the renderer process (e.g. browser_tests.pak). |
| 79 | // |
| 80 | // CanServe is called internally for every resource request to decide whether |
| 81 | // the request can be serviced in-process or if it has to be serviced |
| 82 | // out-of-process. |
| 83 | bool CanServe(const network::ResourceRequest& request) const; |
| 84 | |
| 85 | void GetResourceAndRespond( |
| 86 | const network::ResourceRequest& request, |
| 87 | mojo::PendingRemote<network::mojom::URLLoaderClient> client) const; |
| 88 | |
| 89 | const std::map<url::Origin, Source> sources_; |
| 90 | |
| 91 | // Pipe to fallback factory, which should be the WebUIURLLoaderFactory in the |
| 92 | // browser process. This is required because there are certain resources that |
| 93 | // cannot be serviced in-process and must be serviced by the browser process. |
| 94 | // See the CanServe method for more details. |
| 95 | const mojo::Remote<network::mojom::URLLoaderFactory> fallback_; |
| 96 | |
| 97 | // Pipes to callers of the Clone method. |
| 98 | mojo::ReceiverSet<network::mojom::URLLoaderFactory> receivers_; |
| 99 | }; |
| 100 | |
| 101 | } // namespace content |
| 102 | |
| 103 | #endif // CONTENT_RENDERER_LOCAL_RESOURCE_URL_LOADER_FACTORY_H_ |