1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/web_resource/promo_resource_service.h"
8 #include "base/command_line.h"
9 #include "base/location.h"
10 #include "base/prefs/pref_registry_simple.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/single_thread_task_runner.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "base/threading/thread_restrictions.h"
15 #include "base/values.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/web_resource/notification_promo.h"
18 #include "components/pref_registry/pref_registry_syncable.h"
19 #include "components/web_resource/web_resource_pref_names.h"
20 #include "components/web_resource/web_resource_switches.h"
25 // Delay on first fetch so we don't interfere with startup.
26 const int kStartResourceFetchDelay
= 5000;
28 // Delay between calls to fetch the promo json: 6 hours in production, and 3 min
30 const int kCacheUpdateDelay
= 6 * 60 * 60 * 1000;
31 const int kTestCacheUpdateDelay
= 3 * 60 * 1000;
33 // The promotion type used for Unpack() and ScheduleNotificationOnInit().
34 const NotificationPromo::PromoType kValidPromoTypes
[] = {
35 #if defined(OS_ANDROID) || defined(OS_IOS)
36 NotificationPromo::MOBILE_NTP_SYNC_PROMO
,
38 NotificationPromo::MOBILE_NTP_WHATS_NEW_PROMO
,
39 #endif // defined(OS_IOS)
41 NotificationPromo::NTP_NOTIFICATION_PROMO
,
42 NotificationPromo::NTP_BUBBLE_PROMO
,
46 GURL
GetPromoResourceURL() {
47 const std::string promo_server_url
=
48 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
49 switches::kPromoServerURL
);
50 return promo_server_url
.empty() ?
51 NotificationPromo::PromoServerURL() : GURL(promo_server_url
);
55 return base::CommandLine::ForCurrentProcess()->HasSwitch(
56 switches::kPromoServerURL
);
59 int GetCacheUpdateDelay() {
60 return IsTest() ? kTestCacheUpdateDelay
: kCacheUpdateDelay
;
66 void PromoResourceService::RegisterPrefs(PrefRegistrySimple
* registry
) {
67 registry
->RegisterStringPref(prefs::kNtpPromoResourceCacheUpdate
, "0");
68 NotificationPromo::RegisterPrefs(registry
);
72 void PromoResourceService::RegisterProfilePrefs(
73 user_prefs::PrefRegistrySyncable
* registry
) {
74 // TODO(dbeam): This is registered only for migration; remove in M28
75 // when all prefs have been cleared. http://crbug.com/168887
76 registry
->RegisterStringPref(prefs::kNtpPromoResourceCacheUpdate
, "0");
77 NotificationPromo::RegisterProfilePrefs(registry
);
81 void PromoResourceService::MigrateUserPrefs(PrefService
* user_prefs
) {
82 user_prefs
->ClearPref(prefs::kNtpPromoResourceCacheUpdate
);
83 NotificationPromo::MigrateUserPrefs(user_prefs
);
86 PromoResourceService::PromoResourceService()
87 : ChromeWebResourceService(g_browser_process
->local_state(),
88 GetPromoResourceURL(),
89 true, // append locale to URL
90 prefs::kNtpPromoResourceCacheUpdate
,
91 kStartResourceFetchDelay
,
92 GetCacheUpdateDelay()),
93 weak_ptr_factory_(this) {
94 ScheduleNotificationOnInit();
97 PromoResourceService::~PromoResourceService() {
100 scoped_ptr
<PromoResourceService::StateChangedSubscription
>
101 PromoResourceService::RegisterStateChangedCallback(
102 const base::Closure
& closure
) {
103 return callback_list_
.Add(closure
);
106 void PromoResourceService::ScheduleNotification(
107 const NotificationPromo
& notification_promo
) {
108 const double promo_start
= notification_promo
.StartTimeForGroup();
109 const double promo_end
= notification_promo
.EndTime();
111 if (promo_start
> 0 && promo_end
> 0) {
112 const int64 ms_until_start
=
113 static_cast<int64
>((base::Time::FromDoubleT(
114 promo_start
) - base::Time::Now()).InMilliseconds());
115 const int64 ms_until_end
=
116 static_cast<int64
>((base::Time::FromDoubleT(
117 promo_end
) - base::Time::Now()).InMilliseconds());
118 if (ms_until_start
> 0) {
119 // Schedule the next notification to happen at the start of promotion.
120 PostNotification(ms_until_start
);
121 } else if (ms_until_end
> 0) {
122 if (ms_until_start
<= 0) {
123 // The promo is active. Notify immediately.
126 // Schedule the next notification to happen at the end of promotion.
127 PostNotification(ms_until_end
);
129 // The promo (if any) has finished. Notify immediately.
133 // The promo (if any) was apparently cancelled. Notify immediately.
138 void PromoResourceService::ScheduleNotificationOnInit() {
139 // If the promo start is in the future, set a notification task to
140 // invalidate the NTP cache at the time of the promo start.
141 for (size_t i
= 0; i
< arraysize(kValidPromoTypes
); ++i
) {
142 NotificationPromo notification_promo
;
143 notification_promo
.InitFromPrefs(kValidPromoTypes
[i
]);
144 ScheduleNotification(notification_promo
);
148 void PromoResourceService::PostNotification(int64 delay_ms
) {
149 // Note that this could cause re-issuing a notification every time
150 // we receive an update from a server if something goes wrong.
151 // Given that this couldn't happen more frequently than every
152 // kCacheUpdateDelay milliseconds, we should be fine.
153 // TODO(achuith): This crashes if we post delay_ms = 0 to the message loop.
156 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
157 FROM_HERE
, base::Bind(&PromoResourceService::PromoResourceStateChange
,
158 weak_ptr_factory_
.GetWeakPtr()),
159 base::TimeDelta::FromMilliseconds(delay_ms
));
160 } else if (delay_ms
== 0) {
161 PromoResourceStateChange();
165 void PromoResourceService::PromoResourceStateChange() {
166 callback_list_
.Notify();
169 void PromoResourceService::Unpack(const base::DictionaryValue
& parsed_json
) {
170 for (size_t i
= 0; i
< arraysize(kValidPromoTypes
); ++i
) {
171 NotificationPromo notification_promo
;
172 notification_promo
.InitFromJson(parsed_json
, kValidPromoTypes
[i
]);
173 if (notification_promo
.new_notification())
174 ScheduleNotification(notification_promo
);