blob: d110a3adc064a9e6c3ecc14d53fbaba1eb20c854 [file] [log] [blame]
// Copyright 2018 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 "chrome/browser/ui/webui/webui_util.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "content/public/browser/web_ui_data_source.h"
#include "services/network/public/mojom/content_security_policy.mojom.h"
#include "ui/base/webui/web_ui_util.h"
#include "ui/resources/grit/webui_generated_resources.h"
#include "ui/resources/grit/webui_resources.h"
#include "ui/resources/grit/webui_resources_map.h"
#if BUILDFLAG(IS_CHROMEOS_ASH)
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part.h"
#include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
#elif defined(OS_WIN) || defined(OS_MAC)
#include "base/enterprise_util.h"
#endif
#if defined(TOOLKIT_VIEWS)
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_window.h"
#include "ui/native_theme/native_theme.h"
#include "ui/views/widget/widget.h"
#endif // defined(TOOLKIT_VIEWS)
namespace webui {
void SetJSModuleDefaults(content::WebUIDataSource* source) {
source->OverrideContentSecurityPolicy(
network::mojom::CSPDirectiveName::ScriptSrc,
"script-src chrome://resources chrome://test 'self';");
// TODO(crbug.com/1098690): Trusted Type Polymer
source->DisableTrustedTypesCSP();
source->UseStringsJs();
source->EnableReplaceI18nInJS();
source->AddResourcePath("test_loader.js", IDR_WEBUI_JS_TEST_LOADER_JS);
source->AddResourcePath("test_loader_util.js",
IDR_WEBUI_JS_TEST_LOADER_UTIL_JS);
source->AddResourcePath("test_loader.html", IDR_WEBUI_HTML_TEST_LOADER_HTML);
}
void SetupWebUIDataSource(content::WebUIDataSource* source,
base::span<const ResourcePath> resources,
int default_resource) {
SetJSModuleDefaults(source);
source->AddResourcePaths(resources);
source->AddResourcePath("", default_resource);
}
bool IsEnterpriseManaged() {
#if BUILDFLAG(IS_CHROMEOS_ASH)
policy::BrowserPolicyConnectorChromeOS* connector =
g_browser_process->platform_part()->browser_policy_connector_chromeos();
return connector->IsEnterpriseManaged();
#elif defined(OS_WIN) || defined(OS_MAC)
return base::IsMachineExternallyManaged();
#else
return false;
#endif
}
#if defined(TOOLKIT_VIEWS)
ui::NativeTheme* GetNativeTheme(content::WebContents* web_contents) {
ui::NativeTheme* native_theme = nullptr;
if (web_contents) {
Browser* browser = chrome::FindBrowserWithWebContents(web_contents);
if (browser) {
// Find for WebContents hosted in a tab.
native_theme = browser->window()->GetNativeTheme();
}
if (!native_theme) {
// Find for WebContents hosted in a widget, but not directly in a
// Browser. e.g. Tab Search, Read Later.
views::Widget* widget = views::Widget::GetTopLevelWidgetForNativeView(
web_contents->GetContentNativeView());
if (widget)
native_theme = widget->GetNativeTheme();
}
}
if (!native_theme) {
// Find for isolated WebContents, e.g. in tests.
// Or when |web_contents| is nullptr, because the renderer is not ready.
// TODO(crbug/1056916): Remove global accessor to NativeTheme.
native_theme = ui::NativeTheme::GetInstanceForNativeUi();
}
return native_theme;
}
#endif // !defined(TOOLKIT_VIEWS)
} // namespace webui