OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/dom_ui/ntp_resource_cache.h" | 5 #include "chrome/browser/dom_ui/ntp_resource_cache.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "app/animation.h" | 10 #include "app/animation.h" |
11 #include "app/l10n_util.h" | 11 #include "app/l10n_util.h" |
12 #include "app/resource_bundle.h" | 12 #include "app/resource_bundle.h" |
13 #include "app/theme_provider.h" | 13 #include "app/theme_provider.h" |
14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
15 #include "base/file_util.h" | 15 #include "base/file_util.h" |
16 #include "base/ref_counted_memory.h" | 16 #include "base/ref_counted_memory.h" |
17 #include "base/string16.h" | 17 #include "base/string16.h" |
18 #include "base/string_number_conversions.h" | 18 #include "base/string_number_conversions.h" |
19 #include "base/time.h" | 19 #include "base/time.h" |
20 #include "base/utf_string_conversions.h" | 20 #include "base/utf_string_conversions.h" |
21 #include "base/values.h" | 21 #include "base/values.h" |
22 #include "chrome/browser/browser_thread.h" | 22 #include "chrome/browser/browser_thread.h" |
23 #include "chrome/browser/dom_ui/chrome_url_data_manager.h" | 23 #include "chrome/browser/dom_ui/chrome_url_data_manager.h" |
24 #include "chrome/browser/dom_ui/shown_sections_handler.h" | 24 #include "chrome/browser/dom_ui/shown_sections_handler.h" |
25 #include "chrome/browser/google/google_util.h" | 25 #include "chrome/browser/google/google_util.h" |
26 #include "chrome/browser/prefs/pref_service.h" | 26 #include "chrome/browser/prefs/pref_service.h" |
27 #include "chrome/browser/profile.h" | 27 #include "chrome/browser/profile.h" |
28 #include "chrome/browser/themes/browser_theme_provider.h" | 28 #include "chrome/browser/themes/browser_theme_provider.h" |
| 29 #include "chrome/browser/web_resource/web_resource_service.h" |
29 #include "chrome/common/chrome_switches.h" | 30 #include "chrome/common/chrome_switches.h" |
30 #include "chrome/common/extensions/extension.h" | 31 #include "chrome/common/extensions/extension.h" |
31 #include "chrome/common/extensions/extension_constants.h" | 32 #include "chrome/common/extensions/extension_constants.h" |
32 #include "chrome/common/jstemplate_builder.h" | 33 #include "chrome/common/jstemplate_builder.h" |
33 #include "chrome/common/notification_service.h" | 34 #include "chrome/common/notification_service.h" |
34 #include "chrome/common/notification_type.h" | 35 #include "chrome/common/notification_type.h" |
35 #include "chrome/common/pref_names.h" | 36 #include "chrome/common/pref_names.h" |
36 #include "chrome/common/url_constants.h" | 37 #include "chrome/common/url_constants.h" |
37 #include "gfx/color_utils.h" | 38 #include "gfx/color_utils.h" |
38 #include "grit/browser_resources.h" | 39 #include "grit/browser_resources.h" |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 | 129 |
129 // How the background image on the new tab page should be tiled (see tiling | 130 // How the background image on the new tab page should be tiled (see tiling |
130 // masks in browser_theme_provider.h). | 131 // masks in browser_theme_provider.h). |
131 std::string GetNewTabBackgroundTilingCSS(const ThemeProvider* theme_provider) { | 132 std::string GetNewTabBackgroundTilingCSS(const ThemeProvider* theme_provider) { |
132 int repeat_mode; | 133 int repeat_mode; |
133 theme_provider->GetDisplayProperty( | 134 theme_provider->GetDisplayProperty( |
134 BrowserThemeProvider::NTP_BACKGROUND_TILING, &repeat_mode); | 135 BrowserThemeProvider::NTP_BACKGROUND_TILING, &repeat_mode); |
135 return BrowserThemeProvider::TilingToString(repeat_mode); | 136 return BrowserThemeProvider::TilingToString(repeat_mode); |
136 } | 137 } |
137 | 138 |
| 139 // Is the current time within a given date range? |
| 140 bool InDateRange(double begin, double end) { |
| 141 Time start_time = Time::FromDoubleT(begin); |
| 142 Time end_time = Time::FromDoubleT(end); |
| 143 return start_time < Time::Now() && end_time > Time::Now(); |
| 144 } |
| 145 |
138 } // namespace | 146 } // namespace |
139 | 147 |
140 NTPResourceCache::NTPResourceCache(Profile* profile) : profile_(profile) { | 148 NTPResourceCache::NTPResourceCache(Profile* profile) : profile_(profile) { |
141 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED, | 149 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED, |
142 NotificationService::AllSources()); | 150 NotificationService::AllSources()); |
143 registrar_.Add(this, NotificationType::WEB_RESOURCE_AVAILABLE, | 151 registrar_.Add(this, NotificationType::WEB_RESOURCE_STATE_CHANGED, |
144 NotificationService::AllSources()); | 152 NotificationService::AllSources()); |
145 | 153 |
146 // Watch for pref changes that cause us to need to invalidate the HTML cache. | 154 // Watch for pref changes that cause us to need to invalidate the HTML cache. |
147 pref_change_registrar_.Init(profile_->GetPrefs()); | 155 pref_change_registrar_.Init(profile_->GetPrefs()); |
148 pref_change_registrar_.Add(prefs::kShowBookmarkBar, this); | 156 pref_change_registrar_.Add(prefs::kShowBookmarkBar, this); |
149 pref_change_registrar_.Add(prefs::kNTPShownSections, this); | 157 pref_change_registrar_.Add(prefs::kNTPShownSections, this); |
150 } | 158 } |
151 | 159 |
152 NTPResourceCache::~NTPResourceCache() {} | 160 NTPResourceCache::~NTPResourceCache() {} |
153 | 161 |
(...skipping 20 matching lines...) Expand all Loading... |
174 CreateNewTabCSS(); | 182 CreateNewTabCSS(); |
175 } | 183 } |
176 return is_off_the_record ? new_tab_incognito_css_.get() | 184 return is_off_the_record ? new_tab_incognito_css_.get() |
177 : new_tab_css_.get(); | 185 : new_tab_css_.get(); |
178 } | 186 } |
179 | 187 |
180 void NTPResourceCache::Observe(NotificationType type, | 188 void NTPResourceCache::Observe(NotificationType type, |
181 const NotificationSource& source, const NotificationDetails& details) { | 189 const NotificationSource& source, const NotificationDetails& details) { |
182 // Invalidate the cache. | 190 // Invalidate the cache. |
183 if (NotificationType::BROWSER_THEME_CHANGED == type || | 191 if (NotificationType::BROWSER_THEME_CHANGED == type || |
184 NotificationType::WEB_RESOURCE_AVAILABLE == type) { | 192 NotificationType::WEB_RESOURCE_STATE_CHANGED == type) { |
185 new_tab_incognito_html_ = NULL; | 193 new_tab_incognito_html_ = NULL; |
186 new_tab_html_ = NULL; | 194 new_tab_html_ = NULL; |
187 new_tab_incognito_css_ = NULL; | 195 new_tab_incognito_css_ = NULL; |
188 new_tab_css_ = NULL; | 196 new_tab_css_ = NULL; |
189 } else if (NotificationType::PREF_CHANGED == type) { | 197 } else if (NotificationType::PREF_CHANGED == type) { |
190 std::string* pref_name = Details<std::string>(details).ptr(); | 198 std::string* pref_name = Details<std::string>(details).ptr(); |
191 if (*pref_name == prefs::kShowBookmarkBar || | 199 if (*pref_name == prefs::kShowBookmarkBar || |
192 *pref_name == prefs::kHomePageIsNewTabPage || | 200 *pref_name == prefs::kHomePageIsNewTabPage || |
193 *pref_name == prefs::kNTPShownSections) { | 201 *pref_name == prefs::kNTPShownSections) { |
194 new_tab_incognito_html_ = NULL; | 202 new_tab_incognito_html_ = NULL; |
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
343 | 351 |
344 // Pass the shown_sections pref early so that we can prevent flicker. | 352 // Pass the shown_sections pref early so that we can prevent flicker. |
345 const int shown_sections = ShownSectionsHandler::GetShownSections( | 353 const int shown_sections = ShownSectionsHandler::GetShownSections( |
346 profile_->GetPrefs()); | 354 profile_->GetPrefs()); |
347 localized_strings.SetInteger("shown_sections", shown_sections); | 355 localized_strings.SetInteger("shown_sections", shown_sections); |
348 | 356 |
349 // If the user has preferences for a start and end time for a custom logo, | 357 // If the user has preferences for a start and end time for a custom logo, |
350 // and the time now is between these two times, show the custom logo. | 358 // and the time now is between these two times, show the custom logo. |
351 if (profile_->GetPrefs()->FindPreference(prefs::kNTPCustomLogoStart) && | 359 if (profile_->GetPrefs()->FindPreference(prefs::kNTPCustomLogoStart) && |
352 profile_->GetPrefs()->FindPreference(prefs::kNTPCustomLogoEnd)) { | 360 profile_->GetPrefs()->FindPreference(prefs::kNTPCustomLogoEnd)) { |
353 Time start_time = Time::FromDoubleT( | |
354 profile_->GetPrefs()->GetReal(prefs::kNTPCustomLogoStart)); | |
355 Time end_time = Time::FromDoubleT( | |
356 profile_->GetPrefs()->GetReal(prefs::kNTPCustomLogoEnd)); | |
357 localized_strings.SetString("customlogo", | 361 localized_strings.SetString("customlogo", |
358 (start_time < Time::Now() && end_time > Time::Now()) ? | 362 InDateRange(profile_->GetPrefs()->GetReal(prefs::kNTPCustomLogoStart), |
| 363 profile_->GetPrefs()->GetReal(prefs::kNTPCustomLogoEnd)) ? |
359 "true" : "false"); | 364 "true" : "false"); |
360 } else { | 365 } else { |
361 localized_strings.SetString("customlogo", "false"); | 366 localized_strings.SetString("customlogo", "false"); |
362 } | 367 } |
363 | 368 |
| 369 // If the user has preferences for a start and end time for a promo from |
| 370 // the server, and this promo string exists, set the localized string. |
| 371 if (profile_->GetPrefs()->FindPreference(prefs::kNTPPromoStart) && |
| 372 profile_->GetPrefs()->FindPreference(prefs::kNTPPromoEnd) && |
| 373 profile_->GetPrefs()->FindPreference(prefs::kNTPPromoLine) && |
| 374 WebResourceServiceUtil::CanShowPromo(profile_)) { |
| 375 localized_strings.SetString("serverpromo", |
| 376 InDateRange(profile_->GetPrefs()->GetReal(prefs::kNTPPromoStart), |
| 377 profile_->GetPrefs()->GetReal(prefs::kNTPPromoEnd)) ? |
| 378 profile_->GetPrefs()->GetString(prefs::kNTPPromoLine) : |
| 379 std::string()); |
| 380 } |
| 381 |
364 base::StringPiece new_tab_html(ResourceBundle::GetSharedInstance(). | 382 base::StringPiece new_tab_html(ResourceBundle::GetSharedInstance(). |
365 GetRawDataResource(IDR_NEW_NEW_TAB_HTML)); | 383 GetRawDataResource(IDR_NEW_NEW_TAB_HTML)); |
366 | 384 |
367 // Inject the template data into the HTML so that it is available before any | 385 // Inject the template data into the HTML so that it is available before any |
368 // layout is needed. | 386 // layout is needed. |
369 std::string json_html; | 387 std::string json_html; |
370 jstemplate_builder::AppendJsonHtml(&localized_strings, &json_html); | 388 jstemplate_builder::AppendJsonHtml(&localized_strings, &json_html); |
371 | 389 |
372 static const base::StringPiece template_data_placeholder( | 390 static const base::StringPiece template_data_placeholder( |
373 "<!-- template data placeholder -->"); | 391 "<!-- template data placeholder -->"); |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
523 std::string css_string; | 541 std::string css_string; |
524 css_string = ReplaceStringPlaceholders(new_tab_theme_css, subst, NULL); | 542 css_string = ReplaceStringPlaceholders(new_tab_theme_css, subst, NULL); |
525 css_string = ReplaceStringPlaceholders(css_string, subst2, NULL); | 543 css_string = ReplaceStringPlaceholders(css_string, subst2, NULL); |
526 css_string = ReplaceStringPlaceholders(css_string, subst3, NULL); | 544 css_string = ReplaceStringPlaceholders(css_string, subst3, NULL); |
527 | 545 |
528 new_tab_css_ = new RefCountedBytes; | 546 new_tab_css_ = new RefCountedBytes; |
529 new_tab_css_->data.resize(css_string.size()); | 547 new_tab_css_->data.resize(css_string.size()); |
530 std::copy(css_string.begin(), css_string.end(), | 548 std::copy(css_string.begin(), css_string.end(), |
531 new_tab_css_->data.begin()); | 549 new_tab_css_->data.begin()); |
532 } | 550 } |
OLD | NEW |