Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 235 return NTPResourceCache::INCOGNITO; | 235 return NTPResourceCache::INCOGNITO; |
| 236 } else if (profile->IsOffTheRecord()) { | 236 } else if (profile->IsOffTheRecord()) { |
| 237 return NTPResourceCache::INCOGNITO; | 237 return NTPResourceCache::INCOGNITO; |
| 238 } | 238 } |
| 239 return NTPResourceCache::NORMAL; | 239 return NTPResourceCache::NORMAL; |
| 240 } | 240 } |
| 241 | 241 |
| 242 base::RefCountedMemory* NTPResourceCache::GetNewTabHTML(WindowType win_type) { | 242 base::RefCountedMemory* NTPResourceCache::GetNewTabHTML(WindowType win_type) { |
| 243 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 243 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 244 if (win_type == GUEST) { | 244 if (win_type == GUEST) { |
| 245 if (!new_tab_guest_html_.get()) | 245 if (!new_tab_guest_html_) |
| 246 CreateNewTabGuestHTML(); | 246 CreateNewTabGuestHTML(); |
| 247 return new_tab_guest_html_.get(); | 247 return new_tab_guest_html_.get(); |
| 248 } else if (win_type == INCOGNITO) { | 248 } else if (win_type == INCOGNITO) { |
| 249 if (!new_tab_incognito_html_.get()) | 249 if (!new_tab_incognito_html_) |
| 250 CreateNewTabIncognitoHTML(); | 250 CreateNewTabIncognitoHTML(); |
| 251 return new_tab_incognito_html_.get(); | 251 return new_tab_incognito_html_.get(); |
| 252 } else { | 252 } else { |
| 253 // Refresh the cached HTML if necessary. | 253 // Refresh the cached HTML if necessary. |
| 254 // NOTE: NewTabCacheNeedsRefresh() must be called every time the new tab | 254 // NOTE: NewTabCacheNeedsRefresh() must be called every time the new tab |
| 255 // HTML is fetched, because it needs to initialize cached values. | 255 // HTML is fetched, because it needs to initialize cached values. |
| 256 if (NewTabCacheNeedsRefresh() || !new_tab_html_.get()) | 256 if (NewTabCacheNeedsRefresh() || !new_tab_html_) |
| 257 CreateNewTabHTML(); | 257 CreateNewTabHTML(); |
| 258 return new_tab_html_.get(); | 258 return new_tab_html_.get(); |
| 259 } | 259 } |
| 260 } | 260 } |
| 261 | 261 |
| 262 base::RefCountedMemory* NTPResourceCache::GetNewTabCSS(WindowType win_type) { | 262 base::RefCountedMemory* NTPResourceCache::GetNewTabCSS(WindowType win_type) { |
| 263 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 263 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 264 if (win_type == GUEST) { | 264 DCHECK_NE(win_type, GUEST); |
| 265 if (!new_tab_guest_css_.get()) | 265 |
| 266 CreateNewTabGuestCSS(); | 266 if (win_type == INCOGNITO) { |
| 267 return new_tab_guest_css_.get(); | 267 if (!new_tab_incognito_css_.) |
|
Dan Beam
2015/11/20 00:48:45
oops
Evan Stade
2015/11/20 22:05:17
Done.
| |
| 268 } else if (win_type == INCOGNITO) { | |
| 269 if (!new_tab_incognito_css_.get()) | |
| 270 CreateNewTabIncognitoCSS(); | 268 CreateNewTabIncognitoCSS(); |
| 271 return new_tab_incognito_css_.get(); | 269 return new_tab_incognito_css_.get(); |
| 272 } else { | |
| 273 if (!new_tab_css_.get()) | |
| 274 CreateNewTabCSS(); | |
| 275 return new_tab_css_.get(); | |
| 276 } | 270 } |
| 271 | |
| 272 if (!new_tab_css_) | |
| 273 CreateNewTabCSS(); | |
| 274 return new_tab_css_.get(); | |
| 277 } | 275 } |
| 278 | 276 |
| 279 void NTPResourceCache::Observe(int type, | 277 void NTPResourceCache::Observe(int type, |
| 280 const content::NotificationSource& source, | 278 const content::NotificationSource& source, |
| 281 const content::NotificationDetails& details) { | 279 const content::NotificationDetails& details) { |
| 282 // Invalidate the cache. | 280 // Invalidate the cache. |
| 283 if (chrome::NOTIFICATION_BROWSER_THEME_CHANGED == type || | 281 if (chrome::NOTIFICATION_BROWSER_THEME_CHANGED == type || |
| 284 chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED == type) { | 282 chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED == type) { |
| 285 Invalidate(); | 283 Invalidate(); |
| 286 } else { | 284 } else { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 330 } | 328 } |
| 331 | 329 |
| 332 localized_strings.SetString("learnMore", | 330 localized_strings.SetString("learnMore", |
| 333 l10n_util::GetStringUTF16(new_tab_link_ids)); | 331 l10n_util::GetStringUTF16(new_tab_link_ids)); |
| 334 localized_strings.SetString("learnMoreLink", new_tab_link); | 332 localized_strings.SetString("learnMoreLink", new_tab_link); |
| 335 | 333 |
| 336 bool bookmark_bar_attached = | 334 bool bookmark_bar_attached = |
| 337 profile_->GetPrefs()->GetBoolean(bookmarks::prefs::kShowBookmarkBar); | 335 profile_->GetPrefs()->GetBoolean(bookmarks::prefs::kShowBookmarkBar); |
| 338 localized_strings.SetBoolean("bookmarkbarattached", bookmark_bar_attached); | 336 localized_strings.SetBoolean("bookmarkbarattached", bookmark_bar_attached); |
| 339 | 337 |
| 338 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); | |
| 339 localized_strings.SetBoolean("hasCustomBackground", | |
| 340 tp->HasCustomImage(IDR_THEME_NTP_BACKGROUND)); | |
| 341 | |
| 340 const std::string& app_locale = g_browser_process->GetApplicationLocale(); | 342 const std::string& app_locale = g_browser_process->GetApplicationLocale(); |
| 341 webui::SetLoadTimeDataDefaults(app_locale, &localized_strings); | 343 webui::SetLoadTimeDataDefaults(app_locale, &localized_strings); |
| 342 | 344 |
| 343 static const base::StringPiece incognito_tab_html( | 345 static const base::StringPiece incognito_tab_html( |
| 344 ResourceBundle::GetSharedInstance().GetRawDataResource( | 346 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 345 new_tab_html_idr)); | 347 new_tab_html_idr)); |
| 346 | 348 |
| 347 std::string full_html = webui::GetI18nTemplateHtml( | 349 std::string full_html = webui::GetI18nTemplateHtml( |
| 348 incognito_tab_html, &localized_strings); | 350 incognito_tab_html, &localized_strings); |
| 349 | 351 |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 532 webui::GetI18nTemplateHtml(new_tab_html, &load_time_data); | 534 webui::GetI18nTemplateHtml(new_tab_html, &load_time_data); |
| 533 new_tab_html_ = base::RefCountedString::TakeString(&full_html); | 535 new_tab_html_ = base::RefCountedString::TakeString(&full_html); |
| 534 } | 536 } |
| 535 | 537 |
| 536 void NTPResourceCache::CreateNewTabIncognitoCSS() { | 538 void NTPResourceCache::CreateNewTabIncognitoCSS() { |
| 537 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); | 539 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); |
| 538 DCHECK(tp); | 540 DCHECK(tp); |
| 539 | 541 |
| 540 // Get our theme colors | 542 // Get our theme colors |
| 541 SkColor color_background = | 543 SkColor color_background = |
| 542 GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND); | 544 tp->HasCustomImage(IDR_THEME_NTP_BACKGROUND) |
| 545 ? GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND) | |
| 546 : SkColorSetRGB(0x32, 0x32, 0x32); | |
| 543 | 547 |
| 544 // Generate the replacements. | 548 // Generate the replacements. |
| 545 std::map<base::StringPiece, std::string> substitutions; | 549 std::map<base::StringPiece, std::string> substitutions; |
| 546 | 550 |
| 547 // Cache-buster for background. | 551 // Cache-buster for background. |
| 548 substitutions["themeId"] = | 552 substitutions["themeId"] = |
| 549 profile_->GetPrefs()->GetString(prefs::kCurrentThemeID); | 553 profile_->GetPrefs()->GetString(prefs::kCurrentThemeID); |
| 550 | 554 |
| 551 // Colors. | 555 // Colors. |
| 552 substitutions["colorBackground"] = SkColorToRGBAString(color_background); | 556 substitutions["colorBackground"] = SkColorToRGBAString(color_background); |
| 553 substitutions["backgroundBarDetached"] = GetNewTabBackgroundCSS(tp, false); | 557 substitutions["backgroundBarDetached"] = GetNewTabBackgroundCSS(tp, false); |
| 554 substitutions["backgroundBarAttached"] = GetNewTabBackgroundCSS(tp, true); | 558 substitutions["backgroundBarAttached"] = GetNewTabBackgroundCSS(tp, true); |
| 555 substitutions["backgroundTiling"] = GetNewTabBackgroundTilingCSS(tp); | 559 substitutions["backgroundTiling"] = GetNewTabBackgroundTilingCSS(tp); |
| 556 | 560 |
| 557 // Get our template. | 561 // Get our template. |
| 558 static const base::StringPiece new_tab_theme_css( | 562 static const base::StringPiece new_tab_theme_css( |
| 559 ResourceBundle::GetSharedInstance().GetRawDataResource( | 563 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 560 IDR_NEW_INCOGNITO_TAB_THEME_CSS)); | 564 IDR_NEW_INCOGNITO_TAB_THEME_CSS)); |
| 561 | 565 |
| 562 // Create the string from our template and the replacements. | 566 // Create the string from our template and the replacements. |
| 563 std::string full_css = | 567 std::string full_css = |
| 564 ui::ReplaceTemplateExpressions(new_tab_theme_css, substitutions); | 568 ui::ReplaceTemplateExpressions(new_tab_theme_css, substitutions); |
| 565 | 569 |
| 566 new_tab_incognito_css_ = base::RefCountedString::TakeString(&full_css); | 570 new_tab_incognito_css_ = base::RefCountedString::TakeString(&full_css); |
| 567 } | 571 } |
| 568 | 572 |
| 569 void NTPResourceCache::CreateNewTabGuestCSS() { | |
| 570 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); | |
| 571 DCHECK(tp); | |
| 572 | |
| 573 // Get our theme colors | |
| 574 SkColor color_background = | |
| 575 GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND); | |
| 576 | |
| 577 // Generate the replacements. | |
| 578 std::map<base::StringPiece, std::string> substitutions; | |
| 579 | |
| 580 // Cache-buster for background. | |
| 581 substitutions["themeId"] = | |
| 582 profile_->GetPrefs()->GetString(prefs::kCurrentThemeID); | |
| 583 | |
| 584 // Colors. | |
| 585 substitutions["colorBackground"] = SkColorToRGBAString(color_background); | |
| 586 substitutions["backgroundBarDetached"] = GetNewTabBackgroundCSS(tp, false); | |
| 587 substitutions["backgroundBarAttached"] = GetNewTabBackgroundCSS(tp, true); | |
| 588 substitutions["backgroundTiling"] = GetNewTabBackgroundTilingCSS(tp); | |
| 589 | |
| 590 // Get our template. | |
| 591 static const base::StringPiece new_tab_theme_css( | |
| 592 ResourceBundle::GetSharedInstance().GetRawDataResource( | |
| 593 IDR_NEW_INCOGNITO_TAB_THEME_CSS)); | |
| 594 | |
| 595 // Create the string from our template and the replacements. | |
| 596 std::string full_css = | |
| 597 ui::ReplaceTemplateExpressions(new_tab_theme_css, substitutions); | |
| 598 | |
| 599 new_tab_guest_css_ = base::RefCountedString::TakeString(&full_css); | |
| 600 } | |
| 601 | |
| 602 void NTPResourceCache::CreateNewTabCSS() { | 573 void NTPResourceCache::CreateNewTabCSS() { |
| 603 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); | 574 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); |
| 604 DCHECK(tp); | 575 DCHECK(tp); |
| 605 | 576 |
| 606 // Get our theme colors | 577 // Get our theme colors |
| 607 SkColor color_background = | 578 SkColor color_background = |
| 608 GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND); | 579 GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND); |
| 609 SkColor color_text = GetThemeColor(tp, ThemeProperties::COLOR_NTP_TEXT); | 580 SkColor color_text = GetThemeColor(tp, ThemeProperties::COLOR_NTP_TEXT); |
| 610 SkColor color_text_light = | 581 SkColor color_text_light = |
| 611 GetThemeColor(tp, ThemeProperties::COLOR_NTP_TEXT_LIGHT); | 582 GetThemeColor(tp, ThemeProperties::COLOR_NTP_TEXT_LIGHT); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 666 // Get our template. | 637 // Get our template. |
| 667 static const base::StringPiece new_tab_theme_css( | 638 static const base::StringPiece new_tab_theme_css( |
| 668 ResourceBundle::GetSharedInstance().GetRawDataResource( | 639 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 669 IDR_NEW_TAB_4_THEME_CSS)); | 640 IDR_NEW_TAB_4_THEME_CSS)); |
| 670 | 641 |
| 671 // Create the string from our template and the replacements. | 642 // Create the string from our template and the replacements. |
| 672 std::string css_string = | 643 std::string css_string = |
| 673 ui::ReplaceTemplateExpressions(new_tab_theme_css, substitutions); | 644 ui::ReplaceTemplateExpressions(new_tab_theme_css, substitutions); |
| 674 new_tab_css_ = base::RefCountedString::TakeString(&css_string); | 645 new_tab_css_ = base::RefCountedString::TakeString(&css_string); |
| 675 } | 646 } |
| OLD | NEW |