diff options
author | mirandac@chromium.org <mirandac@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-30 01:28:04 +0000 |
---|---|---|
committer | mirandac@chromium.org <mirandac@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-11-30 01:28:04 +0000 |
commit | 891152327d2eac696d2ea4f745046e9abd5bbc0e (patch) | |
tree | 19ad2413722a36357e2eadf4d98b3cea7dfad828 /chrome/browser/web_resource | |
parent | 4ecd652ecfa52a74569219af376edab92478947a (diff) | |
download | chromium_src-891152327d2eac696d2ea4f745046e9abd5bbc0e.zip chromium_src-891152327d2eac696d2ea4f745046e9abd5bbc0e.tar.gz chromium_src-891152327d2eac696d2ea4f745046e9abd5bbc0e.tar.bz2 |
Fix web resource service bugs:
Removed web resource server from ntp preferences; no longer needed.
Fixed bug that caused the last updated cache time to not be stored correctly.
Scheduled a notification to be sent at the time a web resource element becomes available to force NTP cache update.
BUG=64597
TEST=see me.
Review URL: http://codereview.chromium.org/5302008
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@67646 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/web_resource')
-rw-r--r-- | chrome/browser/web_resource/web_resource_service.cc | 89 | ||||
-rw-r--r-- | chrome/browser/web_resource/web_resource_service.h | 19 |
2 files changed, 83 insertions, 25 deletions
diff --git a/chrome/browser/web_resource/web_resource_service.cc b/chrome/browser/web_resource/web_resource_service.cc index 5ba8ffc..6d5c6bc 100644 --- a/chrome/browser/web_resource/web_resource_service.cc +++ b/chrome/browser/web_resource/web_resource_service.cc @@ -17,6 +17,7 @@ #include "chrome/browser/profile.h" #include "chrome/browser/sync/sync_ui_util.h" #include "chrome/common/chrome_switches.h" +#include "chrome/common/extensions/extension.h" #include "chrome/common/net/url_fetcher.h" #include "chrome/common/notification_service.h" #include "chrome/common/notification_type.h" @@ -73,7 +74,7 @@ class WebResourceService::WebResourceFetcher web_resource_service_->in_fetch_ = true; url_fetcher_.reset(new URLFetcher(GURL( - web_resource_service_->web_resource_server_), + kDefaultWebResourceServer), URLFetcher::GET, this)); // Do not let url fetcher affect existing state in profile (by setting // cookies, for example. @@ -210,7 +211,9 @@ const char* WebResourceService::kDefaultWebResourceServer = WebResourceService::WebResourceService(Profile* profile) : prefs_(profile->GetPrefs()), profile_(profile), - in_fetch_(false) { + ALLOW_THIS_IN_INITIALIZER_LIST(service_factory_(this)), + in_fetch_(false), + web_resource_update_scheduled_(false) { Init(); } @@ -231,14 +234,11 @@ void WebResourceService::Init() { prefs_->RegisterStringPref(prefs::kNTPPromoLine, std::string()); prefs_->RegisterBooleanPref(prefs::kNTPPromoClosed, false); - if (prefs_->HasPrefPath(prefs::kNTPWebResourceCache)) { - web_resource_server_ = prefs_->GetString(prefs::kNTPWebResourceCache); - return; - } - - // If we have not yet set a server, reset and force an immediate update. - web_resource_server_ = kDefaultWebResourceServer; - prefs_->SetString(prefs::kNTPWebResourceCacheUpdate, ""); + // 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_->GetReal(prefs::kNTPPromoStart); + double promo_end = prefs_->GetReal(prefs::kNTPPromoEnd); + ScheduleNotification(promo_start, promo_end); } void WebResourceService::EndFetch() { @@ -253,6 +253,44 @@ void WebResourceService::OnWebResourceUnpacked( EndFetch(); } +void WebResourceService::WebResourceStateChange() { + web_resource_update_scheduled_ = false; + NotificationService* service = NotificationService::current(); + service->Notify(NotificationType::WEB_RESOURCE_STATE_CHANGED, + Source<WebResourceService>(this), + NotificationService::NoDetails()); +} + +void WebResourceService::ScheduleNotification(double promo_start, + double promo_end) { + if (promo_start > 0 && promo_end > 0 && !web_resource_update_scheduled_) { + int ms_until_start = + static_cast<int>((base::Time::FromDoubleT( + promo_start) - base::Time::Now()).InMilliseconds()); + int ms_until_end = + static_cast<int>((base::Time::FromDoubleT( + promo_end) - base::Time::Now()).InMilliseconds()); + if (ms_until_start > 0) { + web_resource_update_scheduled_ = true; + MessageLoop::current()->PostDelayedTask(FROM_HERE, + service_factory_.NewRunnableMethod( + &WebResourceService::WebResourceStateChange), + ms_until_start); + } + if (ms_until_end > 0) { + web_resource_update_scheduled_ = true; + MessageLoop::current()->PostDelayedTask(FROM_HERE, + service_factory_.NewRunnableMethod( + &WebResourceService::WebResourceStateChange), + ms_until_end); + if (ms_until_start <= 0) { + // Notify immediately if time is between start and end. + WebResourceStateChange(); + } + } + } +} + void WebResourceService::StartAfterDelay() { int delay = kStartResourceFetchDelay; // Check whether we have ever put a value in the web resource cache; @@ -266,13 +304,11 @@ void WebResourceService::StartAfterDelay() { int ms_until_update = cache_update_delay_ - static_cast<int>((base::Time::Now() - base::Time::FromDoubleT( last_update_value)).InMilliseconds()); - delay = ms_until_update > cache_update_delay_ ? cache_update_delay_ : (ms_until_update < kStartResourceFetchDelay ? kStartResourceFetchDelay : ms_until_update); } } - // Start fetch and wait for UpdateResourceCache. web_resource_fetcher_->StartAfterDelay(static_cast<int>(delay)); } @@ -281,10 +317,9 @@ void WebResourceService::UpdateResourceCache(const std::string& json_data) { UnpackerClient* client = new UnpackerClient(this, json_data); client->Start(); - // Update resource server and cache update time in preferences. + // Set cache update time in preferences. prefs_->SetString(prefs::kNTPWebResourceCacheUpdate, base::DoubleToString(base::Time::Now().ToDoubleT())); - prefs_->SetString(prefs::kNTPWebResourceServer, web_resource_server_); } void WebResourceService::UnpackTips(const DictionaryValue& parsed_json) { @@ -390,10 +425,7 @@ void WebResourceService::UnpackPromoSignal(const DictionaryValue& parsed_json) { !(old_promo_end == promo_end)) { prefs_->SetReal(prefs::kNTPPromoStart, promo_start); prefs_->SetReal(prefs::kNTPPromoEnd, promo_end); - NotificationService* service = NotificationService::current(); - service->Notify(NotificationType::WEB_RESOURCE_STATE_CHANGED, - Source<WebResourceService>(this), - NotificationService::NoDetails()); + ScheduleNotification(promo_start, promo_end); } } @@ -464,7 +496,6 @@ void WebResourceService::UnpackLogoSignal(const DictionaryValue& parsed_json) { } } - namespace WebResourceServiceUtil { bool CanShowPromo(Profile* profile) { @@ -472,14 +503,28 @@ bool CanShowPromo(Profile* profile) { PrefService* prefs = profile->GetPrefs(); if (prefs->HasPrefPath(prefs::kNTPPromoClosed)) promo_closed = prefs->GetBoolean(prefs::kNTPPromoClosed); + + bool has_extensions = false; ExtensionsService* extensions_service = profile->GetExtensionsService(); - bool promo_options_set = + if (extensions_service) { + const ExtensionList* extensions = extensions_service->extensions(); + for (ExtensionList::const_iterator iter = extensions->begin(); + iter != extensions->end(); + ++iter) { + if ((*iter)->location() == Extension::INTERNAL) { + has_extensions = true; + break; + } + } + } + + bool promo_options = sync_ui_util::GetStatus( profile->GetProfileSyncService()) == sync_ui_util::SYNCED || - (extensions_service && extensions_service->HasInstalledExtensions()); + has_extensions; return !promo_closed && - promo_options_set && + promo_options && g_browser_process->GetApplicationLocale() == "en-US"; } diff --git a/chrome/browser/web_resource/web_resource_service.h b/chrome/browser/web_resource/web_resource_service.h index 43f5e71..70c8000 100644 --- a/chrome/browser/web_resource/web_resource_service.h +++ b/chrome/browser/web_resource/web_resource_service.h @@ -140,6 +140,13 @@ class WebResourceService // Puts parsed json data in the right places, and writes to prefs file. void OnWebResourceUnpacked(const DictionaryValue& parsed_json); + // Notify listeners that the state of a web resource has changed. + void WebResourceStateChange(); + + // Schedule a notification that a web resource is either going to become + // available or be no longer valid. + void ScheduleNotification(double ms_start_time, double ms_end_time); + // We need to be able to load parsed resource data into preferences file, // and get proper install directory. PrefService* prefs_; @@ -148,13 +155,15 @@ class WebResourceService // profile. Profile* profile_; - // Server from which we are currently pulling web resource data. - std::string web_resource_server_; - scoped_ptr<WebResourceFetcher> web_resource_fetcher_; ResourceDispatcherHost* resource_dispatcher_host_; + // Allows the creation of tasks to send a WEB_RESOURCE_STATE_CHANGED + // notification. This allows the WebResourceService to notify the New Tab + // Page immediately when a new web resource should be shown or removed. + ScopedRunnableMethodFactory<WebResourceService> service_factory_; + // Gets mutable dictionary attached to user's preferences, so that we // can write resource data back to user's pref file. DictionaryValue* web_resource_cache_; @@ -168,6 +177,10 @@ class WebResourceService // different for different builds of Chrome. int cache_update_delay_; + // True if a task has been set to update the cache when a new web resource + // becomes available. + bool web_resource_update_scheduled_; + DISALLOW_COPY_AND_ASSIGN(WebResourceService); }; |