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 #ifndef COMPONENTS_WEB_RESOURCE_NOTIFICATION_PROMO_H_
6 #define COMPONENTS_WEB_RESOURCE_NOTIFICATION_PROMO_H_
10 #include "base/basictypes.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
16 class PrefRegistrySimple
;
20 class DictionaryValue
;
24 namespace user_prefs
{
25 class PrefRegistrySyncable
;
28 namespace version_info
{
32 namespace web_resource
{
34 // Helper class for PromoResourceService that parses promo notification info
35 // from json or prefs.
36 class NotificationPromo
{
38 static GURL
PromoServerURL(version_info::Channel channel
);
42 NTP_NOTIFICATION_PROMO
,
44 MOBILE_NTP_SYNC_PROMO
,
45 MOBILE_NTP_WHATS_NEW_PROMO
,
48 explicit NotificationPromo(PrefService
* local_state
);
51 // Initialize from json/prefs.
52 void InitFromJson(const base::DictionaryValue
& json
, PromoType promo_type
);
53 void InitFromPrefs(PromoType promo_type
);
55 // Can this promo be shown?
58 // Calculates promo notification start time with group-based time slice
60 double StartTimeForGroup() const;
61 double EndTime() const;
63 // Helpers for NewTabPageHandler.
64 // Mark the promo as closed when the user dismisses it.
65 static void HandleClosed(PromoType promo_type
, PrefService
* local_state
);
66 // Mark the promo has having been viewed. This returns true if views
67 // exceeds the maximum allowed.
68 static bool HandleViewed(PromoType promo_type
, PrefService
* local_state
);
70 bool new_notification() const { return new_notification_
; }
72 const std::string
& promo_text() const { return promo_text_
; }
73 PromoType
promo_type() const { return promo_type_
; }
74 const base::DictionaryValue
* promo_payload() const {
75 return promo_payload_
.get();
78 // Register preferences.
79 static void RegisterPrefs(PrefRegistrySimple
* registry
);
80 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
81 static void MigrateUserPrefs(PrefService
* user_prefs
);
85 friend class NotificationPromoTest
;
87 // Check if this promo notification is new based on start/end times,
88 // and trigger events accordingly.
89 void CheckForNewNotification();
91 // Actions on receiving a new promo notification.
92 void OnNewNotification();
94 // Flush data members to prefs for storage.
97 // Tests group_ against max_group_.
98 // When max_group_ is 0, all groups pass.
99 bool ExceedsMaxGroup() const;
101 // Tests views_ against max_views_.
102 // When max_views_ is 0, we don't cap the number of views.
103 bool ExceedsMaxViews() const;
105 // Tests |first_view_time_| + |max_seconds_| and -now().
106 // When either is 0, we don't cap the number of seconds.
107 bool ExceedsMaxSeconds() const;
109 PrefService
* local_state_
;
111 PromoType promo_type_
;
112 std::string promo_text_
;
114 scoped_ptr
<const base::DictionaryValue
> promo_payload_
;
120 int initial_segment_
;
125 // When max_views_ is 0, we don't cap the number of views.
128 // When max_seconds_ is 0 or not set, we don't cap the number of seconds a
129 // promo can be visible.
132 // Set when the promo is viewed for the first time.
133 double first_view_time_
;
139 bool new_notification_
;
141 DISALLOW_COPY_AND_ASSIGN(NotificationPromo
);
144 } // namespace web_resource
146 #endif // COMPONENTS_WEB_RESOURCE_NOTIFICATION_PROMO_H_