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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h" 6 7 #include <string> 8 #include <vector> 9 10 #include "apps/app_launcher.h" 11 #include "apps/pref_names.h" 12 #include "base/command_line.h" 13 #include "base/memory/ref_counted_memory.h" 14 #include "base/prefs/pref_service.h" 15 #include "base/strings/string16.h" 16 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/stringprintf.h" 18 #include "base/strings/utf_string_conversions.h" 19 #include "base/values.h" 20 #include "chrome/browser/browser_process.h" 21 #include "chrome/browser/chrome_notification_types.h" 22 #include "chrome/browser/first_run/first_run.h" 23 #include "chrome/browser/google/google_util.h" 24 #include "chrome/browser/policy/browser_policy_connector.h" 25 #include "chrome/browser/profiles/profile.h" 26 #include "chrome/browser/search/search.h" 27 #include "chrome/browser/sync/profile_sync_service.h" 28 #include "chrome/browser/sync/profile_sync_service_factory.h" 29 #include "chrome/browser/themes/theme_properties.h" 30 #include "chrome/browser/themes/theme_service.h" 31 #include "chrome/browser/themes/theme_service_factory.h" 32 #include "chrome/browser/ui/bookmarks/bookmark_bar_constants.h" 33 #include "chrome/browser/ui/sync/sync_promo_ui.h" 34 #include "chrome/browser/ui/webui/ntp/new_tab_page_handler.h" 35 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" 36 #include "chrome/browser/ui/webui/ntp/ntp_login_handler.h" 37 #include "chrome/browser/ui/webui/sync_setup_handler.h" 38 #include "chrome/browser/web_resource/notification_promo.h" 39 #include "chrome/common/chrome_switches.h" 40 #include "chrome/common/extensions/extension.h" 41 #include "chrome/common/extensions/extension_constants.h" 42 #include "chrome/common/pref_names.h" 43 #include "chrome/common/url_constants.h" 44 #include "content/public/browser/browser_thread.h" 45 #include "content/public/browser/notification_service.h" 46 #include "grit/browser_resources.h" 47 #include "grit/chromium_strings.h" 48 #include "grit/generated_resources.h" 49 #include "grit/locale_settings.h" 50 #include "grit/theme_resources.h" 51 #include "ui/base/animation/animation.h" 52 #include "ui/base/l10n/l10n_util.h" 53 #include "ui/base/resource/resource_bundle.h" 54 #include "ui/base/theme_provider.h" 55 #include "ui/gfx/color_utils.h" 56 #include "ui/gfx/sys_color_change_listener.h" 57 #include "ui/webui/jstemplate_builder.h" 58 #include "ui/webui/web_ui_util.h" 59 60 #if defined(OS_CHROMEOS) 61 #include "chromeos/chromeos_switches.h" 62 #endif 63 64 #if defined(OS_MACOSX) 65 #include "chrome/browser/platform_util.h" 66 #endif 67 68 using content::BrowserThread; 69 70 namespace { 71 72 // The URL for the the Learn More page shown on incognito new tab. 73 const char kLearnMoreIncognitoUrl[] = 74 #if defined(OS_CHROMEOS) 75 "https://www.google.com/support/chromeos/bin/answer.py?answer=95464"; 76 #else 77 "https://www.google.com/support/chrome/bin/answer.py?answer=95464"; 78 #endif 79 80 // The URL for the Learn More page shown on guest session new tab. 81 const char kLearnMoreGuestSessionUrl[] = 82 "https://www.google.com/support/chromeos/bin/answer.py?answer=1057090"; 83 84 // The URL for bookmark sync service help. 85 const char kSyncServiceHelpUrl[] = 86 "https://www.google.com/support/chrome/bin/answer.py?answer=165139"; 87 88 // The URL to be loaded to display Help. 89 const char kHelpContentUrl[] = 90 "https://www.google.com/support/chrome/"; 91 92 string16 GetUrlWithLang(const GURL& url) { 93 return ASCIIToUTF16(google_util::AppendGoogleLocaleParam(url).spec()); 94 } 95 96 std::string SkColorToRGBAString(SkColor color) { 97 // We convert the alpha using DoubleToString because StringPrintf will use 98 // locale specific formatters (e.g., use , instead of . in German). 99 return base::StringPrintf( 100 "rgba(%d,%d,%d,%s)", 101 SkColorGetR(color), 102 SkColorGetG(color), 103 SkColorGetB(color), 104 base::DoubleToString(SkColorGetA(color) / 255.0).c_str()); 105 } 106 107 // Creates an rgb string for an SkColor, but leaves the alpha blank so that the 108 // css can fill it in. 109 std::string SkColorToRGBComponents(SkColor color) { 110 return base::StringPrintf( 111 "%d,%d,%d", 112 SkColorGetR(color), 113 SkColorGetG(color), 114 SkColorGetB(color)); 115 } 116 117 SkColor GetThemeColor(ui::ThemeProvider* tp, int id) { 118 SkColor color = tp->GetColor(id); 119 // If web contents are being inverted because the system is in high-contrast 120 // mode, any system theme colors we use must be inverted too to cancel out. 121 return gfx::IsInvertedColorScheme() ? 122 color_utils::InvertColor(color) : color; 123 } 124 125 // Get the CSS string for the background position on the new tab page for the 126 // states when the bar is attached or detached. 127 std::string GetNewTabBackgroundCSS(const ui::ThemeProvider* theme_provider, 128 bool bar_attached) { 129 int alignment; 130 theme_provider->GetDisplayProperty( 131 ThemeProperties::NTP_BACKGROUND_ALIGNMENT, &alignment); 132 133 // TODO(glen): This is a quick workaround to hide the notused.png image when 134 // no image is provided - we don't have time right now to figure out why 135 // this is painting as white. 136 // http://crbug.com/17593 137 if (!theme_provider->HasCustomImage(IDR_THEME_NTP_BACKGROUND)) { 138 return "-64px"; 139 } 140 141 if (bar_attached) 142 return ThemeProperties::AlignmentToString(alignment); 143 144 if (alignment & ThemeProperties::ALIGN_TOP) { 145 // The bar is detached, so we must offset the background by the bar size 146 // if it's a top-aligned bar. 147 int offset = chrome::kNTPBookmarkBarHeight; 148 149 if (alignment & ThemeProperties::ALIGN_LEFT) 150 return "left " + base::IntToString(-offset) + "px"; 151 else if (alignment & ThemeProperties::ALIGN_RIGHT) 152 return "right " + base::IntToString(-offset) + "px"; 153 return "center " + base::IntToString(-offset) + "px"; 154 } 155 156 return ThemeProperties::AlignmentToString(alignment); 157 } 158 159 // How the background image on the new tab page should be tiled (see tiling 160 // masks in theme_service.h). 161 std::string GetNewTabBackgroundTilingCSS( 162 const ui::ThemeProvider* theme_provider) { 163 int repeat_mode; 164 theme_provider->GetDisplayProperty( 165 ThemeProperties::NTP_BACKGROUND_TILING, &repeat_mode); 166 return ThemeProperties::TilingToString(repeat_mode); 167 } 168 169 } // namespace 170 171 NTPResourceCache::NTPResourceCache(Profile* profile) 172 : profile_(profile), is_swipe_tracking_from_scroll_events_enabled_(false), 173 should_show_apps_page_(NewTabUI::ShouldShowApps()), 174 should_show_most_visited_page_(true), 175 should_show_other_devices_menu_(true), 176 should_show_recently_closed_menu_(true) { 177 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, 178 content::Source<ThemeService>( 179 ThemeServiceFactory::GetForProfile(profile))); 180 registrar_.Add(this, chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, 181 content::NotificationService::AllSources()); 182 183 base::Closure callback = base::Bind(&NTPResourceCache::OnPreferenceChanged, 184 base::Unretained(this)); 185 186 // Watch for pref changes that cause us to need to invalidate the HTML cache. 187 profile_pref_change_registrar_.Init(profile_->GetPrefs()); 188 profile_pref_change_registrar_.Add(prefs::kSyncAcknowledgedSyncTypes, 189 callback); 190 profile_pref_change_registrar_.Add(prefs::kShowBookmarkBar, callback); 191 profile_pref_change_registrar_.Add(prefs::kNtpShownPage, callback); 192 profile_pref_change_registrar_.Add(prefs::kSignInPromoShowNTPBubble, 193 callback); 194 profile_pref_change_registrar_.Add(prefs::kHideWebStoreIcon, callback); 195 196 // Some tests don't have a local state. 197 if (g_browser_process->local_state()) { 198 local_state_pref_change_registrar_.Init(g_browser_process->local_state()); 199 local_state_pref_change_registrar_.Add(apps::prefs::kShowAppLauncherPromo, 200 callback); 201 local_state_pref_change_registrar_.Add( 202 apps::prefs::kAppLauncherHasBeenEnabled, callback); 203 } 204 } 205 206 NTPResourceCache::~NTPResourceCache() {} 207 208 bool NTPResourceCache::NewTabCacheNeedsRefresh() { 209 #if defined(OS_MACOSX) 210 // Invalidate if the current value is different from the cached value. 211 bool is_enabled = platform_util::IsSwipeTrackingFromScrollEventsEnabled(); 212 if (is_enabled != is_swipe_tracking_from_scroll_events_enabled_) { 213 is_swipe_tracking_from_scroll_events_enabled_ = is_enabled; 214 return true; 215 } 216 #endif 217 bool should_show_apps_page = NewTabUI::ShouldShowApps(); 218 if (should_show_apps_page != should_show_apps_page_) { 219 should_show_apps_page_ = should_show_apps_page; 220 return true; 221 } 222 return false; 223 } 224 225 base::RefCountedMemory* NTPResourceCache::GetNewTabHTML(bool is_incognito) { 226 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 227 if (is_incognito) { 228 if (!new_tab_incognito_html_.get()) 229 CreateNewTabIncognitoHTML(); 230 } else { 231 // Refresh the cached HTML if necessary. 232 // NOTE: NewTabCacheNeedsRefresh() must be called every time the new tab 233 // HTML is fetched, because it needs to initialize cached values. 234 if (NewTabCacheNeedsRefresh() || !new_tab_html_.get()) 235 CreateNewTabHTML(); 236 } 237 return is_incognito ? new_tab_incognito_html_.get() : 238 new_tab_html_.get(); 239 } 240 241 base::RefCountedMemory* NTPResourceCache::GetNewTabCSS(bool is_incognito) { 242 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 243 if (is_incognito) { 244 if (!new_tab_incognito_css_.get()) 245 CreateNewTabIncognitoCSS(); 246 } else { 247 if (!new_tab_css_.get()) 248 CreateNewTabCSS(); 249 } 250 return is_incognito ? new_tab_incognito_css_.get() : 251 new_tab_css_.get(); 252 } 253 254 void NTPResourceCache::Observe(int type, 255 const content::NotificationSource& source, 256 const content::NotificationDetails& details) { 257 // Invalidate the cache. 258 if (chrome::NOTIFICATION_BROWSER_THEME_CHANGED == type || 259 chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED == type) { 260 new_tab_incognito_html_ = NULL; 261 new_tab_html_ = NULL; 262 new_tab_incognito_css_ = NULL; 263 new_tab_css_ = NULL; 264 } else { 265 NOTREACHED(); 266 } 267 } 268 269 void NTPResourceCache::OnPreferenceChanged() { 270 // A change occurred to one of the preferences we care about, so flush the 271 // cache. 272 new_tab_incognito_html_ = NULL; 273 new_tab_html_ = NULL; 274 new_tab_css_ = NULL; 275 } 276 277 void NTPResourceCache::CreateNewTabIncognitoHTML() { 278 DictionaryValue localized_strings; 279 localized_strings.SetString("title", 280 l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE)); 281 int new_tab_message_ids = IDS_NEW_TAB_OTR_MESSAGE; 282 int new_tab_html_idr = IDR_INCOGNITO_TAB_HTML; 283 const char* new_tab_link = kLearnMoreIncognitoUrl; 284 // TODO(altimofeev): consider implementation without 'if def' usage. 285 #if defined(OS_CHROMEOS) 286 if (CommandLine::ForCurrentProcess()->HasSwitch( 287 chromeos::switches::kGuestSession)) { 288 new_tab_message_ids = IDS_NEW_TAB_GUEST_SESSION_MESSAGE; 289 new_tab_html_idr = IDR_GUEST_SESSION_TAB_HTML; 290 new_tab_link = kLearnMoreGuestSessionUrl; 291 292 std::string enterprise_domain = 293 g_browser_process->browser_policy_connector()->GetEnterpriseDomain(); 294 if (!enterprise_domain.empty()) { 295 // Device is enterprise enrolled. 296 localized_strings.SetString("enterpriseInfoVisible", "true"); 297 string16 enterprise_info = l10n_util::GetStringFUTF16( 298 IDS_DEVICE_OWNED_BY_NOTICE, 299 UTF8ToUTF16(enterprise_domain)); 300 localized_strings.SetString("enterpriseInfoMessage", enterprise_info); 301 localized_strings.SetString("learnMore", 302 l10n_util::GetStringUTF16(IDS_LEARN_MORE)); 303 localized_strings.SetString("enterpriseInfoHintLink", 304 GetUrlWithLang(GURL(chrome::kLearnMoreEnterpriseURL))); 305 } else { 306 localized_strings.SetString("enterpriseInfoVisible", "false"); 307 } 308 } 309 #endif 310 localized_strings.SetString("content", 311 l10n_util::GetStringFUTF16(new_tab_message_ids, 312 GetUrlWithLang(GURL(new_tab_link)))); 313 localized_strings.SetString("extensionsmessage", 314 l10n_util::GetStringFUTF16( 315 IDS_NEW_TAB_OTR_EXTENSIONS_MESSAGE, 316 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME), 317 ASCIIToUTF16(chrome::kChromeUIExtensionsURL))); 318 bool bookmark_bar_attached = profile_->GetPrefs()->GetBoolean( 319 prefs::kShowBookmarkBar); 320 localized_strings.SetBoolean("bookmarkbarattached", bookmark_bar_attached); 321 322 webui::SetFontAndTextDirection(&localized_strings); 323 324 static const base::StringPiece incognito_tab_html( 325 ResourceBundle::GetSharedInstance().GetRawDataResource( 326 new_tab_html_idr)); 327 328 std::string full_html = webui::GetI18nTemplateHtml( 329 incognito_tab_html, &localized_strings); 330 331 new_tab_incognito_html_ = base::RefCountedString::TakeString(&full_html); 332 } 333 334 void NTPResourceCache::CreateNewTabHTML() { 335 // TODO(estade): these strings should be defined in their relevant handlers 336 // (in GetLocalizedValues) and should have more legible names. 337 // Show the profile name in the title and most visited labels if the current 338 // profile is not the default. 339 PrefService* prefs = profile_->GetPrefs(); 340 DictionaryValue load_time_data; 341 load_time_data.SetBoolean("bookmarkbarattached", 342 prefs->GetBoolean(prefs::kShowBookmarkBar)); 343 load_time_data.SetBoolean("hasattribution", 344 ThemeServiceFactory::GetForProfile(profile_)->HasCustomImage( 345 IDR_THEME_NTP_ATTRIBUTION)); 346 load_time_data.SetBoolean("showMostvisited", should_show_most_visited_page_); 347 load_time_data.SetBoolean("showAppLauncherPromo", 348 apps::ShouldShowAppLauncherPromo()); 349 load_time_data.SetBoolean("showRecentlyClosed", 350 should_show_recently_closed_menu_); 351 load_time_data.SetString("title", 352 l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE)); 353 load_time_data.SetString("mostvisited", 354 l10n_util::GetStringUTF16(IDS_NEW_TAB_MOST_VISITED)); 355 load_time_data.SetString("suggestions", 356 l10n_util::GetStringUTF16(IDS_NEW_TAB_SUGGESTIONS)); 357 load_time_data.SetString("restoreThumbnailsShort", 358 l10n_util::GetStringUTF16(IDS_NEW_TAB_RESTORE_THUMBNAILS_SHORT_LINK)); 359 load_time_data.SetString("recentlyclosed", 360 l10n_util::GetStringUTF16(IDS_NEW_TAB_RECENTLY_CLOSED)); 361 load_time_data.SetString("webStoreTitle", 362 l10n_util::GetStringUTF16(IDS_EXTENSION_WEB_STORE_TITLE)); 363 load_time_data.SetString("webStoreTitleShort", 364 l10n_util::GetStringUTF16(IDS_EXTENSION_WEB_STORE_TITLE_SHORT)); 365 load_time_data.SetString("closedwindowsingle", 366 l10n_util::GetStringUTF16(IDS_NEW_TAB_RECENTLY_CLOSED_WINDOW_SINGLE)); 367 load_time_data.SetString("closedwindowmultiple", 368 l10n_util::GetStringUTF16(IDS_NEW_TAB_RECENTLY_CLOSED_WINDOW_MULTIPLE)); 369 load_time_data.SetString("attributionintro", 370 l10n_util::GetStringUTF16(IDS_NEW_TAB_ATTRIBUTION_INTRO)); 371 load_time_data.SetString("thumbnailremovednotification", 372 l10n_util::GetStringUTF16(IDS_NEW_TAB_THUMBNAIL_REMOVED_NOTIFICATION)); 373 load_time_data.SetString("undothumbnailremove", 374 l10n_util::GetStringUTF16(IDS_NEW_TAB_UNDO_THUMBNAIL_REMOVE)); 375 load_time_data.SetString("removethumbnailtooltip", 376 l10n_util::GetStringUTF16(IDS_NEW_TAB_REMOVE_THUMBNAIL_TOOLTIP)); 377 load_time_data.SetString("appuninstall", 378 l10n_util::GetStringUTF16(IDS_EXTENSIONS_UNINSTALL)); 379 load_time_data.SetString("appoptions", 380 l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_OPTIONS)); 381 load_time_data.SetString("appdetails", 382 l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_DETAILS)); 383 load_time_data.SetString("appcreateshortcut", 384 l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_CREATE_SHORTCUT)); 385 load_time_data.SetString("appDefaultPageName", 386 l10n_util::GetStringUTF16(IDS_APP_DEFAULT_PAGE_NAME)); 387 load_time_data.SetString("applaunchtypepinned", 388 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_PINNED)); 389 load_time_data.SetString("applaunchtyperegular", 390 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_REGULAR)); 391 load_time_data.SetString("applaunchtypewindow", 392 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_WINDOW)); 393 load_time_data.SetString("applaunchtypefullscreen", 394 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_FULLSCREEN)); 395 load_time_data.SetString("syncpromotext", 396 l10n_util::GetStringUTF16(IDS_SYNC_START_SYNC_BUTTON_LABEL)); 397 load_time_data.SetString("syncLinkText", 398 l10n_util::GetStringUTF16(IDS_SYNC_ADVANCED_OPTIONS)); 399 load_time_data.SetBoolean("shouldShowSyncLogin", 400 NTPLoginHandler::ShouldShow(profile_)); 401 load_time_data.SetString("otherSessions", 402 l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_LABEL)); 403 load_time_data.SetString("otherSessionsEmpty", 404 l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_EMPTY)); 405 load_time_data.SetString("otherSessionsLearnMoreUrl", 406 l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_LEARN_MORE_URL)); 407 load_time_data.SetString("learnMore", 408 l10n_util::GetStringUTF16(IDS_LEARN_MORE)); 409 load_time_data.SetString("webStoreLink", 410 GetUrlWithLang(GURL(extension_urls::GetWebstoreLaunchURL()))); 411 load_time_data.SetString("appInstallHintText", 412 l10n_util::GetStringUTF16(IDS_NEW_TAB_APP_INSTALL_HINT_LABEL)); 413 load_time_data.SetBoolean("isDiscoveryInNTPEnabled", 414 NewTabUI::IsDiscoveryInNTPEnabled()); 415 load_time_data.SetString("collapseSessionMenuItemText", 416 l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_COLLAPSE_SESSION)); 417 load_time_data.SetString("expandSessionMenuItemText", 418 l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_EXPAND_SESSION)); 419 load_time_data.SetString("restoreSessionMenuItemText", 420 l10n_util::GetStringUTF16(IDS_NEW_TAB_OTHER_SESSIONS_OPEN_ALL)); 421 load_time_data.SetString("learn_more", 422 l10n_util::GetStringUTF16(IDS_LEARN_MORE)); 423 load_time_data.SetString("tile_grid_screenreader_accessible_description", 424 l10n_util::GetStringUTF16(IDS_NEW_TAB_TILE_GRID_ACCESSIBLE_DESCRIPTION)); 425 load_time_data.SetString("page_switcher_change_title", 426 l10n_util::GetStringUTF16(IDS_NEW_TAB_PAGE_SWITCHER_CHANGE_TITLE)); 427 load_time_data.SetString("page_switcher_same_title", 428 l10n_util::GetStringUTF16(IDS_NEW_TAB_PAGE_SWITCHER_SAME_TITLE)); 429 load_time_data.SetString("appsPromoTitle", 430 l10n_util::GetStringUTF16(IDS_NEW_TAB_PAGE_APPS_PROMO_TITLE)); 431 // On Mac OS X 10.7+, horizontal scrolling can be treated as a back or 432 // forward gesture. Pass through a flag that indicates whether or not that 433 // feature is enabled. 434 load_time_data.SetBoolean("isSwipeTrackingFromScrollEventsEnabled", 435 is_swipe_tracking_from_scroll_events_enabled_); 436 // Managed users can not have apps installed currently so there's no need to 437 // show the app cards. 438 if (profile_->IsManaged()) 439 should_show_apps_page_ = false; 440 441 load_time_data.SetBoolean("showApps", should_show_apps_page_); 442 load_time_data.SetBoolean("showWebStoreIcon", 443 !prefs->GetBoolean(prefs::kHideWebStoreIcon)); 444 445 #if defined(OS_MACOSX) 446 load_time_data.SetBoolean( 447 "disableCreateAppShortcut", 448 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kEnableAppShims)); 449 #endif 450 451 #if defined(OS_CHROMEOS) 452 load_time_data.SetString("expandMenu", 453 l10n_util::GetStringUTF16(IDS_NEW_TAB_CLOSE_MENU_EXPAND)); 454 #endif 455 456 NewTabPageHandler::GetLocalizedValues(profile_, &load_time_data); 457 NTPLoginHandler::GetLocalizedValues(profile_, &load_time_data); 458 459 webui::SetFontAndTextDirection(&load_time_data); 460 461 // Control fade and resize animations. 462 load_time_data.SetBoolean("anim", ui::Animation::ShouldRenderRichAnimation()); 463 464 int alignment; 465 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); 466 tp->GetDisplayProperty(ThemeProperties::NTP_BACKGROUND_ALIGNMENT, 467 &alignment); 468 load_time_data.SetString("themegravity", 469 (alignment & ThemeProperties::ALIGN_RIGHT) ? "right" : ""); 470 471 // Disable the promo if this is the first run, otherwise set the promo string 472 // for display if there is a valid outstanding promo. 473 if (first_run::IsChromeFirstRun()) { 474 NotificationPromo::HandleClosed(NotificationPromo::NTP_NOTIFICATION_PROMO); 475 } else { 476 NotificationPromo notification_promo; 477 notification_promo.InitFromPrefs(NotificationPromo::NTP_NOTIFICATION_PROMO); 478 if (notification_promo.CanShow()) { 479 load_time_data.SetString("notificationPromoText", 480 notification_promo.promo_text()); 481 DVLOG(1) << "Notification promo:" << notification_promo.promo_text(); 482 } 483 484 NotificationPromo bubble_promo; 485 bubble_promo.InitFromPrefs(NotificationPromo::NTP_BUBBLE_PROMO); 486 if (bubble_promo.CanShow()) { 487 load_time_data.SetString("bubblePromoText", 488 bubble_promo.promo_text()); 489 DVLOG(1) << "Bubble promo:" << bubble_promo.promo_text(); 490 } 491 } 492 493 // Determine whether to show the menu for accessing tabs on other devices. 494 bool show_other_sessions_menu = should_show_other_devices_menu_ && 495 !CommandLine::ForCurrentProcess()->HasSwitch( 496 switches::kDisableNTPOtherSessionsMenu); 497 load_time_data.SetBoolean("showOtherSessionsMenu", show_other_sessions_menu); 498 load_time_data.SetBoolean("isUserSignedIn", 499 !prefs->GetString(prefs::kGoogleServicesUsername).empty()); 500 501 // Load the new tab page appropriate for this build. 502 base::StringPiece new_tab_html(ResourceBundle::GetSharedInstance(). 503 GetRawDataResource(IDR_NEW_TAB_4_HTML)); 504 webui::UseVersion2 version2; 505 std::string full_html = 506 webui::GetI18nTemplateHtml(new_tab_html, &load_time_data); 507 new_tab_html_ = base::RefCountedString::TakeString(&full_html); 508 } 509 510 void NTPResourceCache::CreateNewTabIncognitoCSS() { 511 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); 512 DCHECK(tp); 513 514 // Get our theme colors 515 SkColor color_background = 516 GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND); 517 518 // Generate the replacements. 519 std::vector<std::string> subst; 520 521 // Cache-buster for background. 522 subst.push_back( 523 profile_->GetPrefs()->GetString(prefs::kCurrentThemeID)); // $1 524 525 // Colors. 526 subst.push_back(SkColorToRGBAString(color_background)); // $2 527 subst.push_back(GetNewTabBackgroundCSS(tp, false)); // $3 528 subst.push_back(GetNewTabBackgroundCSS(tp, true)); // $4 529 subst.push_back(GetNewTabBackgroundTilingCSS(tp)); // $5 530 531 // Get our template. 532 static const base::StringPiece new_tab_theme_css( 533 ResourceBundle::GetSharedInstance().GetRawDataResource( 534 IDR_NEW_INCOGNITO_TAB_THEME_CSS)); 535 536 // Create the string from our template and the replacements. 537 std::string full_css = ReplaceStringPlaceholders( 538 new_tab_theme_css, subst, NULL); 539 540 new_tab_incognito_css_ = base::RefCountedString::TakeString(&full_css); 541 } 542 543 void NTPResourceCache::CreateNewTabCSS() { 544 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); 545 DCHECK(tp); 546 547 // Get our theme colors 548 SkColor color_background = 549 GetThemeColor(tp, ThemeProperties::COLOR_NTP_BACKGROUND); 550 SkColor color_text = GetThemeColor(tp, ThemeProperties::COLOR_NTP_TEXT); 551 SkColor color_link = GetThemeColor(tp, ThemeProperties::COLOR_NTP_LINK); 552 SkColor color_link_underline = 553 GetThemeColor(tp, ThemeProperties::COLOR_NTP_LINK_UNDERLINE); 554 555 SkColor color_section = 556 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION); 557 SkColor color_section_text = 558 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_TEXT); 559 SkColor color_section_link = 560 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_LINK); 561 SkColor color_section_link_underline = 562 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_LINK_UNDERLINE); 563 SkColor color_section_header_text = 564 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_HEADER_TEXT); 565 SkColor color_section_header_text_hover = 566 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_HEADER_TEXT_HOVER); 567 SkColor color_section_header_rule = 568 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_HEADER_RULE); 569 SkColor color_section_header_rule_light = 570 GetThemeColor(tp, ThemeProperties::COLOR_NTP_SECTION_HEADER_RULE_LIGHT); 571 SkColor color_text_light = 572 GetThemeColor(tp, ThemeProperties::COLOR_NTP_TEXT_LIGHT); 573 574 SkColor color_header = 575 GetThemeColor(tp, ThemeProperties::COLOR_NTP_HEADER); 576 // Generate a lighter color for the header gradients. 577 color_utils::HSL header_lighter; 578 color_utils::SkColorToHSL(color_header, &header_lighter); 579 header_lighter.l += (1 - header_lighter.l) * 0.33; 580 SkColor color_header_gradient_light = 581 color_utils::HSLToSkColor(header_lighter, SkColorGetA(color_header)); 582 583 // Generate section border color from the header color. See 584 // BookmarkBarView::Paint for how we do this for the bookmark bar 585 // borders. 586 SkColor color_section_border = 587 SkColorSetARGB(80, 588 SkColorGetR(color_header), 589 SkColorGetG(color_header), 590 SkColorGetB(color_header)); 591 592 // Generate the replacements. 593 std::vector<std::string> subst; 594 595 // Cache-buster for background. 596 subst.push_back( 597 profile_->GetPrefs()->GetString(prefs::kCurrentThemeID)); // $1 598 599 // Colors. 600 subst.push_back(SkColorToRGBAString(color_background)); // $2 601 subst.push_back(GetNewTabBackgroundCSS(tp, false)); // $3 602 subst.push_back(GetNewTabBackgroundCSS(tp, true)); // $4 603 subst.push_back(GetNewTabBackgroundTilingCSS(tp)); // $5 604 subst.push_back(SkColorToRGBAString(color_header)); // $6 605 subst.push_back(SkColorToRGBAString(color_header_gradient_light)); // $7 606 subst.push_back(SkColorToRGBAString(color_text)); // $8 607 subst.push_back(SkColorToRGBAString(color_link)); // $9 608 subst.push_back(SkColorToRGBAString(color_section)); // $10 609 subst.push_back(SkColorToRGBAString(color_section_border)); // $11 610 subst.push_back(SkColorToRGBAString(color_section_text)); // $12 611 subst.push_back(SkColorToRGBAString(color_section_link)); // $13 612 subst.push_back(SkColorToRGBAString(color_link_underline)); // $14 613 subst.push_back(SkColorToRGBAString(color_section_link_underline)); // $15 614 subst.push_back(SkColorToRGBAString(color_section_header_text)); // $16 615 subst.push_back(SkColorToRGBAString( 616 color_section_header_text_hover)); // $17 617 subst.push_back(SkColorToRGBAString(color_section_header_rule)); // $18 618 subst.push_back(SkColorToRGBAString( 619 color_section_header_rule_light)); // $19 620 subst.push_back(SkColorToRGBAString( 621 SkColorSetA(color_section_header_rule, 0))); // $20 622 subst.push_back(SkColorToRGBAString(color_text_light)); // $21 623 subst.push_back(SkColorToRGBComponents(color_section_border)); // $22 624 subst.push_back(SkColorToRGBComponents(color_text)); // $23 625 626 // Get our template. 627 static const base::StringPiece new_tab_theme_css( 628 ResourceBundle::GetSharedInstance().GetRawDataResource( 629 IDR_NEW_TAB_4_THEME_CSS)); 630 631 // Create the string from our template and the replacements. 632 std::string css_string; 633 css_string = ReplaceStringPlaceholders(new_tab_theme_css, subst, NULL); 634 new_tab_css_ = base::RefCountedString::TakeString(&css_string); 635 } 636