Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(160)

Side by Side Diff: chrome/browser/ui/webui/ntp/ntp_resource_cache.cc

Issue 1492423003: Rejigger ThemeService: move exposure of ThemeProvider interface to a (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix that unittest Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h" 5 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 // Creates an rgb string for an SkColor, but leaves the alpha blank so that the 105 // Creates an rgb string for an SkColor, but leaves the alpha blank so that the
106 // css can fill it in. 106 // css can fill it in.
107 std::string SkColorToRGBComponents(SkColor color) { 107 std::string SkColorToRGBComponents(SkColor color) {
108 return base::StringPrintf( 108 return base::StringPrintf(
109 "%d,%d,%d", 109 "%d,%d,%d",
110 SkColorGetR(color), 110 SkColorGetR(color),
111 SkColorGetG(color), 111 SkColorGetG(color),
112 SkColorGetB(color)); 112 SkColorGetB(color));
113 } 113 }
114 114
115 SkColor GetThemeColor(ui::ThemeProvider* tp, int id) { 115 SkColor GetThemeColor(const ui::ThemeProvider& tp, int id) {
116 SkColor color = tp->GetColor(id); 116 SkColor color = tp.GetColor(id);
117 // If web contents are being inverted because the system is in high-contrast 117 // If web contents are being inverted because the system is in high-contrast
118 // mode, any system theme colors we use must be inverted too to cancel out. 118 // mode, any system theme colors we use must be inverted too to cancel out.
119 return color_utils::IsInvertedColorScheme() ? 119 return color_utils::IsInvertedColorScheme() ?
120 color_utils::InvertColor(color) : color; 120 color_utils::InvertColor(color) : color;
121 } 121 }
122 122
123 // Get the CSS string for the background position on the new tab page for the 123 // Get the CSS string for the background position on the new tab page for the
124 // states when the bar is attached or detached. 124 // states when the bar is attached or detached.
125 std::string GetNewTabBackgroundCSS(const ui::ThemeProvider* theme_provider, 125 std::string GetNewTabBackgroundCSS(const ui::ThemeProvider& theme_provider,
126 bool bar_attached) { 126 bool bar_attached) {
127 // TODO(glen): This is a quick workaround to hide the notused.png image when 127 // TODO(glen): This is a quick workaround to hide the notused.png image when
128 // no image is provided - we don't have time right now to figure out why 128 // no image is provided - we don't have time right now to figure out why
129 // this is painting as white. 129 // this is painting as white.
130 // http://crbug.com/17593 130 // http://crbug.com/17593
131 if (!theme_provider->HasCustomImage(IDR_THEME_NTP_BACKGROUND)) { 131 if (!theme_provider.HasCustomImage(IDR_THEME_NTP_BACKGROUND)) {
132 return "-64px"; 132 return "-64px";
133 } 133 }
134 134
135 int alignment = theme_provider->GetDisplayProperty( 135 int alignment = theme_provider.GetDisplayProperty(
136 ThemeProperties::NTP_BACKGROUND_ALIGNMENT); 136 ThemeProperties::NTP_BACKGROUND_ALIGNMENT);
137 137
138 if (bar_attached) 138 if (bar_attached)
139 return ThemeProperties::AlignmentToString(alignment); 139 return ThemeProperties::AlignmentToString(alignment);
140 140
141 if (alignment & ThemeProperties::ALIGN_TOP) { 141 if (alignment & ThemeProperties::ALIGN_TOP) {
142 // The bar is detached, so we must offset the background by the bar size 142 // The bar is detached, so we must offset the background by the bar size
143 // if it's a top-aligned bar. 143 // if it's a top-aligned bar.
144 int offset = chrome::kNTPBookmarkBarHeight; 144 int offset = chrome::kNTPBookmarkBarHeight;
145 145
146 if (alignment & ThemeProperties::ALIGN_LEFT) 146 if (alignment & ThemeProperties::ALIGN_LEFT)
147 return "left " + base::IntToString(-offset) + "px"; 147 return "left " + base::IntToString(-offset) + "px";
148 else if (alignment & ThemeProperties::ALIGN_RIGHT) 148 else if (alignment & ThemeProperties::ALIGN_RIGHT)
149 return "right " + base::IntToString(-offset) + "px"; 149 return "right " + base::IntToString(-offset) + "px";
150 return "center " + base::IntToString(-offset) + "px"; 150 return "center " + base::IntToString(-offset) + "px";
151 } 151 }
152 152
153 return ThemeProperties::AlignmentToString(alignment); 153 return ThemeProperties::AlignmentToString(alignment);
154 } 154 }
155 155
156 // How the background image on the new tab page should be tiled (see tiling 156 // How the background image on the new tab page should be tiled (see tiling
157 // masks in theme_service.h). 157 // masks in theme_service.h).
158 std::string GetNewTabBackgroundTilingCSS( 158 std::string GetNewTabBackgroundTilingCSS(
159 const ui::ThemeProvider* theme_provider) { 159 const ui::ThemeProvider& theme_provider) {
160 int repeat_mode = theme_provider->GetDisplayProperty( 160 int repeat_mode =
161 ThemeProperties::NTP_BACKGROUND_TILING); 161 theme_provider.GetDisplayProperty(ThemeProperties::NTP_BACKGROUND_TILING);
162 return ThemeProperties::TilingToString(repeat_mode); 162 return ThemeProperties::TilingToString(repeat_mode);
163 } 163 }
164 164
165 } // namespace 165 } // namespace
166 166
167 NTPResourceCache::NTPResourceCache(Profile* profile) 167 NTPResourceCache::NTPResourceCache(Profile* profile)
168 : profile_(profile), is_swipe_tracking_from_scroll_events_enabled_(false), 168 : profile_(profile), is_swipe_tracking_from_scroll_events_enabled_(false),
169 should_show_apps_page_(NewTabUI::ShouldShowApps()), 169 should_show_apps_page_(NewTabUI::ShouldShowApps()),
170 should_show_other_devices_menu_(true) { 170 should_show_other_devices_menu_(true) {
171 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, 171 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 } 331 }
332 332
333 localized_strings.SetString("learnMore", 333 localized_strings.SetString("learnMore",
334 l10n_util::GetStringUTF16(new_tab_link_ids)); 334 l10n_util::GetStringUTF16(new_tab_link_ids));
335 localized_strings.SetString("learnMoreLink", new_tab_link); 335 localized_strings.SetString("learnMoreLink", new_tab_link);
336 336
337 bool bookmark_bar_attached = 337 bool bookmark_bar_attached =
338 profile_->GetPrefs()->GetBoolean(bookmarks::prefs::kShowBookmarkBar); 338 profile_->GetPrefs()->GetBoolean(bookmarks::prefs::kShowBookmarkBar);
339 localized_strings.SetBoolean("bookmarkbarattached", bookmark_bar_attached); 339 localized_strings.SetBoolean("bookmarkbarattached", bookmark_bar_attached);
340 340
341 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); 341 const ui::ThemeProvider& tp =
342 ThemeService::GetThemeProviderForProfile(profile_);
342 localized_strings.SetBoolean("hasCustomBackground", 343 localized_strings.SetBoolean("hasCustomBackground",
343 tp->HasCustomImage(IDR_THEME_NTP_BACKGROUND)); 344 tp.HasCustomImage(IDR_THEME_NTP_BACKGROUND));
344 345
345 const std::string& app_locale = g_browser_process->GetApplicationLocale(); 346 const std::string& app_locale = g_browser_process->GetApplicationLocale();
346 webui::SetLoadTimeDataDefaults(app_locale, &localized_strings); 347 webui::SetLoadTimeDataDefaults(app_locale, &localized_strings);
347 348
348 static const base::StringPiece incognito_tab_html( 349 static const base::StringPiece incognito_tab_html(
349 ResourceBundle::GetSharedInstance().GetRawDataResource( 350 ResourceBundle::GetSharedInstance().GetRawDataResource(
350 new_tab_html_idr)); 351 new_tab_html_idr));
351 352
352 std::string full_html = webui::GetI18nTemplateHtml( 353 std::string full_html = webui::GetI18nTemplateHtml(
353 incognito_tab_html, &localized_strings); 354 incognito_tab_html, &localized_strings);
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
532 533
533 // Load the new tab page appropriate for this build. 534 // Load the new tab page appropriate for this build.
534 base::StringPiece new_tab_html(ResourceBundle::GetSharedInstance(). 535 base::StringPiece new_tab_html(ResourceBundle::GetSharedInstance().
535 GetRawDataResource(IDR_NEW_TAB_4_HTML)); 536 GetRawDataResource(IDR_NEW_TAB_4_HTML));
536 std::string full_html = 537 std::string full_html =
537 webui::GetI18nTemplateHtml(new_tab_html, &load_time_data); 538 webui::GetI18nTemplateHtml(new_tab_html, &load_time_data);
538 new_tab_html_ = base::RefCountedString::TakeString(&full_html); 539 new_tab_html_ = base::RefCountedString::TakeString(&full_html);
539 } 540 }
540 541
541 void NTPResourceCache::CreateNewTabIncognitoCSS() { 542 void NTPResourceCache::CreateNewTabIncognitoCSS() {
542 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); 543 // TODO(estade): this returns a subtly incorrect theme provider because
543 DCHECK(tp); 544 // |profile_| is actually not the incognito profile. See crbug.com/568388
545 const ui::ThemeProvider& tp =
546 ThemeService::GetThemeProviderForProfile(profile_);
544 547
545 // Get our theme colors 548 // Get our theme colors
546 SkColor color_background = 549 SkColor color_background =
547 tp->HasCustomImage(IDR_THEME_NTP_BACKGROUND) 550 tp.HasCustomImage(IDR_THEME_NTP_BACKGROUND)
548 ? GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND) 551 ? GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND)
549 : SkColorSetRGB(0x32, 0x32, 0x32); 552 : SkColorSetRGB(0x32, 0x32, 0x32);
550 553
551 // Generate the replacements. 554 // Generate the replacements.
552 std::map<base::StringPiece, std::string> substitutions; 555 std::map<base::StringPiece, std::string> substitutions;
553 556
554 // Cache-buster for background. 557 // Cache-buster for background.
555 substitutions["themeId"] = 558 substitutions["themeId"] =
556 profile_->GetPrefs()->GetString(prefs::kCurrentThemeID); 559 profile_->GetPrefs()->GetString(prefs::kCurrentThemeID);
557 560
558 // Colors. 561 // Colors.
559 substitutions["colorBackground"] = SkColorToRGBAString(color_background); 562 substitutions["colorBackground"] = SkColorToRGBAString(color_background);
560 substitutions["backgroundBarDetached"] = GetNewTabBackgroundCSS(tp, false); 563 substitutions["backgroundBarDetached"] = GetNewTabBackgroundCSS(tp, false);
561 substitutions["backgroundBarAttached"] = GetNewTabBackgroundCSS(tp, true); 564 substitutions["backgroundBarAttached"] = GetNewTabBackgroundCSS(tp, true);
562 substitutions["backgroundTiling"] = GetNewTabBackgroundTilingCSS(tp); 565 substitutions["backgroundTiling"] = GetNewTabBackgroundTilingCSS(tp);
563 566
564 // Get our template. 567 // Get our template.
565 static const base::StringPiece new_tab_theme_css( 568 static const base::StringPiece new_tab_theme_css(
566 ResourceBundle::GetSharedInstance().GetRawDataResource( 569 ResourceBundle::GetSharedInstance().GetRawDataResource(
567 IDR_NEW_INCOGNITO_TAB_THEME_CSS)); 570 IDR_NEW_INCOGNITO_TAB_THEME_CSS));
568 571
569 // Create the string from our template and the replacements. 572 // Create the string from our template and the replacements.
570 std::string full_css = 573 std::string full_css =
571 ui::ReplaceTemplateExpressions(new_tab_theme_css, substitutions); 574 ui::ReplaceTemplateExpressions(new_tab_theme_css, substitutions);
572 575
573 new_tab_incognito_css_ = base::RefCountedString::TakeString(&full_css); 576 new_tab_incognito_css_ = base::RefCountedString::TakeString(&full_css);
574 } 577 }
575 578
576 void NTPResourceCache::CreateNewTabCSS() { 579 void NTPResourceCache::CreateNewTabCSS() {
577 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); 580 const ui::ThemeProvider& tp =
578 DCHECK(tp); 581 ThemeService::GetThemeProviderForProfile(profile_);
579 582
580 // Get our theme colors 583 // Get our theme colors
581 SkColor color_background = 584 SkColor color_background =
582 GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND); 585 GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND);
583 SkColor color_text = GetThemeColor(tp, ThemeProperties::COLOR_NTP_TEXT); 586 SkColor color_text = GetThemeColor(tp, ThemeProperties::COLOR_NTP_TEXT);
584 SkColor color_text_light = 587 SkColor color_text_light =
585 GetThemeColor(tp, ThemeProperties::COLOR_NTP_TEXT_LIGHT); 588 GetThemeColor(tp, ThemeProperties::COLOR_NTP_TEXT_LIGHT);
586 589
587 SkColor color_header = 590 SkColor color_header =
588 GetThemeColor(tp, ThemeProperties::COLOR_NTP_HEADER); 591 GetThemeColor(tp, ThemeProperties::COLOR_NTP_HEADER);
(...skipping 26 matching lines...) Expand all
615 substitutions["colorTextRgba"] = SkColorToRGBAString(color_text); 618 substitutions["colorTextRgba"] = SkColorToRGBAString(color_text);
616 substitutions["colorSectionBorder"] = 619 substitutions["colorSectionBorder"] =
617 SkColorToRGBAString(color_section_border); 620 SkColorToRGBAString(color_section_border);
618 substitutions["colorTextLight"] = SkColorToRGBAString(color_text_light); 621 substitutions["colorTextLight"] = SkColorToRGBAString(color_text_light);
619 substitutions["colorSectionBorder"] = 622 substitutions["colorSectionBorder"] =
620 SkColorToRGBComponents(color_section_border); 623 SkColorToRGBComponents(color_section_border);
621 substitutions["colorText"] = SkColorToRGBComponents(color_text); 624 substitutions["colorText"] = SkColorToRGBComponents(color_text);
622 625
623 // For themes that right-align the background, we flip the attribution to the 626 // For themes that right-align the background, we flip the attribution to the
624 // left to avoid conflicts. 627 // left to avoid conflicts.
625 int alignment = tp->GetDisplayProperty( 628 int alignment =
626 ThemeProperties::NTP_BACKGROUND_ALIGNMENT); 629 tp.GetDisplayProperty(ThemeProperties::NTP_BACKGROUND_ALIGNMENT);
627 if (alignment & ThemeProperties::ALIGN_RIGHT) { 630 if (alignment & ThemeProperties::ALIGN_RIGHT) {
628 substitutions["leftAlignAttribution"] = "0"; 631 substitutions["leftAlignAttribution"] = "0";
629 substitutions["rightAlignAttribution"] = "auto"; 632 substitutions["rightAlignAttribution"] = "auto";
630 substitutions["textAlignAttribution"] = "right"; 633 substitutions["textAlignAttribution"] = "right";
631 } else { 634 } else {
632 substitutions["leftAlignAttribution"] = "auto"; 635 substitutions["leftAlignAttribution"] = "auto";
633 substitutions["rightAlignAttribution"] = "0"; 636 substitutions["rightAlignAttribution"] = "0";
634 substitutions["textAlignAttribution"] = "left"; 637 substitutions["textAlignAttribution"] = "left";
635 } 638 }
636 639
637 substitutions["displayAttribution"] = 640 substitutions["displayAttribution"] =
638 tp->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION) ? "inline" : "none"; 641 tp.HasCustomImage(IDR_THEME_NTP_ATTRIBUTION) ? "inline" : "none";
639 642
640 // Get our template. 643 // Get our template.
641 static const base::StringPiece new_tab_theme_css( 644 static const base::StringPiece new_tab_theme_css(
642 ResourceBundle::GetSharedInstance().GetRawDataResource( 645 ResourceBundle::GetSharedInstance().GetRawDataResource(
643 IDR_NEW_TAB_4_THEME_CSS)); 646 IDR_NEW_TAB_4_THEME_CSS));
644 647
645 // Create the string from our template and the replacements. 648 // Create the string from our template and the replacements.
646 std::string css_string = 649 std::string css_string =
647 ui::ReplaceTemplateExpressions(new_tab_theme_css, substitutions); 650 ui::ReplaceTemplateExpressions(new_tab_theme_css, substitutions);
648 new_tab_css_ = base::RefCountedString::TakeString(&css_string); 651 new_tab_css_ = base::RefCountedString::TakeString(&css_string);
649 } 652 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/toolbar/toolbar_view.cc ('k') | chrome/browser/ui/webui/theme_handler.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698