| 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 "components/web_resource/promo_resource_service.h" | 5 #include "components/web_resource/promo_resource_service.h" |
| 6 | 6 |
| 7 #include <stddef.h> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 9 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/macros.h" |
| 10 #include "base/prefs/pref_registry_simple.h" | 13 #include "base/prefs/pref_registry_simple.h" |
| 11 #include "base/prefs/pref_service.h" | 14 #include "base/prefs/pref_service.h" |
| 12 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
| 13 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
| 14 #include "base/threading/thread_restrictions.h" | 17 #include "base/threading/thread_restrictions.h" |
| 15 #include "base/values.h" | 18 #include "base/values.h" |
| 19 #include "build/build_config.h" |
| 16 #include "components/pref_registry/pref_registry_syncable.h" | 20 #include "components/pref_registry/pref_registry_syncable.h" |
| 17 #include "components/web_resource/notification_promo.h" | 21 #include "components/web_resource/notification_promo.h" |
| 18 #include "components/web_resource/web_resource_pref_names.h" | 22 #include "components/web_resource/web_resource_pref_names.h" |
| 19 #include "components/web_resource/web_resource_switches.h" | 23 #include "components/web_resource/web_resource_switches.h" |
| 20 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 21 | 25 |
| 22 namespace web_resource { | 26 namespace web_resource { |
| 23 | 27 |
| 24 namespace { | 28 namespace { |
| 25 | 29 |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 const base::Closure& closure) { | 115 const base::Closure& closure) { |
| 112 return callback_list_.Add(closure); | 116 return callback_list_.Add(closure); |
| 113 } | 117 } |
| 114 | 118 |
| 115 void PromoResourceService::ScheduleNotification( | 119 void PromoResourceService::ScheduleNotification( |
| 116 const NotificationPromo& notification_promo) { | 120 const NotificationPromo& notification_promo) { |
| 117 const double promo_start = notification_promo.StartTimeForGroup(); | 121 const double promo_start = notification_promo.StartTimeForGroup(); |
| 118 const double promo_end = notification_promo.EndTime(); | 122 const double promo_end = notification_promo.EndTime(); |
| 119 | 123 |
| 120 if (promo_start > 0 && promo_end > 0) { | 124 if (promo_start > 0 && promo_end > 0) { |
| 121 const int64 ms_until_start = static_cast<int64>( | 125 const int64_t ms_until_start = static_cast<int64_t>( |
| 122 (base::Time::FromDoubleT(promo_start) - base::Time::Now()) | 126 (base::Time::FromDoubleT(promo_start) - base::Time::Now()) |
| 123 .InMilliseconds()); | 127 .InMilliseconds()); |
| 124 const int64 ms_until_end = static_cast<int64>( | 128 const int64_t ms_until_end = static_cast<int64_t>( |
| 125 (base::Time::FromDoubleT(promo_end) - base::Time::Now()) | 129 (base::Time::FromDoubleT(promo_end) - base::Time::Now()) |
| 126 .InMilliseconds()); | 130 .InMilliseconds()); |
| 127 if (ms_until_start > 0) { | 131 if (ms_until_start > 0) { |
| 128 // Schedule the next notification to happen at the start of promotion. | 132 // Schedule the next notification to happen at the start of promotion. |
| 129 PostNotification(ms_until_start); | 133 PostNotification(ms_until_start); |
| 130 } else if (ms_until_end > 0) { | 134 } else if (ms_until_end > 0) { |
| 131 if (ms_until_start <= 0) { | 135 if (ms_until_start <= 0) { |
| 132 // The promo is active. Notify immediately. | 136 // The promo is active. Notify immediately. |
| 133 PostNotification(0); | 137 PostNotification(0); |
| 134 } | 138 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 147 void PromoResourceService::ScheduleNotificationOnInit() { | 151 void PromoResourceService::ScheduleNotificationOnInit() { |
| 148 // If the promo start is in the future, set a notification task to | 152 // If the promo start is in the future, set a notification task to |
| 149 // invalidate the NTP cache at the time of the promo start. | 153 // invalidate the NTP cache at the time of the promo start. |
| 150 for (size_t i = 0; i < arraysize(kValidPromoTypes); ++i) { | 154 for (size_t i = 0; i < arraysize(kValidPromoTypes); ++i) { |
| 151 NotificationPromo notification_promo(prefs_); | 155 NotificationPromo notification_promo(prefs_); |
| 152 notification_promo.InitFromPrefs(kValidPromoTypes[i]); | 156 notification_promo.InitFromPrefs(kValidPromoTypes[i]); |
| 153 ScheduleNotification(notification_promo); | 157 ScheduleNotification(notification_promo); |
| 154 } | 158 } |
| 155 } | 159 } |
| 156 | 160 |
| 157 void PromoResourceService::PostNotification(int64 delay_ms) { | 161 void PromoResourceService::PostNotification(int64_t delay_ms) { |
| 158 // Note that this could cause re-issuing a notification every time | 162 // Note that this could cause re-issuing a notification every time |
| 159 // we receive an update from a server if something goes wrong. | 163 // we receive an update from a server if something goes wrong. |
| 160 // Given that this couldn't happen more frequently than every | 164 // Given that this couldn't happen more frequently than every |
| 161 // kCacheUpdateDelay milliseconds, we should be fine. | 165 // kCacheUpdateDelay milliseconds, we should be fine. |
| 162 // TODO(achuith): This crashes if we post delay_ms = 0 to the message loop. | 166 // TODO(achuith): This crashes if we post delay_ms = 0 to the message loop. |
| 163 // during startup. | 167 // during startup. |
| 164 if (delay_ms > 0) { | 168 if (delay_ms > 0) { |
| 165 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 169 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 166 FROM_HERE, base::Bind(&PromoResourceService::PromoResourceStateChange, | 170 FROM_HERE, base::Bind(&PromoResourceService::PromoResourceStateChange, |
| 167 weak_ptr_factory_.GetWeakPtr()), | 171 weak_ptr_factory_.GetWeakPtr()), |
| (...skipping 10 matching lines...) Expand all Loading... |
| 178 void PromoResourceService::Unpack(const base::DictionaryValue& parsed_json) { | 182 void PromoResourceService::Unpack(const base::DictionaryValue& parsed_json) { |
| 179 for (size_t i = 0; i < arraysize(kValidPromoTypes); ++i) { | 183 for (size_t i = 0; i < arraysize(kValidPromoTypes); ++i) { |
| 180 NotificationPromo notification_promo(prefs_); | 184 NotificationPromo notification_promo(prefs_); |
| 181 notification_promo.InitFromJson(parsed_json, kValidPromoTypes[i]); | 185 notification_promo.InitFromJson(parsed_json, kValidPromoTypes[i]); |
| 182 if (notification_promo.new_notification()) | 186 if (notification_promo.new_notification()) |
| 183 ScheduleNotification(notification_promo); | 187 ScheduleNotification(notification_promo); |
| 184 } | 188 } |
| 185 } | 189 } |
| 186 | 190 |
| 187 } // namespace web_resource | 191 } // namespace web_resource |
| OLD | NEW |