| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 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" |
| 11 #include "base/memory/ref_counted_memory.h" | 11 #include "base/memory/ref_counted_memory.h" |
| 12 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/values.h" | 16 #include "base/values.h" |
| 17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 18 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
| 19 #include "chrome/browser/chrome_notification_types.h" | 19 #include "chrome/browser/chrome_notification_types.h" |
| 20 #include "chrome/browser/extensions/extension_util.h" | 20 #include "chrome/browser/extensions/extension_util.h" |
| 21 #include "chrome/browser/first_run/first_run.h" | |
| 22 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
| 23 #include "chrome/browser/search/search.h" | 22 #include "chrome/browser/search/search.h" |
| 24 #include "chrome/browser/signin/signin_manager_factory.h" | 23 #include "chrome/browser/signin/signin_manager_factory.h" |
| 25 #include "chrome/browser/sync/profile_sync_service_factory.h" | 24 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 26 #include "chrome/browser/themes/theme_properties.h" | 25 #include "chrome/browser/themes/theme_properties.h" |
| 27 #include "chrome/browser/themes/theme_service.h" | 26 #include "chrome/browser/themes/theme_service.h" |
| 28 #include "chrome/browser/themes/theme_service_factory.h" | 27 #include "chrome/browser/themes/theme_service_factory.h" |
| 29 #include "chrome/browser/ui/app_list/app_list_util.h" | 28 #include "chrome/browser/ui/app_list/app_list_util.h" |
| 30 #include "chrome/browser/ui/apps/app_info_dialog.h" | 29 #include "chrome/browser/ui/apps/app_info_dialog.h" |
| 31 #include "chrome/browser/ui/bookmarks/bookmark_bar_constants.h" | 30 #include "chrome/browser/ui/bookmarks/bookmark_bar_constants.h" |
| 32 #include "chrome/browser/ui/sync/sync_promo_ui.h" | 31 #include "chrome/browser/ui/sync/sync_promo_ui.h" |
| 33 #include "chrome/browser/ui/webui/app_launcher_login_handler.h" | 32 #include "chrome/browser/ui/webui/app_launcher_login_handler.h" |
| 34 #include "chrome/browser/ui/webui/ntp/new_tab_page_handler.h" | 33 #include "chrome/browser/ui/webui/ntp/new_tab_page_handler.h" |
| 35 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | 34 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
| 36 #include "chrome/browser/web_resource/notification_promo_helper.h" | |
| 37 #include "chrome/common/chrome_switches.h" | 35 #include "chrome/common/chrome_switches.h" |
| 38 #include "chrome/common/pref_names.h" | 36 #include "chrome/common/pref_names.h" |
| 39 #include "chrome/common/url_constants.h" | 37 #include "chrome/common/url_constants.h" |
| 40 #include "chrome/grit/chromium_strings.h" | 38 #include "chrome/grit/chromium_strings.h" |
| 41 #include "chrome/grit/generated_resources.h" | 39 #include "chrome/grit/generated_resources.h" |
| 42 #include "chrome/grit/locale_settings.h" | 40 #include "chrome/grit/locale_settings.h" |
| 43 #include "components/bookmarks/common/bookmark_pref_names.h" | 41 #include "components/bookmarks/common/bookmark_pref_names.h" |
| 44 #include "components/browser_sync/browser/profile_sync_service.h" | 42 #include "components/browser_sync/browser/profile_sync_service.h" |
| 45 #include "components/google/core/browser/google_util.h" | 43 #include "components/google/core/browser/google_util.h" |
| 46 #include "components/prefs/pref_service.h" | 44 #include "components/prefs/pref_service.h" |
| 47 #include "components/signin/core/browser/signin_manager.h" | 45 #include "components/signin/core/browser/signin_manager.h" |
| 48 #include "components/strings/grit/components_strings.h" | 46 #include "components/strings/grit/components_strings.h" |
| 49 #include "components/web_resource/notification_promo.h" | |
| 50 #include "content/public/browser/browser_thread.h" | 47 #include "content/public/browser/browser_thread.h" |
| 51 #include "content/public/browser/notification_service.h" | 48 #include "content/public/browser/notification_service.h" |
| 52 #include "content/public/browser/render_process_host.h" | 49 #include "content/public/browser/render_process_host.h" |
| 53 #include "extensions/common/extension.h" | 50 #include "extensions/common/extension.h" |
| 54 #include "extensions/common/extension_urls.h" | 51 #include "extensions/common/extension_urls.h" |
| 55 #include "grit/browser_resources.h" | 52 #include "grit/browser_resources.h" |
| 56 #include "grit/components_strings.h" | 53 #include "grit/components_strings.h" |
| 57 #include "grit/theme_resources.h" | 54 #include "grit/theme_resources.h" |
| 58 #include "ui/base/l10n/l10n_util.h" | 55 #include "ui/base/l10n/l10n_util.h" |
| 59 #include "ui/base/resource/resource_bundle.h" | 56 #include "ui/base/resource/resource_bundle.h" |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 | 163 |
| 167 } // namespace | 164 } // namespace |
| 168 | 165 |
| 169 NTPResourceCache::NTPResourceCache(Profile* profile) | 166 NTPResourceCache::NTPResourceCache(Profile* profile) |
| 170 : profile_(profile), is_swipe_tracking_from_scroll_events_enabled_(false), | 167 : profile_(profile), is_swipe_tracking_from_scroll_events_enabled_(false), |
| 171 should_show_apps_page_(NewTabUI::ShouldShowApps()), | 168 should_show_apps_page_(NewTabUI::ShouldShowApps()), |
| 172 should_show_other_devices_menu_(true) { | 169 should_show_other_devices_menu_(true) { |
| 173 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, | 170 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
| 174 content::Source<ThemeService>( | 171 content::Source<ThemeService>( |
| 175 ThemeServiceFactory::GetForProfile(profile))); | 172 ThemeServiceFactory::GetForProfile(profile))); |
| 176 registrar_.Add(this, chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, | |
| 177 content::NotificationService::AllSources()); | |
| 178 | |
| 179 web_resource::PromoResourceService* promo_service = | |
| 180 g_browser_process->promo_resource_service(); | |
| 181 if (promo_service) { | |
| 182 promo_resource_subscription_ = promo_service->RegisterStateChangedCallback( | |
| 183 base::Bind(&NTPResourceCache::Invalidate, base::Unretained(this))); | |
| 184 } | |
| 185 | 173 |
| 186 base::Closure callback = base::Bind(&NTPResourceCache::OnPreferenceChanged, | 174 base::Closure callback = base::Bind(&NTPResourceCache::OnPreferenceChanged, |
| 187 base::Unretained(this)); | 175 base::Unretained(this)); |
| 188 | 176 |
| 189 // 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. |
| 190 profile_pref_change_registrar_.Init(profile_->GetPrefs()); | 178 profile_pref_change_registrar_.Init(profile_->GetPrefs()); |
| 191 profile_pref_change_registrar_.Add(bookmarks::prefs::kShowBookmarkBar, | 179 profile_pref_change_registrar_.Add(bookmarks::prefs::kShowBookmarkBar, |
| 192 callback); | 180 callback); |
| 193 profile_pref_change_registrar_.Add(prefs::kNtpShownPage, callback); | 181 profile_pref_change_registrar_.Add(prefs::kNtpShownPage, callback); |
| 194 profile_pref_change_registrar_.Add(prefs::kSignInPromoShowNTPBubble, | 182 profile_pref_change_registrar_.Add(prefs::kSignInPromoShowNTPBubble, |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 | 264 |
| 277 if (!new_tab_css_) | 265 if (!new_tab_css_) |
| 278 CreateNewTabCSS(); | 266 CreateNewTabCSS(); |
| 279 return new_tab_css_.get(); | 267 return new_tab_css_.get(); |
| 280 } | 268 } |
| 281 | 269 |
| 282 void NTPResourceCache::Observe(int type, | 270 void NTPResourceCache::Observe(int type, |
| 283 const content::NotificationSource& source, | 271 const content::NotificationSource& source, |
| 284 const content::NotificationDetails& details) { | 272 const content::NotificationDetails& details) { |
| 285 // Invalidate the cache. | 273 // Invalidate the cache. |
| 286 if (chrome::NOTIFICATION_BROWSER_THEME_CHANGED == type || | 274 if (chrome::NOTIFICATION_BROWSER_THEME_CHANGED == type) |
| 287 chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED == type) { | |
| 288 Invalidate(); | 275 Invalidate(); |
| 289 } else { | 276 else |
| 290 NOTREACHED(); | 277 NOTREACHED(); |
| 291 } | |
| 292 } | 278 } |
| 293 | 279 |
| 294 void NTPResourceCache::OnPreferenceChanged() { | 280 void NTPResourceCache::OnPreferenceChanged() { |
| 295 // A change occurred to one of the preferences we care about, so flush the | 281 // A change occurred to one of the preferences we care about, so flush the |
| 296 // cache. | 282 // cache. |
| 297 new_tab_incognito_html_ = NULL; | 283 new_tab_incognito_html_ = NULL; |
| 298 new_tab_html_ = NULL; | 284 new_tab_html_ = NULL; |
| 299 new_tab_css_ = NULL; | 285 new_tab_css_ = NULL; |
| 300 } | 286 } |
| 301 | 287 |
| 288 // TODO(dbeam): why must Invalidate() and OnPreferenceChanged() both exist? |
| 302 void NTPResourceCache::Invalidate() { | 289 void NTPResourceCache::Invalidate() { |
| 303 new_tab_incognito_html_ = nullptr; | 290 new_tab_incognito_html_ = nullptr; |
| 304 new_tab_html_ = nullptr; | 291 new_tab_html_ = nullptr; |
| 305 new_tab_incognito_css_ = nullptr; | 292 new_tab_incognito_css_ = nullptr; |
| 306 // TODO(dbeam): Check if it is necessary to clear the CSS on promo changes. | |
| 307 new_tab_css_ = nullptr; | 293 new_tab_css_ = nullptr; |
| 308 } | 294 } |
| 309 | 295 |
| 310 void NTPResourceCache::CreateNewTabIncognitoHTML() { | 296 void NTPResourceCache::CreateNewTabIncognitoHTML() { |
| 311 base::DictionaryValue localized_strings; | 297 base::DictionaryValue localized_strings; |
| 312 localized_strings.SetString("title", | 298 localized_strings.SetString("title", |
| 313 l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE)); | 299 l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE)); |
| 314 int new_tab_description_ids = IDS_NEW_TAB_OTR_DESCRIPTION; | 300 int new_tab_description_ids = IDS_NEW_TAB_OTR_DESCRIPTION; |
| 315 int new_tab_heading_ids = IDS_NEW_TAB_OTR_HEADING; | 301 int new_tab_heading_ids = IDS_NEW_TAB_OTR_HEADING; |
| 316 int new_tab_link_ids = IDS_NEW_TAB_OTR_LEARN_MORE_LINK; | 302 int new_tab_link_ids = IDS_NEW_TAB_OTR_LEARN_MORE_LINK; |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 495 | 481 |
| 496 NewTabPageHandler::GetLocalizedValues(profile_, &load_time_data); | 482 NewTabPageHandler::GetLocalizedValues(profile_, &load_time_data); |
| 497 AppLauncherLoginHandler::GetLocalizedValues(profile_, &load_time_data); | 483 AppLauncherLoginHandler::GetLocalizedValues(profile_, &load_time_data); |
| 498 | 484 |
| 499 webui::SetLoadTimeDataDefaults(app_locale, &load_time_data); | 485 webui::SetLoadTimeDataDefaults(app_locale, &load_time_data); |
| 500 | 486 |
| 501 // Control fade and resize animations. | 487 // Control fade and resize animations. |
| 502 load_time_data.SetBoolean("anim", | 488 load_time_data.SetBoolean("anim", |
| 503 gfx::Animation::ShouldRenderRichAnimation()); | 489 gfx::Animation::ShouldRenderRichAnimation()); |
| 504 | 490 |
| 505 // Disable the promo if this is the first run, otherwise set the promo string | |
| 506 // for display if there is a valid outstanding promo. | |
| 507 if (first_run::IsChromeFirstRun()) { | |
| 508 web_resource::HandleNotificationPromoClosed( | |
| 509 web_resource::NotificationPromo::NTP_NOTIFICATION_PROMO); | |
| 510 } else { | |
| 511 web_resource::NotificationPromo notification_promo( | |
| 512 g_browser_process->local_state()); | |
| 513 notification_promo.InitFromPrefs( | |
| 514 web_resource::NotificationPromo::NTP_NOTIFICATION_PROMO); | |
| 515 if (notification_promo.CanShow()) { | |
| 516 load_time_data.SetString("notificationPromoText", | |
| 517 notification_promo.promo_text()); | |
| 518 DVLOG(1) << "Notification promo:" << notification_promo.promo_text(); | |
| 519 } | |
| 520 | |
| 521 web_resource::NotificationPromo bubble_promo( | |
| 522 g_browser_process->local_state()); | |
| 523 bubble_promo.InitFromPrefs( | |
| 524 web_resource::NotificationPromo::NTP_BUBBLE_PROMO); | |
| 525 if (bubble_promo.CanShow()) { | |
| 526 load_time_data.SetString("bubblePromoText", | |
| 527 bubble_promo.promo_text()); | |
| 528 DVLOG(1) << "Bubble promo:" << bubble_promo.promo_text(); | |
| 529 } | |
| 530 } | |
| 531 | |
| 532 load_time_data.SetBoolean( | 491 load_time_data.SetBoolean( |
| 533 "isUserSignedIn", | 492 "isUserSignedIn", |
| 534 SigninManagerFactory::GetForProfile(profile_)->IsAuthenticated()); | 493 SigninManagerFactory::GetForProfile(profile_)->IsAuthenticated()); |
| 535 | 494 |
| 536 // Load the new tab page appropriate for this build. | 495 // Load the new tab page appropriate for this build. |
| 537 base::StringPiece new_tab_html(ResourceBundle::GetSharedInstance(). | 496 base::StringPiece new_tab_html(ResourceBundle::GetSharedInstance(). |
| 538 GetRawDataResource(IDR_NEW_TAB_4_HTML)); | 497 GetRawDataResource(IDR_NEW_TAB_4_HTML)); |
| 539 std::string full_html = | 498 std::string full_html = |
| 540 webui::GetI18nTemplateHtml(new_tab_html, &load_time_data); | 499 webui::GetI18nTemplateHtml(new_tab_html, &load_time_data); |
| 541 new_tab_html_ = base::RefCountedString::TakeString(&full_html); | 500 new_tab_html_ = base::RefCountedString::TakeString(&full_html); |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 645 // Get our template. | 604 // Get our template. |
| 646 static const base::StringPiece new_tab_theme_css( | 605 static const base::StringPiece new_tab_theme_css( |
| 647 ResourceBundle::GetSharedInstance().GetRawDataResource( | 606 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 648 IDR_NEW_TAB_4_THEME_CSS)); | 607 IDR_NEW_TAB_4_THEME_CSS)); |
| 649 | 608 |
| 650 // Create the string from our template and the replacements. | 609 // Create the string from our template and the replacements. |
| 651 std::string css_string = | 610 std::string css_string = |
| 652 ui::ReplaceTemplateExpressions(new_tab_theme_css, substitutions); | 611 ui::ReplaceTemplateExpressions(new_tab_theme_css, substitutions); |
| 653 new_tab_css_ = base::RefCountedString::TakeString(&css_string); | 612 new_tab_css_ = base::RefCountedString::TakeString(&css_string); |
| 654 } | 613 } |
| OLD | NEW |