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/shown_sections_handler.h" | 28 #include "chrome/browser/ui/webui/ntp/shown_sections_handler.h" |
28 #include "chrome/browser/ui/webui/sync_setup_handler.h" | 29 #include "chrome/browser/ui/webui/sync_setup_handler.h" |
29 #include "chrome/browser/web_resource/promo_resource_service.h" | 30 #include "chrome/browser/web_resource/promo_resource_service.h" |
30 #include "chrome/common/chrome_notification_types.h" | 31 #include "chrome/common/chrome_notification_types.h" |
31 #include "chrome/common/chrome_switches.h" | 32 #include "chrome/common/chrome_switches.h" |
32 #include "chrome/common/extensions/extension.h" | 33 #include "chrome/common/extensions/extension.h" |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
167 | 168 |
168 NTPResourceCache::NTPResourceCache(Profile* profile) : profile_(profile) { | 169 NTPResourceCache::NTPResourceCache(Profile* profile) : profile_(profile) { |
169 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, | 170 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, |
170 Source<ThemeService>( | 171 Source<ThemeService>( |
171 ThemeServiceFactory::GetForProfile(profile))); | 172 ThemeServiceFactory::GetForProfile(profile))); |
172 registrar_.Add(this, chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, | 173 registrar_.Add(this, chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, |
173 NotificationService::AllSources()); | 174 NotificationService::AllSources()); |
174 | 175 |
175 // Watch for pref changes that cause us to need to invalidate the HTML cache. | 176 // Watch for pref changes that cause us to need to invalidate the HTML cache. |
176 pref_change_registrar_.Init(profile_->GetPrefs()); | 177 pref_change_registrar_.Init(profile_->GetPrefs()); |
178 pref_change_registrar_.Add(prefs::kAcknowledgedSyncTypes, this); | |
177 pref_change_registrar_.Add(prefs::kShowBookmarkBar, this); | 179 pref_change_registrar_.Add(prefs::kShowBookmarkBar, this); |
178 pref_change_registrar_.Add(prefs::kEnableBookmarkBar, this); | 180 pref_change_registrar_.Add(prefs::kEnableBookmarkBar, this); |
181 pref_change_registrar_.Add(prefs::kHomePageIsNewTabPage, this); | |
179 pref_change_registrar_.Add(prefs::kNTPShownSections, this); | 182 pref_change_registrar_.Add(prefs::kNTPShownSections, this); |
180 pref_change_registrar_.Add(prefs::kNTPShownPage, this); | 183 pref_change_registrar_.Add(prefs::kNTPShownPage, this); |
181 } | 184 } |
182 | 185 |
183 NTPResourceCache::~NTPResourceCache() {} | 186 NTPResourceCache::~NTPResourceCache() {} |
184 | 187 |
185 RefCountedMemory* NTPResourceCache::GetNewTabHTML(bool is_incognito) { | 188 RefCountedMemory* NTPResourceCache::GetNewTabHTML(bool is_incognito) { |
186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
187 if (is_incognito) { | 190 if (is_incognito) { |
188 if (!new_tab_incognito_html_.get()) | 191 if (!new_tab_incognito_html_.get()) |
(...skipping 22 matching lines...) Expand all Loading... | |
211 void NTPResourceCache::Observe(int type, | 214 void NTPResourceCache::Observe(int type, |
212 const NotificationSource& source, const NotificationDetails& details) { | 215 const NotificationSource& source, const NotificationDetails& details) { |
213 // Invalidate the cache. | 216 // Invalidate the cache. |
214 if (chrome::NOTIFICATION_BROWSER_THEME_CHANGED == type || | 217 if (chrome::NOTIFICATION_BROWSER_THEME_CHANGED == type || |
215 chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED == type) { | 218 chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED == type) { |
216 new_tab_incognito_html_ = NULL; | 219 new_tab_incognito_html_ = NULL; |
217 new_tab_html_ = NULL; | 220 new_tab_html_ = NULL; |
218 new_tab_incognito_css_ = NULL; | 221 new_tab_incognito_css_ = NULL; |
219 new_tab_css_ = NULL; | 222 new_tab_css_ = NULL; |
220 } else if (chrome::NOTIFICATION_PREF_CHANGED == type) { | 223 } else if (chrome::NOTIFICATION_PREF_CHANGED == type) { |
221 std::string* pref_name = Details<std::string>(details).ptr(); | 224 // A change occurred to one of the preferences we care about, so flush the |
222 if (*pref_name == prefs::kShowBookmarkBar || | 225 // cache. |
223 *pref_name == prefs::kEnableBookmarkBar || | 226 new_tab_incognito_html_ = NULL; |
224 *pref_name == prefs::kHomePageIsNewTabPage || | 227 new_tab_html_ = NULL; |
225 *pref_name == prefs::kNTPShownSections || | |
226 *pref_name == prefs::kNTPShownPage) { | |
227 new_tab_incognito_html_ = NULL; | |
228 new_tab_html_ = NULL; | |
229 } else { | |
230 NOTREACHED(); | |
231 } | |
232 } else { | 228 } else { |
233 NOTREACHED(); | 229 NOTREACHED(); |
234 } | 230 } |
235 } | 231 } |
236 | 232 |
237 void NTPResourceCache::CreateNewTabIncognitoHTML() { | 233 void NTPResourceCache::CreateNewTabIncognitoHTML() { |
238 DictionaryValue localized_strings; | 234 DictionaryValue localized_strings; |
239 localized_strings.SetString("title", | 235 localized_strings.SetString("title", |
240 l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE)); | 236 l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE)); |
241 int new_tab_message_ids = IDS_NEW_TAB_OTR_MESSAGE; | 237 int new_tab_message_ids = IDS_NEW_TAB_OTR_MESSAGE; |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
352 localized_strings.SetString("applaunchtypewindow", | 348 localized_strings.SetString("applaunchtypewindow", |
353 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_WINDOW)); | 349 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_WINDOW)); |
354 localized_strings.SetString("applaunchtypefullscreen", | 350 localized_strings.SetString("applaunchtypefullscreen", |
355 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_FULLSCREEN)); | 351 l10n_util::GetStringUTF16(IDS_APP_CONTEXT_MENU_OPEN_FULLSCREEN)); |
356 localized_strings.SetString("web_store_title", | 352 localized_strings.SetString("web_store_title", |
357 l10n_util::GetStringUTF16(IDS_EXTENSION_WEB_STORE_TITLE)); | 353 l10n_util::GetStringUTF16(IDS_EXTENSION_WEB_STORE_TITLE)); |
358 localized_strings.SetString("web_store_url", | 354 localized_strings.SetString("web_store_url", |
359 GetUrlWithLang(GURL(Extension::ChromeStoreLaunchURL()))); | 355 GetUrlWithLang(GURL(Extension::ChromeStoreLaunchURL()))); |
360 localized_strings.SetString("syncpromotext", | 356 localized_strings.SetString("syncpromotext", |
361 l10n_util::GetStringUTF16(IDS_SYNC_START_SYNC_BUTTON_LABEL)); | 357 l10n_util::GetStringUTF16(IDS_SYNC_START_SYNC_BUTTON_LABEL)); |
358 localized_strings.SetString("synclinktext", | |
359 l10n_util::GetStringUTF16(IDS_SYNC_ADVANCED_OPTIONS)); | |
362 localized_strings.SetString("trashLabel", | 360 localized_strings.SetString("trashLabel", |
363 l10n_util::GetStringFUTF16( | 361 l10n_util::GetStringFUTF16( |
364 IDS_NEW_TAB_TRASH_LABEL, | 362 IDS_NEW_TAB_TRASH_LABEL, |
365 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME))); | 363 l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME))); |
366 #if defined(OS_CHROMEOS) | 364 #if defined(OS_CHROMEOS) |
367 localized_strings.SetString("expandMenu", | 365 localized_strings.SetString("expandMenu", |
368 l10n_util::GetStringUTF16(IDS_NEW_TAB_CLOSE_MENU_EXPAND)); | 366 l10n_util::GetStringUTF16(IDS_NEW_TAB_CLOSE_MENU_EXPAND)); |
369 #endif | 367 #endif |
370 | 368 |
371 NewTabPageHandler::GetLocalizedValues(profile_, &localized_strings); | 369 NewTabPageHandler::GetLocalizedValues(profile_, &localized_strings); |
(...skipping 29 matching lines...) Expand all Loading... | |
401 "true" : "false"); | 399 "true" : "false"); |
402 } else { | 400 } else { |
403 localized_strings.SetString("customlogo", "false"); | 401 localized_strings.SetString("customlogo", "false"); |
404 } | 402 } |
405 | 403 |
406 // If the user has preferences for a start and end time for a promo from | 404 // If the user has preferences for a start and end time for a promo from |
407 // the server, and this promo string exists, set the localized string. | 405 // the server, and this promo string exists, set the localized string. |
408 if (profile_->GetPrefs()->FindPreference(prefs::kNTPPromoStart) && | 406 if (profile_->GetPrefs()->FindPreference(prefs::kNTPPromoStart) && |
409 profile_->GetPrefs()->FindPreference(prefs::kNTPPromoEnd) && | 407 profile_->GetPrefs()->FindPreference(prefs::kNTPPromoEnd) && |
410 profile_->GetPrefs()->FindPreference(prefs::kNTPPromoLine) && | 408 profile_->GetPrefs()->FindPreference(prefs::kNTPPromoLine) && |
411 PromoResourceServiceUtil::CanShowPromo(profile_)) { | 409 PromoResourceServiceUtil::CanShowPromo(profile_) && |
410 InDateRange(profile_->GetPrefs()->GetDouble(prefs::kNTPPromoStart), | |
411 profile_->GetPrefs()->GetDouble(prefs::kNTPPromoEnd))) { | |
412 localized_strings.SetString("serverpromo", | 412 localized_strings.SetString("serverpromo", |
413 InDateRange(profile_->GetPrefs()->GetDouble(prefs::kNTPPromoStart), | 413 profile_->GetPrefs()->GetString( |
Evan Stade
2011/08/02 00:33:04
the indentation here is inconsistent with the way
Andrew T Wilson (Slow)
2011/08/02 22:07:42
Done.
| |
414 profile_->GetPrefs()->GetDouble(prefs::kNTPPromoEnd)) ? | 414 prefs::kNTPPromoLine)); |
415 profile_->GetPrefs()->GetString(prefs::kNTPPromoLine) : | |
416 std::string()); | |
417 UserMetrics::RecordAction(UserMetricsAction("NTPPromoShown")); | 415 UserMetrics::RecordAction(UserMetricsAction("NTPPromoShown")); |
416 } else { | |
417 // Not showing a server-provided promo, so check if we need to show a | |
418 // notification from the sync service instead (don't want to show both). | |
419 // If no notification is desired, the "syncnotification" attribute is set | |
420 // to the empty string. | |
421 if (profile_->HasProfileSyncService()) { | |
422 localized_strings.SetString("syncnotification", | |
423 profile_->GetProfileSyncService()->GetSyncNotification()); | |
424 } | |
418 } | 425 } |
419 | 426 |
420 // Load the new tab page appropriate for this build | 427 // Load the new tab page appropriate for this build |
421 // Note that some builds (eg. TOUCHUI) don't make use of everything we | 428 // Note that some builds (eg. TOUCHUI) don't make use of everything we |
422 // do here (all of the template data, etc.), but we keep the back end | 429 // do here (all of the template data, etc.), but we keep the back end |
423 // consistent across builds, supporting the union of all NTP front-ends | 430 // consistent across builds, supporting the union of all NTP front-ends |
424 // for simplicity. | 431 // for simplicity. |
425 std::string full_html; | 432 std::string full_html; |
426 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kNewTabPage4)) { | 433 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kNewTabPage4)) { |
427 base::StringPiece new_tab_html(ResourceBundle::GetSharedInstance(). | 434 base::StringPiece new_tab_html(ResourceBundle::GetSharedInstance(). |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
577 IDR_NEW_TAB_4_THEME_CSS : IDR_NEW_TAB_THEME_CSS; | 584 IDR_NEW_TAB_4_THEME_CSS : IDR_NEW_TAB_THEME_CSS; |
578 static const base::StringPiece new_tab_theme_css( | 585 static const base::StringPiece new_tab_theme_css( |
579 ResourceBundle::GetSharedInstance().GetRawDataResource( | 586 ResourceBundle::GetSharedInstance().GetRawDataResource( |
580 ntp_css_resource_id)); | 587 ntp_css_resource_id)); |
581 | 588 |
582 // Create the string from our template and the replacements. | 589 // Create the string from our template and the replacements. |
583 std::string css_string; | 590 std::string css_string; |
584 css_string = ReplaceStringPlaceholders(new_tab_theme_css, subst, NULL); | 591 css_string = ReplaceStringPlaceholders(new_tab_theme_css, subst, NULL); |
585 new_tab_css_ = base::RefCountedString::TakeString(&css_string); | 592 new_tab_css_ = base::RefCountedString::TakeString(&css_string); |
586 } | 593 } |
OLD | NEW |