[Sync] Rename PSS::IsSyncEnabled to PSS::IsSyncAllowedByFlag.
[chromium-blink-merge.git] / chrome / browser / web_resource / promo_resource_service.cc
blob3fdeda2b4a8fdd4777440fd52ad4e5ebcfcadf3e
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"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/prefs/pref_registry_simple.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/threading/thread_restrictions.h"
13 #include "base/values.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/web_resource/notification_promo.h"
16 #include "components/pref_registry/pref_registry_syncable.h"
17 #include "components/web_resource/web_resource_pref_names.h"
18 #include "components/web_resource/web_resource_switches.h"
19 #include "url/gurl.h"
21 namespace {
23 // Delay on first fetch so we don't interfere with startup.
24 const int kStartResourceFetchDelay = 5000;
26 // Delay between calls to fetch the promo json: 6 hours in production, and 3 min
27 // in debug.
28 const int kCacheUpdateDelay = 6 * 60 * 60 * 1000;
29 const int kTestCacheUpdateDelay = 3 * 60 * 1000;
31 // The promotion type used for Unpack() and ScheduleNotificationOnInit().
32 const NotificationPromo::PromoType kValidPromoTypes[] = {
33 #if defined(OS_ANDROID) || defined(OS_IOS)
34 NotificationPromo::MOBILE_NTP_SYNC_PROMO,
35 #if defined(OS_IOS)
36 NotificationPromo::MOBILE_NTP_WHATS_NEW_PROMO,
37 #endif // defined(OS_IOS)
38 #else
39 NotificationPromo::NTP_NOTIFICATION_PROMO,
40 NotificationPromo::NTP_BUBBLE_PROMO,
41 #endif
44 GURL GetPromoResourceURL() {
45 const std::string promo_server_url =
46 base::CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
47 switches::kPromoServerURL);
48 return promo_server_url.empty() ?
49 NotificationPromo::PromoServerURL() : GURL(promo_server_url);
52 bool IsTest() {
53 return base::CommandLine::ForCurrentProcess()->HasSwitch(
54 switches::kPromoServerURL);
57 int GetCacheUpdateDelay() {
58 return IsTest() ? kTestCacheUpdateDelay : kCacheUpdateDelay;
61 } // namespace
63 // static
64 void PromoResourceService::RegisterPrefs(PrefRegistrySimple* registry) {
65 registry->RegisterStringPref(prefs::kNtpPromoResourceCacheUpdate, "0");
66 NotificationPromo::RegisterPrefs(registry);
69 // static
70 void PromoResourceService::RegisterProfilePrefs(
71 user_prefs::PrefRegistrySyncable* registry) {
72 // TODO(dbeam): This is registered only for migration; remove in M28
73 // when all prefs have been cleared. http://crbug.com/168887
74 registry->RegisterStringPref(prefs::kNtpPromoResourceCacheUpdate, "0");
75 NotificationPromo::RegisterProfilePrefs(registry);
78 // static
79 void PromoResourceService::MigrateUserPrefs(PrefService* user_prefs) {
80 user_prefs->ClearPref(prefs::kNtpPromoResourceCacheUpdate);
81 NotificationPromo::MigrateUserPrefs(user_prefs);
84 PromoResourceService::PromoResourceService()
85 : ChromeWebResourceService(g_browser_process->local_state(),
86 GetPromoResourceURL(),
87 true, // append locale to URL
88 prefs::kNtpPromoResourceCacheUpdate,
89 kStartResourceFetchDelay,
90 GetCacheUpdateDelay()),
91 weak_ptr_factory_(this) {
92 ScheduleNotificationOnInit();
95 PromoResourceService::~PromoResourceService() {
98 scoped_ptr<PromoResourceService::StateChangedSubscription>
99 PromoResourceService::RegisterStateChangedCallback(
100 const base::Closure& closure) {
101 return callback_list_.Add(closure);
104 void PromoResourceService::ScheduleNotification(
105 const NotificationPromo& notification_promo) {
106 const double promo_start = notification_promo.StartTimeForGroup();
107 const double promo_end = notification_promo.EndTime();
109 if (promo_start > 0 && promo_end > 0) {
110 const int64 ms_until_start =
111 static_cast<int64>((base::Time::FromDoubleT(
112 promo_start) - base::Time::Now()).InMilliseconds());
113 const int64 ms_until_end =
114 static_cast<int64>((base::Time::FromDoubleT(
115 promo_end) - base::Time::Now()).InMilliseconds());
116 if (ms_until_start > 0) {
117 // Schedule the next notification to happen at the start of promotion.
118 PostNotification(ms_until_start);
119 } else if (ms_until_end > 0) {
120 if (ms_until_start <= 0) {
121 // The promo is active. Notify immediately.
122 PostNotification(0);
124 // Schedule the next notification to happen at the end of promotion.
125 PostNotification(ms_until_end);
126 } else {
127 // The promo (if any) has finished. Notify immediately.
128 PostNotification(0);
130 } else {
131 // The promo (if any) was apparently cancelled. Notify immediately.
132 PostNotification(0);
136 void PromoResourceService::ScheduleNotificationOnInit() {
137 // If the promo start is in the future, set a notification task to
138 // invalidate the NTP cache at the time of the promo start.
139 for (size_t i = 0; i < arraysize(kValidPromoTypes); ++i) {
140 NotificationPromo notification_promo;
141 notification_promo.InitFromPrefs(kValidPromoTypes[i]);
142 ScheduleNotification(notification_promo);
146 void PromoResourceService::PostNotification(int64 delay_ms) {
147 // Note that this could cause re-issuing a notification every time
148 // we receive an update from a server if something goes wrong.
149 // Given that this couldn't happen more frequently than every
150 // kCacheUpdateDelay milliseconds, we should be fine.
151 // TODO(achuith): This crashes if we post delay_ms = 0 to the message loop.
152 // during startup.
153 if (delay_ms > 0) {
154 base::MessageLoop::current()->PostDelayedTask(
155 FROM_HERE,
156 base::Bind(&PromoResourceService::PromoResourceStateChange,
157 weak_ptr_factory_.GetWeakPtr()),
158 base::TimeDelta::FromMilliseconds(delay_ms));
159 } else if (delay_ms == 0) {
160 PromoResourceStateChange();
164 void PromoResourceService::PromoResourceStateChange() {
165 callback_list_.Notify();
168 void PromoResourceService::Unpack(const base::DictionaryValue& parsed_json) {
169 for (size_t i = 0; i < arraysize(kValidPromoTypes); ++i) {
170 NotificationPromo notification_promo;
171 notification_promo.InitFromJson(parsed_json, kValidPromoTypes[i]);
172 if (notification_promo.new_notification())
173 ScheduleNotification(notification_promo);