This source file includes following definitions.
- GetPromoResourceURL
- IsTest
- GetCacheUpdateDelay
- RegisterPrefs
- RegisterProfilePrefs
- MigrateUserPrefs
- weak_ptr_factory_
- ScheduleNotification
- ScheduleNotificationOnInit
- PostNotification
- PromoResourceStateChange
- Unpack
#include "chrome/browser/web_resource/promo_resource_service.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/message_loop/message_loop.h"
#include "base/prefs/pref_registry_simple.h"
#include "base/prefs/pref_service.h"
#include "base/threading/thread_restrictions.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/web_resource/notification_promo.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "components/user_prefs/pref_registry_syncable.h"
#include "content/public/browser/notification_service.h"
#include "url/gurl.h"
namespace {
const int kStartResourceFetchDelay = 5000;
const int kCacheUpdateDelay = 6 * 60 * 60 * 1000;
const int kTestCacheUpdateDelay = 3 * 60 * 1000;
const NotificationPromo::PromoType kValidPromoTypes[] = {
#if defined(OS_ANDROID) || defined(OS_IOS)
NotificationPromo::MOBILE_NTP_SYNC_PROMO,
#else
NotificationPromo::NTP_NOTIFICATION_PROMO,
NotificationPromo::NTP_BUBBLE_PROMO,
#endif
};
GURL GetPromoResourceURL() {
const std::string promo_server_url = CommandLine::ForCurrentProcess()->
GetSwitchValueASCII(switches::kPromoServerURL);
return promo_server_url.empty() ?
NotificationPromo::PromoServerURL() : GURL(promo_server_url);
}
bool IsTest() {
return CommandLine::ForCurrentProcess()->HasSwitch(switches::kPromoServerURL);
}
int GetCacheUpdateDelay() {
return IsTest() ? kTestCacheUpdateDelay : kCacheUpdateDelay;
}
}
void PromoResourceService::RegisterPrefs(PrefRegistrySimple* registry) {
registry->RegisterStringPref(prefs::kNtpPromoResourceCacheUpdate, "0");
NotificationPromo::RegisterPrefs(registry);
}
void PromoResourceService::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterStringPref(
prefs::kNtpPromoResourceCacheUpdate,
"0",
user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
NotificationPromo::RegisterProfilePrefs(registry);
}
void PromoResourceService::MigrateUserPrefs(PrefService* user_prefs) {
user_prefs->ClearPref(prefs::kNtpPromoResourceCacheUpdate);
NotificationPromo::MigrateUserPrefs(user_prefs);
}
PromoResourceService::PromoResourceService()
: WebResourceService(g_browser_process->local_state(),
GetPromoResourceURL(),
true,
prefs::kNtpPromoResourceCacheUpdate,
kStartResourceFetchDelay,
GetCacheUpdateDelay()),
weak_ptr_factory_(this) {
ScheduleNotificationOnInit();
}
PromoResourceService::~PromoResourceService() {
}
void PromoResourceService::ScheduleNotification(
const NotificationPromo& notification_promo) {
const double promo_start = notification_promo.StartTimeForGroup();
const double promo_end = notification_promo.EndTime();
if (promo_start > 0 && promo_end > 0) {
const int64 ms_until_start =
static_cast<int64>((base::Time::FromDoubleT(
promo_start) - base::Time::Now()).InMilliseconds());
const int64 ms_until_end =
static_cast<int64>((base::Time::FromDoubleT(
promo_end) - base::Time::Now()).InMilliseconds());
if (ms_until_start > 0) {
PostNotification(ms_until_start);
} else if (ms_until_end > 0) {
if (ms_until_start <= 0) {
PostNotification(0);
}
PostNotification(ms_until_end);
} else {
PostNotification(0);
}
} else {
PostNotification(0);
}
}
void PromoResourceService::ScheduleNotificationOnInit() {
for (size_t i = 0; i < arraysize(kValidPromoTypes); ++i) {
NotificationPromo notification_promo;
notification_promo.InitFromPrefs(kValidPromoTypes[i]);
ScheduleNotification(notification_promo);
}
}
void PromoResourceService::PostNotification(int64 delay_ms) {
if (delay_ms > 0) {
base::MessageLoop::current()->PostDelayedTask(
FROM_HERE,
base::Bind(&PromoResourceService::PromoResourceStateChange,
weak_ptr_factory_.GetWeakPtr()),
base::TimeDelta::FromMilliseconds(delay_ms));
} else if (delay_ms == 0) {
PromoResourceStateChange();
}
}
void PromoResourceService::PromoResourceStateChange() {
content::NotificationService* service =
content::NotificationService::current();
service->Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED,
content::Source<WebResourceService>(this),
content::NotificationService::NoDetails());
}
void PromoResourceService::Unpack(const base::DictionaryValue& parsed_json) {
for (size_t i = 0; i < arraysize(kValidPromoTypes); ++i) {
NotificationPromo notification_promo;
notification_promo.InitFromJson(parsed_json, kValidPromoTypes[i]);
if (notification_promo.new_notification())
ScheduleNotification(notification_promo);
}
}