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
|
// 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/bind.h"
#include "base/command_line.h"
#include "base/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/web_resource/notification_promo.h"
#include "chrome/common/chrome_notification_types.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 "googleurl/src/gurl.h"
namespace {
// Delay on first fetch so we don't interfere with startup.
const int kStartResourceFetchDelay = 5000;
// Delay between calls to fetch the promo json: 6 hours in production, and 3 min
// in debug.
const int kCacheUpdateDelay = 6 * 60 * 60 * 1000;
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).
const int kPromoServiceVersion = 7;
// The promotion type used for Unpack() and ScheduleNotificationOnInit().
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;
}
} // namespace
// static
void PromoResourceService::RegisterPrefs(PrefRegistrySimple* registry) {
registry->RegisterStringPref(prefs::kNtpPromoResourceCacheUpdate, "0");
NotificationPromo::RegisterPrefs(registry);
}
// static
void PromoResourceService::RegisterUserPrefs(PrefRegistrySyncable* registry) {
// TODO(dbeam): This is registered only for migration; remove in M28
// when all prefs have been cleared. http://crbug.com/168887
registry->RegisterStringPref(prefs::kNtpPromoResourceCacheUpdate,
"0",
PrefRegistrySyncable::UNSYNCABLE_PREF);
NotificationPromo::RegisterUserPrefs(registry);
}
// static
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, // append locale to URL
prefs::kNtpPromoResourceCacheUpdate,
kStartResourceFetchDelay,
GetCacheUpdateDelay()),
ALLOW_THIS_IN_INITIALIZER_LIST(
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) {
// 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) {
// The promo is active. Notify immediately.
PostNotification(0);
}
// Schedule the next notification to happen at the end of promotion.
PostNotification(ms_until_end);
} else {
// The promo (if any) has finished. Notify immediately.
PostNotification(0);
}
} else {
// The promo (if any) was apparently cancelled. Notify immediately.
PostNotification(0);
}
}
void PromoResourceService::ScheduleNotificationOnInit() {
// If the promo start is in the future, set a notification task to
// invalidate the NTP cache at the time of the promo start.
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) {
// Note that this could cause re-issuing a notification every time
// we receive an update from a server if something goes wrong.
// Given that this couldn't happen more frequently than every
// kCacheUpdateDelay milliseconds, we should be fine.
// TODO(achuith): This crashes if we post delay_ms = 0 to the message loop.
// during startup.
if (delay_ms > 0) {
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 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);
}
}
|