promo_resource_service.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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.
4
5#include "chrome/browser/web_resource/promo_resource_service.h"
6
7#include "base/bind.h"
8#include "base/command_line.h"
9#include "base/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 "chrome/common/chrome_notification_types.h"
17#include "chrome/common/chrome_switches.h"
18#include "chrome/common/pref_names.h"
19#include "components/user_prefs/pref_registry_syncable.h"
20#include "content/public/browser/notification_service.h"
21#include "googleurl/src/gurl.h"
22
23namespace {
24
25// Delay on first fetch so we don't interfere with startup.
26const int kStartResourceFetchDelay = 5000;
27
28// Delay between calls to fetch the promo json: 6 hours in production, and 3 min
29// in debug.
30const int kCacheUpdateDelay = 6 * 60 * 60 * 1000;
31const int kTestCacheUpdateDelay = 3 * 60 * 1000;
32
33// The version of the service (used to expire the cache when upgrading Chrome
34// to versions with different types of promos).
35const int kPromoServiceVersion = 7;
36
37// The promotion type used for Unpack() and ScheduleNotificationOnInit().
38const NotificationPromo::PromoType kValidPromoTypes[] = {
39#if defined(OS_ANDROID) || defined(OS_IOS)
40    NotificationPromo::MOBILE_NTP_SYNC_PROMO,
41#else
42    NotificationPromo::NTP_NOTIFICATION_PROMO,
43    NotificationPromo::NTP_BUBBLE_PROMO,
44#endif
45};
46
47GURL GetPromoResourceURL() {
48  const std::string promo_server_url = CommandLine::ForCurrentProcess()->
49      GetSwitchValueASCII(switches::kPromoServerURL);
50  return promo_server_url.empty() ?
51      NotificationPromo::PromoServerURL() : GURL(promo_server_url);
52}
53
54bool IsTest() {
55  return CommandLine::ForCurrentProcess()->HasSwitch(switches::kPromoServerURL);
56}
57
58int GetCacheUpdateDelay() {
59  return IsTest() ? kTestCacheUpdateDelay : kCacheUpdateDelay;
60}
61
62}  // namespace
63
64// static
65void PromoResourceService::RegisterPrefs(PrefRegistrySimple* registry) {
66  registry->RegisterStringPref(prefs::kNtpPromoResourceCacheUpdate, "0");
67  NotificationPromo::RegisterPrefs(registry);
68}
69
70// static
71void PromoResourceService::RegisterUserPrefs(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,
75                               "0",
76                               PrefRegistrySyncable::UNSYNCABLE_PREF);
77  NotificationPromo::RegisterUserPrefs(registry);
78}
79
80// static
81void PromoResourceService::MigrateUserPrefs(PrefService* user_prefs) {
82  user_prefs->ClearPref(prefs::kNtpPromoResourceCacheUpdate);
83  NotificationPromo::MigrateUserPrefs(user_prefs);
84}
85
86PromoResourceService::PromoResourceService()
87    : WebResourceService(g_browser_process->local_state(),
88                         GetPromoResourceURL(),
89                         true,  // append locale to URL
90                         prefs::kNtpPromoResourceCacheUpdate,
91                         kStartResourceFetchDelay,
92                         GetCacheUpdateDelay()),
93                         ALLOW_THIS_IN_INITIALIZER_LIST(
94                             weak_ptr_factory_(this)) {
95  ScheduleNotificationOnInit();
96}
97
98PromoResourceService::~PromoResourceService() {
99}
100
101void PromoResourceService::ScheduleNotification(
102    const NotificationPromo& notification_promo) {
103  const double promo_start = notification_promo.StartTimeForGroup();
104  const double promo_end = notification_promo.EndTime();
105
106  if (promo_start > 0 && promo_end > 0) {
107    const int64 ms_until_start =
108        static_cast<int64>((base::Time::FromDoubleT(
109            promo_start) - base::Time::Now()).InMilliseconds());
110    const int64 ms_until_end =
111        static_cast<int64>((base::Time::FromDoubleT(
112            promo_end) - base::Time::Now()).InMilliseconds());
113    if (ms_until_start > 0) {
114      // Schedule the next notification to happen at the start of promotion.
115      PostNotification(ms_until_start);
116    } else if (ms_until_end > 0) {
117      if (ms_until_start <= 0) {
118        // The promo is active.  Notify immediately.
119        PostNotification(0);
120      }
121      // Schedule the next notification to happen at the end of promotion.
122      PostNotification(ms_until_end);
123    } else {
124      // The promo (if any) has finished.  Notify immediately.
125      PostNotification(0);
126    }
127  } else {
128      // The promo (if any) was apparently cancelled.  Notify immediately.
129      PostNotification(0);
130  }
131}
132
133void PromoResourceService::ScheduleNotificationOnInit() {
134  // If the promo start is in the future, set a notification task to
135  // invalidate the NTP cache at the time of the promo start.
136  for (size_t i = 0; i < arraysize(kValidPromoTypes); ++i) {
137    NotificationPromo notification_promo;
138    notification_promo.InitFromPrefs(kValidPromoTypes[i]);
139    ScheduleNotification(notification_promo);
140  }
141}
142
143void PromoResourceService::PostNotification(int64 delay_ms) {
144  // Note that this could cause re-issuing a notification every time
145  // we receive an update from a server if something goes wrong.
146  // Given that this couldn't happen more frequently than every
147  // kCacheUpdateDelay milliseconds, we should be fine.
148  // TODO(achuith): This crashes if we post delay_ms = 0 to the message loop.
149  // during startup.
150  if (delay_ms > 0) {
151    MessageLoop::current()->PostDelayedTask(
152        FROM_HERE,
153        base::Bind(&PromoResourceService::PromoResourceStateChange,
154                   weak_ptr_factory_.GetWeakPtr()),
155        base::TimeDelta::FromMilliseconds(delay_ms));
156  } else if (delay_ms == 0) {
157    PromoResourceStateChange();
158  }
159}
160
161void PromoResourceService::PromoResourceStateChange() {
162  content::NotificationService* service =
163      content::NotificationService::current();
164  service->Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED,
165                  content::Source<WebResourceService>(this),
166                  content::NotificationService::NoDetails());
167}
168
169void PromoResourceService::Unpack(const 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);
175  }
176}
177