Commit 50a9fe1d authored by Giovanni Ortuño Urquidi's avatar Giovanni Ortuño Urquidi Committed by Chromium LUCI CQ

sample-swa: Use a WebUIConfig for the Sample System Web App untrusted context

Creates a WebUIConfig and WebUIController for the Sample System Web App's
untrusted context.

Bug: 1080384
Change-Id: I516339dd8b2412f377b460817d16fdb51146aefa
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2592075
Commit-Queue: Giovanni Ortuño Urquidi <ortuno@chromium.org>
Reviewed-by: default avatarcalamity <calamity@chromium.org>
Cr-Commit-Position: refs/heads/master@{#837948}
parent 14adce60
...@@ -13,6 +13,10 @@ ...@@ -13,6 +13,10 @@
#include "ui/webui/webui_config.h" #include "ui/webui/webui_config.h"
#include "url/gurl.h" #include "url/gurl.h"
#if BUILDFLAG(IS_CHROMEOS_ASH) && !defined(OFFICIAL_BUILD)
#include "chromeos/components/sample_system_web_app_ui/untrusted_sample_system_web_app_ui.h"
#endif
using WebUIConfigList = using WebUIConfigList =
std::vector<std::pair<std::string, std::unique_ptr<ui::WebUIConfig>>>; std::vector<std::pair<std::string, std::unique_ptr<ui::WebUIConfig>>>;
...@@ -24,18 +28,19 @@ namespace { ...@@ -24,18 +28,19 @@ namespace {
// constructing from a vector is O(n log n). // constructing from a vector is O(n log n).
WebUIConfigList CreateConfigs() { WebUIConfigList CreateConfigs() {
WebUIConfigList config_list; WebUIConfigList config_list;
#if BUILDFLAG(IS_CHROMEOS_ASH) && !defined(OFFICIAL_BUILD)
auto register_config = auto register_config =
[&config_list](std::unique_ptr<ui::WebUIConfig> config) { [&config_list](std::unique_ptr<ui::WebUIConfig> config) {
DCHECK_EQ(config->scheme(), content::kChromeUIUntrustedScheme); DCHECK_EQ(config->scheme(), content::kChromeUIUntrustedScheme);
const std::string& host = config->host(); const std::string& host = config->host();
config_list.emplace_back(host, std::move(config)); config_list.emplace_back(host, std::move(config));
}; };
// Delete once register_config is used outside of Chrome OS.
ALLOW_UNUSED_LOCAL(register_config);
// Register WebUIConfigs below. // Register WebUIConfigs below.
#if BUILDFLAG(IS_CHROMEOS_ASH) && !defined(OFFICIAL_BUILD)
// TODO(ortuno): Remove when `register_config` is used to register configs. register_config(
DCHECK(&register_config); std::make_unique<chromeos::UntrustedSampleSystemWebAppUIConfig>());
#endif #endif
return config_list; return config_list;
......
...@@ -14,6 +14,8 @@ static_library("sample_system_web_app_ui") { ...@@ -14,6 +14,8 @@ static_library("sample_system_web_app_ui") {
sources = [ sources = [
"sample_system_web_app_ui.cc", "sample_system_web_app_ui.cc",
"sample_system_web_app_ui.h", "sample_system_web_app_ui.h",
"untrusted_sample_system_web_app_ui.cc",
"untrusted_sample_system_web_app_ui.h",
"url_constants.cc", "url_constants.cc",
"url_constants.h", "url_constants.h",
] ]
......
...@@ -17,18 +17,6 @@ ...@@ -17,18 +17,6 @@
#include "ui/webui/webui_allowlist.h" #include "ui/webui/webui_allowlist.h"
namespace chromeos { namespace chromeos {
namespace {
content::WebUIDataSource* CreateUntrustedSampleSystemWebAppDataSource() {
content::WebUIDataSource* untrusted_source =
content::WebUIDataSource::Create(kChromeUIUntrustedSampleSystemWebAppURL);
untrusted_source->AddResourcePath(
"untrusted.html", IDR_CHROMEOS_SAMPLE_SYSTEM_WEB_APP_UNTRUSTED_HTML);
untrusted_source->AddResourcePath(
"untrusted.js", IDR_CHROMEOS_SAMPLE_SYSTEM_WEB_APP_UNTRUSTED_JS);
untrusted_source->AddFrameAncestor(GURL(kChromeUISampleSystemWebAppURL));
return untrusted_source;
}
} // namespace
SampleSystemWebAppUI::SampleSystemWebAppUI(content::WebUI* web_ui) SampleSystemWebAppUI::SampleSystemWebAppUI(content::WebUI* web_ui)
: ui::MojoWebUIController(web_ui) { : ui::MojoWebUIController(web_ui) {
...@@ -58,8 +46,6 @@ SampleSystemWebAppUI::SampleSystemWebAppUI(content::WebUI* web_ui) ...@@ -58,8 +46,6 @@ SampleSystemWebAppUI::SampleSystemWebAppUI(content::WebUI* web_ui)
network::mojom::CSPDirectiveName::FrameSrc, csp); network::mojom::CSPDirectiveName::FrameSrc, csp);
auto* browser_context = web_ui->GetWebContents()->GetBrowserContext(); auto* browser_context = web_ui->GetWebContents()->GetBrowserContext();
content::WebUIDataSource::Add(browser_context, trusted_source.release()); content::WebUIDataSource::Add(browser_context, trusted_source.release());
content::WebUIDataSource::Add(browser_context,
CreateUntrustedSampleSystemWebAppDataSource());
// Add ability to request chrome-untrusted: URLs // Add ability to request chrome-untrusted: URLs
web_ui->AddRequestableScheme(content::kChromeUIUntrustedScheme); web_ui->AddRequestableScheme(content::kChromeUIUntrustedScheme);
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/components/sample_system_web_app_ui/untrusted_sample_system_web_app_ui.h"
#include "chromeos/components/sample_system_web_app_ui/url_constants.h"
#include "chromeos/grit/chromeos_sample_system_web_app_resources.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/common/url_constants.h"
#include "url/gurl.h"
namespace chromeos {
UntrustedSampleSystemWebAppUIConfig::UntrustedSampleSystemWebAppUIConfig()
: WebUIConfig(content::kChromeUIUntrustedScheme,
kChromeUIUntrustedSampleSystemWebAppHost) {}
UntrustedSampleSystemWebAppUIConfig::~UntrustedSampleSystemWebAppUIConfig() =
default;
std::unique_ptr<content::WebUIController>
UntrustedSampleSystemWebAppUIConfig::CreateWebUIController(
content::WebUI* web_ui) {
return std::make_unique<UntrustedSampleSystemWebAppUI>(web_ui);
}
UntrustedSampleSystemWebAppUI::UntrustedSampleSystemWebAppUI(
content::WebUI* web_ui)
: ui::UntrustedWebUIController(web_ui) {
content::WebUIDataSource* untrusted_source =
content::WebUIDataSource::Create(kChromeUIUntrustedSampleSystemWebAppURL);
untrusted_source->AddResourcePath(
"untrusted.html", IDR_CHROMEOS_SAMPLE_SYSTEM_WEB_APP_UNTRUSTED_HTML);
untrusted_source->AddResourcePath(
"untrusted.js", IDR_CHROMEOS_SAMPLE_SYSTEM_WEB_APP_UNTRUSTED_JS);
untrusted_source->AddFrameAncestor(GURL(kChromeUISampleSystemWebAppURL));
#if !DCHECK_IS_ON()
// When DCHECKs are off and a user goes to an invalid url serve a default page
// to avoid crashing. We crash when DCHECKs are on to make it clearer that
// a resource path was not property specified.
untrusted_source->SetDefaultResource(
IDR_CHROMEOS_SAMPLE_SYSTEM_WEB_APP_UNTRUSTED_HTML);
#endif // !DCHECK_IS_ON()
auto* browser_context = web_ui->GetWebContents()->GetBrowserContext();
content::WebUIDataSource::Add(browser_context, untrusted_source);
}
UntrustedSampleSystemWebAppUI::~UntrustedSampleSystemWebAppUI() = default;
} // namespace chromeos
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROMEOS_COMPONENTS_SAMPLE_SYSTEM_WEB_APP_UI_UNTRUSTED_SAMPLE_SYSTEM_WEB_APP_UI_H_
#define CHROMEOS_COMPONENTS_SAMPLE_SYSTEM_WEB_APP_UI_UNTRUSTED_SAMPLE_SYSTEM_WEB_APP_UI_H_
#include "ui/webui/untrusted_web_ui_controller.h"
#include "ui/webui/webui_config.h"
#if defined(OFFICIAL_BUILD)
#error Sample System Web App should only be included in unofficial builds.
#endif
namespace content {
class WebUI;
} // namespace content
namespace chromeos {
class UntrustedSampleSystemWebAppUIConfig : public ui::WebUIConfig {
public:
UntrustedSampleSystemWebAppUIConfig();
~UntrustedSampleSystemWebAppUIConfig() override;
std::unique_ptr<content::WebUIController> CreateWebUIController(
content::WebUI* web_ui) override;
};
class UntrustedSampleSystemWebAppUI : public ui::UntrustedWebUIController {
public:
explicit UntrustedSampleSystemWebAppUI(content::WebUI* web_ui);
UntrustedSampleSystemWebAppUI(const UntrustedSampleSystemWebAppUI&) = delete;
UntrustedSampleSystemWebAppUI& operator=(
const UntrustedSampleSystemWebAppUI&) = delete;
~UntrustedSampleSystemWebAppUI() override;
};
} // namespace chromeos
#endif // CHROMEOS_COMPONENTS_SAMPLE_SYSTEM_WEB_APP_UI_UNTRUSTED_SAMPLE_SYSTEM_WEB_APP_UI_H_
...@@ -8,6 +8,7 @@ namespace chromeos { ...@@ -8,6 +8,7 @@ namespace chromeos {
const char kChromeUISampleSystemWebAppHost[] = "sample-system-web-app"; const char kChromeUISampleSystemWebAppHost[] = "sample-system-web-app";
const char kChromeUISampleSystemWebAppURL[] = "chrome://sample-system-web-app"; const char kChromeUISampleSystemWebAppURL[] = "chrome://sample-system-web-app";
const char kChromeUIUntrustedSampleSystemWebAppHost[] = "sample-system-web-app";
const char kChromeUIUntrustedSampleSystemWebAppURL[] = const char kChromeUIUntrustedSampleSystemWebAppURL[] =
"chrome-untrusted://sample-system-web-app/"; "chrome-untrusted://sample-system-web-app/";
......
...@@ -9,6 +9,7 @@ namespace chromeos { ...@@ -9,6 +9,7 @@ namespace chromeos {
extern const char kChromeUISampleSystemWebAppHost[]; extern const char kChromeUISampleSystemWebAppHost[];
extern const char kChromeUISampleSystemWebAppURL[]; extern const char kChromeUISampleSystemWebAppURL[];
extern const char kChromeUIUntrustedSampleSystemWebAppHost[];
extern const char kChromeUIUntrustedSampleSystemWebAppURL[]; extern const char kChromeUIUntrustedSampleSystemWebAppURL[];
} // namespace chromeos } // namespace chromeos
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment