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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
// Copyright 2016 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/ntp_tiles_internals_ui.h"
#include <memory>
#include <string>
#include <string_view>
#include <vector>
#include "base/functional/bind.h"
#include "build/build_config.h"
#include "chrome/browser/favicon/favicon_service_factory.h"
#include "chrome/browser/history/top_sites_factory.h"
#include "chrome/browser/ntp_tiles/chrome_most_visited_sites_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/common/url_constants.h"
#include "components/grit/ntp_tiles_internals_resources.h"
#include "components/grit/ntp_tiles_internals_resources_map.h"
#include "components/history/core/browser/top_sites.h"
#include "components/image_fetcher/core/image_fetcher_impl.h"
#include "components/ntp_tiles/features.h"
#include "components/ntp_tiles/icon_cacher.h"
#include "components/ntp_tiles/most_visited_sites.h"
#include "components/ntp_tiles/webui/ntp_tiles_internals_message_handler.h"
#include "components/ntp_tiles/webui/ntp_tiles_internals_message_handler_client.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_data_source.h"
#include "content/public/browser/web_ui_message_handler.h"
#include "services/network/public/mojom/content_security_policy.mojom.h"
#include "ui/webui/webui_util.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/feature_list.h"
#include "chrome/browser/flags/android/chrome_feature_list.h"
#endif
#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/ui/webui/new_tab_page/ntp_pref_names.h"
#include "components/prefs/pref_service.h"
#endif
namespace {
// The implementation for the chrome://ntp-tiles-internals page.
class ChromeNTPTilesInternalsMessageHandlerClient
: public content::WebUIMessageHandler,
public ntp_tiles::NTPTilesInternalsMessageHandlerClient {
public:
// |favicon_service| must not be null and must outlive this object.
explicit ChromeNTPTilesInternalsMessageHandlerClient(
favicon::FaviconService* favicon_service)
: handler_(favicon_service) {}
ChromeNTPTilesInternalsMessageHandlerClient(
const ChromeNTPTilesInternalsMessageHandlerClient&) = delete;
ChromeNTPTilesInternalsMessageHandlerClient& operator=(
const ChromeNTPTilesInternalsMessageHandlerClient&) = delete;
private:
// content::WebUIMessageHandler:
void RegisterMessages() override;
// ntp_tiles::NTPTilesInternalsMessageHandlerClient
bool SupportsNTPTiles() override;
std::unique_ptr<ntp_tiles::MostVisitedSites> MakeMostVisitedSites() override;
PrefService* GetPrefs() override;
void RegisterMessageCallback(
std::string_view message,
base::RepeatingCallback<void(const base::Value::List&)> callback)
override;
void CallJavascriptFunctionSpan(
std::string_view name,
base::span<const base::ValueView> values) override;
ntp_tiles::NTPTilesInternalsMessageHandler handler_;
};
void ChromeNTPTilesInternalsMessageHandlerClient::RegisterMessages() {
handler_.RegisterMessages(this);
}
bool ChromeNTPTilesInternalsMessageHandlerClient::SupportsNTPTiles() {
Profile* profile = Profile::FromWebUI(web_ui());
return !(profile->IsGuestSession() || profile->IsOffTheRecord());
}
std::unique_ptr<ntp_tiles::MostVisitedSites>
ChromeNTPTilesInternalsMessageHandlerClient::MakeMostVisitedSites() {
auto most_visited_sites = ChromeMostVisitedSitesFactory::NewForProfile(
Profile::FromWebUI(web_ui()));
#if BUILDFLAG(IS_ANDROID)
// Custom links on Android: ntp_prefs::kNtpShortcutsType is
// unavailable. Use feature list instead.
most_visited_sites->EnableTileTypes(
ntp_tiles::MostVisitedSites::EnableTileTypesOptions().with_custom_links(
base::FeatureList::IsEnabled(
chrome::android::kMostVisitedTilesCustomization)));
#else
// Custom links on Desktop.
// TODO(crbug.com/438304256): Add `enable_enterprise_shortcuts` as a parameter
// to support enterprise shortcuts.
const ntp_tiles::TileType type = static_cast<ntp_tiles::TileType>(
GetPrefs()->GetInteger(ntp_prefs::kNtpShortcutsType));
const bool enterprise_shortcuts_feature_enabled =
base::FeatureList::IsEnabled(ntp_tiles::kNtpEnterpriseShortcuts);
// If the enterprise shortcuts feature is disabled, but the preference is set
// to enterprise shortcuts, treat MostVisitedSites as if enterpise shortcuts
// is disabled and custom links is enabled. This may occur if the user is
// moved in and out of the experiment.
const bool custom_links_enabled =
type == ntp_tiles::TileType::kCustomLinks ||
(type == ntp_tiles::TileType::kEnterpriseShortcuts &&
!enterprise_shortcuts_feature_enabled);
const bool enterprise_shortcuts_enabled =
type == ntp_tiles::TileType::kEnterpriseShortcuts &&
enterprise_shortcuts_feature_enabled;
most_visited_sites->EnableTileTypes(
ntp_tiles::MostVisitedSites::EnableTileTypesOptions()
.with_custom_links(custom_links_enabled)
.with_enterprise_shortcuts(enterprise_shortcuts_enabled));
#endif
return most_visited_sites;
}
PrefService* ChromeNTPTilesInternalsMessageHandlerClient::GetPrefs() {
return Profile::FromWebUI(web_ui())->GetPrefs();
}
void ChromeNTPTilesInternalsMessageHandlerClient::RegisterMessageCallback(
std::string_view message,
base::RepeatingCallback<void(const base::Value::List&)> callback) {
web_ui()->RegisterMessageCallback(message, std::move(callback));
}
void ChromeNTPTilesInternalsMessageHandlerClient::CallJavascriptFunctionSpan(
std::string_view name,
base::span<const base::ValueView> values) {
web_ui()->CallJavascriptFunctionUnsafe(name, values);
}
void CreateAndAddNTPTilesInternalsHTMLSource(Profile* profile) {
content::WebUIDataSource* source = content::WebUIDataSource::CreateAndAdd(
profile, chrome::kChromeUINTPTilesInternalsHost);
webui::SetupWebUIDataSource(
source,
base::span<const webui::ResourcePath>(kNtpTilesInternalsResources),
IDR_NTP_TILES_INTERNALS_NTP_TILES_INTERNALS_HTML);
source->OverrideContentSecurityPolicy(
network::mojom::CSPDirectiveName::ScriptSrc,
"script-src chrome://resources 'self';");
source->OverrideContentSecurityPolicy(
network::mojom::CSPDirectiveName::TrustedTypes,
"trusted-types lit-html-desktop;");
}
} // namespace
NTPTilesInternalsUI::NTPTilesInternalsUI(content::WebUI* web_ui)
: WebUIController(web_ui) {
Profile* profile = Profile::FromWebUI(web_ui);
CreateAndAddNTPTilesInternalsHTMLSource(profile);
web_ui->AddMessageHandler(
std::make_unique<ChromeNTPTilesInternalsMessageHandlerClient>(
FaviconServiceFactory::GetForProfile(
profile, ServiceAccessType::EXPLICIT_ACCESS)));
}
NTPTilesInternalsUI::~NTPTilesInternalsUI() = default;
|