diff options
author | mirandac@chromium.org <mirandac@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-04 16:08:52 +0000 |
---|---|---|
committer | mirandac@chromium.org <mirandac@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-04-04 16:08:52 +0000 |
commit | 81bb1092f354f3cb3c597ab3c9ed568905078e49 (patch) | |
tree | 7e8c5f324f49fc7302e6f1ee742c39169340e03f /chrome/browser | |
parent | 96dcc176c46003af0176f0f4772affc741aa772a (diff) | |
download | chromium_src-81bb1092f354f3cb3c597ab3c9ed568905078e49.zip chromium_src-81bb1092f354f3cb3c597ab3c9ed568905078e49.tar.gz chromium_src-81bb1092f354f3cb3c597ab3c9ed568905078e49.tar.bz2 |
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
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@80318 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser')
-rw-r--r-- | chrome/browser/browser_main.cc | 11 | ||||
-rw-r--r-- | chrome/browser/profiles/profile.h | 3 | ||||
-rw-r--r-- | chrome/browser/profiles/profile_impl.cc | 9 | ||||
-rw-r--r-- | chrome/browser/profiles/profile_impl.h | 2 | ||||
-rw-r--r-- | chrome/browser/profiles/profile_manager.cc | 3 | ||||
-rw-r--r-- | chrome/browser/ui/browser.cc | 1 | ||||
-rw-r--r-- | chrome/browser/ui/webui/new_tab_ui.cc | 2 | ||||
-rw-r--r-- | chrome/browser/ui/webui/ntp_resource_cache.cc | 40 | ||||
-rw-r--r-- | chrome/browser/web_resource/gpu_blacklist_updater.cc | 8 | ||||
-rw-r--r-- | chrome/browser/web_resource/promo_resource_service.cc | 154 | ||||
-rw-r--r-- | chrome/browser/web_resource/promo_resource_service.h | 17 | ||||
-rw-r--r-- | chrome/browser/web_resource/promo_resource_service_factory.cc | 20 | ||||
-rw-r--r-- | chrome/browser/web_resource/promo_resource_service_factory.h | 41 | ||||
-rw-r--r-- | chrome/browser/web_resource/promo_resource_service_unittest.cc | 112 | ||||
-rw-r--r-- | chrome/browser/web_resource/web_resource_service.cc | 29 | ||||
-rw-r--r-- | chrome/browser/web_resource/web_resource_service.h | 19 |
16 files changed, 279 insertions, 192 deletions
diff --git a/chrome/browser/browser_main.cc b/chrome/browser/browser_main.cc index 8c7002f..0e60c55 100644 --- a/chrome/browser/browser_main.cc +++ b/chrome/browser/browser_main.cc @@ -74,6 +74,7 @@ #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" @@ -1776,6 +1777,16 @@ 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 e7d1741..47a17bb 100644 --- a/chrome/browser/profiles/profile.h +++ b/chrome/browser/profiles/profile.h @@ -470,9 +470,6 @@ 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 c25f7cf..aee7b6c 100644 --- a/chrome/browser/profiles/profile_impl.cc +++ b/chrome/browser/profiles/profile_impl.cc @@ -76,7 +76,6 @@ #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" @@ -494,14 +493,6 @@ 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 e25079e..f03e750 100644 --- a/chrome/browser/profiles/profile_impl.h +++ b/chrome/browser/profiles/profile_impl.h @@ -116,7 +116,6 @@ 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(); @@ -213,7 +212,6 @@ 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 b893b25..0ce74e4 100644 --- a/chrome/browser/profiles/profile_manager.cc +++ b/chrome/browser/profiles/profile_manager.cc @@ -237,9 +237,6 @@ 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 57e3f8b..e90afd3 100644 --- a/chrome/browser/ui/browser.cc +++ b/chrome/browser/ui/browser.cc @@ -2074,6 +2074,7 @@ 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 1f0604d..4bd9100 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_RESOURCE_STATE_CHANGED, + service->Notify(NotificationType::PROMO_CLOSED, 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 438cb64..2cfbab4 100644 --- a/chrome/browser/ui/webui/ntp_resource_cache.cc +++ b/chrome/browser/ui/webui/ntp_resource_cache.cc @@ -15,6 +15,7 @@ #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" @@ -153,6 +154,8 @@ 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()); @@ -191,13 +194,18 @@ RefCountedBytes* NTPResourceCache::GetNewTabCSS(bool is_incognito) { void NTPResourceCache::Observe(NotificationType type, const NotificationSource& source, const NotificationDetails& details) { // Invalidate the cache. - if (NotificationType::BROWSER_THEME_CHANGED == type || - NotificationType::PROMO_RESOURCE_STATE_CHANGED == type) { + if (type == NotificationType::BROWSER_THEME_CHANGED || + type == NotificationType::PROMO_RESOURCE_STATE_CHANGED || + type == NotificationType::PROMO_CLOSED) { new_tab_incognito_html_ = NULL; new_tab_html_ = NULL; new_tab_incognito_css_ = NULL; new_tab_css_ = NULL; - } else if (NotificationType::PREF_CHANGED == type) { + // 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) { std::string* pref_name = Details<std::string>(details).ptr(); if (*pref_name == prefs::kShowBookmarkBar || *pref_name == prefs::kHomePageIsNewTabPage || @@ -368,11 +376,13 @@ 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. - if (profile_->GetPrefs()->FindPreference(prefs::kNTPCustomLogoStart) && - profile_->GetPrefs()->FindPreference(prefs::kNTPCustomLogoEnd)) { + DCHECK(g_browser_process); + PrefService* local_state = g_browser_process->local_state(); + if (local_state->FindPreference(prefs::kNTPCustomLogoStart) && + local_state->FindPreference(prefs::kNTPCustomLogoEnd)) { localized_strings.SetString("customlogo", - InDateRange(profile_->GetPrefs()->GetDouble(prefs::kNTPCustomLogoStart), - profile_->GetPrefs()->GetDouble(prefs::kNTPCustomLogoEnd)) ? + InDateRange(local_state->GetDouble(prefs::kNTPCustomLogoStart), + local_state->GetDouble(prefs::kNTPCustomLogoEnd)) ? "true" : "false"); } else { localized_strings.SetString("customlogo", "false"); @@ -380,15 +390,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 (profile_->GetPrefs()->FindPreference(prefs::kNTPPromoStart) && - profile_->GetPrefs()->FindPreference(prefs::kNTPPromoEnd) && - profile_->GetPrefs()->FindPreference(prefs::kNTPPromoLine) && - PromoResourceServiceUtil::CanShowPromo(profile_)) { + if (local_state->FindPreference(prefs::kNTPPromoStart) && + local_state->FindPreference(prefs::kNTPPromoEnd) && + local_state->FindPreference(prefs::kNTPPromoLine) && + web_resource::CanShowPromo(profile_)) { localized_strings.SetString("serverpromo", - InDateRange(profile_->GetPrefs()->GetDouble(prefs::kNTPPromoStart), - profile_->GetPrefs()->GetDouble(prefs::kNTPPromoEnd)) ? - profile_->GetPrefs()->GetString(prefs::kNTPPromoLine) : - std::string()); + InDateRange(local_state->GetDouble(prefs::kNTPPromoStart), + local_state->GetDouble(prefs::kNTPPromoEnd)) ? + local_state->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 a12f6fd..3c50c0c 100644 --- a/chrome/browser/web_resource/gpu_blacklist_updater.cc +++ b/chrome/browser/web_resource/gpu_blacklist_updater.cc @@ -27,9 +27,7 @@ const char* GpuBlacklistUpdater::kDefaultGpuBlacklistURL = "https://dl.google.com/dl/edgedl/chrome/gpu/software_rendering_list.json"; GpuBlacklistUpdater::GpuBlacklistUpdater() - : WebResourceService(ProfileManager::GetDefaultProfile(), - g_browser_process->local_state(), - GpuBlacklistUpdater::kDefaultGpuBlacklistURL, + : WebResourceService(GpuBlacklistUpdater::kDefaultGpuBlacklistURL, false, // don't append locale to URL NotificationType::NOTIFICATION_TYPE_COUNT, prefs::kGpuBlacklistUpdate, @@ -41,8 +39,10 @@ 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 = - prefs_->GetMutableDictionary(prefs::kGpuBlacklist); + local_state->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 947fe95..9ca78e5 100644 --- a/chrome/browser/web_resource/promo_resource_service.cc +++ b/chrome/browser/web_resource/promo_resource_service.cc @@ -9,6 +9,7 @@ #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" @@ -44,15 +45,52 @@ 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(Profile* profile) - : WebResourceService(profile, - profile->GetPrefs(), - PromoResourceService::kDefaultPromoResourceServer, +PromoResourceService::PromoResourceService() + : WebResourceService(PromoResourceService::kDefaultPromoResourceServer, true, // append locale to URL NotificationType::PROMO_RESOURCE_STATE_CHANGED, prefs::kNTPPromoResourceCacheUpdate, @@ -65,21 +103,18 @@ PromoResourceService::PromoResourceService(Profile* profile) PromoResourceService::~PromoResourceService() { } void PromoResourceService::Init() { - 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); + 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); - // 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); + // 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); ScheduleNotification(promo_start, promo_end); } @@ -111,6 +146,7 @@ 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; @@ -119,10 +155,10 @@ void PromoResourceService::UnpackPromoSignal( double promo_end = 0; // Check for preexisting start and end values. - if (prefs_->HasPrefPath(prefs::kNTPPromoStart) && - prefs_->HasPrefPath(prefs::kNTPPromoEnd)) { - old_promo_start = prefs_->GetDouble(prefs::kNTPPromoStart); - old_promo_end = prefs_->GetDouble(prefs::kNTPPromoEnd); + 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); } // Check for newly received start and end values. @@ -154,20 +190,14 @@ void PromoResourceService::UnpackPromoSignal( promo_build_type <= (DEV_BUILD | BETA_BUILD | STABLE_BUILD) && time_slice_hrs >= 0 && time_slice_hrs <= kMaxTimeSliceHours) { - prefs_->SetInteger(prefs::kNTPPromoBuild, promo_build_type); - prefs_->SetInteger(prefs::kNTPPromoGroupTimeSlice, - time_slice_hrs); + local_state->SetInteger(prefs::kNTPPromoBuild, promo_build_type); } else { // If no time data or bad time data are set, do not show promo. - prefs_->SetInteger(prefs::kNTPPromoBuild, NO_BUILD); - prefs_->SetInteger(prefs::kNTPPromoGroupTimeSlice, 0); + local_state->SetInteger(prefs::kNTPPromoBuild, NO_BUILD); } a_dic->GetString("inproduct", &promo_start_string); a_dic->GetString("tooltip", &promo_string); - prefs_->SetString(prefs::kNTPPromoLine, promo_string); - srand(static_cast<uint32>(time(NULL))); - prefs_->SetInteger(prefs::kNTPPromoGroup, - rand() % kNTPPromoGroupSize); + local_state->SetString(prefs::kNTPPromoLine, promo_string); } else if (promo_signal == "promo_end") { a_dic->GetString("inproduct", &promo_end_string); } @@ -184,10 +214,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() + - (prefs_->FindPreference(prefs::kNTPPromoGroup) ? - prefs_->GetInteger(prefs::kNTPPromoGroup) * - time_slice_hrs * 60 * 60 : 0); + promo_group_ * time_slice_hrs * 60 * 60; promo_end = end_time.ToDoubleT(); } } @@ -201,15 +231,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)) { - prefs_->SetDouble(prefs::kNTPPromoStart, promo_start); - prefs_->SetDouble(prefs::kNTPPromoEnd, promo_end); - prefs_->SetBoolean(prefs::kNTPPromoClosed, false); + local_state->SetDouble(prefs::kNTPPromoStart, promo_start); + local_state->SetDouble(prefs::kNTPPromoEnd, promo_end); 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; @@ -218,10 +248,10 @@ void PromoResourceService::UnpackLogoSignal( double logo_end = 0; // Check for preexisting start and end values. - if (prefs_->HasPrefPath(prefs::kNTPCustomLogoStart) && - prefs_->HasPrefPath(prefs::kNTPCustomLogoEnd)) { - old_logo_start = prefs_->GetDouble(prefs::kNTPCustomLogoStart); - old_logo_end = prefs_->GetDouble(prefs::kNTPCustomLogoEnd); + 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); } // Check for newly received start and end values. @@ -267,8 +297,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)) { - prefs_->SetDouble(prefs::kNTPCustomLogoStart, logo_start); - prefs_->SetDouble(prefs::kNTPCustomLogoEnd, logo_end); + local_state->SetDouble(prefs::kNTPCustomLogoStart, logo_start); + local_state->SetDouble(prefs::kNTPCustomLogoEnd, logo_end); NotificationService* service = NotificationService::current(); service->Notify(NotificationType::PROMO_RESOURCE_STATE_CHANGED, Source<WebResourceService>(this), @@ -276,41 +306,3 @@ 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 733e6ae..3155b52 100644 --- a/chrome/browser/web_resource/promo_resource_service.h +++ b/chrome/browser/web_resource/promo_resource_service.h @@ -8,13 +8,16 @@ #include "chrome/browser/web_resource/web_resource_service.h" -namespace PromoResourceServiceUtil { +class Profile; +class PromoResourceServiceTest; + +namespace web_resource { // 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 PromoResourceServiceUtil +} // namespace web_resource // 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 @@ -27,7 +30,7 @@ bool CanShowPromo(Profile* profile); class PromoResourceService : public WebResourceService { public: - explicit PromoResourceService(Profile* profile); + PromoResourceService(); // 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 @@ -65,7 +68,8 @@ 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 ":". + // number of hours that each promo group should see it, separated by ":": + // BuildType bitmask : time slice in hours // For example, "7:24" would indicate that all builds should see the promo, // and each group should see it for 24 hours. // @@ -105,6 +109,8 @@ class PromoResourceService static const char* kDefaultPromoResourceServer; private: + friend class PromoResourceServiceTest; + virtual ~PromoResourceService(); virtual void Unpack(const DictionaryValue& parsed_json); @@ -119,6 +125,9 @@ 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 new file mode 100644 index 0000000..c5853f8 --- /dev/null +++ b/chrome/browser/web_resource/promo_resource_service_factory.cc @@ -0,0 +1,20 @@ +// 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 new file mode 100644 index 0000000..3ecbab7 --- /dev/null +++ b/chrome/browser/web_resource/promo_resource_service_factory.h @@ -0,0 +1,41 @@ +// 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 e84e43b..b80cea4 100644 --- a/chrome/browser/web_resource/promo_resource_service_unittest.cc +++ b/chrome/browser/web_resource/promo_resource_service_unittest.cc @@ -6,13 +6,66 @@ #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_profile.h" +#include "chrome/test/testing_browser_process.h" +#include "chrome/test/testing_pref_service.h" #include "testing/gtest/include/gtest/gtest.h" -typedef testing::Test PromoResourceServiceTest; +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); +} namespace { @@ -28,11 +81,6 @@ 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\": {" @@ -52,15 +100,13 @@ TEST_F(PromoResourceServiceTest, UnpackLogoSignal) { base::JSONReader::Read(json, false))); // Check that prefs are set correctly. - web_resource_service->UnpackLogoSignal(*(test_json.get())); - PrefService* prefs = profile.GetPrefs(); - ASSERT_TRUE(prefs != NULL); + web_resource_service()->UnpackLogoSignal(*(test_json.get())); double logo_start = - prefs->GetDouble(prefs::kNTPCustomLogoStart); + local_state()->GetDouble(prefs::kNTPCustomLogoStart); EXPECT_EQ(logo_start, 1264899600); // unix epoch for Jan 31 2010 0100 GMT. double logo_end = - prefs->GetDouble(prefs::kNTPCustomLogoEnd); + local_state()->GetDouble(prefs::kNTPCustomLogoEnd); EXPECT_EQ(logo_end, 1327971600); // unix epoch for Jan 31 2012 0100 GMT. // Change the start only and recheck. @@ -83,9 +129,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 = prefs->GetDouble(prefs::kNTPCustomLogoStart); + logo_start = local_state()->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. @@ -102,19 +148,14 @@ TEST_F(PromoResourceServiceTest, UnpackLogoSignal) { base::JSONReader::Read(json, false))); // Check that prefs are set correctly. - web_resource_service->UnpackLogoSignal(*(test_json.get())); - logo_start = prefs->GetDouble(prefs::kNTPCustomLogoStart); + web_resource_service()->UnpackLogoSignal(*(test_json.get())); + logo_start = local_state()->GetDouble(prefs::kNTPCustomLogoStart); EXPECT_EQ(logo_start, 0); // date value reset to 0; - logo_end = prefs->GetDouble(prefs::kNTPCustomLogoEnd); + logo_end = local_state()->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 = "{ " @@ -140,33 +181,26 @@ TEST_F(PromoResourceServiceTest, UnpackPromoSignal) { MessageLoop loop; // Check that prefs are set correctly. - web_resource_service->UnpackPromoSignal(*(test_json.get())); - PrefService* prefs = profile.GetPrefs(); - ASSERT_TRUE(prefs != NULL); + web_resource_service()->UnpackPromoSignal(*(test_json.get())); - std::string promo_line = prefs->GetString(prefs::kNTPPromoLine); + std::string promo_line = local_state()->GetString(prefs::kNTPPromoLine); EXPECT_EQ(promo_line, "Eat more pie!"); - 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); + int promo_build_type = local_state()->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 = - 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); + 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); double promo_end = - prefs->GetDouble(prefs::kNTPPromoEnd); + local_state()->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 717c850..27118ac 100644 --- a/chrome/browser/web_resource/web_resource_service.cc +++ b/chrome/browser/web_resource/web_resource_service.cc @@ -14,7 +14,6 @@ #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" @@ -67,13 +66,12 @@ class WebResourceService::WebResourceFetcher url_fetcher_.reset(new URLFetcher(GURL( web_resource_server), URLFetcher::GET, this)); - // Do not let url fetcher affect existing state in profile (by setting - // cookies, for example. + // Do not let url fetcher affect existing state (by setting cookies, for + // example). url_fetcher_->set_load_flags(net::LOAD_DISABLE_CACHE | net::LOAD_DO_NOT_SAVE_COOKIES); - net::URLRequestContextGetter* url_request_context_getter = - web_resource_service_->profile_->GetRequestContext(); - url_fetcher_->set_request_context(url_request_context_getter); + url_fetcher_->set_request_context( + g_browser_process->system_request_context()); url_fetcher_->Start(); } @@ -197,17 +195,13 @@ 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) - : prefs_(prefs), - profile_(profile), - ALLOW_THIS_IN_INITIALIZER_LIST(service_factory_(this)), + : 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), @@ -216,9 +210,9 @@ WebResourceService::WebResourceService( start_fetch_delay_(start_fetch_delay), cache_update_delay_(cache_update_delay), web_resource_update_scheduled_(false) { - DCHECK(prefs); - DCHECK(profile); - prefs_->RegisterStringPref(last_update_time_pref_name, "0"); + PrefService* local_state = g_browser_process->local_state(); + DCHECK(local_state); + local_state->RegisterStringPref(last_update_time_pref_name, "0"); resource_dispatcher_host_ = g_browser_process->resource_dispatcher_host(); web_resource_fetcher_.reset(new WebResourceFetcher(this)); } @@ -262,9 +256,10 @@ 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. - if (prefs_->HasPrefPath(last_update_time_pref_name_)) { + PrefService* local_state = g_browser_process->local_state(); + if (local_state->HasPrefPath(last_update_time_pref_name_)) { std::string last_update_pref = - prefs_->GetString(last_update_time_pref_name_); + local_state->GetString(last_update_time_pref_name_); if (!last_update_pref.empty()) { double last_update_value; base::StringToDouble(last_update_pref, &last_update_value); @@ -285,6 +280,6 @@ void WebResourceService::UpdateResourceCache(const std::string& json_data) { client->Start(); // Set cache update time in preferences. - prefs_->SetString(last_update_time_pref_name_, + g_browser_process->local_state()->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 1a1074d..44058c0 100644 --- a/chrome/browser/web_resource/web_resource_service.h +++ b/chrome/browser/web_resource/web_resource_service.h @@ -11,19 +11,16 @@ #include "chrome/browser/utility_process_host.h" #include "content/common/notification_type.h" -class PrefService; -class Profile; - -// A WebResourceService fetches data from a web resource server and store -// locally as user preference. +// 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 WebResourceService : public UtilityProcessHost::Client { public: // Pass notification_type = NOTIFICATION_TYPE_COUNT if notification is not // required. - WebResourceService(Profile* profile, - PrefService* prefs, - const char* web_resource_server, + WebResourceService(const char* web_resource_server, bool apply_locale_to_url_, NotificationType::Type notification_type, const char* last_update_time_pref_name, @@ -48,10 +45,6 @@ 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; @@ -67,8 +60,6 @@ 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_; |