Revert "WIP: Serve untrusted resource to NTP"

This reverts commit 4246e2a0e60e0b79a35ff790b1a41c8bbb8e31bd.
This commit is contained in:
Simon Hong
2022-02-18 15:00:47 +09:00
parent c278fde04c
commit 16dedda7fe
8 changed files with 2 additions and 181 deletions
-2
View File
@@ -117,8 +117,6 @@ source_set("ui") {
"webui/new_tab_page/brave_new_tab_ui.h",
"webui/new_tab_page/brave_new_tab_ui_utils.cc",
"webui/new_tab_page/brave_new_tab_ui_utils.h",
"webui/new_tab_page/brave_untrusted_source.cc",
"webui/new_tab_page/brave_untrusted_source.h",
"webui/new_tab_page/top_sites_message_handler.cc",
"webui/new_tab_page/top_sites_message_handler.h",
"webui/settings/brave_appearance_handler.cc",
@@ -8,14 +8,12 @@
#include <utility>
#include "base/feature_list.h"
#include "base/strings/stringprintf.h"
#include "brave/browser/brave_news/brave_news_controller_factory.h"
#include "brave/browser/new_tab/new_tab_shows_options.h"
#include "brave/browser/ntp_background_images/ntp_custom_background_images_service_factory.h"
#include "brave/browser/ui/webui/brave_webui_source.h"
#include "brave/browser/ui/webui/new_tab_page/brave_new_tab_message_handler.h"
#include "brave/browser/ui/webui/new_tab_page/brave_new_tab_page_handler.h"
#include "brave/browser/ui/webui/new_tab_page/brave_untrusted_source.h"
#include "brave/browser/ui/webui/new_tab_page/top_sites_message_handler.h"
#include "brave/components/brave_new_tab/resources/grit/brave_new_tab_generated_map.h"
#include "brave/components/brave_today/browser/brave_news_controller.h"
@@ -23,12 +21,11 @@
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search/background/ntp_custom_background_service.h"
#include "chrome/browser/search/background/ntp_custom_background_service_factory.h"
#include "chrome/common/url_constants.h"
#include "chrome/browser/ui/webui/new_tab_page/untrusted_source.h"
#include "components/grit/brave_components_resources.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/browser/url_data_source.h"
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/common/url_constants.h"
#include "ui/base/l10n/l10n_util.h"
BraveNewTabUI::BraveNewTabUI(content::WebUI* web_ui, const std::string& name)
@@ -67,15 +64,8 @@ BraveNewTabUI::BraveNewTabUI(content::WebUI* web_ui, const std::string& name)
base::WrapUnique(new TopSitesMessageHandler(profile)));
// For custom background images.
// Allow embedding of iframes for chrome-untrusted://new-tab-page
// for user uploaded image resources.
web_ui->AddRequestableScheme(content::kChromeUIUntrustedScheme);
source->OverrideContentSecurityPolicy(
network::mojom::CSPDirectiveName::FrameSrc,
base::StringPrintf("frame-src %s;",
chrome::kChromeUIUntrustedNewTabPageUrl));
content::URLDataSource::Add(profile,
std::make_unique<BraveUntrustedSource>(profile));
std::make_unique<UntrustedSource>(profile));
}
BraveNewTabUI::~BraveNewTabUI() = default;
@@ -1,107 +0,0 @@
// Copyright (c) 2022 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.
#include "brave/browser/ui/webui/new_tab_page/brave_untrusted_source.h"
#include <unordered_map>
#include <utility>
#include "base/memory/ref_counted_memory.h"
#include "base/strings/string_piece.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/common/url_constants.h"
#include "components/grit/brave_components_resources.h"
#include "content/public/common/url_constants.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/template_expressions.h"
#include "url/gurl.h"
#include "url/url_util.h"
namespace {
constexpr int kMaxUriDecodeLen = 2048;
std::string FormatTemplate(int resource_id,
const ui::TemplateReplacements& replacements) {
ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
scoped_refptr<base::RefCountedMemory> bytes =
bundle.LoadDataResourceBytes(resource_id);
base::StringPiece string_piece(reinterpret_cast<const char*>(bytes->front()),
bytes->size());
return ui::ReplaceTemplateExpressions(
string_piece, replacements,
/* skip_unexpected_placeholder_check= */ true);
}
} // namespace
BraveUntrustedSource::~BraveUntrustedSource() = default;
std::string BraveUntrustedSource::GetContentSecurityPolicy(
network::mojom::CSPDirectiveName directive) {
if (directive == network::mojom::CSPDirectiveName::FrameAncestors) {
return base::StringPrintf("frame-ancestors %s %s",
chrome::kChromeUINewTabPageURL,
chrome::kChromeUINewTabURL);
}
return UntrustedSource::GetContentSecurityPolicy(directive);
}
void BraveUntrustedSource::StartDataRequest(
const GURL& url,
const content::WebContents::Getter& wc_getter,
content::URLDataSource::GotDataCallback callback) {
const std::string path = url.has_path() ? url.path().substr(1) : "";
if (path == "brave_custom_background_image") {
// Parse all query parameters to hash map and decode values.
std::unordered_map<std::string, std::string> params;
url::Component query(0, url.query().length());
url::Component key, value;
while (
url::ExtractQueryKeyValue(url.query().c_str(), &query, &key, &value)) {
url::RawCanonOutputW<kMaxUriDecodeLen> output;
url::DecodeURLEscapeSequences(
url.query().c_str() + value.begin, value.len,
url::DecodeURLMode::kUTF8OrIsomorphic, &output);
params.insert(
{url.query().substr(key.begin, key.len),
base::UTF16ToUTF8(std::u16string(output.data(), output.length()))});
}
ui::TemplateReplacements replacements;
replacements["url"] = params["url"];
std::string html = FormatTemplate(
IDR_BRAVE_NEW_TAB_CUSTOM_BACKGROUND_IMAGE_HTML, replacements);
std::move(callback).Run(base::RefCountedString::TakeString(&html));
return;
}
if (path == "custom_background_image.js") {
ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
std::move(callback).Run(bundle.LoadDataResourceBytes(
IDR_BRAVE_NEW_TAB_CUSTOM_BACKGROUND_IMAGE_JS));
return;
}
UntrustedSource::StartDataRequest(url, wc_getter, std::move(callback));
}
bool BraveUntrustedSource::ShouldServiceRequest(
const GURL& url,
content::BrowserContext* browser_context,
int render_process_id) {
if (!url.SchemeIs(content::kChromeUIUntrustedScheme) || !url.has_path()) {
return false;
}
const std::string path = url.path().substr(1);
if (path == "brave_custom_background_image" ||
path == "custom_background_image.js") {
return true;
}
return UntrustedSource::ShouldServiceRequest(url, browser_context,
render_process_id);
}
@@ -1,33 +0,0 @@
// Copyright (c) 2022 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.
#ifndef BRAVE_BROWSER_UI_WEBUI_NEW_TAB_PAGE_BRAVE_UNTRUSTED_SOURCE_H_
#define BRAVE_BROWSER_UI_WEBUI_NEW_TAB_PAGE_BRAVE_UNTRUSTED_SOURCE_H_
#include <string>
#include "chrome/browser/ui/webui/new_tab_page/untrusted_source.h"
// For special handling from brave://newtab.
class BraveUntrustedSource : public UntrustedSource {
public:
using UntrustedSource::UntrustedSource;
~BraveUntrustedSource() override;
BraveUntrustedSource(const BraveUntrustedSource&) = delete;
BraveUntrustedSource& operator=(const BraveUntrustedSource&) = delete;
// UntrustedSource overrides:
std::string GetContentSecurityPolicy(
network::mojom::CSPDirectiveName directive) override;
void StartDataRequest(
const GURL& url,
const content::WebContents::Getter& wc_getter,
content::URLDataSource::GotDataCallback callback) override;
bool ShouldServiceRequest(const GURL& url,
content::BrowserContext* browser_context,
int render_process_id) override;
};
#endif // BRAVE_BROWSER_UI_WEBUI_NEW_TAB_PAGE_BRAVE_UNTRUSTED_SOURCE_H_
@@ -1,10 +0,0 @@
// Copyright (c) 2022 The Brave Authors
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.
#include "brave/browser/ui/webui/new_tab_page/brave_untrusted_source.h"
#define UntrustedSource BraveUntrustedSource
#include "src/chrome/browser/ui/webui/new_tab_page/new_tab_page_ui.cc"
#undef UntrustedSource
@@ -1,7 +0,0 @@
<!doctype html>
<html>
<body>
<script src="custom_background_image.js"></script>
<img src="$i18n{url}" onload="onImageLoad()" hidden></img>
</body>
</html>
@@ -1,8 +0,0 @@
// Copyright (c) 2021 The Brave Authors. All rights reserved.
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.
function onImageLoad() {
window.parent.postMessage('imageLoaded', 'chrome://newtab');
}
@@ -33,8 +33,6 @@
<include name="IDR_BRAVE_NEW_TAB_IMG1" file="../img/newtab/bookmarks_btn.svg" type="BINDATA" />
<include name="IDR_BRAVE_NEW_TAB_IMG4" file="../img/newtab/history_btn.svg" type="BINDATA" />
<include name="IDR_BRAVE_NEW_TAB_IMG2" file="../img/newtab/settings_prefs_btn.svg" type="BINDATA" />
<include name="IDR_BRAVE_NEW_TAB_CUSTOM_BACKGROUND_IMAGE_HTML" file="../brave_new_tab_ui/untrusted/custom_background_image.html" type="BINDATA" />
<include name="IDR_BRAVE_NEW_TAB_CUSTOM_BACKGROUND_IMAGE_JS" file="../brave_new_tab_ui/untrusted/custom_background_image.js" type="BINDATA" />
<!-- WebUI wallet resources -->
<if expr="not is_android">