1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
|
// Copyright 2021 The Chromium Authors
// 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/whats_new/whats_new_ui.h"
#include "base/feature_list.h"
#include "base/version.h"
#include "chrome/browser/browser_features.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/global_features.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/webui/browser_command/browser_command_handler.h"
#include "chrome/browser/ui/webui/whats_new/whats_new_handler.h"
#include "chrome/browser/ui/webui/whats_new/whats_new_util.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/grit/theme_resources.h"
#include "chrome/grit/whats_new_resources.h"
#include "chrome/grit/whats_new_resources_map.h"
#include "components/prefs/pref_service.h"
#include "components/strings/grit/components_strings.h"
#include "components/user_education/webui/whats_new_registry.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/common/url_constants.h"
#include "services/network/public/mojom/content_security_policy.mojom.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/webui/web_ui_util.h"
#include "ui/webui/webui_util.h"
namespace {
void CreateAndAddWhatsNewUIHtmlSource(Profile* profile, bool enable_staging) {
content::WebUIDataSource* source = content::WebUIDataSource::CreateAndAdd(
profile, chrome::kChromeUIWhatsNewHost);
webui::SetupWebUIDataSource(
source, base::span<const webui::ResourcePath>(kWhatsNewResources),
IDR_WHATS_NEW_WHATS_NEW_HTML);
static constexpr webui::LocalizedString kStrings[] = {
{"title", IDS_WHATS_NEW_TITLE},
};
source->AddLocalizedStrings(kStrings);
source->AddBoolean("isStaging", enable_staging);
// Allow embedding of iframe from chrome.com
source->OverrideContentSecurityPolicy(
network::mojom::CSPDirectiveName::ChildSrc,
enable_staging
? "child-src chrome://webui-test https://www.google.com/ "
"https://chrome-staging.corp.google.com/;"
: "child-src chrome://webui-test https://www.google.com/;");
}
} // namespace
WhatsNewUIConfig::WhatsNewUIConfig()
: DefaultWebUIConfig(content::kChromeUIScheme,
chrome::kChromeUIWhatsNewHost) {}
bool WhatsNewUIConfig::IsWebUIEnabled(
content::BrowserContext* browser_context) {
return whats_new::IsEnabled();
}
// static
void WhatsNewUI::RegisterLocalStatePrefs(PrefRegistrySimple* registry) {
registry->RegisterDictionaryPref(prefs::kWhatsNewEditionUsed);
registry->RegisterListPref(prefs::kWhatsNewFirstEnabledOrder);
registry->RegisterIntegerPref(prefs::kWhatsNewVersionUsed, 0);
}
WhatsNewUI::WhatsNewUI(content::WebUI* web_ui)
: ui::MojoWebUIController(web_ui, /*enable_chrome_send=*/true),
content::WebContentsObserver(web_ui->GetWebContents()),
page_factory_receiver_(this),
browser_command_factory_receiver_(this),
profile_(Profile::FromWebUI(web_ui)) {
GURL url = web_ui->GetWebContents()->GetVisibleURL();
bool enable_staging = url.query_piece().compare("staging=true") == 0;
CreateAndAddWhatsNewUIHtmlSource(profile_, enable_staging);
}
// static
base::RefCountedMemory* WhatsNewUI::GetFaviconResourceBytes(
ui::ResourceScaleFactor scale_factor) {
return static_cast<base::RefCountedMemory*>(
ui::ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale(
IDR_NTP_FAVICON, scale_factor));
}
WEB_UI_CONTROLLER_TYPE_IMPL(WhatsNewUI)
void WhatsNewUI::BindInterface(
mojo::PendingReceiver<whats_new::mojom::PageHandlerFactory> receiver) {
page_factory_receiver_.reset();
page_factory_receiver_.Bind(std::move(receiver));
}
void WhatsNewUI::CreatePageHandler(
mojo::PendingRemote<whats_new::mojom::Page> page,
mojo::PendingReceiver<whats_new::mojom::PageHandler> receiver) {
DCHECK(page);
page_handler_ = std::make_unique<WhatsNewHandler>(
std::move(receiver), std::move(page), profile_,
web_ui()->GetWebContents(), navigation_start_time_,
g_browser_process->GetFeatures()->whats_new_registry());
}
void WhatsNewUI::DidStartNavigation(
content::NavigationHandle* navigation_handle) {
if (navigation_handle->GetURL() == GURL(chrome::kChromeUIWhatsNewURL)) {
navigation_start_time_ = base::Time::Now();
}
}
void WhatsNewUI::BindInterface(
mojo::PendingReceiver<browser_command::mojom::CommandHandlerFactory>
pending_receiver) {
if (browser_command_factory_receiver_.is_bound()) {
browser_command_factory_receiver_.reset();
}
browser_command_factory_receiver_.Bind(std::move(pending_receiver));
}
void WhatsNewUI::CreateBrowserCommandHandler(
mojo::PendingReceiver<browser_command::mojom::CommandHandler>
pending_handler) {
std::vector<browser_command::mojom::Command> supported_commands = {};
auto* registry = g_browser_process->GetFeatures()->whats_new_registry();
CHECK(registry);
supported_commands = registry->GetActiveCommands();
// Legacy list. Do not add browser commands here. Browser commands
// should instead be added through the WhatsNewRegistry.
supported_commands.insert(
supported_commands.end(),
{
browser_command::mojom::Command::kStartSavedTabGroupTutorial,
browser_command::mojom::Command::kOpenAISettings,
browser_command::mojom::Command::kOpenSafetyCheckFromWhatsNew,
});
command_handler_ = std::make_unique<BrowserCommandHandler>(
std::move(pending_handler), profile_, supported_commands,
web_ui()->GetWebContents());
}
WhatsNewUI::~WhatsNewUI() = default;
|