| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/memory/ref_counted_memory.h" | 12 #include "base/memory/ref_counted_memory.h" |
| 13 #include "base/string16.h" | 13 #include "base/string16.h" |
| 14 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
| 15 #include "base/string_number_conversions.h" | 15 #include "base/string_number_conversions.h" |
| 16 #include "base/time.h" | 16 #include "base/time.h" |
| 17 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
| 18 #include "base/values.h" | 18 #include "base/values.h" |
| 19 #include "chrome/browser/defaults.h" | 19 #include "chrome/browser/defaults.h" |
| 20 #include "chrome/browser/google/google_util.h" | 20 #include "chrome/browser/google/google_util.h" |
| 21 #include "chrome/browser/prefs/pref_service.h" | 21 #include "chrome/browser/prefs/pref_service.h" |
| 22 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
| 23 #include "chrome/browser/sync/profile_sync_service.h" |
| 23 #include "chrome/browser/themes/theme_service.h" | 24 #include "chrome/browser/themes/theme_service.h" |
| 24 #include "chrome/browser/themes/theme_service_factory.h" | 25 #include "chrome/browser/themes/theme_service_factory.h" |
| 25 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | 26 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
| 26 #include "chrome/browser/ui/webui/ntp/new_tab_page_handler.h" | 27 #include "chrome/browser/ui/webui/ntp/new_tab_page_handler.h" |
| 27 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | 28 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
| 28 #include "chrome/browser/ui/webui/ntp/shown_sections_handler.h" | 29 #include "chrome/browser/ui/webui/ntp/shown_sections_handler.h" |
| 29 #include "chrome/browser/ui/webui/sync_setup_handler.h" | 30 #include "chrome/browser/ui/webui/sync_setup_handler.h" |
| 30 #include "chrome/browser/web_resource/promo_resource_service.h" | 31 #include "chrome/browser/web_resource/promo_resource_service.h" |
| 31 #include "chrome/common/chrome_notification_types.h" | 32 #include "chrome/common/chrome_notification_types.h" |
| 32 #include "chrome/common/chrome_switches.h" | 33 #include "chrome/common/chrome_switches.h" |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 | 169 |
| 169 NTPResourceCache::NTPResourceCache(Profile* profile) : profile_(profile) { | 170 NTPResourceCache::NTPResourceCache(Profile* profile) : profile_(profile) { |
| 170 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, | 171 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
| 171 Source<ThemeService>( | 172 Source<ThemeService>( |
| 172 ThemeServiceFactory::GetForProfile(profile))); | 173 ThemeServiceFactory::GetForProfile(profile))); |
| 173 registrar_.Add(this, chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, | 174 registrar_.Add(this, chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, |
| 174 NotificationService::AllSources()); | 175 NotificationService::AllSources()); |
| 175 | 176 |
| 176 // Watch for pref changes that cause us to need to invalidate the HTML cache. | 177 // Watch for pref changes that cause us to need to invalidate the HTML cache. |
| 177 pref_change_registrar_.Init(profile_->GetPrefs()); | 178 pref_change_registrar_.Init(profile_->GetPrefs()); |
| 179 pref_change_registrar_.Add(prefs::kAcknowledgedSyncTypes, this); |
| 178 pref_change_registrar_.Add(prefs::kShowBookmarkBar, this); | 180 pref_change_registrar_.Add(prefs::kShowBookmarkBar, this); |
| 179 pref_change_registrar_.Add(prefs::kEnableBookmarkBar, this); | 181 pref_change_registrar_.Add(prefs::kEnableBookmarkBar, this); |
| 182 pref_change_registrar_.Add(prefs::kHomePageIsNewTabPage, this); |
| 180 pref_change_registrar_.Add(prefs::kNTPShownSections, this); | 183 pref_change_registrar_.Add(prefs::kNTPShownSections, this); |
| 181 pref_change_registrar_.Add(prefs::kNTPShownPage, this); | 184 pref_change_registrar_.Add(prefs::kNTPShownPage, this); |
| 182 } | 185 } |
| 183 | 186 |
| 184 NTPResourceCache::~NTPResourceCache() {} | 187 NTPResourceCache::~NTPResourceCache() {} |
| 185 | 188 |
| 186 RefCountedMemory* NTPResourceCache::GetNewTabHTML(bool is_incognito) { | 189 RefCountedMemory* NTPResourceCache::GetNewTabHTML(bool is_incognito) { |
| 187 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 188 if (is_incognito) { | 191 if (is_incognito) { |
| 189 if (!new_tab_incognito_html_.get()) | 192 if (!new_tab_incognito_html_.get()) |
| (...skipping 22 matching lines...) Expand all Loading... |
| 212 void NTPResourceCache::Observe(int type, | 215 void NTPResourceCache::Observe(int type, |
| 213 const NotificationSource& source, const NotificationDetails& details) { | 216 const NotificationSource& source, const NotificationDetails& details) { |
| 214 // Invalidate the cache. | 217 // Invalidate the cache. |
| 215 if (chrome::NOTIFICATION_BROWSER_THEME_CHANGED == type || | 218 if (chrome::NOTIFICATION_BROWSER_THEME_CHANGED == type || |
| 216 chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED == type) { | 219 chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED == type) { |
| 217 new_tab_incognito_html_ = NULL; | 220 new_tab_incognito_html_ = NULL; |
| 218 new_tab_html_ = NULL; | 221 new_tab_html_ = NULL; |
| 219 new_tab_incognito_css_ = NULL; | 222 new_tab_incognito_css_ = NULL; |
| 220 new_tab_css_ = NULL; | 223 new_tab_css_ = NULL; |
| 221 } else if (chrome::NOTIFICATION_PREF_CHANGED == type) { | 224 } else if (chrome::NOTIFICATION_PREF_CHANGED == type) { |
| 222 std::string* pref_name = Details<std::string>(details).ptr(); | 225 // A change occurred to one of the preferences we care about, so flush the |
| 223 if (*pref_name == prefs::kShowBookmarkBar || | 226 // cache. |
| 224 *pref_name == prefs::kEnableBookmarkBar || | 227 new_tab_incognito_html_ = NULL; |
| 225 *pref_name == prefs::kHomePageIsNewTabPage || | 228 new_tab_html_ = NULL; |
| 226 *pref_name == prefs::kNTPShownSections || | |
| 227 *pref_name == prefs::kNTPShownPage) { | |
| 228 new_tab_incognito_html_ = NULL; | |
| 229 new_tab_html_ = NULL; | |
| 230 } else { | |
| 231 NOTREACHED(); | |
| 232 } | |
| 233 } else { | 229 } else { |
| 234 NOTREACHED(); | 230 NOTREACHED(); |
| 235 } | 231 } |
| 236 } | 232 } |
| 237 | 233 |
| 238 void NTPResourceCache::CreateNewTabIncognitoHTML() { | 234 void NTPResourceCache::CreateNewTabIncognitoHTML() { |
| 239 DictionaryValue localized_strings; | 235 DictionaryValue localized_strings; |
| 240 localized_strings.SetString("title", | 236 localized_strings.SetString("title", |
| 241 l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE)); | 237 l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE)); |
| 242 int new_tab_message_ids = IDS_NEW_TAB_OTR_MESSAGE; | 238 int new_tab_message_ids = IDS_NEW_TAB_OTR_MESSAGE; |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 localized_strings.SetString("applaunchtypewindow", | 347 localized_strings.SetString("applaunchtypewindow", |
| 352 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_WINDOW)); | 348 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_WINDOW)); |
| 353 localized_strings.SetString("applaunchtypefullscreen", | 349 localized_strings.SetString("applaunchtypefullscreen", |
| 354 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_FULLSCREEN)); | 350 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_FULLSCREEN)); |
| 355 localized_strings.SetString("web_store_title", | 351 localized_strings.SetString("web_store_title", |
| 356 l10n_util::GetStringUTF16(IDS_EXTENSION_WEB_STORE_TITLE)); | 352 l10n_util::GetStringUTF16(IDS_EXTENSION_WEB_STORE_TITLE)); |
| 357 localized_strings.SetString("web_store_url", | 353 localized_strings.SetString("web_store_url", |
| 358 GetUrlWithLang(GURL(Extension::ChromeStoreLaunchURL()))); | 354 GetUrlWithLang(GURL(Extension::ChromeStoreLaunchURL()))); |
| 359 localized_strings.SetString("syncpromotext", | 355 localized_strings.SetString("syncpromotext", |
| 360 l10n_util::GetStringUTF16(IDS_SYNC_START_SYNC_BUTTON_LABEL)); | 356 l10n_util::GetStringUTF16(IDS_SYNC_START_SYNC_BUTTON_LABEL)); |
| 357 localized_strings.SetString("syncLinkText", |
| 358 l10n_util::GetStringUTF16(IDS_SYNC_ADVANCED_OPTIONS)); |
| 361 localized_strings.SetString("trashLabel", | 359 localized_strings.SetString("trashLabel", |
| 362 l10n_util::GetStringFUTF16( | 360 l10n_util::GetStringFUTF16( |
| 363 IDS_NEW_TAB_TRASH_LABEL, | 361 IDS_NEW_TAB_TRASH_LABEL, |
| 364 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME))); | 362 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME))); |
| 365 #if defined(OS_CHROMEOS) | 363 #if defined(OS_CHROMEOS) |
| 366 localized_strings.SetString("expandMenu", | 364 localized_strings.SetString("expandMenu", |
| 367 l10n_util::GetStringUTF16(IDS_NEW_TAB_CLOSE_MENU_EXPAND)); | 365 l10n_util::GetStringUTF16(IDS_NEW_TAB_CLOSE_MENU_EXPAND)); |
| 368 #endif | 366 #endif |
| 369 | 367 |
| 370 NewTabPageHandler::GetLocalizedValues(profile_, &localized_strings); | 368 NewTabPageHandler::GetLocalizedValues(profile_, &localized_strings); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 400 "true" : "false"); | 398 "true" : "false"); |
| 401 } else { | 399 } else { |
| 402 localized_strings.SetString("customlogo", "false"); | 400 localized_strings.SetString("customlogo", "false"); |
| 403 } | 401 } |
| 404 | 402 |
| 405 // If the user has preferences for a start and end time for a promo from | 403 // If the user has preferences for a start and end time for a promo from |
| 406 // the server, and this promo string exists, set the localized string. | 404 // the server, and this promo string exists, set the localized string. |
| 407 if (profile_->GetPrefs()->FindPreference(prefs::kNTPPromoStart) && | 405 if (profile_->GetPrefs()->FindPreference(prefs::kNTPPromoStart) && |
| 408 profile_->GetPrefs()->FindPreference(prefs::kNTPPromoEnd) && | 406 profile_->GetPrefs()->FindPreference(prefs::kNTPPromoEnd) && |
| 409 profile_->GetPrefs()->FindPreference(prefs::kNTPPromoLine) && | 407 profile_->GetPrefs()->FindPreference(prefs::kNTPPromoLine) && |
| 410 PromoResourceServiceUtil::CanShowPromo(profile_)) { | 408 PromoResourceServiceUtil::CanShowPromo(profile_) && |
| 411 localized_strings.SetString("serverpromo", | 409 InDateRange(profile_->GetPrefs()->GetDouble(prefs::kNTPPromoStart), |
| 412 InDateRange(profile_->GetPrefs()->GetDouble(prefs::kNTPPromoStart), | 410 profile_->GetPrefs()->GetDouble(prefs::kNTPPromoEnd))) { |
| 413 profile_->GetPrefs()->GetDouble(prefs::kNTPPromoEnd)) ? | 411 localized_strings.SetString( |
| 414 profile_->GetPrefs()->GetString(prefs::kNTPPromoLine) : | 412 "serverpromo", |
| 415 std::string()); | 413 profile_->GetPrefs()->GetString(prefs::kNTPPromoLine)); |
| 416 UserMetrics::RecordAction(UserMetricsAction("NTPPromoShown")); | 414 UserMetrics::RecordAction(UserMetricsAction("NTPPromoShown")); |
| 415 } else { |
| 416 // Not showing a server-provided promo, so check if we need to show a |
| 417 // notification from the sync service instead (don't want to show both). |
| 418 // If no notification is desired, the "syncNotification" attribute is set |
| 419 // to the empty string. |
| 420 localized_strings.SetString("syncNotification", GetSyncTypeMessage()); |
| 417 } | 421 } |
| 418 | 422 |
| 419 // Load the new tab page appropriate for this build | 423 // Load the new tab page appropriate for this build |
| 420 // Note that some builds (eg. TOUCHUI) don't make use of everything we | 424 // Note that some builds (eg. TOUCHUI) don't make use of everything we |
| 421 // do here (all of the template data, etc.), but we keep the back end | 425 // do here (all of the template data, etc.), but we keep the back end |
| 422 // consistent across builds, supporting the union of all NTP front-ends | 426 // consistent across builds, supporting the union of all NTP front-ends |
| 423 // for simplicity. | 427 // for simplicity. |
| 424 std::string full_html; | 428 std::string full_html; |
| 425 if (NewTabUI::Ntp4Enabled()) { | 429 if (NewTabUI::Ntp4Enabled()) { |
| 426 base::StringPiece new_tab_html(ResourceBundle::GetSharedInstance(). | 430 base::StringPiece new_tab_html(ResourceBundle::GetSharedInstance(). |
| (...skipping 21 matching lines...) Expand all Loading... |
| 448 new_tab_html.size() - after_offset); | 452 new_tab_html.size() - after_offset); |
| 449 } else { | 453 } else { |
| 450 NOTREACHED(); | 454 NOTREACHED(); |
| 451 full_html.assign(new_tab_html.data(), new_tab_html.size()); | 455 full_html.assign(new_tab_html.data(), new_tab_html.size()); |
| 452 } | 456 } |
| 453 } | 457 } |
| 454 | 458 |
| 455 new_tab_html_ = base::RefCountedString::TakeString(&full_html); | 459 new_tab_html_ = base::RefCountedString::TakeString(&full_html); |
| 456 } | 460 } |
| 457 | 461 |
| 462 string16 NTPResourceCache::GetSyncTypeMessage() { |
| 463 if (profile_->HasProfileSyncService()) { |
| 464 syncable::ModelTypeBitSet unacknowledged = |
| 465 profile_->GetProfileSyncService()->GetUnacknowledgedTypes(); |
| 466 |
| 467 // TODO(sync): As we add new data types, we'll probably need some more |
| 468 // generic string to display to the user, since the method below won't |
| 469 // scale indefinitely (we'd need N*(N+1)/2 different strings to represent |
| 470 // all the combinations of unacknowledged types). But for now, we just |
| 471 // have sessions and typed urls so this is OK. |
| 472 if (unacknowledged.test(syncable::SESSIONS) && |
| 473 unacknowledged.test(syncable::TYPED_URLS)) { |
| 474 return l10n_util::GetStringUTF16(IDS_SYNC_ADDED_SESSIONS_AND_TYPED_URLS); |
| 475 } else if (unacknowledged.test(syncable::SESSIONS)) { |
| 476 return l10n_util::GetStringUTF16(IDS_SYNC_ADDED_SESSIONS); |
| 477 } else if (unacknowledged.test(syncable::TYPED_URLS)) { |
| 478 return l10n_util::GetStringUTF16(IDS_SYNC_ADDED_TYPED_URLS); |
| 479 } else { |
| 480 // Shouldn't be possible for any other types to be unacknowledged. |
| 481 CHECK(!unacknowledged.any()); |
| 482 } |
| 483 } |
| 484 return string16(); |
| 485 } |
| 486 |
| 458 void NTPResourceCache::CreateNewTabIncognitoCSS() { | 487 void NTPResourceCache::CreateNewTabIncognitoCSS() { |
| 459 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); | 488 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); |
| 460 DCHECK(tp); | 489 DCHECK(tp); |
| 461 | 490 |
| 462 // Get our theme colors | 491 // Get our theme colors |
| 463 SkColor color_background = | 492 SkColor color_background = |
| 464 tp->GetColor(ThemeService::COLOR_NTP_BACKGROUND); | 493 tp->GetColor(ThemeService::COLOR_NTP_BACKGROUND); |
| 465 | 494 |
| 466 // Generate the replacements. | 495 // Generate the replacements. |
| 467 std::vector<std::string> subst; | 496 std::vector<std::string> subst; |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 575 IDR_NEW_TAB_4_THEME_CSS : IDR_NEW_TAB_THEME_CSS; | 604 IDR_NEW_TAB_4_THEME_CSS : IDR_NEW_TAB_THEME_CSS; |
| 576 static const base::StringPiece new_tab_theme_css( | 605 static const base::StringPiece new_tab_theme_css( |
| 577 ResourceBundle::GetSharedInstance().GetRawDataResource( | 606 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 578 ntp_css_resource_id)); | 607 ntp_css_resource_id)); |
| 579 | 608 |
| 580 // Create the string from our template and the replacements. | 609 // Create the string from our template and the replacements. |
| 581 std::string css_string; | 610 std::string css_string; |
| 582 css_string = ReplaceStringPlaceholders(new_tab_theme_css, subst, NULL); | 611 css_string = ReplaceStringPlaceholders(new_tab_theme_css, subst, NULL); |
| 583 new_tab_css_ = base::RefCountedString::TakeString(&css_string); | 612 new_tab_css_ = base::RefCountedString::TakeString(&css_string); |
| 584 } | 613 } |
| OLD | NEW |