blob: 222c7999df9b3edc1d1f554bdb8cd99fc280659c [file] [log] [blame]
Avi Drissman4e1b7bc32022-09-15 14:03:501// Copyright 2012 The Chromium Authors
[email protected]248ce192011-02-10 15:26:342// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]33c1c26a2013-01-24 21:56:265#ifndef CONTENT_BROWSER_WEBUI_URL_DATA_MANAGER_BACKEND_H_
6#define CONTENT_BROWSER_WEBUI_URL_DATA_MANAGER_BACKEND_H_
[email protected]248ce192011-02-10 15:26:347
[email protected]248ce192011-02-10 15:26:348#include <map>
dcheng59716272016-04-09 05:19:089#include <memory>
[email protected]248ce192011-02-10 15:26:3410#include <string>
11#include <vector>
12
Chris Hamiltonbed9bc82018-07-11 23:13:3513#include "base/memory/weak_ptr.h"
[email protected]33c1c26a2013-01-24 21:56:2614#include "base/supports_user_data.h"
dbeam25af85bd2016-12-09 03:34:3715#include "base/values.h"
[email protected]33c1c26a2013-01-24 21:56:2616#include "content/browser/webui/url_data_manager.h"
jam8c4edd02017-05-06 18:50:3317#include "net/http/http_response_headers.h"
[email protected]19677192011-11-12 03:54:5618
[email protected]248ce192011-02-10 15:26:3419class GURL;
[email protected]248ce192011-02-10 15:26:3420
[email protected]68c7630b2012-05-02 22:37:4221namespace base {
22class RefCountedMemory;
23}
24
[email protected]5bf1646f52013-01-28 03:57:0225namespace content {
[email protected]0234b1c2014-06-28 06:12:2826
John Abd-El-Malek76ad70a2020-05-18 16:08:1227class BrowserContext;
[email protected]5bf1646f52013-01-28 03:57:0228class URLDataSourceImpl;
[email protected]248ce192011-02-10 15:26:3429
John Abd-El-Malek76ad70a2020-05-18 16:08:1230// URLDataManagerBackend is used internally by ChromeURLDataManager on the UI
[email protected]5bf1646f52013-01-28 03:57:0231// thread. In most cases you can use the API in ChromeURLDataManager and ignore
John Abd-El-Malek76ad70a2020-05-18 16:08:1232// this class. URLDataManagerBackend is owned by BrowserContext.
Alex Yang5aed8052024-10-07 23:41:4333class CONTENT_EXPORT URLDataManagerBackend
34 : public base::SupportsUserData::Data {
[email protected]248ce192011-02-10 15:26:3435 public:
36 typedef int RequestID;
Alex Yangcc8ef632024-10-08 20:13:3737 using DataSourceMap = std::map<std::string, scoped_refptr<URLDataSourceImpl>>;
[email protected]248ce192011-02-10 15:26:3438
[email protected]5bf1646f52013-01-28 03:57:0239 URLDataManagerBackend();
Peter Boström828b9022021-09-21 02:28:4340
41 URLDataManagerBackend(const URLDataManagerBackend&) = delete;
42 URLDataManagerBackend& operator=(const URLDataManagerBackend&) = delete;
43
dchengc2282aa2014-10-21 12:07:5844 ~URLDataManagerBackend() override;
[email protected]248ce192011-02-10 15:26:3445
John Abd-El-Malek76ad70a2020-05-18 16:08:1246 static URLDataManagerBackend* GetForBrowserContext(BrowserContext* context);
47
[email protected]248ce192011-02-10 15:26:3448 // Adds a DataSource to the collection of data sources.
[email protected]90d9f1e2013-01-16 02:46:4149 void AddDataSource(URLDataSourceImpl* source);
[email protected]248ce192011-02-10 15:26:3450
dbeam25af85bd2016-12-09 03:34:3751 void UpdateWebUIDataSource(const std::string& source_name,
David Bienvenuead449e2022-04-07 19:30:0252 const base::Value::Dict& update);
dbeam25af85bd2016-12-09 03:34:3753
pkasting5b923372016-09-03 04:53:1554 // DataSource invokes this. Sends the data to the URLRequest. |bytes| may be
55 // null, which signals an error handling the request.
[email protected]68c7630b2012-05-02 22:37:4256 void DataAvailable(RequestID request_id, base::RefCountedMemory* bytes);
[email protected]248ce192011-02-10 15:26:3457
jam8c4edd02017-05-06 18:50:3358 // 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 Yangcc8ef632024-10-08 20:13:3762 const DataSourceMap& data_sources() const { return data_sources_; }
63
jam8c4edd02017-05-06 18:50:3364 // Creates and sets the response headers for the given request.
65 static scoped_refptr<net::HttpResponseHeaders> GetHeaders(
66 URLDataSourceImpl* source,
Giovanni Ortuño Urquidi66512d52022-07-20 01:22:1167 const GURL& url,
jam8c4edd02017-05-06 18:50:3368 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
jam4374b952017-05-10 21:16:4173 // Check if the given integer is a valid network error code.
74 static bool IsValidNetworkErrorCode(int error_code);
75
jam1a97290b2017-05-09 04:30:5076 // 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 Moshchukb76927762023-05-09 05:46:3180 // 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 Yang5aed8052024-10-07 23:41:4383 static void SetDisallowWebUISchemeCachingForTesting(bool disallow_caching);
Alex Moshchukb76927762023-05-09 05:46:3184
[email protected]248ce192011-02-10 15:26:3485 private:
[email protected]248ce192011-02-10 15:26:3486 // Custom sources of data, keyed by source path (e.g. "favicon").
87 DataSourceMap data_sources_;
88
Chris Hamiltonbed9bc82018-07-11 23:13:3589 // 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 Roman3bca4bf2019-07-11 03:41:2593 base::WeakPtrFactory<URLDataManagerBackend> weak_factory_{this};
[email protected]248ce192011-02-10 15:26:3494};
95
[email protected]5bf1646f52013-01-28 03:57:0296} // namespace content
97
[email protected]33c1c26a2013-01-24 21:56:2698#endif // CONTENT_BROWSER_WEBUI_URL_DATA_MANAGER_BACKEND_H_