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 | #ifndef CONTENT_BROWSER_WEBUI_URL_DATA_MANAGER_BACKEND_H_ |
| 6 | #define CONTENT_BROWSER_WEBUI_URL_DATA_MANAGER_BACKEND_H_ |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 7 | |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 8 | #include <map> |
dcheng | 5971627 | 2016-04-09 05:19:08 | [diff] [blame] | 9 | #include <memory> |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
Chris Hamilton | bed9bc8 | 2018-07-11 23:13:35 | [diff] [blame] | 13 | #include "base/memory/weak_ptr.h" |
[email protected] | 33c1c26a | 2013-01-24 21:56:26 | [diff] [blame] | 14 | #include "base/supports_user_data.h" |
dbeam | 25af85bd | 2016-12-09 03:34:37 | [diff] [blame] | 15 | #include "base/values.h" |
[email protected] | 33c1c26a | 2013-01-24 21:56:26 | [diff] [blame] | 16 | #include "content/browser/webui/url_data_manager.h" |
jam | 8c4edd0 | 2017-05-06 18:50:33 | [diff] [blame] | 17 | #include "net/http/http_response_headers.h" |
[email protected] | 1967719 | 2011-11-12 03:54:56 | [diff] [blame] | 18 | |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 19 | class GURL; |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 20 | |
[email protected] | 68c7630b | 2012-05-02 22:37:42 | [diff] [blame] | 21 | namespace base { |
| 22 | class RefCountedMemory; |
| 23 | } |
| 24 | |
[email protected] | 5bf1646f5 | 2013-01-28 03:57:02 | [diff] [blame] | 25 | namespace content { |
[email protected] | 0234b1c | 2014-06-28 06:12:28 | [diff] [blame] | 26 | |
John Abd-El-Malek | 76ad70a | 2020-05-18 16:08:12 | [diff] [blame] | 27 | class BrowserContext; |
[email protected] | 5bf1646f5 | 2013-01-28 03:57:02 | [diff] [blame] | 28 | class URLDataSourceImpl; |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 29 | |
John Abd-El-Malek | 76ad70a | 2020-05-18 16:08:12 | [diff] [blame] | 30 | // URLDataManagerBackend is used internally by ChromeURLDataManager on the UI |
[email protected] | 5bf1646f5 | 2013-01-28 03:57:02 | [diff] [blame] | 31 | // thread. In most cases you can use the API in ChromeURLDataManager and ignore |
John Abd-El-Malek | 76ad70a | 2020-05-18 16:08:12 | [diff] [blame] | 32 | // this class. URLDataManagerBackend is owned by BrowserContext. |
Alex Yang | 5aed805 | 2024-10-07 23:41:43 | [diff] [blame] | 33 | class CONTENT_EXPORT URLDataManagerBackend |
| 34 | : public base::SupportsUserData::Data { |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 35 | public: |
| 36 | typedef int RequestID; |
Alex Yang | cc8ef63 | 2024-10-08 20:13:37 | [diff] [blame] | 37 | using DataSourceMap = std::map<std::string, scoped_refptr<URLDataSourceImpl>>; |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 38 | |
[email protected] | 5bf1646f5 | 2013-01-28 03:57:02 | [diff] [blame] | 39 | URLDataManagerBackend(); |
Peter Boström | 828b902 | 2021-09-21 02:28:43 | [diff] [blame] | 40 | |
| 41 | URLDataManagerBackend(const URLDataManagerBackend&) = delete; |
| 42 | URLDataManagerBackend& operator=(const URLDataManagerBackend&) = delete; |
| 43 | |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 44 | ~URLDataManagerBackend() override; |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 45 | |
John Abd-El-Malek | 76ad70a | 2020-05-18 16:08:12 | [diff] [blame] | 46 | static URLDataManagerBackend* GetForBrowserContext(BrowserContext* context); |
| 47 | |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 48 | // Adds a DataSource to the collection of data sources. |
[email protected] | 90d9f1e | 2013-01-16 02:46:41 | [diff] [blame] | 49 | void AddDataSource(URLDataSourceImpl* source); |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 50 | |
dbeam | 25af85bd | 2016-12-09 03:34:37 | [diff] [blame] | 51 | void UpdateWebUIDataSource(const std::string& source_name, |
David Bienvenu | ead449e | 2022-04-07 19:30:02 | [diff] [blame] | 52 | const base::Value::Dict& update); |
dbeam | 25af85bd | 2016-12-09 03:34:37 | [diff] [blame] | 53 | |
pkasting | 5b92337 | 2016-09-03 04:53:15 | [diff] [blame] | 54 | // DataSource invokes this. Sends the data to the URLRequest. |bytes| may be |
| 55 | // null, which signals an error handling the request. |
[email protected] | 68c7630b | 2012-05-02 22:37:42 | [diff] [blame] | 56 | void DataAvailable(RequestID request_id, base::RefCountedMemory* bytes); |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 57 | |
jam | 8c4edd0 | 2017-05-06 18:50:33 | [diff] [blame] | 58 | // Look up the data source for the request. Returns the source if it is found, |
| 59 | // else NULL. |
| 60 | URLDataSourceImpl* GetDataSourceFromURL(const GURL& url); |
| 61 | |
Alex Yang | cc8ef63 | 2024-10-08 20:13:37 | [diff] [blame] | 62 | const DataSourceMap& data_sources() const { return data_sources_; } |
| 63 | |
jam | 8c4edd0 | 2017-05-06 18:50:33 | [diff] [blame] | 64 | // Creates and sets the response headers for the given request. |
| 65 | static scoped_refptr<net::HttpResponseHeaders> GetHeaders( |
| 66 | URLDataSourceImpl* source, |
Giovanni Ortuño Urquidi | 66512d5 | 2022-07-20 01:22:11 | [diff] [blame] | 67 | const GURL& url, |
jam | 8c4edd0 | 2017-05-06 18:50:33 | [diff] [blame] | 68 | const std::string& origin); |
| 69 | |
| 70 | // Returns whether |url| passes some sanity checks and is a valid GURL. |
| 71 | static bool CheckURLIsValid(const GURL& url); |
| 72 | |
jam | 4374b95 | 2017-05-10 21:16:41 | [diff] [blame] | 73 | // Check if the given integer is a valid network error code. |
| 74 | static bool IsValidNetworkErrorCode(int error_code); |
| 75 | |
jam | 1a97290b | 2017-05-09 04:30:50 | [diff] [blame] | 76 | // Returns the schemes that are used by WebUI (i.e. the set from content and |
| 77 | // its embedder). |
| 78 | static std::vector<std::string> GetWebUISchemes(); |
| 79 | |
Alex Moshchuk | b7692776 | 2023-05-09 05:46:31 | [diff] [blame] | 80 | // When this is true, GetWebUISchemes() bypasses the caching of its result |
| 81 | // and always recomputes the list of WebUI schemes. This should be used in |
| 82 | // tests that inject custom WebUI schemes, which may otherwise not be seen. |
Alex Yang | 5aed805 | 2024-10-07 23:41:43 | [diff] [blame] | 83 | static void SetDisallowWebUISchemeCachingForTesting(bool disallow_caching); |
Alex Moshchuk | b7692776 | 2023-05-09 05:46:31 | [diff] [blame] | 84 | |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 85 | private: |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 86 | // Custom sources of data, keyed by source path (e.g. "favicon"). |
| 87 | DataSourceMap data_sources_; |
| 88 | |
Chris Hamilton | bed9bc8 | 2018-07-11 23:13:35 | [diff] [blame] | 89 | // Vends weak pointers to URLDataSources, allowing them to continue referring |
| 90 | // to the backend that originally owned them, even if they've been replaced |
| 91 | // and detached from the backend. This allows outstanding asynchronous queries |
| 92 | // to be served and routed to the backend to which they were original issued. |
Jeremy Roman | 3bca4bf | 2019-07-11 03:41:25 | [diff] [blame] | 93 | base::WeakPtrFactory<URLDataManagerBackend> weak_factory_{this}; |
[email protected] | 248ce19 | 2011-02-10 15:26:34 | [diff] [blame] | 94 | }; |
| 95 | |
[email protected] | 5bf1646f5 | 2013-01-28 03:57:02 | [diff] [blame] | 96 | } // namespace content |
| 97 | |
[email protected] | 33c1c26a | 2013-01-24 21:56:26 | [diff] [blame] | 98 | #endif // CONTENT_BROWSER_WEBUI_URL_DATA_MANAGER_BACKEND_H_ |