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

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

Issue 1220793010: [ui/base;css] adding string template expression replacement (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removing changes to base/strings Created 5 years, 5 months 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
46 #include "content/public/browser/browser_thread.h" 46 #include "content/public/browser/browser_thread.h"
47 #include "content/public/browser/notification_service.h" 47 #include "content/public/browser/notification_service.h"
48 #include "content/public/browser/render_process_host.h" 48 #include "content/public/browser/render_process_host.h"
49 #include "extensions/common/extension.h" 49 #include "extensions/common/extension.h"
50 #include "extensions/common/extension_urls.h" 50 #include "extensions/common/extension_urls.h"
51 #include "grit/browser_resources.h" 51 #include "grit/browser_resources.h"
52 #include "grit/components_strings.h" 52 #include "grit/components_strings.h"
53 #include "grit/theme_resources.h" 53 #include "grit/theme_resources.h"
54 #include "ui/base/l10n/l10n_util.h" 54 #include "ui/base/l10n/l10n_util.h"
55 #include "ui/base/resource/resource_bundle.h" 55 #include "ui/base/resource/resource_bundle.h"
56 #include "ui/base/template_expressions.h"
56 #include "ui/base/theme_provider.h" 57 #include "ui/base/theme_provider.h"
57 #include "ui/base/webui/jstemplate_builder.h" 58 #include "ui/base/webui/jstemplate_builder.h"
58 #include "ui/base/webui/web_ui_util.h" 59 #include "ui/base/webui/web_ui_util.h"
59 #include "ui/gfx/animation/animation.h" 60 #include "ui/gfx/animation/animation.h"
60 #include "ui/gfx/color_utils.h" 61 #include "ui/gfx/color_utils.h"
61 #include "ui/gfx/sys_color_change_listener.h" 62 #include "ui/gfx/sys_color_change_listener.h"
62 63
63 #if defined(OS_CHROMEOS) 64 #if defined(OS_CHROMEOS)
64 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" 65 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
65 #include "chromeos/chromeos_switches.h" 66 #include "chromeos/chromeos_switches.h"
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after
539 540
540 void NTPResourceCache::CreateNewTabIncognitoCSS() { 541 void NTPResourceCache::CreateNewTabIncognitoCSS() {
541 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); 542 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
542 DCHECK(tp); 543 DCHECK(tp);
543 544
544 // Get our theme colors 545 // Get our theme colors
545 SkColor color_background = 546 SkColor color_background =
546 GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND); 547 GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND);
547 548
548 // Generate the replacements. 549 // Generate the replacements.
549 std::vector<std::string> subst; 550 std::map<std::string, std::string> substitutions;
550 551
551 // Cache-buster for background. 552 // Cache-buster for background.
552 subst.push_back( 553 substitutions["themeId"] =
553 profile_->GetPrefs()->GetString(prefs::kCurrentThemeID)); // $1 554 profile_->GetPrefs()->GetString(prefs::kCurrentThemeID);
554 555
555 // Colors. 556 // Colors.
556 subst.push_back(SkColorToRGBAString(color_background)); // $2 557 substitutions["colorBackground"] = SkColorToRGBAString(color_background);
557 subst.push_back(GetNewTabBackgroundCSS(tp, false)); // $3 558 substitutions["backgroundBarDetached"] = GetNewTabBackgroundCSS(tp, false);
558 subst.push_back(GetNewTabBackgroundCSS(tp, true)); // $4 559 substitutions["backgroundBarAttached"] = GetNewTabBackgroundCSS(tp, true);
559 subst.push_back(GetNewTabBackgroundTilingCSS(tp)); // $5 560 substitutions["backgroundTiling"] = GetNewTabBackgroundTilingCSS(tp);
560 561
561 // Get our template. 562 // Get our template.
562 static const base::StringPiece new_tab_theme_css( 563 static const base::StringPiece new_tab_theme_css(
563 ResourceBundle::GetSharedInstance().GetRawDataResource( 564 ResourceBundle::GetSharedInstance().GetRawDataResource(
564 IDR_NEW_INCOGNITO_TAB_THEME_CSS)); 565 IDR_NEW_INCOGNITO_TAB_THEME_CSS));
565 566
567 std::string source_string(new_tab_theme_css.data(),
568 new_tab_theme_css.length());
569
566 // Create the string from our template and the replacements. 570 // Create the string from our template and the replacements.
567 std::string full_css = ReplaceStringPlaceholders( 571 std::string full_css =
568 new_tab_theme_css, subst, NULL); 572 ui::ReplaceTemplateExpressions(source_string, substitutions);
569 573
570 new_tab_incognito_css_ = base::RefCountedString::TakeString(&full_css); 574 new_tab_incognito_css_ = base::RefCountedString::TakeString(&full_css);
571 } 575 }
572 576
573 void NTPResourceCache::CreateNewTabGuestCSS() { 577 void NTPResourceCache::CreateNewTabGuestCSS() {
574 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); 578 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
575 DCHECK(tp); 579 DCHECK(tp);
576 580
577 // Get our theme colors 581 // Get our theme colors
578 SkColor color_background = 582 SkColor color_background =
579 GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND); 583 GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND);
580 584
581 // Generate the replacements. 585 // Generate the replacements.
582 std::vector<std::string> subst; 586 std::map<std::string, std::string> substitutions;
583 587
584 // Cache-buster for background. 588 // Cache-buster for background.
585 subst.push_back( 589 substitutions["themeId"] =
586 profile_->GetPrefs()->GetString(prefs::kCurrentThemeID)); // $1 590 profile_->GetPrefs()->GetString(prefs::kCurrentThemeID);
587 591
588 // Colors. 592 // Colors.
589 subst.push_back(SkColorToRGBAString(color_background)); // $2 593 substitutions["colorBackground"] = SkColorToRGBAString(color_background);
590 subst.push_back(GetNewTabBackgroundCSS(tp, false)); // $3 594 substitutions["backgroundBarDetached"] = GetNewTabBackgroundCSS(tp, false);
591 subst.push_back(GetNewTabBackgroundCSS(tp, true)); // $4 595 substitutions["backgroundBarAttached"] = GetNewTabBackgroundCSS(tp, true);
592 subst.push_back(GetNewTabBackgroundTilingCSS(tp)); // $5 596 substitutions["backgroundTiling"] = GetNewTabBackgroundTilingCSS(tp);
593 597
594 // Get our template. 598 // Get our template.
595 static const base::StringPiece new_tab_theme_css( 599 static const base::StringPiece new_tab_theme_css(
596 ResourceBundle::GetSharedInstance().GetRawDataResource( 600 ResourceBundle::GetSharedInstance().GetRawDataResource(
597 IDR_NEW_INCOGNITO_TAB_THEME_CSS)); 601 IDR_NEW_INCOGNITO_TAB_THEME_CSS));
598 602
603 std::string source_string(new_tab_theme_css.data(),
604 new_tab_theme_css.length());
Dan Beam 2015/07/09 19:26:01 if we could do less *potential* copies, that'd be
605
599 // Create the string from our template and the replacements. 606 // Create the string from our template and the replacements.
600 std::string full_css = ReplaceStringPlaceholders( 607 std::string full_css =
601 new_tab_theme_css, subst, NULL); 608 ui::ReplaceTemplateExpressions(source_string, substitutions);
602 609
603 new_tab_guest_css_ = base::RefCountedString::TakeString(&full_css); 610 new_tab_guest_css_ = base::RefCountedString::TakeString(&full_css);
604 } 611 }
605 612
606 void NTPResourceCache::CreateNewTabCSS() { 613 void NTPResourceCache::CreateNewTabCSS() {
607 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); 614 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
608 DCHECK(tp); 615 DCHECK(tp);
609 616
610 // Get our theme colors 617 // Get our theme colors
611 SkColor color_background = 618 SkColor color_background =
612 GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND); 619 GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND);
613 SkColor color_text = GetThemeColor(tp, ThemeProperties::COLOR_NTP_TEXT); 620 SkColor color_text = GetThemeColor(tp, ThemeProperties::COLOR_NTP_TEXT);
614 SkColor color_link = GetThemeColor(tp, ThemeProperties::COLOR_NTP_LINK);
615 SkColor color_link_underline =
616 GetThemeColor(tp, ThemeProperties::COLOR_NTP_LINK_UNDERLINE);
617
618 SkColor color_section =
619 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION);
620 SkColor color_section_text =
621 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_TEXT);
622 SkColor color_section_link =
623 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_LINK);
624 SkColor color_section_link_underline =
625 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_LINK_UNDERLINE);
626 SkColor color_section_header_text =
627 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_HEADER_TEXT);
628 SkColor color_section_header_text_hover =
629 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_HEADER_TEXT_HOVER);
630 SkColor color_section_header_rule =
631 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_HEADER_RULE);
632 SkColor color_section_header_rule_light =
633 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_HEADER_RULE_LIGHT);
634 SkColor color_text_light = 621 SkColor color_text_light =
635 GetThemeColor(tp, ThemeProperties::COLOR_NTP_TEXT_LIGHT); 622 GetThemeColor(tp, ThemeProperties::COLOR_NTP_TEXT_LIGHT);
636 623
637 SkColor color_header = 624 SkColor color_header =
638 GetThemeColor(tp, ThemeProperties::COLOR_NTP_HEADER); 625 GetThemeColor(tp, ThemeProperties::COLOR_NTP_HEADER);
639 // Generate a lighter color for the header gradients. 626 // Generate a lighter color for the header gradients.
640 color_utils::HSL header_lighter; 627 color_utils::HSL header_lighter;
641 color_utils::SkColorToHSL(color_header, &header_lighter); 628 color_utils::SkColorToHSL(color_header, &header_lighter);
642 header_lighter.l += (1 - header_lighter.l) * 0.33; 629 header_lighter.l += (1 - header_lighter.l) * 0.33;
643 SkColor color_header_gradient_light =
644 color_utils::HSLToSkColor(header_lighter, SkColorGetA(color_header));
645 630
646 // Generate section border color from the header color. See 631 // Generate section border color from the header color. See
647 // BookmarkBarView::Paint for how we do this for the bookmark bar 632 // BookmarkBarView::Paint for how we do this for the bookmark bar
648 // borders. 633 // borders.
649 SkColor color_section_border = 634 SkColor color_section_border =
650 SkColorSetARGB(80, 635 SkColorSetARGB(80,
651 SkColorGetR(color_header), 636 SkColorGetR(color_header),
652 SkColorGetG(color_header), 637 SkColorGetG(color_header),
653 SkColorGetB(color_header)); 638 SkColorGetB(color_header));
654 639
655 // Generate the replacements. 640 // Generate the replacements.
656 std::vector<std::string> subst; 641 std::map<std::string, std::string> substitutions;
657 642
658 // Cache-buster for background. 643 // Cache-buster for background.
659 subst.push_back( 644 substitutions["themeId"] =
660 profile_->GetPrefs()->GetString(prefs::kCurrentThemeID)); // $1 645 profile_->GetPrefs()->GetString(prefs::kCurrentThemeID);
661 646
662 // Colors. 647 // Colors.
663 subst.push_back(SkColorToRGBAString(color_background)); // $2 648 substitutions["colorBackground"] = SkColorToRGBAString(color_background);
664 subst.push_back(GetNewTabBackgroundCSS(tp, false)); // $3 649 substitutions["backgroundBarDetached"] = GetNewTabBackgroundCSS(tp, false);
665 subst.push_back(GetNewTabBackgroundCSS(tp, true)); // $4 650 substitutions["backgroundBarAttached"] = GetNewTabBackgroundCSS(tp, true);
666 subst.push_back(GetNewTabBackgroundTilingCSS(tp)); // $5 651 substitutions["backgroundTiling"] = GetNewTabBackgroundTilingCSS(tp);
667 subst.push_back(SkColorToRGBAString(color_header)); // $6 652 substitutions["colorTextCall"] = SkColorToRGBAString(color_text);
668 subst.push_back(SkColorToRGBAString(color_header_gradient_light)); // $7 653 substitutions["colorSectionBorder"] =
669 subst.push_back(SkColorToRGBAString(color_text)); // $8 654 SkColorToRGBAString(color_section_border);
670 subst.push_back(SkColorToRGBAString(color_link)); // $9 655 substitutions["colorTextLight"] = SkColorToRGBAString(color_text_light);
671 subst.push_back(SkColorToRGBAString(color_section)); // $10 656 substitutions["colorSectionBorder"] =
672 subst.push_back(SkColorToRGBAString(color_section_border)); // $11 657 SkColorToRGBComponents(color_section_border);
673 subst.push_back(SkColorToRGBAString(color_section_text)); // $12 658 substitutions["colorText"] = SkColorToRGBComponents(color_text);
674 subst.push_back(SkColorToRGBAString(color_section_link)); // $13
675 subst.push_back(SkColorToRGBAString(color_link_underline)); // $14
676 subst.push_back(SkColorToRGBAString(color_section_link_underline)); // $15
677 subst.push_back(SkColorToRGBAString(color_section_header_text)); // $16
678 subst.push_back(SkColorToRGBAString(
679 color_section_header_text_hover)); // $17
680 subst.push_back(SkColorToRGBAString(color_section_header_rule)); // $18
681 subst.push_back(SkColorToRGBAString(
682 color_section_header_rule_light)); // $19
683 subst.push_back(SkColorToRGBAString(
684 SkColorSetA(color_section_header_rule, 0))); // $20
685 subst.push_back(SkColorToRGBAString(color_text_light)); // $21
686 subst.push_back(SkColorToRGBComponents(color_section_border)); // $22
687 subst.push_back(SkColorToRGBComponents(color_text)); // $23
688 659
689 // Get our template. 660 // Get our template.
690 static const base::StringPiece new_tab_theme_css( 661 static const base::StringPiece new_tab_theme_css(
691 ResourceBundle::GetSharedInstance().GetRawDataResource( 662 ResourceBundle::GetSharedInstance().GetRawDataResource(
692 IDR_NEW_TAB_4_THEME_CSS)); 663 IDR_NEW_TAB_4_THEME_CSS));
693 664
665 std::string source_string(new_tab_theme_css.data(),
666 new_tab_theme_css.length());
667
694 // Create the string from our template and the replacements. 668 // Create the string from our template and the replacements.
695 std::string css_string; 669 std::string css_string;
696 css_string = ReplaceStringPlaceholders(new_tab_theme_css, subst, NULL); 670 css_string = ui::ReplaceTemplateExpressions(source_string, substitutions);
697 new_tab_css_ = base::RefCountedString::TakeString(&css_string); 671 new_tab_css_ = base::RefCountedString::TakeString(&css_string);
698 } 672 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698