Skip to content

Commit

Permalink
[ntp] Populate several 1P theme properties in WebUI NTP
Browse files Browse the repository at this point in the history
The intend is to eventually sever the dependency of the WebUI NTP on the
InstantService for theming. This CL populates theme properties only
relevant for the 1P NTP (sans anything related to custom backgrounds) in
NewTabPageHandler; and removes plumbing to send those properties to
instant NTPs where they are no longer needed since the removal of the 1P
local NTP. Handling of custom backgrounds will be tackled in follow-up
CLs.

Change-Id: Ie32b42aca7d1a32e093f909dff83d6c13ec36874
Bug: 1084363, 1041125
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3057834
Reviewed-by: Alex Gough <ajgo@chromium.org>
Reviewed-by: Moe Ahmadi <mahmadi@chromium.org>
Commit-Queue: Tibor Goldschwendt <tiborg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#906946}
  • Loading branch information
Tibor Goldschwendt authored and Chromium LUCI CQ committed Jul 30, 2021
1 parent 8b2f9b8 commit ce22d56
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 308 deletions.
28 changes: 0 additions & 28 deletions chrome/browser/resources/new_tab_page_instant/externs.js
Expand Up @@ -38,41 +38,13 @@ window.chrome.embeddedSearch.newTabPage.getMostVisitedItemData;
* attributionUrl: (string|undefined),
* backgroundColorRgba: !Array<number>,
* collectionId: (string|undefined),
* colorDark: (!Array<number>|undefined),
* colorId: (number|undefined),
* colorLight: (!Array<number>|undefined),
* colorPicked: (!Array<number>|undefined),
* customBackgroundConfigured: boolean,
* customBackgroundDisabledByPolicy: boolean,
* iconBackgroundColor: !Array<number>,
* imageHorizontalAlignment: (string|undefined),
* imageTiling: (string|undefined),
* imageUrl: (string|undefined),
* imageVerticalAlignment: (string|undefined),
* isNtpBackgroundDark: boolean,
* logoColor: (!Array<number>|undefined),
* searchBox: (!{
* bg: !Array<number>,
* icon: !Array<number>,
* iconSelected: !Array<number>,
* placeholder: !Array<number>,
* resultsBg: !Array<number>,
* resultsBgHovered: !Array<number>,
* resultsBgSelected: !Array<number>,
* resultsDim: !Array<number>,
* resultsDimSelected: !Array<number>,
* resultsText: !Array<number>,
* resultsTextSelected: !Array<number>,
* resultsUrl: !Array<number>,
* resultsUrlSelected: !Array<number>,
* text: !Array<number>,
* }|undefined),
* textColorLightRgba: !Array<number>,
* textColorRgba: !Array<number>,
* themeId: (string|undefined),
* themeName: (string|undefined),
* useTitleContainer: boolean,
* useWhiteAddIcon: boolean,
* usingDefaultTheme: boolean,
* }}
*/
Expand Down
59 changes: 0 additions & 59 deletions chrome/browser/search/instant_service.cc
Expand Up @@ -32,7 +32,6 @@
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/omnibox/omnibox_theme.h"
#include "chrome/browser/ui/webui/favicon_source.h"
#include "chrome/browser/ui/webui/theme_source.h"
#include "chrome/common/chrome_paths.h"
Expand Down Expand Up @@ -292,9 +291,6 @@ void InstantService::UpdateNtpTheme() {
ApplyOrResetCustomBackgroundNtpTheme();
SetNtpElementsNtpTheme();

GetInitializedNtpTheme()->custom_background_disabled_by_policy =
IsCustomBackgroundDisabledByPolicy();

NotifyAboutNtpTheme();
}

