Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 33c1c26a | 2013-01-24 21:56:26 | [diff] [blame] | 5 | #include "content/browser/webui/url_data_manager_backend.h" |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 6 | |
[email protected] | 19e6c4c | 2011-10-05 18:03:04 | [diff] [blame] | 7 | #include <set> |
thestig | 922d34c | 2016-08-23 03:40:52 | [diff] [blame] | 8 | #include <utility> |
[email protected] | 19e6c4c | 2011-10-05 18:03:04 | [diff] [blame] | 9 | |
Lei Zhang | de19767 | 2021-04-29 08:11:24 | [diff] [blame] | 10 | #include "base/containers/contains.h" |
Avi Drissman | adac2199 | 2023-01-11 23:46:39 | [diff] [blame] | 11 | #include "base/functional/bind.h" |
skyostil | 95082a6 | 2015-06-05 19:53:07 | [diff] [blame] | 12 | #include "base/location.h" |
Lei Zhang | c77274da | 2018-08-28 23:14:00 | [diff] [blame] | 13 | #include "base/memory/ptr_util.h" |
[email protected] | bebc8e12 | 2012-06-12 19:52:34 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 15 | #include "base/memory/ref_counted_memory.h" |
Jayson Adams | 20123ee9 | 2021-07-27 01:38:43 | [diff] [blame] | 16 | #include "base/no_destructor.h" |
edwardjung | 6e6ebd1 | 2015-12-03 13:18:46 | [diff] [blame] | 17 | #include "base/strings/string_number_conversions.h" |
[email protected] | 348fbaac | 2013-06-11 06:31:51 | [diff] [blame] | 18 | #include "base/strings/string_util.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 19 | #include "base/task/single_thread_task_runner.h" |
ssid | 3e76561 | 2015-01-28 04:03:42 | [diff] [blame] | 20 | #include "base/trace_event/trace_event.h" |
David Bienvenu | ead449e | 2022-04-07 19:30:02 | [diff] [blame] | 21 | #include "base/values.h" |
[email protected] | 33c1c26a | 2013-01-24 21:56:26 | [diff] [blame] | 22 | #include "content/browser/webui/shared_resources_data_source.h" |
[email protected] | 5bf1646f5 | 2013-01-28 03:57:02 | [diff] [blame] | 23 | #include "content/browser/webui/url_data_source_impl.h" |
dbeam | 25af85bd | 2016-12-09 03:34:37 | [diff] [blame] | 24 | #include "content/browser/webui/web_ui_data_source_impl.h" |
[email protected] | 4413d87 | 2014-02-20 20:26:55 | [diff] [blame] | 25 | #include "content/public/browser/browser_context.h" |
Eric Seckler | 8652dcd5 | 2018-09-20 10:42:28 | [diff] [blame] | 26 | #include "content/public/browser/browser_task_traits.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 27 | #include "content/public/browser/browser_thread.h" |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 28 | #include "content/public/browser/content_browser_client.h" |
[email protected] | e671c27 | 2013-04-23 22:45:03 | [diff] [blame] | 29 | #include "content/public/browser/render_process_host.h" |
Hans Wennborg | 5ffd139 | 2019-10-16 11:00:02 | [diff] [blame] | 30 | #include "content/public/common/content_client.h" |
[email protected] | be22ebe | 2013-01-23 17:03:08 | [diff] [blame] | 31 | #include "content/public/common/url_constants.h" |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 32 | #include "net/base/io_buffer.h" |
| 33 | #include "net/base/net_errors.h" |
Hans Wennborg | 0d7c5ba | 2021-07-06 17:52:20 | [diff] [blame] | 34 | #include "net/http/http_request_headers.h" |
[email protected] | e3421da | 2013-04-02 07:46:46 | [diff] [blame] | 35 | #include "net/http/http_status_code.h" |
edwardjung | 6e6ebd1 | 2015-12-03 13:18:46 | [diff] [blame] | 36 | #include "net/log/net_log_util.h" |
Becca Hughes | 88a4afe | 2020-06-23 01:30:46 | [diff] [blame] | 37 | #include "services/network/public/mojom/content_security_policy.mojom.h" |
dschuyler | 613a103 | 2016-12-15 19:22:35 | [diff] [blame] | 38 | #include "ui/base/template_expressions.h" |
Reilly Grant | f31ae65 | 2017-11-16 20:40:04 | [diff] [blame] | 39 | #include "ui/base/webui/i18n_source_stream.h" |
[email protected] | 707e1c4 | 2013-07-09 21:18:58 | [diff] [blame] | 40 | #include "url/url_util.h" |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 41 | |
[email protected] | 5bf1646f5 | 2013-01-28 03:57:02 | [diff] [blame] | 42 | namespace content { |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 43 | |
[email protected] | e02a8376 | 2011-03-10 19:42:18 | [diff] [blame] | 44 | namespace { |
| 45 | |
Matt Menke | 4426a15 | 2020-04-17 03:45:48 | [diff] [blame] | 46 | const char kChromeURLContentSecurityPolicyHeaderName[] = |
| 47 | "Content-Security-Policy"; |
[email protected] | a8c119d | 2012-02-15 23:00:09 | [diff] [blame] | 48 | |
Trent Apted | dbdebc1 | 2021-04-06 11:27:00 | [diff] [blame] | 49 | const char kChromeURLCrossOriginOpenerPolicyName[] = |
| 50 | "Cross-Origin-Opener-Policy"; |
| 51 | const char kChromeURLCrossOriginEmbedderPolicyName[] = |
| 52 | "Cross-Origin-Embedder-Policy"; |
| 53 | const char kChromeURLCrossOriginResourcePolicyName[] = |
| 54 | "Cross-Origin-Resource-Policy"; |
| 55 | |
Matt Menke | 4426a15 | 2020-04-17 03:45:48 | [diff] [blame] | 56 | const char kChromeURLXFrameOptionsHeaderName[] = "X-Frame-Options"; |
| 57 | const char kChromeURLXFrameOptionsHeaderValue[] = "DENY"; |
thestig | 922d34c | 2016-08-23 03:40:52 | [diff] [blame] | 58 | const char kNetworkErrorKey[] = "netError"; |
John Abd-El-Malek | 76ad70a | 2020-05-18 16:08:12 | [diff] [blame] | 59 | const char kURLDataManagerBackendKeyName[] = "url_data_manager_backend"; |
[email protected] | 31dc635 | 2014-03-17 13:41:04 | [diff] [blame] | 60 | |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 61 | bool SchemeIsInSchemes(const std::string& scheme, |
| 62 | const std::vector<std::string>& schemes) { |
Jan Wilken Dörrie | 77c581a | 2019-06-07 16:25:06 | [diff] [blame] | 63 | return base::Contains(schemes, scheme); |
[email protected] | 672c8c1 | 2013-03-07 12:30:06 | [diff] [blame] | 64 | } |
| 65 | |
Alex Moshchuk | b7692776 | 2023-05-09 05:46:31 | [diff] [blame] | 66 | bool g_disallow_webui_scheme_caching_for_testing = false; |
| 67 | |
| 68 | std::vector<std::string> GetWebUISchemesSlow() { |
| 69 | std::vector<std::string> schemes; |
| 70 | schemes.emplace_back(kChromeUIScheme); |
| 71 | schemes.emplace_back(kChromeUIUntrustedScheme); |
| 72 | GetContentClient()->browser()->GetAdditionalWebUISchemes(&schemes); |
| 73 | return schemes; |
| 74 | } |
| 75 | |
| 76 | std::vector<std::string> GetWebUISchemesCached() { |
| 77 | // It's OK to cache this in a static because the class implementing |
| 78 | // GetAdditionalWebUISchemes() won't change while the application is |
| 79 | // running, and because those methods always add the same items. |
| 80 | // |
| 81 | // However, be careful using this with unit tests which use |
| 82 | // GetAdditionalWebUISchemes() to change the list of WebUI schemes, since |
| 83 | // this caching may persist across tests. For those, this caching should be |
| 84 | // disabled via SetDisallowWebUISchemeCachingForTesting(). |
| 85 | static base::NoDestructor<std::vector<std::string>> webui_schemes( |
| 86 | GetWebUISchemesSlow()); |
| 87 | |
| 88 | return *webui_schemes; |
| 89 | } |
| 90 | |
[email protected] | ad423e4d | 2011-05-16 20:04:13 | [diff] [blame] | 91 | } // namespace |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 92 | |
Keren Zhu | 42e0437 | 2024-08-27 17:05:57 | [diff] [blame] | 93 | URLDataManagerBackend::URLDataManagerBackend() { |
Lei Zhang | 4fcf840 | 2023-03-17 22:58:51 | [diff] [blame] | 94 | { |
| 95 | // Add a shared data source for chrome://resources. |
| 96 | auto* source = new WebUIDataSourceImpl(kChromeUIResourcesHost); |
| 97 | PopulateSharedResourcesDataSource(source); |
| 98 | AddDataSource(source); // Takes ownership. |
| 99 | } |
Jiewei Qian | 155bcb0 | 2021-09-30 08:23:28 | [diff] [blame] | 100 | |
Lei Zhang | 4fcf840 | 2023-03-17 22:58:51 | [diff] [blame] | 101 | { |
| 102 | // Add a shared data source for chrome-untrusted://resources. |
| 103 | auto* source = new WebUIDataSourceImpl(kChromeUIUntrustedResourcesURL); |
| 104 | PopulateSharedResourcesDataSource(source); |
| 105 | AddDataSource(source); // Takes ownership. |
| 106 | } |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 107 | } |
| 108 | |
Chris Hamilton | bed9bc8 | 2018-07-11 23:13:35 | [diff] [blame] | 109 | URLDataManagerBackend::~URLDataManagerBackend() = default; |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 110 | |
John Abd-El-Malek | 76ad70a | 2020-05-18 16:08:12 | [diff] [blame] | 111 | URLDataManagerBackend* URLDataManagerBackend::GetForBrowserContext( |
| 112 | BrowserContext* context) { |
| 113 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 114 | if (!context->GetUserData(kURLDataManagerBackendKeyName)) { |
| 115 | context->SetUserData(kURLDataManagerBackendKeyName, |
| 116 | std::make_unique<URLDataManagerBackend>()); |
| 117 | } |
| 118 | return static_cast<URLDataManagerBackend*>( |
| 119 | context->GetUserData(kURLDataManagerBackendKeyName)); |
| 120 | } |
| 121 | |
| 122 | void URLDataManagerBackend::AddDataSource(URLDataSourceImpl* source) { |
| 123 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Chris Hamilton | bed9bc8 | 2018-07-11 23:13:35 | [diff] [blame] | 124 | if (!source->source()->ShouldReplaceExistingSource()) { |
jdoerrie | 55ec69d | 2018-10-08 13:34:46 | [diff] [blame] | 125 | auto i = data_sources_.find(source->source_name()); |
Chris Hamilton | bed9bc8 | 2018-07-11 23:13:35 | [diff] [blame] | 126 | if (i != data_sources_.end()) |
[email protected] | ffe3d6f | 2011-02-23 16:42:52 | [diff] [blame] | 127 | return; |
[email protected] | ffe3d6f | 2011-02-23 16:42:52 | [diff] [blame] | 128 | } |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 129 | data_sources_[source->source_name()] = source; |
Chris Hamilton | bed9bc8 | 2018-07-11 23:13:35 | [diff] [blame] | 130 | source->backend_ = weak_factory_.GetWeakPtr(); |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 131 | } |
| 132 | |
dbeam | 25af85bd | 2016-12-09 03:34:37 | [diff] [blame] | 133 | void URLDataManagerBackend::UpdateWebUIDataSource( |
| 134 | const std::string& source_name, |
David Bienvenu | ead449e | 2022-04-07 19:30:02 | [diff] [blame] | 135 | const base::Value::Dict& update) { |
jdoerrie | 55ec69d | 2018-10-08 13:34:46 | [diff] [blame] | 136 | auto it = data_sources_.find(source_name); |
dbeam | 25af85bd | 2016-12-09 03:34:37 | [diff] [blame] | 137 | if (it == data_sources_.end() || !it->second->IsWebUIDataSourceImpl()) { |
Peter Boström | fc7ddc18 | 2024-10-31 19:37:21 | [diff] [blame] | 138 | NOTREACHED(); |
dbeam | 25af85bd | 2016-12-09 03:34:37 | [diff] [blame] | 139 | } |
| 140 | static_cast<WebUIDataSourceImpl*>(it->second.get()) |
| 141 | ->AddLocalizedStrings(update); |
| 142 | } |
| 143 | |
[email protected] | 73b718f | 2014-01-27 02:59:46 | [diff] [blame] | 144 | URLDataSourceImpl* URLDataManagerBackend::GetDataSourceFromURL( |
| 145 | const GURL& url) { |
Giovanni Ortuño Urquidi | 0a3622b | 2020-01-14 01:54:22 | [diff] [blame] | 146 | // chrome-untrusted:// sources keys are of the form "chrome-untrusted://host". |
| 147 | if (url.scheme() == kChromeUIUntrustedScheme) { |
Mike West | 800532c | 2021-10-14 09:26:52 | [diff] [blame] | 148 | auto i = data_sources_.find(url.DeprecatedGetOriginAsURL().spec()); |
Giovanni Ortuño Urquidi | 0a3622b | 2020-01-14 01:54:22 | [diff] [blame] | 149 | if (i == data_sources_.end()) |
| 150 | return nullptr; |
| 151 | return i->second.get(); |
| 152 | } |
| 153 | |
[email protected] | 73b718f | 2014-01-27 02:59:46 | [diff] [blame] | 154 | // The input usually looks like: chrome://source_name/extra_bits?foo |
| 155 | // so do a lookup using the host of the URL. |
jdoerrie | 55ec69d | 2018-10-08 13:34:46 | [diff] [blame] | 156 | auto i = data_sources_.find(url.host()); |
[email protected] | 73b718f | 2014-01-27 02:59:46 | [diff] [blame] | 157 | if (i != data_sources_.end()) |
| 158 | return i->second.get(); |
| 159 | |
| 160 | // No match using the host of the URL, so do a lookup using the scheme for |
| 161 | // URLs on the form source_name://extra_bits/foo . |
[email protected] | 9cb6c30 | 2014-01-31 22:49:46 | [diff] [blame] | 162 | i = data_sources_.find(url.scheme() + "://"); |
[email protected] | 73b718f | 2014-01-27 02:59:46 | [diff] [blame] | 163 | if (i != data_sources_.end()) |
| 164 | return i->second.get(); |
| 165 | |
| 166 | // No matches found, so give up. |
thestig | 922d34c | 2016-08-23 03:40:52 | [diff] [blame] | 167 | return nullptr; |
[email protected] | 73b718f | 2014-01-27 02:59:46 | [diff] [blame] | 168 | } |
| 169 | |
jam | 8c4edd0 | 2017-05-06 18:50:33 | [diff] [blame] | 170 | scoped_refptr<net::HttpResponseHeaders> URLDataManagerBackend::GetHeaders( |
| 171 | URLDataSourceImpl* source_impl, |
Giovanni Ortuño Urquidi | 66512d5 | 2022-07-20 01:22:11 | [diff] [blame] | 172 | const GURL& url, |
jam | 8c4edd0 | 2017-05-06 18:50:33 | [diff] [blame] | 173 | const std::string& origin) { |
| 174 | // Set the headers so that requests serviced by ChromeURLDataManager return a |
| 175 | // status code of 200. Without this they return a 0, which makes the status |
| 176 | // indistiguishable from other error types. Instant relies on getting a 200. |
Lei Zhang | c77274da | 2018-08-28 23:14:00 | [diff] [blame] | 177 | auto headers = |
| 178 | base::MakeRefCounted<net::HttpResponseHeaders>("HTTP/1.1 200 OK"); |
jam | 8c4edd0 | 2017-05-06 18:50:33 | [diff] [blame] | 179 | if (!source_impl) |
| 180 | return headers; |
| 181 | |
| 182 | URLDataSource* source = source_impl->source(); |
| 183 | // Determine the least-privileged content security policy header, if any, |
| 184 | // that is compatible with a given WebUI URL, and append it to the existing |
| 185 | // response headers. |
| 186 | if (source->ShouldAddContentSecurityPolicy()) { |
Matt Menke | 4426a15 | 2020-04-17 03:45:48 | [diff] [blame] | 187 | std::string csp_header; |
Becca Hughes | 88a4afe | 2020-06-23 01:30:46 | [diff] [blame] | 188 | |
| 189 | const network::mojom::CSPDirectiveName kAllDirectives[] = { |
Jun Kokatsu | 509e38f | 2021-02-11 05:00:57 | [diff] [blame] | 190 | network::mojom::CSPDirectiveName::BaseURI, |
Becca Hughes | 88a4afe | 2020-06-23 01:30:46 | [diff] [blame] | 191 | network::mojom::CSPDirectiveName::ChildSrc, |
Oleh Lamzin | 54b81ab | 2020-07-20 17:54:28 | [diff] [blame] | 192 | network::mojom::CSPDirectiveName::ConnectSrc, |
Becca Hughes | 88a4afe | 2020-06-23 01:30:46 | [diff] [blame] | 193 | network::mojom::CSPDirectiveName::DefaultSrc, |
Nan Lin | efd66c8 | 2022-01-24 20:33:15 | [diff] [blame] | 194 | network::mojom::CSPDirectiveName::FencedFrameSrc, |
Jun Kokatsu | 509e38f | 2021-02-11 05:00:57 | [diff] [blame] | 195 | network::mojom::CSPDirectiveName::FormAction, |
Toby Huang | 90da0c6 | 2022-02-17 00:54:20 | [diff] [blame] | 196 | network::mojom::CSPDirectiveName::FontSrc, |
Oleh Lamzin | 171aff9f | 2020-07-17 08:23:41 | [diff] [blame] | 197 | network::mojom::CSPDirectiveName::FrameSrc, |
Becca Hughes | 88a4afe | 2020-06-23 01:30:46 | [diff] [blame] | 198 | network::mojom::CSPDirectiveName::ImgSrc, |
| 199 | network::mojom::CSPDirectiveName::MediaSrc, |
| 200 | network::mojom::CSPDirectiveName::ObjectSrc, |
Jun Kokatsu | 5cb72d5 | 2020-08-21 21:27:19 | [diff] [blame] | 201 | network::mojom::CSPDirectiveName::RequireTrustedTypesFor, |
Becca Hughes | 88a4afe | 2020-06-23 01:30:46 | [diff] [blame] | 202 | network::mojom::CSPDirectiveName::ScriptSrc, |
| 203 | network::mojom::CSPDirectiveName::StyleSrc, |
Jun Kokatsu | 5cb72d5 | 2020-08-21 21:27:19 | [diff] [blame] | 204 | network::mojom::CSPDirectiveName::TrustedTypes, |
Oleh Lamzin | 54b81ab | 2020-07-20 17:54:28 | [diff] [blame] | 205 | network::mojom::CSPDirectiveName::WorkerSrc}; |
Becca Hughes | 88a4afe | 2020-06-23 01:30:46 | [diff] [blame] | 206 | |
| 207 | for (auto& directive : kAllDirectives) { |
| 208 | csp_header.append(source->GetContentSecurityPolicy(directive)); |
| 209 | } |
| 210 | |
Alison Gale | 81f4f2c7 | 2024-04-22 19:33:31 | [diff] [blame] | 211 | // TODO(crbug.com/40118579): Both CSP frame ancestors and XFO headers may be |
jessing | 8eb67f09 | 2020-02-20 01:29:27 | [diff] [blame] | 212 | // added to the response but frame ancestors would take precedence. In the |
| 213 | // future, XFO will be removed so when that happens remove the check and |
| 214 | // always add frame ancestors. |
Becca Hughes | 88a4afe | 2020-06-23 01:30:46 | [diff] [blame] | 215 | if (source->ShouldDenyXFrameOptions()) { |
| 216 | csp_header.append(source->GetContentSecurityPolicy( |
| 217 | network::mojom::CSPDirectiveName::FrameAncestors)); |
| 218 | } |
| 219 | |
Matt Menke | 4426a15 | 2020-04-17 03:45:48 | [diff] [blame] | 220 | headers->SetHeader(kChromeURLContentSecurityPolicyHeaderName, csp_header); |
jam | 8c4edd0 | 2017-05-06 18:50:33 | [diff] [blame] | 221 | } |
| 222 | |
Matt Menke | 4426a15 | 2020-04-17 03:45:48 | [diff] [blame] | 223 | if (source->ShouldDenyXFrameOptions()) { |
| 224 | headers->SetHeader(kChromeURLXFrameOptionsHeaderName, |
| 225 | kChromeURLXFrameOptionsHeaderValue); |
| 226 | } |
jam | 8c4edd0 | 2017-05-06 18:50:33 | [diff] [blame] | 227 | |
| 228 | if (!source->AllowCaching()) |
Matt Menke | 4426a15 | 2020-04-17 03:45:48 | [diff] [blame] | 229 | headers->SetHeader("Cache-Control", "no-cache"); |
jam | 8c4edd0 | 2017-05-06 18:50:33 | [diff] [blame] | 230 | |
Giovanni Ortuño Urquidi | 66512d5 | 2022-07-20 01:22:11 | [diff] [blame] | 231 | std::string mime_type = source->GetMimeType(url); |
Matt Menke | 4426a15 | 2020-04-17 03:45:48 | [diff] [blame] | 232 | if (source->ShouldServeMimeTypeAsContentTypeHeader() && !mime_type.empty()) |
| 233 | headers->SetHeader(net::HttpRequestHeaders::kContentType, mime_type); |
jam | 8c4edd0 | 2017-05-06 18:50:33 | [diff] [blame] | 234 | |
Trent Apted | dbdebc1 | 2021-04-06 11:27:00 | [diff] [blame] | 235 | const std::string coop_value = source->GetCrossOriginOpenerPolicy(); |
| 236 | if (!coop_value.empty()) { |
| 237 | headers->SetHeader(kChromeURLCrossOriginOpenerPolicyName, coop_value); |
| 238 | } |
| 239 | const std::string coep_value = source->GetCrossOriginEmbedderPolicy(); |
| 240 | if (!coep_value.empty()) { |
| 241 | headers->SetHeader(kChromeURLCrossOriginEmbedderPolicyName, coep_value); |
| 242 | } |
| 243 | const std::string corp_value = source->GetCrossOriginResourcePolicy(); |
| 244 | if (!corp_value.empty()) { |
| 245 | headers->SetHeader(kChromeURLCrossOriginResourcePolicyName, corp_value); |
| 246 | } |
| 247 | |
jam | 8c4edd0 | 2017-05-06 18:50:33 | [diff] [blame] | 248 | if (!origin.empty()) { |
| 249 | std::string header = source->GetAccessControlAllowOriginForOrigin(origin); |
| 250 | DCHECK(header.empty() || header == origin || header == "*" || |
| 251 | header == "null"); |
| 252 | if (!header.empty()) { |
Matt Menke | 4426a15 | 2020-04-17 03:45:48 | [diff] [blame] | 253 | headers->SetHeader("Access-Control-Allow-Origin", header); |
| 254 | headers->SetHeader("Vary", "Origin"); |
jam | 8c4edd0 | 2017-05-06 18:50:33 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | return headers; |
| 259 | } |
| 260 | |
| 261 | bool URLDataManagerBackend::CheckURLIsValid(const GURL& url) { |
| 262 | std::vector<std::string> additional_schemes; |
Andrey Kosyakov | 031e9e3 | 2017-08-18 21:00:35 | [diff] [blame] | 263 | DCHECK(url.SchemeIs(kChromeUIScheme) || |
Giovanni Ortuño Urquidi | 0a3622b | 2020-01-14 01:54:22 | [diff] [blame] | 264 | url.SchemeIs(kChromeUIUntrustedScheme) || |
jam | 8c4edd0 | 2017-05-06 18:50:33 | [diff] [blame] | 265 | (GetContentClient()->browser()->GetAdditionalWebUISchemes( |
| 266 | &additional_schemes), |
| 267 | SchemeIsInSchemes(url.scheme(), additional_schemes))); |
| 268 | |
| 269 | if (!url.is_valid()) { |
Peter Boström | fc7ddc18 | 2024-10-31 19:37:21 | [diff] [blame] | 270 | NOTREACHED(); |
jam | 8c4edd0 | 2017-05-06 18:50:33 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | return true; |
| 274 | } |
| 275 | |
jam | 4374b95 | 2017-05-10 21:16:41 | [diff] [blame] | 276 | bool URLDataManagerBackend::IsValidNetworkErrorCode(int error_code) { |
Matt Menke | f6d1512a | 2022-06-02 00:29:45 | [diff] [blame] | 277 | base::Value::Dict error_codes = net::GetNetConstants(); |
| 278 | const base::Value::Dict* net_error_codes_dict = |
| 279 | error_codes.FindDict(kNetworkErrorKey); |
jam | 4374b95 | 2017-05-10 21:16:41 | [diff] [blame] | 280 | |
| 281 | if (net_error_codes_dict != nullptr) { |
Matt Menke | f6d1512a | 2022-06-02 00:29:45 | [diff] [blame] | 282 | for (auto it = net_error_codes_dict->begin(); |
| 283 | it != net_error_codes_dict->end(); ++it) { |
| 284 | if (error_code == it->second.GetInt()) |
jam | 4374b95 | 2017-05-10 21:16:41 | [diff] [blame] | 285 | return true; |
| 286 | } |
| 287 | } |
| 288 | return false; |
| 289 | } |
| 290 | |
jam | 1a97290b | 2017-05-09 04:30:50 | [diff] [blame] | 291 | std::vector<std::string> URLDataManagerBackend::GetWebUISchemes() { |
Alex Moshchuk | b7692776 | 2023-05-09 05:46:31 | [diff] [blame] | 292 | if (g_disallow_webui_scheme_caching_for_testing) { |
| 293 | return GetWebUISchemesSlow(); |
| 294 | } |
Jayson Adams | 20123ee9 | 2021-07-27 01:38:43 | [diff] [blame] | 295 | |
Alex Moshchuk | b7692776 | 2023-05-09 05:46:31 | [diff] [blame] | 296 | return GetWebUISchemesCached(); |
| 297 | } |
| 298 | |
| 299 | void URLDataManagerBackend::SetDisallowWebUISchemeCachingForTesting( |
| 300 | bool disallow_caching) { |
| 301 | g_disallow_webui_scheme_caching_for_testing = disallow_caching; |
jam | 1a97290b | 2017-05-09 04:30:50 | [diff] [blame] | 302 | } |
| 303 | |
[email protected] | 5bf1646f5 | 2013-01-28 03:57:02 | [diff] [blame] | 304 | } // namespace content |