diff options
author | cmp@chromium.org <cmp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-04 20:42:53 +0000 |
---|---|---|
committer | cmp@chromium.org <cmp@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-04 20:42:53 +0000 |
commit | 8fda0ab08a219c0d96e3d51d26479d10f386ee30 (patch) | |
tree | fc16acfbfebd0795629f7e4c87e0500258566cfc /chrome | |
parent | 38b8971cd5d5a9481d571d7bfca44a06566111d5 (diff) | |
download | chromium_src-8fda0ab08a219c0d96e3d51d26479d10f386ee30.zip chromium_src-8fda0ab08a219c0d96e3d51d26479d10f386ee30.tar.gz chromium_src-8fda0ab08a219c0d96e3d51d26479d10f386ee30.tar.bz2 |
Revert 80318 - Detach the PromoResourceService and WebResourceService from the Profile; have them store web resource data in local state instead.
We don't need multiple services for different Profiles; instead, there is a single service that captures data in local_state. The only preference stored in the Profile about the promo will be a bool indicating whether it's been closed or not, which will now be set outside the service.
Reviewers:
arv for general changes to PromoResourceService, & unittest.
erg for pulling the service out of the Profile and serving it up as a Singleton.
zmo for the changes I made to gpu_data_manager.
jstritar FYI, for changes to the PromoResourceService.
BUG=77155
TEST=promo resource service unittests.
Review URL: http://codereview.chromium.org/6736028
TBR=mirandac@chromium.org
Review URL: http://codereview.chromium.org/6677140
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@80369 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
19 files changed, 213 insertions, 292 deletions
diff --git a/chrome/browser/browser_main.cc b/chrome/browser/browser_main.cc index 737f01b..74253dd 100644 --- a/chrome/browser/browser_main.cc +++ b/chrome/browser/browser_main.cc @@ -74,7 +74,6 @@ #include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser_init.h" #include "chrome/browser/ui/webui/chrome_url_data_manager_backend.h" -#include "chrome/browser/web_resource/promo_resource_service_factory.h" #include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_paths.h" #include "chrome/common/chrome_switches.h" @@ -1774,16 +1773,6 @@ int BrowserMain(const MainFunctionParams& parameters) { GpuDataManager* gpu_data_manager = GpuDataManager::GetInstance(); DCHECK(gpu_data_manager); - // Need to initialize PromoResourceServiceFactory to load any currently - // available promo data into local state. Must be initialized after - // ResourceBundle::GetSharedInstance. - if (!parsed_command_line.HasSwitch(switches::kDisableWebResources)) { - PromoResourceServiceFactory* promo_resource_service_factory = - PromoResourceServiceFactory::GetInstance(); - DCHECK(promo_resource_service_factory); - promo_resource_service_factory->StartPromoResourceService(); - } - // Start watching all browser threads for responsiveness. ThreadWatcherList::StartWatchingAll(); diff --git a/chrome/browser/profiles/profile.h b/chrome/browser/profiles/profile.h index 47a17bb..e7d1741 100644 --- a/chrome/browser/profiles/profile.h +++ b/chrome/browser/profiles/profile.h @@ -470,6 +470,9 @@ class Profile { virtual void InitExtensions() = 0; + // Start up service that gathers data from a promo resource feed. + virtual void InitPromoResources() = 0; + // Register URLRequestFactories for protocols registered with // registerProtocolHandler. virtual void InitRegisteredProtocolHandlers() = 0; diff --git a/chrome/browser/profiles/profile_impl.cc b/chrome/browser/profiles/profile_impl.cc index aee7b6c..c25f7cf 100644 --- a/chrome/browser/profiles/profile_impl.cc +++ b/chrome/browser/profiles/profile_impl.cc @@ -76,6 +76,7 @@ #include "chrome/browser/user_style_sheet_watcher.h" #include "chrome/browser/visitedlink/visitedlink_event_listener.h" #include "chrome/browser/visitedlink/visitedlink_master.h" +#include "chrome/browser/web_resource/promo_resource_service.h" #include "chrome/browser/webdata/web_data_service.h" #include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_paths.h" @@ -493,6 +494,14 @@ void ProfileImpl::InstallDefaultApps() { } } +void ProfileImpl::InitPromoResources() { + if (promo_resource_service_) + return; + + promo_resource_service_ = new PromoResourceService(this); + promo_resource_service_->StartAfterDelay(); +} + void ProfileImpl::InitRegisteredProtocolHandlers() { if (protocol_handler_registry_) return; diff --git a/chrome/browser/profiles/profile_impl.h b/chrome/browser/profiles/profile_impl.h index f03e750..e25079e 100644 --- a/chrome/browser/profiles/profile_impl.h +++ b/chrome/browser/profiles/profile_impl.h @@ -116,6 +116,7 @@ class ProfileImpl : public Profile, virtual StatusTray* GetStatusTray(); virtual void MarkAsCleanShutdown(); virtual void InitExtensions(); + virtual void InitPromoResources(); virtual void InitRegisteredProtocolHandlers(); virtual NTPResourceCache* GetNTPResourceCache(); virtual FilePath last_selected_directory(); @@ -212,6 +213,7 @@ class ProfileImpl : public Profile, scoped_ptr<TemplateURLFetcher> template_url_fetcher_; scoped_ptr<TemplateURLModel> template_url_model_; scoped_ptr<BookmarkModel> bookmark_bar_model_; + scoped_refptr<PromoResourceService> promo_resource_service_; scoped_refptr<ProtocolHandlerRegistry> protocol_handler_registry_; scoped_ptr<NTPResourceCache> ntp_resource_cache_; diff --git a/chrome/browser/profiles/profile_manager.cc b/chrome/browser/profiles/profile_manager.cc index 0ce74e4..b893b25 100644 --- a/chrome/browser/profiles/profile_manager.cc +++ b/chrome/browser/profiles/profile_manager.cc @@ -237,6 +237,9 @@ bool ProfileManager::AddProfile(Profile* profile, bool init_extensions) { profiles_.insert(profiles_.end(), profile); if (init_extensions) profile->InitExtensions(); + const CommandLine& command_line = *CommandLine::ForCurrentProcess(); + if (!command_line.HasSwitch(switches::kDisableWebResources)) + profile->InitPromoResources(); return true; } diff --git a/chrome/browser/ui/browser.cc b/chrome/browser/ui/browser.cc index 173700e..35bd471 100644 --- a/chrome/browser/ui/browser.cc +++ b/chrome/browser/ui/browser.cc @@ -2079,7 +2079,6 @@ void Browser::RegisterUserPrefs(PrefService* prefs) { prefs->RegisterBooleanPref(prefs::kDeleteFormData, false); prefs->RegisterIntegerPref(prefs::kDeleteTimePeriod, 0); prefs->RegisterBooleanPref(prefs::kCheckDefaultBrowser, true); - prefs->RegisterBooleanPref(prefs::kNTPPromoClosed, false); prefs->RegisterBooleanPref(prefs::kShowOmniboxSearchHint, true); prefs->RegisterBooleanPref(prefs::kWebAppCreateOnDesktop, true); prefs->RegisterBooleanPref(prefs::kWebAppCreateInAppsMenu, true); diff --git a/chrome/browser/ui/webui/new_tab_ui.cc b/chrome/browser/ui/webui/new_tab_ui.cc index 4bd9100..1f0604d 100644 --- a/chrome/browser/ui/webui/new_tab_ui.cc +++ b/chrome/browser/ui/webui/new_tab_ui.cc @@ -275,7 +275,7 @@ void NewTabPageClosePromoHandler::HandleClosePromo( const ListValue* args) { web_ui_->GetProfile()->GetPrefs()->SetBoolean(prefs::kNTPPromoClosed, true); NotificationService* service = NotificationService::current(); - service->Notify(NotificationType::PROMO_CLOSED, + service->Notify(NotificationType::PROMO_RESOURCE_STATE_CHANGED, Source<NewTabPageClosePromoHandler>(this), NotificationService::NoDetails()); } diff --git a/chrome/browser/ui/webui/ntp_resource_cache.cc b/chrome/browser/ui/webui/ntp_resource_cache.cc index 2cfbab4..438cb64 100644 --- a/chrome/browser/ui/webui/ntp_resource_cache.cc +++ b/chrome/browser/ui/webui/ntp_resource_cache.cc @@ -15,7 +15,6 @@ #include "base/time.h" #include "base/utf_string_conversions.h" #include "base/values.h" -#include "chrome/browser/browser_process.h" #include "chrome/browser/google/google_util.h" #include "chrome/browser/metrics/user_metrics.h" #include "chrome/browser/prefs/pref_service.h" @@ -154,8 +153,6 @@ NTPResourceCache::NTPResourceCache(Profile* profile) : profile_(profile) { NotificationService::AllSources()); registrar_.Add(this, NotificationType::PROMO_RESOURCE_STATE_CHANGED, NotificationService::AllSources()); - registrar_.Add(this, NotificationType::PROMO_CLOSED, - NotificationService::AllSources()); // Watch for pref changes that cause us to need to invalidate the HTML cache. pref_change_registrar_.Init(profile_->GetPrefs()); @@ -194,18 +191,13 @@ RefCountedBytes* NTPResourceCache::GetNewTabCSS(bool is_incognito) { void NTPResourceCache::Observe(NotificationType type, const NotificationSource& source, const NotificationDetails& details) { // Invalidate the cache. - if (type == NotificationType::BROWSER_THEME_CHANGED || - type == NotificationType::PROMO_RESOURCE_STATE_CHANGED || - type == NotificationType::PROMO_CLOSED) { + if (NotificationType::BROWSER_THEME_CHANGED == type || + NotificationType::PROMO_RESOURCE_STATE_CHANGED == type) { new_tab_incognito_html_ = NULL; new_tab_html_ = NULL; new_tab_incognito_css_ = NULL; new_tab_css_ = NULL; - // Reset the promo closed preference if a new promo is being displayed. - if (type == NotificationType::PROMO_RESOURCE_STATE_CHANGED) { - profile_->GetPrefs()->SetBoolean(prefs::kNTPPromoClosed, false); - } - } else if (type == NotificationType::PREF_CHANGED) { + } else if (NotificationType::PREF_CHANGED == type) { std::string* pref_name = Details<std::string>(details).ptr(); if (*pref_name == prefs::kShowBookmarkBar || *pref_name == prefs::kHomePageIsNewTabPage || @@ -376,13 +368,11 @@ void NTPResourceCache::CreateNewTabHTML() { // If the user has preferences for a start and end time for a custom logo, // and the time now is between these two times, show the custom logo. - DCHECK(g_browser_process); - PrefService* local_state = g_browser_process->local_state(); - if (local_state->FindPreference(prefs::kNTPCustomLogoStart) && - local_state->FindPreference(prefs::kNTPCustomLogoEnd)) { + if (profile_->GetPrefs()->FindPreference(prefs::kNTPCustomLogoStart) && + profile_->GetPrefs()->FindPreference(prefs::kNTPCustomLogoEnd)) { localized_strings.SetString("customlogo", - InDateRange(local_state->GetDouble(prefs::kNTPCustomLogoStart), - local_state->GetDouble(prefs::kNTPCustomLogoEnd)) ? + InDateRange(profile_->GetPrefs()->GetDouble(prefs::kNTPCustomLogoStart), + profile_->GetPrefs()->GetDouble(prefs::kNTPCustomLogoEnd)) ? "true" : "false"); } else { localized_strings.SetString("customlogo", "false"); @@ -390,15 +380,15 @@ void NTPResourceCache::CreateNewTabHTML() { // If the user has preferences for a start and end time for a promo from // the server, and this promo string exists, set the localized string. - if (local_state->FindPreference(prefs::kNTPPromoStart) && - local_state->FindPreference(prefs::kNTPPromoEnd) && - local_state->FindPreference(prefs::kNTPPromoLine) && - web_resource::CanShowPromo(profile_)) { + if (profile_->GetPrefs()->FindPreference(prefs::kNTPPromoStart) && + profile_->GetPrefs()->FindPreference(prefs::kNTPPromoEnd) && + profile_->GetPrefs()->FindPreference(prefs::kNTPPromoLine) && + PromoResourceServiceUtil::CanShowPromo(profile_)) { localized_strings.SetString("serverpromo", - InDateRange(local_state->GetDouble(prefs::kNTPPromoStart), - local_state->GetDouble(prefs::kNTPPromoEnd)) ? - local_state->GetString(prefs::kNTPPromoLine) : - std::string()); + InDateRange(profile_->GetPrefs()->GetDouble(prefs::kNTPPromoStart), + profile_->GetPrefs()->GetDouble(prefs::kNTPPromoEnd)) ? + profile_->GetPrefs()->GetString(prefs::kNTPPromoLine) : + std::string()); UserMetrics::RecordAction(UserMetricsAction("NTPPromoShown")); } diff --git a/chrome/browser/web_resource/gpu_blacklist_updater.cc b/chrome/browser/web_resource/gpu_blacklist_updater.cc index 3c50c0c..a12f6fd 100644 --- a/chrome/browser/web_resource/gpu_blacklist_updater.cc +++ b/chrome/browser/web_resource/gpu_blacklist_updater.cc @@ -27,7 +27,9 @@ const char* GpuBlacklistUpdater::kDefaultGpuBlacklistURL = "https://dl.google.com/dl/edgedl/chrome/gpu/software_rendering_list.json"; GpuBlacklistUpdater::GpuBlacklistUpdater() - : WebResourceService(GpuBlacklistUpdater::kDefaultGpuBlacklistURL, + : WebResourceService(ProfileManager::GetDefaultProfile(), + g_browser_process->local_state(), + GpuBlacklistUpdater::kDefaultGpuBlacklistURL, false, // don't append locale to URL NotificationType::NOTIFICATION_TYPE_COUNT, prefs::kGpuBlacklistUpdate, @@ -39,10 +41,8 @@ GpuBlacklistUpdater::~GpuBlacklistUpdater() { } void GpuBlacklistUpdater::Unpack(const DictionaryValue& parsed_json) { DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); - PrefService* local_state = g_browser_process->local_state(); - DCHECK(local_state); DictionaryValue* gpu_blacklist_cache = - local_state->GetMutableDictionary(prefs::kGpuBlacklist); + prefs_->GetMutableDictionary(prefs::kGpuBlacklist); DCHECK(gpu_blacklist_cache); gpu_blacklist_cache->Clear(); gpu_blacklist_cache->MergeDictionary(&parsed_json); diff --git a/chrome/browser/web_resource/promo_resource_service.cc b/chrome/browser/web_resource/promo_resource_service.cc index 9ca78e5..947fe95 100644 --- a/chrome/browser/web_resource/promo_resource_service.cc +++ b/chrome/browser/web_resource/promo_resource_service.cc @@ -9,7 +9,6 @@ #include "base/time.h" #include "base/values.h" #include "chrome/browser/browser_process.h" -#include "chrome/browser/browser_process_impl.h" #include "chrome/browser/platform_util.h" #include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/profiles/profile.h" @@ -45,52 +44,15 @@ enum BuildType { } // namespace -namespace web_resource { - -bool CanShowPromo(Profile* profile) { - bool promo_closed = false; - PrefService* prefs = profile->GetPrefs(); - if (prefs->HasPrefPath(prefs::kNTPPromoClosed)) - promo_closed = prefs->GetBoolean(prefs::kNTPPromoClosed); - - // Only show if not synced. - bool is_synced = - (profile->HasProfileSyncService() && - sync_ui_util::GetStatus( - profile->GetProfileSyncService()) == sync_ui_util::SYNCED); - - // GetVersionStringModifier hits the registry. See http://crbug.com/70898. - base::ThreadRestrictions::ScopedAllowIO allow_io; - const std::string channel = platform_util::GetVersionStringModifier(); - bool is_promo_build = false; - PrefService* local_state = g_browser_process->local_state(); - if (local_state->HasPrefPath(prefs::kNTPPromoBuild)) { - int builds_allowed = local_state->GetInteger(prefs::kNTPPromoBuild); - if (builds_allowed == NO_BUILD) - return false; - if (channel == "dev" || channel == "dev-m") { - is_promo_build = (DEV_BUILD & builds_allowed) != 0; - } else if (channel == "beta" || channel == "beta-m") { - is_promo_build = (BETA_BUILD & builds_allowed) != 0; - } else if (channel == "" || channel == "m") { - is_promo_build = (STABLE_BUILD & builds_allowed) != 0; - } else { - is_promo_build = false; - } - } - - return !promo_closed && !is_synced && is_promo_build; -} - -} // namespace web_resource - // Server for dynamically loaded NTP HTML elements. TODO(mirandac): append // locale for future usage, when we're serving localizable strings. const char* PromoResourceService::kDefaultPromoResourceServer = "https://www.google.com/support/chrome/bin/topic/1142433/inproduct?hl="; -PromoResourceService::PromoResourceService() - : WebResourceService(PromoResourceService::kDefaultPromoResourceServer, +PromoResourceService::PromoResourceService(Profile* profile) + : WebResourceService(profile, + profile->GetPrefs(), + PromoResourceService::kDefaultPromoResourceServer, true, // append locale to URL NotificationType::PROMO_RESOURCE_STATE_CHANGED, prefs::kNTPPromoResourceCacheUpdate, @@ -103,18 +65,21 @@ PromoResourceService::PromoResourceService() PromoResourceService::~PromoResourceService() { } void PromoResourceService::Init() { - PrefService* local_state = g_browser_process->local_state(); - local_state->RegisterDoublePref(prefs::kNTPCustomLogoStart, 0); - local_state->RegisterDoublePref(prefs::kNTPCustomLogoEnd, 0); - local_state->RegisterDoublePref(prefs::kNTPPromoStart, 0); - local_state->RegisterDoublePref(prefs::kNTPPromoEnd, 0); - local_state->RegisterStringPref(prefs::kNTPPromoLine, std::string()); - local_state->RegisterIntegerPref(prefs::kNTPPromoBuild, NO_BUILD); + prefs_->RegisterDoublePref(prefs::kNTPCustomLogoStart, 0); + prefs_->RegisterDoublePref(prefs::kNTPCustomLogoEnd, 0); + prefs_->RegisterDoublePref(prefs::kNTPPromoStart, 0); + prefs_->RegisterDoublePref(prefs::kNTPPromoEnd, 0); + prefs_->RegisterStringPref(prefs::kNTPPromoLine, std::string()); + prefs_->RegisterBooleanPref(prefs::kNTPPromoClosed, false); + prefs_->RegisterIntegerPref(prefs::kNTPPromoGroup, -1); + prefs_->RegisterIntegerPref(prefs::kNTPPromoBuild, + DEV_BUILD | BETA_BUILD | STABLE_BUILD); + prefs_->RegisterIntegerPref(prefs::kNTPPromoGroupTimeSlice, 0); - // If the promo start is in the future, set a notification task to - // invalidate the NTP cache at the time of the promo start. - double promo_start = local_state->GetDouble(prefs::kNTPPromoStart); - double promo_end = local_state->GetDouble(prefs::kNTPPromoEnd); + // If the promo start is in the future, set a notification task to invalidate + // the NTP cache at the time of the promo start. + double promo_start = prefs_->GetDouble(prefs::kNTPPromoStart); + double promo_end = prefs_->GetDouble(prefs::kNTPPromoEnd); ScheduleNotification(promo_start, promo_end); } @@ -146,7 +111,6 @@ void PromoResourceService::ScheduleNotification(double promo_start, void PromoResourceService::UnpackPromoSignal( const DictionaryValue& parsed_json) { - PrefService* local_state = g_browser_process->local_state(); DictionaryValue* topic_dict; ListValue* answer_list; double old_promo_start = 0; @@ -155,10 +119,10 @@ void PromoResourceService::UnpackPromoSignal( double promo_end = 0; // Check for preexisting start and end values. - if (local_state->HasPrefPath(prefs::kNTPPromoStart) && - local_state->HasPrefPath(prefs::kNTPPromoEnd)) { - old_promo_start = local_state->GetDouble(prefs::kNTPPromoStart); - old_promo_end = local_state->GetDouble(prefs::kNTPPromoEnd); + if (prefs_->HasPrefPath(prefs::kNTPPromoStart) && + prefs_->HasPrefPath(prefs::kNTPPromoEnd)) { + old_promo_start = prefs_->GetDouble(prefs::kNTPPromoStart); + old_promo_end = prefs_->GetDouble(prefs::kNTPPromoEnd); } // Check for newly received start and end values. @@ -190,14 +154,20 @@ void PromoResourceService::UnpackPromoSignal( promo_build_type <= (DEV_BUILD | BETA_BUILD | STABLE_BUILD) && time_slice_hrs >= 0 && time_slice_hrs <= kMaxTimeSliceHours) { - local_state->SetInteger(prefs::kNTPPromoBuild, promo_build_type); + prefs_->SetInteger(prefs::kNTPPromoBuild, promo_build_type); + prefs_->SetInteger(prefs::kNTPPromoGroupTimeSlice, + time_slice_hrs); } else { // If no time data or bad time data are set, do not show promo. - local_state->SetInteger(prefs::kNTPPromoBuild, NO_BUILD); + prefs_->SetInteger(prefs::kNTPPromoBuild, NO_BUILD); + prefs_->SetInteger(prefs::kNTPPromoGroupTimeSlice, 0); } a_dic->GetString("inproduct", &promo_start_string); a_dic->GetString("tooltip", &promo_string); - local_state->SetString(prefs::kNTPPromoLine, promo_string); + prefs_->SetString(prefs::kNTPPromoLine, promo_string); + srand(static_cast<uint32>(time(NULL))); + prefs_->SetInteger(prefs::kNTPPromoGroup, + rand() % kNTPPromoGroupSize); } else if (promo_signal == "promo_end") { a_dic->GetString("inproduct", &promo_end_string); } @@ -214,10 +184,10 @@ void PromoResourceService::UnpackPromoSignal( base::Time::FromString( ASCIIToWide(promo_end_string).c_str(), &end_time)) { // Add group time slice, adjusted from hours to seconds. - srand(static_cast<uint32>(time(NULL))); - promo_group_ = rand() % kNTPPromoGroupSize; promo_start = start_time.ToDoubleT() + - promo_group_ * time_slice_hrs * 60 * 60; + (prefs_->FindPreference(prefs::kNTPPromoGroup) ? + prefs_->GetInteger(prefs::kNTPPromoGroup) * + time_slice_hrs * 60 * 60 : 0); promo_end = end_time.ToDoubleT(); } } @@ -231,15 +201,15 @@ void PromoResourceService::UnpackPromoSignal( // Also reset the promo closed preference, to signal a new promo. if (!(old_promo_start == promo_start) || !(old_promo_end == promo_end)) { - local_state->SetDouble(prefs::kNTPPromoStart, promo_start); - local_state->SetDouble(prefs::kNTPPromoEnd, promo_end); + prefs_->SetDouble(prefs::kNTPPromoStart, promo_start); + prefs_->SetDouble(prefs::kNTPPromoEnd, promo_end); + prefs_->SetBoolean(prefs::kNTPPromoClosed, false); ScheduleNotification(promo_start, promo_end); } } void PromoResourceService::UnpackLogoSignal( const DictionaryValue& parsed_json) { - PrefService* local_state = g_browser_process->local_state(); DictionaryValue* topic_dict; ListValue* answer_list; double old_logo_start = 0; @@ -248,10 +218,10 @@ void PromoResourceService::UnpackLogoSignal( double logo_end = 0; // Check for preexisting start and end values. - if (local_state->HasPrefPath(prefs::kNTPCustomLogoStart) && - local_state->HasPrefPath(prefs::kNTPCustomLogoEnd)) { - old_logo_start = local_state->GetDouble(prefs::kNTPCustomLogoStart); - old_logo_end = local_state->GetDouble(prefs::kNTPCustomLogoEnd); + if (prefs_->HasPrefPath(prefs::kNTPCustomLogoStart) && + prefs_->HasPrefPath(prefs::kNTPCustomLogoEnd)) { + old_logo_start = prefs_->GetDouble(prefs::kNTPCustomLogoStart); + old_logo_end = prefs_->GetDouble(prefs::kNTPCustomLogoEnd); } // Check for newly received start and end values. @@ -297,8 +267,8 @@ void PromoResourceService::UnpackLogoSignal( // dates counts as a triggering change if there were dates before. if (!(old_logo_start == logo_start) || !(old_logo_end == logo_end)) { - local_state->SetDouble(prefs::kNTPCustomLogoStart, logo_start); - local_state->SetDouble(prefs::kNTPCustomLogoEnd, logo_end); + prefs_->SetDouble(prefs::kNTPCustomLogoStart, logo_start); + prefs_->SetDouble(prefs::kNTPCustomLogoEnd, logo_end); NotificationService* service = NotificationService::current(); service->Notify(NotificationType::PROMO_RESOURCE_STATE_CHANGED, Source<WebResourceService>(this), @@ -306,3 +276,41 @@ void PromoResourceService::UnpackLogoSignal( } } +namespace PromoResourceServiceUtil { + +bool CanShowPromo(Profile* profile) { + bool promo_closed = false; + PrefService* prefs = profile->GetPrefs(); + if (prefs->HasPrefPath(prefs::kNTPPromoClosed)) + promo_closed = prefs->GetBoolean(prefs::kNTPPromoClosed); + + // Only show if not synced. + bool is_synced = + (profile->HasProfileSyncService() && + sync_ui_util::GetStatus( + profile->GetProfileSyncService()) == sync_ui_util::SYNCED); + + // GetVersionStringModifier hits the registry. See http://crbug.com/70898. + base::ThreadRestrictions::ScopedAllowIO allow_io; + const std::string channel = platform_util::GetVersionStringModifier(); + bool is_promo_build = false; + if (prefs->HasPrefPath(prefs::kNTPPromoBuild)) { + int builds_allowed = prefs->GetInteger(prefs::kNTPPromoBuild); + if (builds_allowed == NO_BUILD) + return false; + if (channel == "dev" || channel == "dev-m") { + is_promo_build = (DEV_BUILD & builds_allowed) != 0; + } else if (channel == "beta" || channel == "beta-m") { + is_promo_build = (BETA_BUILD & builds_allowed) != 0; + } else if (channel == "" || channel == "m") { + is_promo_build = (STABLE_BUILD & builds_allowed) != 0; + } else { + is_promo_build = false; + } + } + + return !promo_closed && !is_synced && is_promo_build; +} + +} // namespace PromoResourceServiceUtil + diff --git a/chrome/browser/web_resource/promo_resource_service.h b/chrome/browser/web_resource/promo_resource_service.h index 3155b52..733e6ae 100644 --- a/chrome/browser/web_resource/promo_resource_service.h +++ b/chrome/browser/web_resource/promo_resource_service.h @@ -8,16 +8,13 @@ #include "chrome/browser/web_resource/web_resource_service.h" -class Profile; -class PromoResourceServiceTest; - -namespace web_resource { +namespace PromoResourceServiceUtil { // Certain promotions should only be shown to certain classes of users. This // function will change to reflect each kind of promotion. bool CanShowPromo(Profile* profile); -} // namespace web_resource +} // namespace PromoResourceServiceUtil // A PromoResourceService fetches data from a web resource server to be used to // dynamically change the appearance of the New Tab Page. For example, it has @@ -30,7 +27,7 @@ bool CanShowPromo(Profile* profile); class PromoResourceService : public WebResourceService { public: - PromoResourceService(); + explicit PromoResourceService(Profile* profile); // Unpack the web resource as a custom promo signal. Expects a start and end // signal, with the promo to be shown in the tooltip of the start signal @@ -68,8 +65,7 @@ class PromoResourceService // For "promo_start", the promotional line itself is given in the "tooltip" // field. The "question" field gives the type of builds that should be shown // this promo (see the BuildType enum in web_resource_service.cc) and the - // number of hours that each promo group should see it, separated by ":": - // BuildType bitmask : time slice in hours + // number of hours that each promo group should see it, separated by ":". // For example, "7:24" would indicate that all builds should see the promo, // and each group should see it for 24 hours. // @@ -109,8 +105,6 @@ class PromoResourceService static const char* kDefaultPromoResourceServer; private: - friend class PromoResourceServiceTest; - virtual ~PromoResourceService(); virtual void Unpack(const DictionaryValue& parsed_json); @@ -125,9 +119,6 @@ class PromoResourceService // can write resource data back to user's pref file. DictionaryValue* web_resource_cache_; - // Saved for use in unit testing. - int promo_group_; - DISALLOW_COPY_AND_ASSIGN(PromoResourceService); }; diff --git a/chrome/browser/web_resource/promo_resource_service_factory.cc b/chrome/browser/web_resource/promo_resource_service_factory.cc deleted file mode 100644 index c5853f8..0000000 --- a/chrome/browser/web_resource/promo_resource_service_factory.cc +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/web_resource/promo_resource_service_factory.h" - -PromoResourceServiceFactory* PromoResourceServiceFactory::GetInstance() { - return Singleton<PromoResourceServiceFactory>::get(); -} - -PromoResourceServiceFactory::PromoResourceServiceFactory() { - promo_resource_service_ = new PromoResourceService(); -} - -void PromoResourceServiceFactory::StartPromoResourceService() { - promo_resource_service_->StartAfterDelay(); -} - -PromoResourceServiceFactory::~PromoResourceServiceFactory() {} - diff --git a/chrome/browser/web_resource/promo_resource_service_factory.h b/chrome/browser/web_resource/promo_resource_service_factory.h deleted file mode 100644 index 3ecbab7..0000000 --- a/chrome/browser/web_resource/promo_resource_service_factory.h +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_WEB_RESOURCE_PROMO_RESOURCE_SERVICE_FACTORY_H_ -#define CHROME_BROWSER_WEB_RESOURCE_PROMO_RESOURCE_SERVICE_FACTORY_H_ -#pragma once - -#include "base/memory/scoped_ptr.h" -#include "base/memory/singleton.h" -#include "chrome/browser/web_resource/promo_resource_service.h" - -// Singleton class that provides and wraps the refcounted PromoResourceService. -class PromoResourceServiceFactory { - public: - // Static singleton getter. - static PromoResourceServiceFactory* GetInstance(); - - // Start the PromoResourceService; separated from constructor so it can be - // skipped in unit testing. - void StartPromoResourceService(); - - private: - friend struct DefaultSingletonTraits<PromoResourceServiceFactory>; - friend class PromoResourceServiceTest; - - PromoResourceServiceFactory(); - ~PromoResourceServiceFactory(); - - // Exposed for unit testing. - PromoResourceService* promo_resource_service() { - return promo_resource_service_; - } - - scoped_refptr<PromoResourceService> promo_resource_service_; - - DISALLOW_COPY_AND_ASSIGN(PromoResourceServiceFactory); -}; - -#endif // CHROME_BROWSER_WEB_RESOURCE_PROMO_RESOURCE_SERVICE_FACTORY_H_ - diff --git a/chrome/browser/web_resource/promo_resource_service_unittest.cc b/chrome/browser/web_resource/promo_resource_service_unittest.cc index b80cea4..e84e43b 100644 --- a/chrome/browser/web_resource/promo_resource_service_unittest.cc +++ b/chrome/browser/web_resource/promo_resource_service_unittest.cc @@ -6,66 +6,13 @@ #include "base/time.h" #include "base/utf_string_conversions.h" #include "base/values.h" -#include "chrome/browser/prefs/browser_prefs.h" #include "chrome/browser/prefs/pref_service.h" #include "chrome/browser/web_resource/promo_resource_service.h" -#include "chrome/browser/web_resource/promo_resource_service_factory.h" #include "chrome/common/pref_names.h" -#include "chrome/test/testing_browser_process.h" -#include "chrome/test/testing_pref_service.h" +#include "chrome/test/testing_profile.h" #include "testing/gtest/include/gtest/gtest.h" -class PromoResourceServiceTest : public testing::Test { - public: - TestingPrefService* local_state() { return &local_state_; } - PromoResourceServiceFactory* promo_resource_service_factory() { - return promo_resource_service_factory_; - } - PromoResourceService* web_resource_service() { - return web_resource_service_; - } - - int promo_group() { - return web_resource_service_->promo_group_; - } - - protected: - PromoResourceServiceTest() {} - virtual ~PromoResourceServiceTest() {} - - // testing::Test - virtual void SetUp(); - virtual void TearDown(); - - private: - // weak Singleton - PromoResourceServiceFactory* promo_resource_service_factory_; - scoped_refptr<PromoResourceService> web_resource_service_; - TestingPrefService local_state_; -}; - -void PromoResourceServiceTest::SetUp() { - // Set up the local state preferences. - browser::RegisterLocalState(&local_state_); - TestingBrowserProcess* testing_browser_process = - static_cast<TestingBrowserProcess*>(g_browser_process); - testing_browser_process->SetPrefService(&local_state_); - - promo_resource_service_factory_ = - PromoResourceServiceFactory::GetInstance(); - web_resource_service_ = - promo_resource_service_factory_->promo_resource_service(); - // To get around the weirdness of using a Singleton across multiple tests, we - // may need to initialize the WebResourceService local state here. - if (!local_state()->FindPreference(prefs::kNTPPromoBuild)) - web_resource_service_->Init(); -} - -void PromoResourceServiceTest::TearDown() { - TestingBrowserProcess* testing_browser_process = - static_cast<TestingBrowserProcess*>(g_browser_process); - testing_browser_process->SetPrefService(NULL); -} +typedef testing::Test PromoResourceServiceTest; namespace { @@ -81,6 +28,11 @@ enum BuildType { // Verifies that custom dates read from a web resource server are written to // the preferences file. TEST_F(PromoResourceServiceTest, UnpackLogoSignal) { + // Set up a testing profile and create a promo resource service. + TestingProfile profile; + scoped_refptr<PromoResourceService> web_resource_service( + new PromoResourceService(&profile)); + // Set up start and end dates in a Dictionary as if parsed from the service. std::string json = "{ " " \"topic\": {" @@ -100,13 +52,15 @@ TEST_F(PromoResourceServiceTest, UnpackLogoSignal) { base::JSONReader::Read(json, false))); // Check that prefs are set correctly. - web_resource_service()->UnpackLogoSignal(*(test_json.get())); + web_resource_service->UnpackLogoSignal(*(test_json.get())); + PrefService* prefs = profile.GetPrefs(); + ASSERT_TRUE(prefs != NULL); double logo_start = - local_state()->GetDouble(prefs::kNTPCustomLogoStart); + prefs->GetDouble(prefs::kNTPCustomLogoStart); EXPECT_EQ(logo_start, 1264899600); // unix epoch for Jan 31 2010 0100 GMT. double logo_end = - local_state()->GetDouble(prefs::kNTPCustomLogoEnd); + prefs->GetDouble(prefs::kNTPCustomLogoEnd); EXPECT_EQ(logo_end, 1327971600); // unix epoch for Jan 31 2012 0100 GMT. // Change the start only and recheck. @@ -129,9 +83,9 @@ TEST_F(PromoResourceServiceTest, UnpackLogoSignal) { base::JSONReader::Read(json, false))); // Check that prefs are set correctly. - web_resource_service()->UnpackLogoSignal(*(test_json.get())); + web_resource_service->UnpackLogoSignal(*(test_json.get())); - logo_start = local_state()->GetDouble(prefs::kNTPCustomLogoStart); + logo_start = prefs->GetDouble(prefs::kNTPCustomLogoStart); EXPECT_EQ(logo_start, 1267365600); // date changes to Feb 28 2010 1400 GMT. // If no date is included in the prefs, reset custom logo dates to 0. @@ -148,14 +102,19 @@ TEST_F(PromoResourceServiceTest, UnpackLogoSignal) { base::JSONReader::Read(json, false))); // Check that prefs are set correctly. - web_resource_service()->UnpackLogoSignal(*(test_json.get())); - logo_start = local_state()->GetDouble(prefs::kNTPCustomLogoStart); + web_resource_service->UnpackLogoSignal(*(test_json.get())); + logo_start = prefs->GetDouble(prefs::kNTPCustomLogoStart); EXPECT_EQ(logo_start, 0); // date value reset to 0; - logo_end = local_state()->GetDouble(prefs::kNTPCustomLogoEnd); + logo_end = prefs->GetDouble(prefs::kNTPCustomLogoEnd); EXPECT_EQ(logo_end, 0); // date value reset to 0; } TEST_F(PromoResourceServiceTest, UnpackPromoSignal) { + // Set up a testing profile and create a promo resource service. + TestingProfile profile; + scoped_refptr<PromoResourceService> web_resource_service( + new PromoResourceService(&profile)); + // Set up start and end dates and promo line in a Dictionary as if parsed // from the service. std::string json = "{ " @@ -181,26 +140,33 @@ TEST_F(PromoResourceServiceTest, UnpackPromoSignal) { MessageLoop loop; // Check that prefs are set correctly. - web_resource_service()->UnpackPromoSignal(*(test_json.get())); + web_resource_service->UnpackPromoSignal(*(test_json.get())); + PrefService* prefs = profile.GetPrefs(); + ASSERT_TRUE(prefs != NULL); - std::string promo_line = local_state()->GetString(prefs::kNTPPromoLine); + std::string promo_line = prefs->GetString(prefs::kNTPPromoLine); EXPECT_EQ(promo_line, "Eat more pie!"); - int promo_build_type = local_state()->GetInteger(prefs::kNTPPromoBuild); + int promo_group = prefs->GetInteger(prefs::kNTPPromoGroup); + EXPECT_GE(promo_group, 0); + EXPECT_LT(promo_group, 16); + + int promo_build_type = prefs->GetInteger(prefs::kNTPPromoBuild); EXPECT_EQ(promo_build_type & DEV_BUILD, DEV_BUILD); EXPECT_EQ(promo_build_type & BETA_BUILD, BETA_BUILD); EXPECT_EQ(promo_build_type & STABLE_BUILD, 0); + int promo_time_slice = prefs->GetInteger(prefs::kNTPPromoGroupTimeSlice); + EXPECT_EQ(promo_time_slice, 2); + double promo_start = - local_state()->GetDouble(prefs::kNTPPromoStart); // In seconds. - int timeslice = 2; // From the second part of the "question" field. - // Start date for group 0, unix epoch for Jan 31 2010 0100 GMT. - int64 start_date = 1264899600; - // Add promo_group * timeslice converted to seconds for actual start. - EXPECT_EQ(promo_start, start_date + promo_group() * timeslice * 60 * 60); + prefs->GetDouble(prefs::kNTPPromoStart); + int64 actual_start = 1264899600 + // unix epoch for Jan 31 2010 0100 GMT. + promo_group * 2 * 60 * 60; + EXPECT_EQ(promo_start, actual_start); double promo_end = - local_state()->GetDouble(prefs::kNTPPromoEnd); + prefs->GetDouble(prefs::kNTPPromoEnd); EXPECT_EQ(promo_end, 1327971600); // unix epoch for Jan 31 2012 0100 GMT. } diff --git a/chrome/browser/web_resource/web_resource_service.cc b/chrome/browser/web_resource/web_resource_service.cc index 27118ac..717c850 100644 --- a/chrome/browser/web_resource/web_resource_service.cc +++ b/chrome/browser/web_resource/web_resource_service.cc @@ -14,6 +14,7 @@ #include "base/values.h" #include "chrome/browser/browser_process.h" #include "chrome/browser/prefs/pref_service.h" +#include "chrome/browser/profiles/profile.h" #include "chrome/browser/sync/sync_ui_util.h" #include "chrome/common/chrome_switches.h" #include "chrome/common/extensions/extension.h" @@ -66,12 +67,13 @@ class WebResourceService::WebResourceFetcher url_fetcher_.reset(new URLFetcher(GURL( web_resource_server), URLFetcher::GET, this)); - // Do not let url fetcher affect existing state (by setting cookies, for - // example). + // Do not let url fetcher affect existing state in profile (by setting + // cookies, for example. url_fetcher_->set_load_flags(net::LOAD_DISABLE_CACHE | net::LOAD_DO_NOT_SAVE_COOKIES); - url_fetcher_->set_request_context( - g_browser_process->system_request_context()); + net::URLRequestContextGetter* url_request_context_getter = + web_resource_service_->profile_->GetRequestContext(); + url_fetcher_->set_request_context(url_request_context_getter); url_fetcher_->Start(); } @@ -195,13 +197,17 @@ class WebResourceService::UnpackerClient }; WebResourceService::WebResourceService( + Profile* profile, + PrefService* prefs, const char* web_resource_server, bool apply_locale_to_url, NotificationType::Type notification_type, const char* last_update_time_pref_name, int start_fetch_delay, int cache_update_delay) - : ALLOW_THIS_IN_INITIALIZER_LIST(service_factory_(this)), + : prefs_(prefs), + profile_(profile), + ALLOW_THIS_IN_INITIALIZER_LIST(service_factory_(this)), in_fetch_(false), web_resource_server_(web_resource_server), apply_locale_to_url_(apply_locale_to_url), @@ -210,9 +216,9 @@ WebResourceService::WebResourceService( start_fetch_delay_(start_fetch_delay), cache_update_delay_(cache_update_delay), web_resource_update_scheduled_(false) { - PrefService* local_state = g_browser_process->local_state(); - DCHECK(local_state); - local_state->RegisterStringPref(last_update_time_pref_name, "0"); + DCHECK(prefs); + DCHECK(profile); + prefs_->RegisterStringPref(last_update_time_pref_name, "0"); resource_dispatcher_host_ = g_browser_process->resource_dispatcher_host(); web_resource_fetcher_.reset(new WebResourceFetcher(this)); } @@ -256,10 +262,9 @@ void WebResourceService::StartAfterDelay() { int64 delay = start_fetch_delay_; // Check whether we have ever put a value in the web resource cache; // if so, pull it out and see if it's time to update again. - PrefService* local_state = g_browser_process->local_state(); - if (local_state->HasPrefPath(last_update_time_pref_name_)) { + if (prefs_->HasPrefPath(last_update_time_pref_name_)) { std::string last_update_pref = - local_state->GetString(last_update_time_pref_name_); + prefs_->GetString(last_update_time_pref_name_); if (!last_update_pref.empty()) { double last_update_value; base::StringToDouble(last_update_pref, &last_update_value); @@ -280,6 +285,6 @@ void WebResourceService::UpdateResourceCache(const std::string& json_data) { client->Start(); // Set cache update time in preferences. - g_browser_process->local_state()->SetString(last_update_time_pref_name_, + prefs_->SetString(last_update_time_pref_name_, base::DoubleToString(base::Time::Now().ToDoubleT())); } diff --git a/chrome/browser/web_resource/web_resource_service.h b/chrome/browser/web_resource/web_resource_service.h index 44058c0..1a1074d 100644 --- a/chrome/browser/web_resource/web_resource_service.h +++ b/chrome/browser/web_resource/web_resource_service.h @@ -11,16 +11,19 @@ #include "chrome/browser/utility_process_host.h" #include "content/common/notification_type.h" -// A WebResourceService fetches data from a web resource server and stores -// cache update time in Local State. Any other data may be stored in Local -// State or the Profile's Preferences, as determined by the type of -// WebResourceService. +class PrefService; +class Profile; + +// A WebResourceService fetches data from a web resource server and store +// locally as user preference. class WebResourceService : public UtilityProcessHost::Client { public: // Pass notification_type = NOTIFICATION_TYPE_COUNT if notification is not // required. - WebResourceService(const char* web_resource_server, + WebResourceService(Profile* profile, + PrefService* prefs, + const char* web_resource_server, bool apply_locale_to_url_, NotificationType::Type notification_type, const char* last_update_time_pref_name, @@ -45,6 +48,10 @@ class WebResourceService // If delay_ms is negative, do nothing. void PostNotification(int64 delay_ms); + // We need to be able to load parsed resource data into preferences file, + // and get proper install directory. + PrefService* prefs_; + private: class WebResourceFetcher; friend class WebResourceFetcher; @@ -60,6 +67,8 @@ class WebResourceService // Notify listeners that the state of a web resource has changed. void WebResourceStateChange(); + Profile* profile_; + scoped_ptr<WebResourceFetcher> web_resource_fetcher_; ResourceDispatcherHost* resource_dispatcher_host_; diff --git a/chrome/chrome_browser.gypi b/chrome/chrome_browser.gypi index f10e07e..751fe32 100644 --- a/chrome/chrome_browser.gypi +++ b/chrome/chrome_browser.gypi @@ -3315,8 +3315,6 @@ 'browser/web_resource/gpu_blacklist_updater.h', 'browser/web_resource/promo_resource_service.cc', 'browser/web_resource/promo_resource_service.h', - 'browser/web_resource/promo_resource_service_factory.cc', - 'browser/web_resource/promo_resource_service_factory.h', 'browser/web_resource/web_resource_service.cc', 'browser/web_resource/web_resource_service.h', 'browser/webdata/autofill_change.cc', diff --git a/chrome/common/pref_names.cc b/chrome/common/pref_names.cc index 252588e..cf08c3a 100644 --- a/chrome/common/pref_names.cc +++ b/chrome/common/pref_names.cc @@ -726,17 +726,6 @@ const char kMetricsInitialLogs[] = const char kMetricsOngoingLogs[] = "user_experience_metrics.ongoing_logs"; -// Whether promo should be shown to Dev builds, Beta and Dev, or all builds. -const char kNTPPromoBuild[] = "ntp.promo_build"; - -// Promo line from server. -const char kNTPPromoLine[] = "ntp.promo_line"; - -// Dates between which the NTP should show a promotional line downloaded -// from the promo server. -const char kNTPPromoStart[] = "ntp.promo_start"; -const char kNTPPromoEnd[] = "ntp.promo_end"; - // Where profile specific metrics are placed. const char kProfileMetrics[] = "user_experience_metrics.profiles"; @@ -1086,9 +1075,28 @@ const char kNTPPrefVersion[] = "ntp.pref_version"; const char kNTPCustomLogoStart[] = "ntp.alt_logo_start"; const char kNTPCustomLogoEnd[] = "ntp.alt_logo_end"; +// Whether promo should be shown to Dev builds, Beta and Dev, or all builds. +const char kNTPPromoBuild[] = "ntp.promo_build"; + // True if user has explicitly closed the promo line. const char kNTPPromoClosed[] = "ntp.promo_closed"; +// Users are randomly divided into 16 groups in order to slowly roll out +// special promos. +const char kNTPPromoGroup[] = "ntp.promo_group"; + +// Amount of time each promo group should be shown a promo that is being slowly +// rolled out, in hours. +const char kNTPPromoGroupTimeSlice[] = "ntp.promo_group_timeslice"; + +// Promo line from server. +const char kNTPPromoLine[] = "ntp.promo_line"; + +// Dates between which the NTP should show a promotional line downloaded +// from the promo server. +const char kNTPPromoStart[] = "ntp.promo_start"; +const char kNTPPromoEnd[] = "ntp.promo_end"; + // The most up-to-date GPU blacklist downloaded from the web, which replaces // the one that's installed with chrome. const char kGpuBlacklist[] = "gpu_blacklist"; diff --git a/chrome/common/pref_names.h b/chrome/common/pref_names.h index 69577ea..acfd6cc 100644 --- a/chrome/common/pref_names.h +++ b/chrome/common/pref_names.h @@ -380,6 +380,8 @@ extern const char kNTPPromoStart[]; extern const char kNTPPromoEnd[]; extern const char kNTPPromoLine[]; extern const char kNTPPromoClosed[]; +extern const char kNTPPromoGroup[]; +extern const char kNTPPromoGroupTimeSlice[]; extern const char kNTPPromoBuild[]; extern const char kGpuBlacklist[]; |