OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/web_resource/promo_resource_service.h" | 5 #include "chrome/browser/web_resource/promo_resource_service.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "base/threading/thread_restrictions.h" | 10 #include "base/threading/thread_restrictions.h" |
11 #include "base/values.h" | 11 #include "base/values.h" |
12 #include "chrome/browser/browser_process.h" | 12 #include "chrome/browser/browser_process.h" |
13 #include "chrome/browser/prefs/pref_service.h" | 13 #include "chrome/browser/prefs/pref_service.h" |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/prefs/pref_service_simple.h" |
15 #include "chrome/browser/prefs/pref_service_syncable.h" | |
15 #include "chrome/browser/web_resource/notification_promo.h" | 16 #include "chrome/browser/web_resource/notification_promo.h" |
16 #include "chrome/common/chrome_notification_types.h" | 17 #include "chrome/common/chrome_notification_types.h" |
17 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
18 #include "chrome/common/pref_names.h" | 19 #include "chrome/common/pref_names.h" |
19 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/notification_service.h" | 21 #include "content/public/browser/notification_service.h" |
21 #include "googleurl/src/gurl.h" | 22 #include "googleurl/src/gurl.h" |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 // Delay on first fetch so we don't interfere with startup. | 26 // Delay on first fetch so we don't interfere with startup. |
26 const int kStartResourceFetchDelay = 5000; | 27 const int kStartResourceFetchDelay = 5000; |
27 | 28 |
28 // Delay between calls to fetch the promo json: 6 hours in production, and 3 min | 29 // Delay between calls to fetch the promo json: 6 hours in production, and 3 min |
29 // in debug. | 30 // in debug. |
30 const int kCacheUpdateDelay = 6 * 60 * 60 * 1000; | 31 const int kCacheUpdateDelay = 6 * 60 * 60 * 1000; |
31 const int kTestCacheUpdateDelay = 3 * 60 * 1000; | 32 const int kTestCacheUpdateDelay = 3 * 60 * 1000; |
32 | 33 |
33 // The version of the service (used to expire the cache when upgrading Chrome | 34 // The version of the service (used to expire the cache when upgrading Chrome |
34 // to versions with different types of promos). | 35 // to versions with different types of promos). |
35 const int kPromoServiceVersion = 7; | 36 const int kPromoServiceVersion = 7; |
36 | 37 |
37 // The promotion type used for Unpack() and ScheduleNotificationOnInit. | 38 // The promotion type used for Unpack() and ScheduleNotificationOnInit(). |
38 const NotificationPromo::PromoType kValidPromoTypes[] = { | 39 const NotificationPromo::PromoType kValidPromoTypes[] = { |
39 #if defined(OS_ANDROID) || defined(OS_IOS) | 40 #if defined(OS_ANDROID) || defined(OS_IOS) |
40 NotificationPromo::MOBILE_NTP_SYNC_PROMO, | 41 NotificationPromo::MOBILE_NTP_SYNC_PROMO, |
41 #else | 42 #else |
42 NotificationPromo::NTP_NOTIFICATION_PROMO, | 43 NotificationPromo::NTP_NOTIFICATION_PROMO, |
43 NotificationPromo::NTP_BUBBLE_PROMO, | 44 NotificationPromo::NTP_BUBBLE_PROMO, |
44 #endif | 45 #endif |
45 }; | 46 }; |
46 | 47 |
47 GURL GetPromoResourceURL() { | 48 GURL GetPromoResourceURL() { |
48 const std::string promo_server_url = CommandLine::ForCurrentProcess()-> | 49 const std::string promo_server_url = CommandLine::ForCurrentProcess()-> |
49 GetSwitchValueASCII(switches::kPromoServerURL); | 50 GetSwitchValueASCII(switches::kPromoServerURL); |
50 return promo_server_url.empty() ? | 51 return promo_server_url.empty() ? |
51 NotificationPromo::PromoServerURL() : GURL(promo_server_url); | 52 NotificationPromo::PromoServerURL() : GURL(promo_server_url); |
52 } | 53 } |
53 | 54 |
54 bool IsTest() { | 55 bool IsTest() { |
55 return CommandLine::ForCurrentProcess()->HasSwitch(switches::kPromoServerURL); | 56 return CommandLine::ForCurrentProcess()->HasSwitch(switches::kPromoServerURL); |
56 } | 57 } |
57 | 58 |
58 int GetCacheUpdateDelay() { | 59 int GetCacheUpdateDelay() { |
59 return IsTest() ? kTestCacheUpdateDelay : kCacheUpdateDelay; | 60 return IsTest() ? kTestCacheUpdateDelay : kCacheUpdateDelay; |
60 } | 61 } |
61 | 62 |
62 } // namespace | 63 } // namespace |
63 | 64 |
64 // static | 65 // static |
65 void PromoResourceService::RegisterPrefs(PrefServiceSimple* local_state) { | 66 void PromoResourceService::RegisterPrefs(PrefServiceSimple* local_state) { |
66 // TODO(achuith): Delete this in M26. http://crbug.com/143773 | 67 local_state->RegisterStringPref(prefs::kNtpPromoResourceCacheUpdate, "0"); |
67 // The promo service version number, and last locale. | 68 NotificationPromo::RegisterPrefs(local_state); |
68 const char kNtpPromoVersion[] = "ntp.promo_version"; | |
69 const char kNtpPromoLocale[] = "ntp.promo_locale"; | |
70 local_state->RegisterIntegerPref(kNtpPromoVersion, 0); | |
71 local_state->RegisterStringPref(kNtpPromoLocale, std::string()); | |
72 local_state->ClearPref(kNtpPromoVersion); | |
73 local_state->ClearPref(kNtpPromoLocale); | |
74 } | 69 } |
75 | 70 |
76 // static | 71 // static |
77 void PromoResourceService::RegisterUserPrefs(PrefServiceSyncable* prefs) { | 72 void PromoResourceService::RegisterUserPrefs(PrefServiceSyncable* prefs) { |
73 // TODO(dbeam): remove in M28 when all prefs have been cleared. | |
78 prefs->RegisterStringPref(prefs::kNtpPromoResourceCacheUpdate, | 74 prefs->RegisterStringPref(prefs::kNtpPromoResourceCacheUpdate, |
79 "0", | 75 "0", |
80 PrefServiceSyncable::UNSYNCABLE_PREF); | 76 PrefServiceSyncable::UNSYNCABLE_PREF); |
77 prefs->ClearPref(prefs::kNtpPromoResourceCacheUpdate); | |
81 NotificationPromo::RegisterUserPrefs(prefs); | 78 NotificationPromo::RegisterUserPrefs(prefs); |
82 } | 79 } |
83 | 80 |
84 PromoResourceService::PromoResourceService(Profile* profile) | 81 PromoResourceService::PromoResourceService() |
85 : WebResourceService(profile->GetPrefs(), | 82 : WebResourceService(g_browser_process->local_state(), |
86 GetPromoResourceURL(), | 83 GetPromoResourceURL(), |
87 true, // append locale to URL | 84 true, // append locale to URL |
88 prefs::kNtpPromoResourceCacheUpdate, | 85 prefs::kNtpPromoResourceCacheUpdate, |
89 kStartResourceFetchDelay, | 86 kStartResourceFetchDelay, |
90 GetCacheUpdateDelay()), | 87 GetCacheUpdateDelay()), |
91 profile_(profile), | |
92 ALLOW_THIS_IN_INITIALIZER_LIST( | 88 ALLOW_THIS_IN_INITIALIZER_LIST( |
93 weak_ptr_factory_(this)) { | 89 weak_ptr_factory_(this)) { |
94 ScheduleNotificationOnInit(); | 90 ScheduleNotificationOnInit(); |
95 } | 91 } |
96 | 92 |
97 PromoResourceService::~PromoResourceService() { | 93 PromoResourceService::~PromoResourceService() { |
98 } | 94 } |
99 | 95 |
96 // For testing. | |
Evan Stade
2013/01/03 05:01:08
put this comment in the header only.
| |
97 PromoResourceService::PromoResourceService(PrefService* prefs) | |
98 : WebResourceService(prefs, | |
99 GetPromoResourceURL(), | |
100 true, // append locale to URL | |
101 prefs::kNtpPromoResourceCacheUpdate, | |
102 kStartResourceFetchDelay, | |
103 GetCacheUpdateDelay()), | |
104 ALLOW_THIS_IN_INITIALIZER_LIST( | |
105 weak_ptr_factory_(this)) { | |
106 ScheduleNotificationOnInitForTesting(prefs); | |
Dan Beam
2013/01/03 05:05:47
this is triggering a presubmit warning, I'll see i
achuithb
2013/01/03 05:42:51
This particular presubmit warning is not reliable,
| |
107 } | |
108 | |
109 void PromoResourceService::ScheduleNotificationOnInitForTesting( | |
110 PrefService* prefs) { | |
111 // If the promo start is in the future, set a notification task to | |
112 // invalidate the NTP cache at the time of the promo start. | |
113 for (size_t i = 0; i < arraysize(kValidPromoTypes); ++i) { | |
114 NotificationPromo notification_promo(prefs); | |
115 notification_promo.InitFromPrefs(kValidPromoTypes[i]); | |
116 ScheduleNotification(notification_promo); | |
117 } | |
118 } | |
119 | |
100 void PromoResourceService::ScheduleNotification( | 120 void PromoResourceService::ScheduleNotification( |
101 const NotificationPromo& notification_promo) { | 121 const NotificationPromo& notification_promo) { |
102 const double promo_start = notification_promo.StartTimeForGroup(); | 122 const double promo_start = notification_promo.StartTimeForGroup(); |
103 const double promo_end = notification_promo.EndTime(); | 123 const double promo_end = notification_promo.EndTime(); |
104 | 124 |
105 if (promo_start > 0 && promo_end > 0) { | 125 if (promo_start > 0 && promo_end > 0) { |
106 const int64 ms_until_start = | 126 const int64 ms_until_start = |
107 static_cast<int64>((base::Time::FromDoubleT( | 127 static_cast<int64>((base::Time::FromDoubleT( |
108 promo_start) - base::Time::Now()).InMilliseconds()); | 128 promo_start) - base::Time::Now()).InMilliseconds()); |
109 const int64 ms_until_end = | 129 const int64 ms_until_end = |
(...skipping 16 matching lines...) Expand all Loading... | |
126 } else { | 146 } else { |
127 // The promo (if any) was apparently cancelled. Notify immediately. | 147 // The promo (if any) was apparently cancelled. Notify immediately. |
128 PostNotification(0); | 148 PostNotification(0); |
129 } | 149 } |
130 } | 150 } |
131 | 151 |
132 void PromoResourceService::ScheduleNotificationOnInit() { | 152 void PromoResourceService::ScheduleNotificationOnInit() { |
133 // If the promo start is in the future, set a notification task to | 153 // If the promo start is in the future, set a notification task to |
134 // invalidate the NTP cache at the time of the promo start. | 154 // invalidate the NTP cache at the time of the promo start. |
135 for (size_t i = 0; i < arraysize(kValidPromoTypes); ++i) { | 155 for (size_t i = 0; i < arraysize(kValidPromoTypes); ++i) { |
136 NotificationPromo notification_promo(profile_); | 156 NotificationPromo notification_promo; |
137 notification_promo.InitFromPrefs(kValidPromoTypes[i]); | 157 notification_promo.InitFromPrefs(kValidPromoTypes[i]); |
138 ScheduleNotification(notification_promo); | 158 ScheduleNotification(notification_promo); |
139 } | 159 } |
140 } | 160 } |
141 | 161 |
142 void PromoResourceService::PostNotification(int64 delay_ms) { | 162 void PromoResourceService::PostNotification(int64 delay_ms) { |
143 // Note that this could cause re-issuing a notification every time | 163 // Note that this could cause re-issuing a notification every time |
144 // we receive an update from a server if something goes wrong. | 164 // we receive an update from a server if something goes wrong. |
145 // Given that this couldn't happen more frequently than every | 165 // Given that this couldn't happen more frequently than every |
146 // kCacheUpdateDelay milliseconds, we should be fine. | 166 // kCacheUpdateDelay milliseconds, we should be fine. |
(...skipping 13 matching lines...) Expand all Loading... | |
160 void PromoResourceService::PromoResourceStateChange() { | 180 void PromoResourceService::PromoResourceStateChange() { |
161 content::NotificationService* service = | 181 content::NotificationService* service = |
162 content::NotificationService::current(); | 182 content::NotificationService::current(); |
163 service->Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, | 183 service->Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, |
164 content::Source<WebResourceService>(this), | 184 content::Source<WebResourceService>(this), |
165 content::NotificationService::NoDetails()); | 185 content::NotificationService::NoDetails()); |
166 } | 186 } |
167 | 187 |
168 void PromoResourceService::Unpack(const DictionaryValue& parsed_json) { | 188 void PromoResourceService::Unpack(const DictionaryValue& parsed_json) { |
169 for (size_t i = 0; i < arraysize(kValidPromoTypes); ++i) { | 189 for (size_t i = 0; i < arraysize(kValidPromoTypes); ++i) { |
170 NotificationPromo notification_promo(profile_); | 190 NotificationPromo notification_promo; |
171 notification_promo.InitFromJson(parsed_json, kValidPromoTypes[i]); | 191 notification_promo.InitFromJson(parsed_json, kValidPromoTypes[i]); |
172 if (notification_promo.new_notification()) | 192 if (notification_promo.new_notification()) |
173 ScheduleNotification(notification_promo); | 193 ScheduleNotification(notification_promo); |
174 } | 194 } |
175 } | 195 } |
OLD | NEW |