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_simple.h" |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/prefs/pref_service_syncable.h" |
15 #include "chrome/browser/web_resource/notification_promo.h" | 15 #include "chrome/browser/web_resource/notification_promo.h" |
16 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" |
17 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
18 #include "chrome/common/pref_names.h" | 18 #include "chrome/common/pref_names.h" |
19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
21 #include "googleurl/src/gurl.h" | 21 #include "googleurl/src/gurl.h" |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 // Delay on first fetch so we don't interfere with startup. | 25 // Delay on first fetch so we don't interfere with startup. |
26 const int kStartResourceFetchDelay = 5000; | 26 const int kStartResourceFetchDelay = 5000; |
27 | 27 |
28 // Delay between calls to fetch the promo json: 6 hours in production, and 3 min | 28 // Delay between calls to fetch the promo json: 6 hours in production, and 3 min |
29 // in debug. | 29 // in debug. |
30 const int kCacheUpdateDelay = 6 * 60 * 60 * 1000; | 30 const int kCacheUpdateDelay = 6 * 60 * 60 * 1000; |
31 const int kTestCacheUpdateDelay = 3 * 60 * 1000; | 31 const int kTestCacheUpdateDelay = 3 * 60 * 1000; |
32 | 32 |
33 // The version of the service (used to expire the cache when upgrading Chrome | 33 // The version of the service (used to expire the cache when upgrading Chrome |
34 // to versions with different types of promos). | 34 // to versions with different types of promos). |
35 const int kPromoServiceVersion = 7; | 35 const int kPromoServiceVersion = 7; |
36 | 36 |
37 // The promotion type used for Unpack() and ScheduleNotificationOnInit. | 37 // The promotion type used for Unpack() and ScheduleNotificationOnInit(). |
38 const NotificationPromo::PromoType kValidPromoTypes[] = { | 38 const NotificationPromo::PromoType kValidPromoTypes[] = { |
39 #if defined(OS_ANDROID) || defined(OS_IOS) | 39 #if defined(OS_ANDROID) || defined(OS_IOS) |
40 NotificationPromo::MOBILE_NTP_SYNC_PROMO, | 40 NotificationPromo::MOBILE_NTP_SYNC_PROMO, |
41 #else | 41 #else |
42 NotificationPromo::NTP_NOTIFICATION_PROMO, | 42 NotificationPromo::NTP_NOTIFICATION_PROMO, |
43 NotificationPromo::NTP_BUBBLE_PROMO, | 43 NotificationPromo::NTP_BUBBLE_PROMO, |
44 #endif | 44 #endif |
45 }; | 45 }; |
46 | 46 |
47 GURL GetPromoResourceURL() { | 47 GURL GetPromoResourceURL() { |
48 const std::string promo_server_url = CommandLine::ForCurrentProcess()-> | 48 const std::string promo_server_url = CommandLine::ForCurrentProcess()-> |
49 GetSwitchValueASCII(switches::kPromoServerURL); | 49 GetSwitchValueASCII(switches::kPromoServerURL); |
50 return promo_server_url.empty() ? | 50 return promo_server_url.empty() ? |
51 NotificationPromo::PromoServerURL() : GURL(promo_server_url); | 51 NotificationPromo::PromoServerURL() : GURL(promo_server_url); |
52 } | 52 } |
53 | 53 |
54 bool IsTest() { | 54 bool IsTest() { |
55 return CommandLine::ForCurrentProcess()->HasSwitch(switches::kPromoServerURL); | 55 return CommandLine::ForCurrentProcess()->HasSwitch(switches::kPromoServerURL); |
56 } | 56 } |
57 | 57 |
58 int GetCacheUpdateDelay() { | 58 int GetCacheUpdateDelay() { |
59 return IsTest() ? kTestCacheUpdateDelay : kCacheUpdateDelay; | 59 return IsTest() ? kTestCacheUpdateDelay : kCacheUpdateDelay; |
60 } | 60 } |
61 | 61 |
62 } // namespace | 62 } // namespace |
63 | 63 |
64 // static | 64 // static |
65 void PromoResourceService::RegisterPrefs(PrefServiceSimple* local_state) { | 65 void PromoResourceService::RegisterPrefs(PrefServiceSimple* local_state) { |
66 // TODO(achuith): Delete this in M26. http://crbug.com/143773 | 66 local_state->RegisterStringPref(prefs::kNtpPromoResourceCacheUpdate, "0"); |
67 // The promo service version number, and last locale. | 67 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 } | 68 } |
75 | 69 |
76 // static | 70 // static |
77 void PromoResourceService::RegisterUserPrefs(PrefServiceSyncable* prefs) { | 71 void PromoResourceService::RegisterUserPrefs(PrefServiceSyncable* prefs) { |
| 72 // TODO(dbeam): remove in M28 when all prefs have been cleared. |
78 prefs->RegisterStringPref(prefs::kNtpPromoResourceCacheUpdate, | 73 prefs->RegisterStringPref(prefs::kNtpPromoResourceCacheUpdate, |
79 "0", | 74 "0", |
80 PrefServiceSyncable::UNSYNCABLE_PREF); | 75 PrefServiceSyncable::UNSYNCABLE_PREF); |
| 76 prefs->ClearPref(prefs::kNtpPromoResourceCacheUpdate); |
81 NotificationPromo::RegisterUserPrefs(prefs); | 77 NotificationPromo::RegisterUserPrefs(prefs); |
82 } | 78 } |
83 | 79 |
84 PromoResourceService::PromoResourceService(Profile* profile) | 80 PromoResourceService::PromoResourceService() |
85 : WebResourceService(profile->GetPrefs(), | 81 : WebResourceService(g_browser_process->local_state(), |
86 GetPromoResourceURL(), | 82 GetPromoResourceURL(), |
87 true, // append locale to URL | 83 true, // append locale to URL |
88 prefs::kNtpPromoResourceCacheUpdate, | 84 prefs::kNtpPromoResourceCacheUpdate, |
89 kStartResourceFetchDelay, | 85 kStartResourceFetchDelay, |
90 GetCacheUpdateDelay()), | 86 GetCacheUpdateDelay()), |
91 profile_(profile), | |
92 ALLOW_THIS_IN_INITIALIZER_LIST( | 87 ALLOW_THIS_IN_INITIALIZER_LIST( |
93 weak_ptr_factory_(this)) { | 88 weak_ptr_factory_(this)) { |
94 ScheduleNotificationOnInit(); | 89 ScheduleNotificationOnInit(); |
95 } | 90 } |
96 | 91 |
97 PromoResourceService::~PromoResourceService() { | 92 PromoResourceService::~PromoResourceService() { |
98 } | 93 } |
99 | 94 |
100 void PromoResourceService::ScheduleNotification( | 95 void PromoResourceService::ScheduleNotification( |
101 const NotificationPromo& notification_promo) { | 96 const NotificationPromo& notification_promo) { |
(...skipping 24 matching lines...) Expand all Loading... |
126 } else { | 121 } else { |
127 // The promo (if any) was apparently cancelled. Notify immediately. | 122 // The promo (if any) was apparently cancelled. Notify immediately. |
128 PostNotification(0); | 123 PostNotification(0); |
129 } | 124 } |
130 } | 125 } |
131 | 126 |
132 void PromoResourceService::ScheduleNotificationOnInit() { | 127 void PromoResourceService::ScheduleNotificationOnInit() { |
133 // If the promo start is in the future, set a notification task to | 128 // 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. | 129 // invalidate the NTP cache at the time of the promo start. |
135 for (size_t i = 0; i < arraysize(kValidPromoTypes); ++i) { | 130 for (size_t i = 0; i < arraysize(kValidPromoTypes); ++i) { |
136 NotificationPromo notification_promo(profile_); | 131 NotificationPromo notification_promo; |
137 notification_promo.InitFromPrefs(kValidPromoTypes[i]); | 132 notification_promo.InitFromPrefs(kValidPromoTypes[i]); |
138 ScheduleNotification(notification_promo); | 133 ScheduleNotification(notification_promo); |
139 } | 134 } |
140 } | 135 } |
141 | 136 |
142 void PromoResourceService::PostNotification(int64 delay_ms) { | 137 void PromoResourceService::PostNotification(int64 delay_ms) { |
143 // Note that this could cause re-issuing a notification every time | 138 // Note that this could cause re-issuing a notification every time |
144 // we receive an update from a server if something goes wrong. | 139 // we receive an update from a server if something goes wrong. |
145 // Given that this couldn't happen more frequently than every | 140 // Given that this couldn't happen more frequently than every |
146 // kCacheUpdateDelay milliseconds, we should be fine. | 141 // kCacheUpdateDelay milliseconds, we should be fine. |
(...skipping 13 matching lines...) Expand all Loading... |
160 void PromoResourceService::PromoResourceStateChange() { | 155 void PromoResourceService::PromoResourceStateChange() { |
161 content::NotificationService* service = | 156 content::NotificationService* service = |
162 content::NotificationService::current(); | 157 content::NotificationService::current(); |
163 service->Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, | 158 service->Notify(chrome::NOTIFICATION_PROMO_RESOURCE_STATE_CHANGED, |
164 content::Source<WebResourceService>(this), | 159 content::Source<WebResourceService>(this), |
165 content::NotificationService::NoDetails()); | 160 content::NotificationService::NoDetails()); |
166 } | 161 } |
167 | 162 |
168 void PromoResourceService::Unpack(const DictionaryValue& parsed_json) { | 163 void PromoResourceService::Unpack(const DictionaryValue& parsed_json) { |
169 for (size_t i = 0; i < arraysize(kValidPromoTypes); ++i) { | 164 for (size_t i = 0; i < arraysize(kValidPromoTypes); ++i) { |
170 NotificationPromo notification_promo(profile_); | 165 NotificationPromo notification_promo; |
171 notification_promo.InitFromJson(parsed_json, kValidPromoTypes[i]); | 166 notification_promo.InitFromJson(parsed_json, kValidPromoTypes[i]); |
172 if (notification_promo.new_notification()) | 167 if (notification_promo.new_notification()) |
173 ScheduleNotification(notification_promo); | 168 ScheduleNotification(notification_promo); |
174 } | 169 } |
175 } | 170 } |
OLD | NEW |