Expand Down Expand Up @@ -507,8 +503,6 @@ void InstantService::BuildNtpTheme() {
ThemeService* theme_service = ThemeServiceFactory::GetForProfile(profile_);
theme_->using_default_theme = theme_service->UsingDefaultTheme();

theme_->using_dark_colors = native_theme_->ShouldUseDarkColors();

// Get theme colors.
const ui::ThemeProvider& theme_provider =
ThemeService::GetThemeProviderForProfile(profile_);
Expand All @@ -520,50 +514,13 @@ void InstantService::BuildNtpTheme() {
theme_provider.GetColor(ThemeProperties::COLOR_NTP_TEXT_LIGHT);
SetNtpElementsNtpTheme();

if (base::FeatureList::IsEnabled(ntp_features::kRealboxMatchOmniboxTheme)) {
theme_->search_box.bg =
GetOmniboxColor(&theme_provider, OmniboxPart::LOCATION_BAR_BACKGROUND);
theme_->search_box.icon =
GetOmniboxColor(&theme_provider, OmniboxPart::RESULTS_ICON);
theme_->search_box.icon_selected = GetOmniboxColor(
&theme_provider, OmniboxPart::RESULTS_ICON, OmniboxPartState::SELECTED);
theme_->search_box.placeholder =
GetOmniboxColor(&theme_provider, OmniboxPart::LOCATION_BAR_TEXT_DIMMED);
theme_->search_box.results_bg =
GetOmniboxColor(&theme_provider, OmniboxPart::RESULTS_BACKGROUND);
theme_->search_box.results_bg_hovered =
GetOmniboxColor(&theme_provider, OmniboxPart::RESULTS_BACKGROUND,
OmniboxPartState::HOVERED);
theme_->search_box.results_bg_selected =
GetOmniboxColor(&theme_provider, OmniboxPart::RESULTS_BACKGROUND,
OmniboxPartState::SELECTED);
theme_->search_box.results_dim =
GetOmniboxColor(&theme_provider, OmniboxPart::RESULTS_TEXT_DIMMED);
theme_->search_box.results_dim_selected =
GetOmniboxColor(&theme_provider, OmniboxPart::RESULTS_TEXT_DIMMED,
OmniboxPartState::SELECTED);
theme_->search_box.results_text =
GetOmniboxColor(&theme_provider, OmniboxPart::RESULTS_TEXT_DEFAULT);
theme_->search_box.results_text_selected =
GetOmniboxColor(&theme_provider, OmniboxPart::RESULTS_TEXT_DEFAULT,
OmniboxPartState::SELECTED);
theme_->search_box.results_url =
GetOmniboxColor(&theme_provider, OmniboxPart::RESULTS_TEXT_URL);
theme_->search_box.results_url_selected =
GetOmniboxColor(&theme_provider, OmniboxPart::RESULTS_TEXT_URL,
OmniboxPartState::SELECTED);
theme_->search_box.text = GetOmniboxColor(
&theme_provider, OmniboxPart::LOCATION_BAR_TEXT_DEFAULT);
}

if (theme_service->UsingExtensionTheme()) {
const extensions::Extension* extension =
extensions::ExtensionRegistry::Get(profile_)
->enabled_extensions()
.GetByID(theme_service->GetThemeID());
if (extension) {
theme_->theme_id = theme_service->GetThemeID();
theme_->theme_name = extension->name();

if (theme_provider.HasCustomImage(IDR_THEME_NTP_BACKGROUND)) {
theme_->has_theme_image = true;
Expand Down Expand Up @@ -608,14 +565,6 @@ void InstantService::BuildNtpTheme() {
theme_provider.HasCustomImage(IDR_THEME_NTP_ATTRIBUTION);
}
}
} else if (theme_service->UsingAutogeneratedTheme()) {
theme_->color_picked = theme_service->GetAutogeneratedThemeColor();
theme_->color_id =
chrome_colors::ChromeColorsService::GetColorId(theme_->color_picked);
theme_->color_dark =
theme_provider.GetColor(ThemeProperties::COLOR_FRAME_ACTIVE);
theme_->color_light =
theme_provider.GetColor(ThemeProperties::COLOR_NTP_BACKGROUND);
}
}

Expand Down Expand Up @@ -909,20 +858,12 @@ void InstantService::SetNtpElementsNtpTheme() {
if (IsCustomBackgroundSet()) {
theme->text_color = gfx::kGoogleGrey050;
theme->logo_alternate = true;
theme->logo_color = ThemeProperties::GetDefaultColor(
ThemeProperties::COLOR_NTP_LOGO, false);
theme->shortcut_color = ThemeProperties::GetDefaultColor(
ThemeProperties::COLOR_NTP_SHORTCUT, false);
} else {
const ui::ThemeProvider& theme_provider =
ThemeService::GetThemeProviderForProfile(profile_);
theme->text_color =
theme_provider.GetColor(ThemeProperties::COLOR_NTP_TEXT);
theme->logo_alternate = theme_provider.GetDisplayProperty(
ThemeProperties::NTP_LOGO_ALTERNATE) == 1;
theme->logo_color =
theme_provider.GetColor(ThemeProperties::COLOR_NTP_LOGO);
theme->shortcut_color =
theme_provider.GetColor(ThemeProperties::COLOR_NTP_SHORTCUT);
}
}
14 changes: 0 additions & 14 deletions chrome/browser/search/instant_service_unittest.cc
Expand Up @@ -690,19 +690,13 @@ TEST_F(InstantServiceTest, SetNTPElementsNtpTheme) {
ThemeService::GetThemeProviderForProfile(profile());
SkColor default_text_color =
theme_provider.GetColor(ThemeProperties::COLOR_NTP_TEXT);
SkColor default_logo_color =
theme_provider.GetColor(ThemeProperties::COLOR_NTP_LOGO);
SkColor default_shortcut_color =
theme_provider.GetColor(ThemeProperties::COLOR_NTP_SHORTCUT);

ASSERT_FALSE(instant_service_->IsCustomBackgroundSet());

// Check defaults when no theme and no custom backgrounds is set.
NtpTheme* theme = instant_service_->GetInitializedNtpTheme();
EXPECT_EQ(default_text_color, theme->text_color);
EXPECT_FALSE(theme->logo_alternate);
EXPECT_EQ(default_logo_color, theme->logo_color);
EXPECT_EQ(default_shortcut_color, theme->shortcut_color);

// Install colors, theme update should trigger SetNTPElementsNtpTheme() and
// update NTP themed elements info.
Expand All @@ -714,8 +708,6 @@ TEST_F(InstantServiceTest, SetNTPElementsNtpTheme) {
theme = instant_service_->GetInitializedNtpTheme();
EXPECT_NE(default_text_color, theme->text_color);
EXPECT_TRUE(theme->logo_alternate);
EXPECT_NE(default_logo_color, theme->logo_color);
EXPECT_NE(default_shortcut_color, theme->shortcut_color);

// Setting a custom background should call SetNTPElementsNtpTheme() and
// update NTP themed elements info.
Expand All @@ -727,10 +719,4 @@ TEST_F(InstantServiceTest, SetNTPElementsNtpTheme) {
theme = instant_service_->GetInitializedNtpTheme();
EXPECT_NE(default_text_color, theme->text_color);
EXPECT_TRUE(theme->logo_alternate);
EXPECT_EQ(default_logo_color, theme->logo_color);
// The shortcut color with a background set should always use the light mode
// default regardless of system setting.
EXPECT_EQ(ThemeProperties::GetDefaultColor(
ThemeProperties::COLOR_NTP_SHORTCUT, false),
theme->shortcut_color);
}
74 changes: 54 additions & 20 deletions chrome/browser/ui/webui/new_tab_page/new_tab_page_handler.cc
Expand Up @@ -30,9 +30,11 @@
#include "chrome/browser/search/background/ntp_background_service_factory.h"
#include "chrome/browser/search/instant_service.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/ui/chrome_select_file_policy.h"
#include "chrome/browser/ui/hats/hats_service.h"
#include "chrome/browser/ui/hats/hats_service_factory.h"
#include "chrome/browser/ui/omnibox/omnibox_theme.h"
#include "chrome/browser/ui/webui/new_tab_page/ntp_pref_names.h"
#include "chrome/browser/ui/webui/realbox/realbox.mojom.h"
#include "chrome/common/pref_names.h"
Expand All @@ -52,27 +54,39 @@
#include "services/network/public/cpp/simple_url_loader.h"
#include "services/network/public/mojom/url_response_head.mojom.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/theme_provider.h"
#include "ui/gfx/color_utils.h"

namespace {

const int64_t kMaxDownloadBytes = 1024 * 1024;

new_tab_page::mojom::ThemePtr MakeTheme(const NtpTheme& ntp_theme) {
new_tab_page::mojom::ThemePtr MakeTheme(
const NtpTheme& ntp_theme,
const ui::ThemeProvider* theme_provider) {
auto theme = new_tab_page::mojom::Theme::New();
auto most_visited = most_visited::mojom::MostVisitedTheme::New();
theme->is_default = ntp_theme.using_default_theme;
theme->background_color = ntp_theme.background_color;
most_visited->background_color = ntp_theme.shortcut_color;
if (!ntp_theme.custom_background_url.is_empty()) {
most_visited->background_color = ThemeProperties::GetDefaultColor(
ThemeProperties::COLOR_NTP_SHORTCUT, false);
theme->logo_color = ThemeProperties::GetDefaultColor(
ThemeProperties::COLOR_NTP_LOGO, false);
} else {
most_visited->background_color =
theme_provider->GetColor(ThemeProperties::COLOR_NTP_SHORTCUT);
if (ntp_theme.logo_alternate) {
theme->logo_color =
theme_provider->GetColor(ThemeProperties::COLOR_NTP_LOGO);
}
}
most_visited->use_white_tile_icon =
color_utils::IsDark(most_visited->background_color);
most_visited->is_dark = !color_utils::IsDark(ntp_theme.text_color);
most_visited->use_title_pill = false;
theme->text_color = ntp_theme.text_color;
theme->is_dark = !color_utils::IsDark(ntp_theme.text_color);
if (ntp_theme.logo_alternate) {
theme->logo_color = ntp_theme.logo_color;
}
auto background_image = new_tab_page::mojom::BackgroundImage::New();
if (!ntp_theme.custom_background_url.is_empty()) {
background_image->url = ntp_theme.custom_background_url;
Expand Down Expand Up @@ -160,21 +174,38 @@ new_tab_page::mojom::ThemePtr MakeTheme(const NtpTheme& ntp_theme) {
theme->most_visited = std::move(most_visited);

auto search_box = realbox::mojom::SearchBoxTheme::New();
search_box->bg = ntp_theme.search_box.bg;
search_box->icon = ntp_theme.search_box.icon;
search_box->icon_selected = ntp_theme.search_box.icon_selected;
search_box->placeholder = ntp_theme.search_box.placeholder;
search_box->results_bg = ntp_theme.search_box.results_bg;
search_box->results_bg_hovered = ntp_theme.search_box.results_bg_hovered;
search_box->results_bg_selected = ntp_theme.search_box.results_bg_selected;
search_box->results_dim = ntp_theme.search_box.results_dim;
search_box->results_dim_selected = ntp_theme.search_box.results_dim_selected;
search_box->results_text = ntp_theme.search_box.results_text;
search_box->bg =
GetOmniboxColor(theme_provider, OmniboxPart::LOCATION_BAR_BACKGROUND);
search_box->icon = GetOmniboxColor(theme_provider, OmniboxPart::RESULTS_ICON);
search_box->icon_selected = GetOmniboxColor(
theme_provider, OmniboxPart::RESULTS_ICON, OmniboxPartState::SELECTED);
search_box->placeholder =
GetOmniboxColor(theme_provider, OmniboxPart::LOCATION_BAR_TEXT_DIMMED);
search_box->results_bg =
GetOmniboxColor(theme_provider, OmniboxPart::RESULTS_BACKGROUND);
search_box->results_bg_hovered =
GetOmniboxColor(theme_provider, OmniboxPart::RESULTS_BACKGROUND,
OmniboxPartState::HOVERED);
search_box->results_bg_selected =
GetOmniboxColor(theme_provider, OmniboxPart::RESULTS_BACKGROUND,
OmniboxPartState::SELECTED);
search_box->results_dim =
GetOmniboxColor(theme_provider, OmniboxPart::RESULTS_TEXT_DIMMED);
search_box->results_dim_selected =
GetOmniboxColor(theme_provider, OmniboxPart::RESULTS_TEXT_DIMMED,
OmniboxPartState::SELECTED);
search_box->results_text =
GetOmniboxColor(theme_provider, OmniboxPart::RESULTS_TEXT_DEFAULT);
search_box->results_text_selected =
ntp_theme.search_box.results_text_selected;
search_box->results_url = ntp_theme.search_box.results_url;
search_box->results_url_selected = ntp_theme.search_box.results_url_selected;
search_box->text = ntp_theme.search_box.text;
GetOmniboxColor(theme_provider, OmniboxPart::RESULTS_TEXT_DEFAULT,
OmniboxPartState::SELECTED);
search_box->results_url =
GetOmniboxColor(theme_provider, OmniboxPart::RESULTS_TEXT_URL);
search_box->results_url_selected =
GetOmniboxColor(theme_provider, OmniboxPart::RESULTS_TEXT_URL,
OmniboxPartState::SELECTED);
search_box->text =
GetOmniboxColor(theme_provider, OmniboxPart::LOCATION_BAR_TEXT_DEFAULT);
theme->search_box = std::move(search_box);

return theme;
Expand Down Expand Up @@ -319,12 +350,14 @@ NewTabPageHandler::NewTabPageHandler(
Profile* profile,
InstantService* instant_service,
search_provider_logos::LogoService* logo_service,
const ui::ThemeProvider* theme_provider,
content::WebContents* web_contents,
const base::Time& ntp_navigation_start_time)
: instant_service_(instant_service),
ntp_background_service_(
NtpBackgroundServiceFactory::GetForProfile(profile)),
logo_service_(logo_service),
theme_provider_(theme_provider),
profile_(profile),
web_contents_(web_contents),
ntp_navigation_start_time_(ntp_navigation_start_time),
Expand All @@ -335,6 +368,7 @@ NewTabPageHandler::NewTabPageHandler(
CHECK(instant_service_);
CHECK(ntp_background_service_);
CHECK(logo_service_);
CHECK(theme_provider_);
CHECK(promo_service_);
CHECK(web_contents_);
instant_service_->AddObserver(this);
Expand Down Expand Up @@ -780,7 +814,7 @@ void NewTabPageHandler::OnPromoLinkClicked() {
}

void NewTabPageHandler::NtpThemeChanged(const NtpTheme& ntp_theme) {
page_->SetTheme(MakeTheme(ntp_theme));
page_->SetTheme(MakeTheme(ntp_theme, theme_provider_));
}

void NewTabPageHandler::MostVisitedInfoChanged(
Expand Down
6 changes: 6 additions & 0 deletions chrome/browser/ui/webui/new_tab_page/new_tab_page_handler.h
Expand Up @@ -43,6 +43,10 @@ namespace search_provider_logos {
class LogoService;
} // namespace search_provider_logos

namespace ui {
class ThemeProvider;
} // namespace ui

class NewTabPageHandler : public new_tab_page::mojom::PageHandler,
public InstantServiceObserver,
public NtpBackgroundServiceObserver,
Expand All @@ -55,6 +59,7 @@ class NewTabPageHandler : public new_tab_page::mojom::PageHandler,
Profile* profile,
InstantService* instant_service,
search_provider_logos::LogoService* logo_service,
const ui::ThemeProvider* theme_provider,
content::WebContents* web_contents,
const base::Time& ntp_navigation_start_time);
~NewTabPageHandler() override;
Expand Down Expand Up @@ -153,6 +158,7 @@ class NewTabPageHandler : public new_tab_page::mojom::PageHandler,
InstantService* instant_service_;
NtpBackgroundService* ntp_background_service_;
search_provider_logos::LogoService* logo_service_;
const ui::ThemeProvider* theme_provider_;
GURL last_blocklisted_;
GetBackgroundCollectionsCallback background_collections_callback_;
base::TimeTicks background_collections_request_start_time_;
Expand Down

0 comments on commit ce22d56

Please sign in to comment.