0

Move webui::AddResourcePathsBulk to content::WebUIDataSource.

Previously it was implemented as a helper function in webui_util.cc,
but it better belongs in WebUIDataSource as an instance method.

With this change, several calls like
webui::AddResourcePathsBulk(
    source, base::make_span(kResources, kResourcesSize));
are converted to
source->AddResourcePaths(
    base::make_span(kResources, kResourcesSize));

A concrete benefit of moving this method in WebUIDataSource is that
it now can be leveraged by more places in the code (for example
chromeos/components/ or content/browser/webui/), which do not have
permission to depend on chrome/.

Bug: 1176299
Change-Id: I4df20cb6067fef885d3b0d48e0ee500238f27177
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2692121
Commit-Queue: dpapad <dpapad@chromium.org>
Reviewed-by: Avi Drissman <avi@chromium.org>
Reviewed-by: Rebekah Potter <rbpotter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#854403}
This commit is contained in:
dpapad
2021-02-16 19:59:19 +00:00
committed by Chromium LUCI CQ
parent 5f464e7b66
commit 71310e5a75
21 changed files with 47 additions and 52 deletions

@ -85,8 +85,7 @@ AppLauncherPageUI::AppLauncherPageUI(content::WebUI* web_ui)
content::WebUIDataSource::Create(chrome::kChromeUIAppLauncherPageHost);
content::WebUIDataSource::Add(GetProfile(), source);
webui::AddResourcePathsBulk(
source, base::make_span(kAppsResources, kAppsResourcesSize));
source->AddResourcePaths(base::make_span(kAppsResources, kAppsResourcesSize));
source->SetDefaultResource(IDR_APPS_NEW_TAB_HTML);
source->UseStringsJs();

@ -33,9 +33,8 @@ BluetoothInternalsUI::BluetoothInternalsUI(content::WebUI* web_ui)
IDR_WEBUI_JS_TEST_LOADER_UTIL_JS);
// Add required resources.
webui::AddResourcePathsBulk(
html_source, base::make_span(kBluetoothInternalsResources,
kBluetoothInternalsResourcesSize));
html_source->AddResourcePaths(base::make_span(
kBluetoothInternalsResources, kBluetoothInternalsResourcesSize));
html_source->SetDefaultResource(
IDR_BLUETOOTH_INTERNALS_BLUETOOTH_INTERNALS_HTML);

