1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
|
// Copyright (c) 2012 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.h"
#include "base/command_line.h"
#include "base/message_loop.h"
#include "base/threading/thread_restrictions.h"
#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/common/chrome_notification_types.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h"
#include "googleurl/src/gurl.h"
namespace {
// Delay on first fetch so we don't interfere with startup.
static const int kStartResourceFetchDelay = 5000;
// Delay between calls to update the cache (12 hours), and 3 min in debug mode.
static const int kCacheUpdateDelay = 12 * 60 * 60 * 1000;
static const int kTestCacheUpdateDelay = 3 * 60 * 1000;
// The version of the service (used to expire the cache when upgrading Chrome
// to versions with different types of promos).
static const int kPromoServiceVersion = 7;
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;
}
} // namespace
// static
void PromoResourceService::RegisterPrefs(PrefService* local_state) {
local_state->RegisterIntegerPref(prefs::kNtpPromoVersion, 0);
local_state->RegisterStringPref(prefs::kNtpPromoLocale, std::string());
}
// static
void PromoResourceService::RegisterUserPrefs(PrefService* prefs) {
prefs->RegisterStringPref(prefs::kNtpPromoResourceCacheUpdate,
"0",
PrefService::UNSYNCABLE_PREF);
NotificationPromo::RegisterUserPrefs(prefs);
// TODO(achuith): Delete this in M22.
prefs->RegisterDoublePref(prefs::kNtpCustomLogoStart,
0,
PrefService::UNSYNCABLE_PREF);
prefs->RegisterDoublePref(prefs::kNtpCustomLogoEnd,
0,
PrefService::UNSYNCABLE_PREF);
prefs->ClearPref(prefs::kNtpCustomLogoStart);
prefs->ClearPref(prefs::kNtpCustomLogoEnd);
}
PromoResourceService::PromoResourceService(Profile* profile)
: WebResourceService(profile->GetPrefs(),
GetPromoResourceURL(),
true, // append locale to URL
prefs::kNtpPromoResourceCacheUpdate,
kStartResourceFetchDelay,
GetCacheUpdateDelay()),
profile_(profile),
ALLOW_THIS_IN_INITIALIZER_LIST(
weak_ptr_factory_(this)),
web_resource_update_scheduled_(false) {
ScheduleNotificationOnInit();
}
PromoResourceService::~PromoResourceService() {
}
void PromoResourceService::ScheduleNotification(double promo_start,
double promo_end) {
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) {
// Schedule the next notification to happen at the start of promotion.
PostNotification(ms_until_start);
} else if (ms_until_end > 0) {
if (ms_until_start <= 0) {
// Notify immediately if time is between start and end.
PostNotification(0);
}
// Schedule the next notification to happen at the end of promotion.
PostNotification(ms_until_end);
}
}
}
void PromoResourceService::ScheduleNotificationOnInit() {
std::string locale = g_browser_process->GetApplicationLocale();
if (GetPromoServiceVersion() != kPromoServiceVersion ||
GetPromoLocale() != locale) {
// If the promo service has been upgraded or Chrome switched locales,
// refresh the promos.
// TODO(achuith): Mixing local_state and prefs does not work for
// multi-profile case. We should probably store version/locale in prefs_
// as well.
PrefService* local_state = g_browser_process->local_state();
local_state->SetInteger(prefs::kNtpPromoVersion, kPromoServiceVersion);
local_state->SetString(prefs::kNtpPromoLocale, locale);
prefs_->ClearPref(prefs::kNtpPromoResourceCacheUpdate);
PostNotification(0);
} else {
// 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);
}
}
void PromoResourceService::PostNotification(int64 delay_ms) {
if (web_resource_update_scheduled_)
return;
// TODO(achuith): This crashes if we post delay_ms = 0 to the message loop.
// during startup.
if (delay_ms > 0) {
web_resource_update_scheduled_ = true;
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() {
web_resource_update_scheduled_ = false;
content::NotificationService* service =
content::NotificationService::current();
service->Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED,
content::Source<WebResourceService>(this),
content::NotificationService::NoDetails());
}
int PromoResourceService::GetPromoServiceVersion() {
PrefService* local_state = g_browser_process->local_state();
return local_state->GetInteger(prefs::kNtpPromoVersion);
}
std::string PromoResourceService::GetPromoLocale() {
PrefService* local_state = g_browser_process->local_state();
return local_state->GetString(prefs::kNtpPromoLocale);
}
void PromoResourceService::Unpack(const DictionaryValue& parsed_json) {
NotificationPromo notification_promo(profile_);
notification_promo.InitFromJson(parsed_json);
if (notification_promo.new_notification()) {
ScheduleNotification(notification_promo.StartTimeForGroup(),
notification_promo.EndTime());
}
}
bool PromoResourceService::CanShowNotificationPromo(Profile* profile) {
NotificationPromo notification_promo(profile);
notification_promo.InitFromPrefs();
return notification_promo.CanShow();
}
|