Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(532)

Side by Side Diff: chrome/browser/ui/webui/ntp/ntp_resource_cache.cc

Issue 7481023: Adding notifications for new sync types. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Now stores acknowledged types in a ListValue, not a StringValue. Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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
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
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_) &&
412 localized_strings.SetString("serverpromo", 410 InDateRange(profile_->GetPrefs()->GetDouble(prefs::kNTPPromoStart),
413 InDateRange(profile_->GetPrefs()->GetDouble(prefs::kNTPPromoStart), 411 profile_->GetPrefs()->GetDouble(prefs::kNTPPromoEnd))) {
414 profile_->GetPrefs()->GetDouble(prefs::kNTPPromoEnd)) ? 412 localized_strings.SetString(
415 profile_->GetPrefs()->GetString(prefs::kNTPPromoLine) : 413 "serverpromo",
416 std::string()); 414 profile_->GetPrefs()->GetString(prefs::kNTPPromoLine));
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 localized_strings.SetString("syncNotification", GetSyncTypeMessage());
418 } 422 }
419 423
420 // Load the new tab page appropriate for this build 424 // Load the new tab page appropriate for this build
421 // Note that some builds (eg. TOUCHUI) don't make use of everything we 425 // 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 426 // 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 427 // consistent across builds, supporting the union of all NTP front-ends
424 // for simplicity. 428 // for simplicity.
425 std::string full_html; 429 std::string full_html;
426 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kNewTabPage4)) { 430 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kNewTabPage4)) {
427 base::StringPiece new_tab_html(ResourceBundle::GetSharedInstance(). 431 base::StringPiece new_tab_html(ResourceBundle::GetSharedInstance().
(...skipping 21 matching lines...) Expand all
449 new_tab_html.size() - after_offset); 453 new_tab_html.size() - after_offset);
450 } else { 454 } else {
451 NOTREACHED(); 455 NOTREACHED();
452 full_html.assign(new_tab_html.data(), new_tab_html.size()); 456 full_html.assign(new_tab_html.data(), new_tab_html.size());
453 } 457 }
454 } 458 }
455 459
456 new_tab_html_ = base::RefCountedString::TakeString(&full_html); 460 new_tab_html_ = base::RefCountedString::TakeString(&full_html);
457 } 461 }
458 462
463 string16 NTPResourceCache::GetSyncTypeMessage() {
464 if (profile_->HasProfileSyncService()) {
465 syncable::ModelTypeBitSet unacknowledged =
466 profile_->GetProfileSyncService()->GetUnacknowledgedTypes();
467
468 // TODO(sync): As we add new data types, we'll probably need some more
469 // generic string to display to the user, since the method below won't
470 // scale indefinitely (we'd need N*(N+1)/2 different strings to represent
471 // all the combinations of unacknowledged types). But for now, we just
472 // have sessions and typed urls so this is OK.
473 if (unacknowledged.test(syncable::SESSIONS) &&
474 unacknowledged.test(syncable::TYPED_URLS)) {
475 return l10n_util::GetStringUTF16(IDS_SYNC_ADDED_SESSIONS_AND_TYPED_URLS);
476 } else if (unacknowledged.test(syncable::SESSIONS)) {
477 return l10n_util::GetStringUTF16(IDS_SYNC_ADDED_SESSIONS);
478 } else if (unacknowledged.test(syncable::TYPED_URLS)) {
479 return l10n_util::GetStringUTF16(IDS_SYNC_ADDED_TYPED_URLS);
480 } else {
481 // Shouldn't be possible for any other types to be unacknowledged.
482 CHECK(!unacknowledged.any());
483 }
484 }
485 return string16();
486 }
487
459 void NTPResourceCache::CreateNewTabIncognitoCSS() { 488 void NTPResourceCache::CreateNewTabIncognitoCSS() {
460 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_); 489 ui::ThemeProvider* tp = ThemeServiceFactory::GetForProfile(profile_);
461 DCHECK(tp); 490 DCHECK(tp);
462 491
463 // Get our theme colors 492 // Get our theme colors
464 SkColor color_background = 493 SkColor color_background =
465 tp->GetColor(ThemeService::COLOR_NTP_BACKGROUND); 494 tp->GetColor(ThemeService::COLOR_NTP_BACKGROUND);
466 495
467 // Generate the replacements. 496 // Generate the replacements.
468 std::vector<std::string> subst; 497 std::vector<std::string> subst;
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
577 IDR_NEW_TAB_4_THEME_CSS : IDR_NEW_TAB_THEME_CSS; 606 IDR_NEW_TAB_4_THEME_CSS : IDR_NEW_TAB_THEME_CSS;
578 static const base::StringPiece new_tab_theme_css( 607 static const base::StringPiece new_tab_theme_css(
579 ResourceBundle::GetSharedInstance().GetRawDataResource( 608 ResourceBundle::GetSharedInstance().GetRawDataResource(
580 ntp_css_resource_id)); 609 ntp_css_resource_id));
581 610
582 // Create the string from our template and the replacements. 611 // Create the string from our template and the replacements.
583 std::string css_string; 612 std::string css_string;
584 css_string = ReplaceStringPlaceholders(new_tab_theme_css, subst, NULL); 613 css_string = ReplaceStringPlaceholders(new_tab_theme_css, subst, NULL);
585 new_tab_css_ = base::RefCountedString::TakeString(&css_string); 614 new_tab_css_ = base::RefCountedString::TakeString(&css_string);
586 } 615 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698