@ -29,8 +29,7 @@ content::WebUIDataSource* CreateInvalidationsHTMLSource() {
source->AddResourcePath("test_loader_util.js",
IDR_WEBUI_JS_TEST_LOADER_UTIL_JS);
source->DisableTrustedTypesCSP();
webui::AddResourcePathsBulk(
source,
source->AddResourcePaths(
base::make_span(kInvalidationsResources, kInvalidationsResourcesSize));
source->SetDefaultResource(IDR_INVALIDATIONS_ABOUT_INVALIDATIONS_HTML);
return source;

@ -79,8 +79,8 @@ content::WebUIDataSource* CreateWebRtcLogsUIHTMLSource() {
source->AddLocalizedStrings(kStrings);
source->UseStringsJs();
webui::AddResourcePathsBulk(
source, base::make_span(kWebrtcLogsResources, kWebrtcLogsResourcesSize));
source->AddResourcePaths(
base::make_span(kWebrtcLogsResources, kWebrtcLogsResourcesSize));
source->SetDefaultResource(IDR_WEBRTC_LOGS_WEBRTC_LOGS_HTML);
return source;
}

@ -44,8 +44,7 @@ namespace {
content::WebUIDataSource* CreateNetInternalsHTMLSource() {
content::WebUIDataSource* source =
content::WebUIDataSource::Create(chrome::kChromeUINetInternalsHost);
webui::AddResourcePathsBulk(
source,
source->AddResourcePaths(
base::make_span(kNetInternalsResources, kNetInternalsResourcesSize));
source->SetDefaultResource(IDR_NET_INTERNALS_INDEX_HTML);
source->OverrideContentSecurityPolicy(

@ -259,7 +259,7 @@ content::WebUIDataSource* CreateNewTabPageUiHtmlSource(Profile* profile) {
{omnibox::kPageIconResourceName, IDR_LOCAL_NTP_ICONS_PAGE},
{omnibox::kSearchIconResourceName, IDR_WEBUI_IMAGES_ICON_SEARCH_SVG},
{omnibox::kTrendingUpIconResourceName, IDR_LOCAL_NTP_ICONS_TRENDING_UP}};
webui::AddResourcePathsBulk(source, kImages);
source->AddResourcePaths(kImages);
source->AddBoolean(
"recipeTasksModuleEnabled",

@ -41,8 +41,8 @@ OmniboxUI::OmniboxUI(content::WebUI* web_ui)
VersionUI::AddVersionDetailStrings(source);
source->UseStringsJs();
webui::AddResourcePathsBulk(
source, base::make_span(kOmniboxResources, kOmniboxResourcesSize));
source->AddResourcePaths(
base::make_span(kOmniboxResources, kOmniboxResourcesSize));
source->SetDefaultResource(IDR_OMNIBOX_OMNIBOX_HTML);
#if !defined(OS_ANDROID)

@ -26,8 +26,7 @@ content::WebUIDataSource* CreateQuotaInternalsHTMLSource() {
content::WebUIDataSource* source =
content::WebUIDataSource::Create(chrome::kChromeUIQuotaInternalsHost);
source->UseStringsJs();
webui::AddResourcePathsBulk(
source,
source->AddResourcePaths(
base::make_span(kQuotaInternalsResources, kQuotaInternalsResourcesSize));
source->AddResourcePath("", IDR_QUOTA_INTERNALS_MAIN_HTML);
source->OverrideContentSecurityPolicy(

@ -176,7 +176,7 @@ content::WebUIDataSource* CreateWebUIDataSource(Profile* profile) {
#endif
#endif // BUILDFLAG(IS_CHROMEOS_ASH)
};
webui::AddResourcePathsBulk(source, kResources);
source->AddResourcePaths(kResources);
static constexpr webui::LocalizedString kLocalizedStrings[] = {
{"title", IDS_CHROME_SIGNIN_TITLE},

@ -83,7 +83,7 @@ content::WebUIDataSource* UserManagerUI::CreateUIDataSource(
{"user_manager_tutorial.html", IDR_USER_MANAGER_TUTORIAL_HTML},
{"user_manager_tutorial.js", IDR_USER_MANAGER_TUTORIAL_JS},
};
webui::AddResourcePathsBulk(source, kResources);
source->AddResourcePaths(kResources);
source->SetDefaultResource(IDR_USER_MANAGER_HTML);

@ -25,9 +25,9 @@ content::WebUIDataSource* CreateSyncFileSystemInternalsHTMLSource() {
content::WebUIDataSource::Create(
chrome::kChromeUISyncFileSystemInternalsHost);
source->UseStringsJs();
webui::AddResourcePathsBulk(
source, base::make_span(kSyncFileSystemInternalsResources,
kSyncFileSystemInternalsResourcesSize));
source->AddResourcePaths(
base::make_span(kSyncFileSystemInternalsResources,
kSyncFileSystemInternalsResourcesSize));
source->SetDefaultResource(IDR_SYNC_FILE_SYSTEM_INTERNALS_MAIN_HTML);
return source;
}

@ -54,7 +54,7 @@ content::WebUIDataSource* CreateSyncInternalsHTMLSource() {
{syncer::sync_ui_util::kInvalidationsJS,
IDR_SYNC_DRIVER_SYNC_INTERNALS_INVALIDATIONS_JS},
};
webui::AddResourcePathsBulk(source, kResources);
source->AddResourcePaths(kResources);
source->SetDefaultResource(IDR_SYNC_DRIVER_SYNC_INTERNALS_INDEX_HTML);
return source;

@ -30,7 +30,7 @@ UsbInternalsUI::UsbInternalsUI(content::WebUI* web_ui)
{"usb_manager_test.mojom-webui.js",
IDR_USB_DEVICE_MANAGER_TEST_MOJOM_WEBUI_JS},
};
webui::AddResourcePathsBulk(source, kPaths);
source->AddResourcePaths(kPaths);
webui::SetupWebUIDataSource(
source,

@ -25,8 +25,8 @@ WebUIDataSource* CreateWebApksUIDataSource() {
WebUIDataSource::Create(chrome::kChromeUIWebApksHost);
html_source->UseStringsJs();
webui::AddResourcePathsBulk(
html_source, base::make_span(kWebapksResources, kWebapksResourcesSize));
html_source->AddResourcePaths(
base::make_span(kWebapksResources, kWebapksResourcesSize));
html_source->SetDefaultResource(IDR_WEBAPKS_ABOUT_WEBAPKS_HTML);
return html_source;

@ -41,16 +41,10 @@ void SetupWebUIDataSource(content::WebUIDataSource* source,
base::span<const ResourcePath> resources,
int default_resource) {
SetJSModuleDefaults(source);
AddResourcePathsBulk(source, resources);
source->AddResourcePaths(resources);
source->AddResourcePath("", default_resource);
}
void AddResourcePathsBulk(content::WebUIDataSource* source,
base::span<const ResourcePath> paths) {
for (const auto& path : paths)
source->AddResourcePath(path.path, path.id);
}
bool IsEnterpriseManaged() {
#if BUILDFLAG(IS_CHROMEOS_ASH)
policy::BrowserPolicyConnectorChromeOS* connector =

@ -30,11 +30,6 @@ void SetupWebUIDataSource(content::WebUIDataSource* source,
base::span<const ResourcePath> resources,
int default_resource);
// Calls content::WebUIDataSource::AddResourcePath() in a for-loop for |paths|.
// Reduces code size vs. reimplementing the same for-loop.
void AddResourcePathsBulk(content::WebUIDataSource* source,
base::span<const ResourcePath> paths);
// Returns whether the device is enterprise managed. Note that on Linux, there's
// no good way of detecting whether the device is managed, so always return
// false.

@ -161,7 +161,7 @@ WelcomeUI::WelcomeUI(content::WebUI* web_ui, const GURL& url)
{"images/set_default_dark.svg", IDR_WELCOME_SET_DEFAULT_DARK},
{"images/set_default_light.svg", IDR_WELCOME_SET_DEFAULT_LIGHT},
};
webui::AddResourcePathsBulk(html_source, kPaths);
html_source->AddResourcePaths(kPaths);
#endif // BUILDFLAG(GOOGLE_CHROME_BRANDING)
#if defined(OS_WIN)

@ -24,6 +24,7 @@
#include "services/network/public/mojom/content_security_policy.mojom.h"
#include "ui/base/template_expressions.h"
#include "ui/base/webui/jstemplate_builder.h"
#include "ui/base/webui/resource_path.h"
#include "ui/base/webui/web_ui_util.h"
namespace content {
@ -183,6 +184,12 @@ void WebUIDataSourceImpl::AddResourcePath(base::StringPiece path,
path_to_idr_map_[path.as_string()] = resource_id;
}
void WebUIDataSourceImpl::AddResourcePaths(
base::span<const webui::ResourcePath> paths) {
for (const auto& path : paths)
AddResourcePath(path.path, path.id);
}
void WebUIDataSourceImpl::SetDefaultResource(int resource_id) {
default_resource_ = resource_id;
}

@ -42,6 +42,7 @@ class CONTENT_EXPORT WebUIDataSourceImpl : public URLDataSourceImpl,
void AddDouble(base::StringPiece name, double value) override;
void UseStringsJs() override;
void AddResourcePath(base::StringPiece path, int resource_id) override;
void AddResourcePaths(base::span<const webui::ResourcePath> paths) override;
void SetDefaultResource(int resource_id) override;
void SetRequestFilter(const WebUIDataSource::ShouldHandleRequestCallback&
should_handle_request_callback,

@ -27,6 +27,7 @@ class RefCountedMemory;
namespace webui {
struct LocalizedString;
struct ResourcePath;
} // namespace webui
namespace content {
@ -90,6 +91,11 @@ class WebUIDataSource {
// Adds a mapping between a path name and a resource to return.
virtual void AddResourcePath(base::StringPiece path, int resource_id) = 0;
// Calls AddResourcePath() in a for-loop for |paths|. Reduces code size vs.
// reimplementing the same for-loop.
virtual void AddResourcePaths(
base::span<const webui::ResourcePath> paths) = 0;
// Sets the resource to returned when no other paths match.
virtual void SetDefaultResource(int resource_id) = 0;

@ -377,13 +377,12 @@ an array of all the strings and use <code>AddLocalizedStrings()</code>:
source->AddLocalizedStrings(kStrings);
```
<a name="AddResourcePathsBulk"></a>
### webui::AddResourcePathsBulk()
<a name="AddResourcePaths"></a>
### WebUIDataSource::AddResourcePaths()
Similar to the localized strings, many Web UIs need to add a large number of
resource paths. In this case, use <code>AddResourcePathsBulk()</code> to
replace repeated calls to <code>AddResourcePath()</code>. There are two
versions. One works almost identically to the strings case:
resource paths. In this case, use <code>AddResourcePaths()</code> to
replace repeated calls to <code>AddResourcePath()</code>.
```c++
static constexpr webui::ResourcePath kPdfResources[] = {
@ -391,20 +390,18 @@ versions. One works almost identically to the strings case:
{"pdf/constants.js", IDR_PDF_CONSTANTS_JS},
{"pdf/controller.js", IDR_PDF_CONTROLLER_JS},
};
webui::AddResourcePathsBulk(source, kStrings);
source->AddResourcePaths(kStrings);
```
The second version instead accepts a span of <code>GritResourceMap</code> so
that it can directly use constants defined by autogenerated grit resources map
header files. For example, the autogenerated print\_preview\_resources\_map.h
header defines a <code>GritResourceMap</code> named
<code>kPrintPreviewResources</code> and a
<code>size\_t kPrintPreviewResourcesSize</code>. All the resources in this
The same method can be leveraged for cases that directly use constants defined
by autogenerated grit resources map header files. For example, the autogenerated
print\_preview\_resources\_map.h header defines a
<code>webui::ResourcePath</code> array named <code>kPrintPreviewResources</code>
and a <code>size\_t kPrintPreviewResourcesSize</code>. All the resources in this
resource map can be added as follows:
```c++
webui::AddResourcePathsBulk(
source,
source->AddResourcePaths(
base::make_span(kPrintPreviewResources, kPrintPreviewResourcesSize));
```