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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
// 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/notification_promo.h"
#include <cmath>
#include <vector>
#include "base/bind.h"
#include "base/rand_util.h"
#include "base/string_number_conversions.h"
#include "base/time.h"
#include "base/values.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile_impl.h"
#include "chrome/browser/web_resource/promo_resource_service.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/net/url_util.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/user_metrics.h"
#include "googleurl/src/gurl.h"
using content::UserMetricsAction;
namespace {
const int kDefaultGroupSize = 100;
const char promo_server_url[] = "https://clients3.google.com/crsignal/client";
double GetTimeFromPrefs(PrefService* prefs, const char* pref) {
return prefs->HasPrefPath(pref) ? prefs->GetDouble(pref) : 0.0;
}
// Returns a string suitable for the Promo Server URL 'osname' value.
const char* PlatformString() {
#if defined(OS_WIN)
return "win";
#elif defined(OS_MACOSX)
return "mac";
#elif defined(OS_CHROMEOS)
return "chromeos";
#elif defined(OS_LINUX)
return "linux";
#else
return "none";
#endif
}
// Returns a string suitable for the Promo Server URL 'dist' value.
const char* ChannelString() {
#if defined (OS_WIN)
// GetChannel hits the registry on Windows. See http://crbug.com/70898.
// TODO(achuith): Move NotificationPromo::PromoServerURL to the blocking pool.
base::ThreadRestrictions::ScopedAllowIO allow_io;
#endif
const chrome::VersionInfo::Channel channel =
chrome::VersionInfo::GetChannel();
switch (channel) {
case chrome::VersionInfo::CHANNEL_CANARY:
return "canary";
case chrome::VersionInfo::CHANNEL_DEV:
return "dev";
case chrome::VersionInfo::CHANNEL_BETA:
return "beta";
case chrome::VersionInfo::CHANNEL_STABLE:
return "stable";
default:
return "none";
}
}
} // namespace
NotificationPromo::NotificationPromo(Profile* profile)
: profile_(profile),
prefs_(profile_->GetPrefs()),
start_(0.0),
end_(0.0),
num_groups_(kDefaultGroupSize),
initial_segment_(0),
increment_(1),
time_slice_(0),
max_group_(0),
max_views_(0),
group_(0),
views_(0),
closed_(false),
gplus_required_(false),
new_notification_(false) {
DCHECK(profile);
DCHECK(prefs_);
}
NotificationPromo::~NotificationPromo() {}
void NotificationPromo::InitFromJson(const DictionaryValue& json) {
DictionaryValue* root = NULL;
if (!json.GetDictionary("ntp_notification_promo", &root))
return;
// Strings. Assume the first one is the promo text.
DictionaryValue* strings;
if (root->GetDictionary("strings", &strings)) {
DictionaryValue::Iterator iter(*strings);
iter.value().GetAsString(&promo_text_);
DVLOG(1) << "promo_text_=" << promo_text_;
}
// Date.
ListValue* date_list;
if (root->GetList("date", &date_list)) {
DictionaryValue* date;
if (date_list->GetDictionary(0, &date)) {
std::string time_str;
base::Time time;
if (date->GetString("start", &time_str) &&
base::Time::FromString(time_str.c_str(), &time)) {
start_ = time.ToDoubleT();
DVLOG(1) << "start str=" << time_str
<< ", start_="<< base::DoubleToString(start_);
}
if (date->GetString("end", &time_str) &&
base::Time::FromString(time_str.c_str(), &time)) {
end_ = time.ToDoubleT();
DVLOG(1) << "end str =" << time_str
<< ", end_=" << base::DoubleToString(end_);
}
}
}
// Grouping.
DictionaryValue* grouping;
if (root->GetDictionary("grouping", &grouping)) {
grouping->GetInteger("buckets", &num_groups_);
grouping->GetInteger("segment", &initial_segment_);
grouping->GetInteger("increment", &increment_);
grouping->GetInteger("increment_frequency", &time_slice_);
grouping->GetInteger("increment_max", &max_group_);
DVLOG(1) << "num_groups_=" << num_groups_;
DVLOG(1) << "initial_segment_ = " << initial_segment_;
DVLOG(1) << "increment_ = " << increment_;
DVLOG(1) << "time_slice_ = " << time_slice_;
DVLOG(1) << "max_group_ = " << max_group_;
}
// Payload.
DictionaryValue* payload;
if (root->GetDictionary("payload", &payload)) {
payload->GetBoolean("gplus_required", &gplus_required_);
DVLOG(1) << "gplus_required_ = " << gplus_required_;
}
root->GetInteger("max_views", &max_views_);
DVLOG(1) << "max_views_ " << max_views_;
CheckForNewNotification();
}
void NotificationPromo::CheckForNewNotification() {
const double old_start = GetTimeFromPrefs(prefs_, prefs::kNtpPromoStart);
const double old_end = GetTimeFromPrefs(prefs_, prefs::kNtpPromoEnd);
new_notification_ = old_start != start_ || old_end != end_;
if (new_notification_)
OnNewNotification();
}
void NotificationPromo::OnNewNotification() {
// Create a new promo group.
group_ = base::RandInt(0, num_groups_ - 1);
WritePrefs();
}
// static
void NotificationPromo::RegisterUserPrefs(PrefService* prefs) {
prefs->RegisterStringPref(prefs::kNtpPromoLine,
std::string(),
PrefService::UNSYNCABLE_PREF);
prefs->RegisterDoublePref(prefs::kNtpPromoStart,
0,
PrefService::UNSYNCABLE_PREF);
prefs->RegisterDoublePref(prefs::kNtpPromoEnd,
0,
PrefService::UNSYNCABLE_PREF);
prefs->RegisterIntegerPref(prefs::kNtpPromoNumGroups,
0,
PrefService::UNSYNCABLE_PREF);
prefs->RegisterIntegerPref(prefs::kNtpPromoInitialSegment,
0,
PrefService::UNSYNCABLE_PREF);
prefs->RegisterIntegerPref(prefs::kNtpPromoIncrement,
1,
PrefService::UNSYNCABLE_PREF);
prefs->RegisterIntegerPref(prefs::kNtpPromoGroupTimeSlice,
0,
PrefService::UNSYNCABLE_PREF);
prefs->RegisterIntegerPref(prefs::kNtpPromoGroupMax,
0,
PrefService::UNSYNCABLE_PREF);
prefs->RegisterIntegerPref(prefs::kNtpPromoViewsMax,
0,
PrefService::UNSYNCABLE_PREF);
prefs->RegisterIntegerPref(prefs::kNtpPromoGroup,
0,
PrefService::UNSYNCABLE_PREF);
prefs->RegisterIntegerPref(prefs::kNtpPromoViews,
0,
PrefService::UNSYNCABLE_PREF);
prefs->RegisterBooleanPref(prefs::kNtpPromoClosed,
false,
PrefService::UNSYNCABLE_PREF);
prefs->RegisterBooleanPref(prefs::kNtpPromoGplusRequired,
false,
PrefService::UNSYNCABLE_PREF);
// TODO(achuith): Delete this in M22.
prefs->RegisterIntegerPref(prefs::kNtpPromoBuild,
0,
PrefService::UNSYNCABLE_PREF);
prefs->RegisterIntegerPref(prefs::kNtpPromoPlatform,
0,
PrefService::UNSYNCABLE_PREF);
prefs->ClearPref(prefs::kNtpPromoBuild);
prefs->ClearPref(prefs::kNtpPromoPlatform);
}
void NotificationPromo::WritePrefs() {
prefs_->SetString(prefs::kNtpPromoLine, promo_text_);
prefs_->SetDouble(prefs::kNtpPromoStart, start_);
prefs_->SetDouble(prefs::kNtpPromoEnd, end_);
prefs_->SetInteger(prefs::kNtpPromoNumGroups, num_groups_);
prefs_->SetInteger(prefs::kNtpPromoInitialSegment, initial_segment_);
prefs_->SetInteger(prefs::kNtpPromoIncrement, increment_);
prefs_->SetInteger(prefs::kNtpPromoGroupTimeSlice, time_slice_);
prefs_->SetInteger(prefs::kNtpPromoGroupMax, max_group_);
prefs_->SetInteger(prefs::kNtpPromoViewsMax, max_views_);
prefs_->SetInteger(prefs::kNtpPromoGroup, group_);
prefs_->SetInteger(prefs::kNtpPromoViews, views_);
prefs_->SetBoolean(prefs::kNtpPromoClosed, closed_);
prefs_->SetBoolean(prefs::kNtpPromoGplusRequired, gplus_required_);
}
void NotificationPromo::InitFromPrefs() {
promo_text_ = prefs_->GetString(prefs::kNtpPromoLine);
start_ = prefs_->GetDouble(prefs::kNtpPromoStart);
end_ = prefs_->GetDouble(prefs::kNtpPromoEnd);
num_groups_ = prefs_->GetInteger(prefs::kNtpPromoNumGroups);
initial_segment_ = prefs_->GetInteger(prefs::kNtpPromoInitialSegment);
increment_ = prefs_->GetInteger(prefs::kNtpPromoIncrement);
time_slice_ = prefs_->GetInteger(prefs::kNtpPromoGroupTimeSlice);
max_group_ = prefs_->GetInteger(prefs::kNtpPromoGroupMax);
max_views_ = prefs_->GetInteger(prefs::kNtpPromoViewsMax);
group_ = prefs_->GetInteger(prefs::kNtpPromoGroup);
views_ = prefs_->GetInteger(prefs::kNtpPromoViews);
closed_ = prefs_->GetBoolean(prefs::kNtpPromoClosed);
gplus_required_ = prefs_->GetBoolean(prefs::kNtpPromoGplusRequired);
}
bool NotificationPromo::CanShow() const {
return !closed_ &&
!promo_text_.empty() &&
group_ < max_group_ &&
!ExceedsMaxViews() &&
base::Time::FromDoubleT(StartTimeForGroup()) < base::Time::Now() &&
base::Time::FromDoubleT(EndTime()) > base::Time::Now() &&
IsGPlusRequired();
}
void NotificationPromo::HandleClosed() {
content::RecordAction(UserMetricsAction("NTPPromoClosed"));
prefs_->SetBoolean(prefs::kNtpPromoClosed, true);
}
bool NotificationPromo::HandleViewed() {
content::RecordAction(UserMetricsAction("NTPPromoShown"));
if (prefs_->HasPrefPath(prefs::kNtpPromoViewsMax))
max_views_ = prefs_->GetInteger(prefs::kNtpPromoViewsMax);
if (prefs_->HasPrefPath(prefs::kNtpPromoViews))
views_ = prefs_->GetInteger(prefs::kNtpPromoViews);
prefs_->SetInteger(prefs::kNtpPromoViews, ++views_);
return ExceedsMaxViews();
}
bool NotificationPromo::ExceedsMaxViews() const {
return (max_views_ == 0) ? false : views_ >= max_views_;
}
bool NotificationPromo::IsGPlusRequired() const {
return !gplus_required_ || prefs_->GetBoolean(prefs::kIsGooglePlusUser);
}
// static
GURL NotificationPromo::PromoServerURL() {
GURL url(promo_server_url);
url = chrome_common_net::AppendQueryParameter(
url, "dist", ChannelString());
url = chrome_common_net::AppendQueryParameter(
url, "osname", PlatformString());
url = chrome_common_net::AppendQueryParameter(
url, "branding", chrome::VersionInfo().Version());
DVLOG(1) << "PromoServerURL=" << url.spec();
// Note that locale param is added by WebResourceService.
return url;
}
double NotificationPromo::StartTimeForGroup() const {
if (group_ < initial_segment_)
return start_;
return start_ +
std::ceil(static_cast<float>(group_ - initial_segment_ + 1) / increment_)
* time_slice_;
}
double NotificationPromo::EndTime() const {
return end_;
}
|