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 #ifndef CHROME_BROWSER_WEB_RESOURCE_NOTIFICATION_PROMO_H_ | 5 #ifndef CHROME_BROWSER_WEB_RESOURCE_NOTIFICATION_PROMO_H_ |
6 #define CHROME_BROWSER_WEB_RESOURCE_NOTIFICATION_PROMO_H_ | 6 #define CHROME_BROWSER_WEB_RESOURCE_NOTIFICATION_PROMO_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
15 | 15 |
16 namespace base { | 16 namespace base { |
17 class DictionaryValue; | 17 class DictionaryValue; |
18 class ListValue; | 18 class ListValue; |
19 } | 19 } |
20 | 20 |
21 class PrefService; | 21 class PrefServiceSimple; |
22 class PrefServiceSyncable; | 22 class PrefServiceSyncable; |
23 class Profile; | |
24 | 23 |
25 // Helper class for PromoResourceService that parses promo notification info | 24 // Helper class for PromoResourceService that parses promo notification info |
26 // from json or prefs. | 25 // from json or prefs. |
27 class NotificationPromo { | 26 class NotificationPromo { |
28 public: | 27 public: |
29 static GURL PromoServerURL(); | 28 static GURL PromoServerURL(); |
30 | 29 |
31 enum PromoType { | 30 enum PromoType { |
32 NO_PROMO, | 31 NO_PROMO, |
33 NTP_NOTIFICATION_PROMO, | 32 NTP_NOTIFICATION_PROMO, |
34 NTP_BUBBLE_PROMO, | 33 NTP_BUBBLE_PROMO, |
35 MOBILE_NTP_SYNC_PROMO, | 34 MOBILE_NTP_SYNC_PROMO, |
36 }; | 35 }; |
37 | 36 |
38 explicit NotificationPromo(Profile* profile); | 37 explicit NotificationPromo(PrefServiceSimple* prefs); |
39 ~NotificationPromo(); | 38 ~NotificationPromo(); |
40 | 39 |
41 // Initialize from json/prefs. | 40 // Initialize from json/prefs. |
42 void InitFromJson(const base::DictionaryValue& json, PromoType promo_type); | 41 void InitFromJson(const base::DictionaryValue& json, PromoType promo_type); |
43 void InitFromPrefs(PromoType promo_type); | 42 void InitFromPrefs(PromoType promo_type); |
44 | 43 |
45 // Can this promo be shown? | 44 // Can this promo be shown? |
46 bool CanShow() const; | 45 bool CanShow() const; |
47 | 46 |
48 // Calculates promo notification start time with group-based time slice | 47 // Calculates promo notification start time with group-based time slice |
49 // offset. | 48 // offset. |
50 double StartTimeForGroup() const; | 49 double StartTimeForGroup() const; |
51 double EndTime() const; | 50 double EndTime() const; |
52 | 51 |
53 // Helpers for NewTabPageHandler. | 52 // Helpers for NewTabPageHandler. |
54 // Mark the promo as closed when the user dismisses it. | 53 // Mark the promo as closed when the user dismisses it. |
55 static void HandleClosed(Profile* profile, PromoType promo_type); | 54 static void HandleClosed(PrefServiceSimple* prefs, PromoType promo_type); |
56 // Mark the promo has having been viewed. This returns true if views | 55 // Mark the promo has having been viewed. This returns true if views |
57 // exceeds the maximum allowed. | 56 // exceeds the maximum allowed. |
58 static bool HandleViewed(Profile* profile, PromoType promo_type); | 57 static bool HandleViewed(PrefServiceSimple* prefs, PromoType promo_type); |
59 | 58 |
60 bool new_notification() const { return new_notification_; } | 59 bool new_notification() const { return new_notification_; } |
61 | 60 |
62 const std::string& promo_text() const { return promo_text_; } | 61 const std::string& promo_text() const { return promo_text_; } |
63 PromoType promo_type() const { return promo_type_; } | 62 PromoType promo_type() const { return promo_type_; } |
64 const base::DictionaryValue* promo_payload() const { | 63 const base::DictionaryValue* promo_payload() const { |
65 return promo_payload_.get(); | 64 return promo_payload_.get(); |
66 } | 65 } |
67 | 66 |
68 // Register preferences. | 67 // Register preferences. |
| 68 static void RegisterPrefs(PrefServiceSimple* local_state); |
69 static void RegisterUserPrefs(PrefServiceSyncable* prefs); | 69 static void RegisterUserPrefs(PrefServiceSyncable* prefs); |
70 | 70 |
71 private: | 71 private: |
72 // For testing. | 72 // For testing. |
73 friend class NotificationPromoTest; | 73 friend class NotificationPromoTest; |
74 | 74 |
75 // Check if this promo notification is new based on start/end times, | 75 // Check if this promo notification is new based on start/end times, |
76 // and trigger events accordingly. | 76 // and trigger events accordingly. |
77 void CheckForNewNotification(); | 77 void CheckForNewNotification(); |
78 | 78 |
79 // Actions on receiving a new promo notification. | 79 // Actions on receiving a new promo notification. |
80 void OnNewNotification(); | 80 void OnNewNotification(); |
81 | 81 |
82 // Flush data members to prefs for storage. | 82 // Flush data members to prefs for storage. |
83 void WritePrefs(); | 83 void WritePrefs(); |
84 | 84 |
85 // Tests group_ against max_group_. | 85 // Tests group_ against max_group_. |
86 // When max_group_ is 0, all groups pass. | 86 // When max_group_ is 0, all groups pass. |
87 bool ExceedsMaxGroup() const; | 87 bool ExceedsMaxGroup() const; |
88 | 88 |
89 // Tests views_ against max_views_. | 89 // Tests views_ against max_views_. |
90 // When max_views_ is 0, we don't cap the number of views. | 90 // When max_views_ is 0, we don't cap the number of views. |
91 bool ExceedsMaxViews() const; | 91 bool ExceedsMaxViews() const; |
92 | 92 |
93 // True if this promo is not targeted to G+ users, or if this is a G+ user. | 93 PrefServiceSimple* prefs_; |
94 bool IsGPlusRequired() const; | |
95 | |
96 Profile* profile_; | |
97 PrefService* prefs_; | |
98 | 94 |
99 PromoType promo_type_; | 95 PromoType promo_type_; |
100 std::string promo_text_; | 96 std::string promo_text_; |
101 | 97 |
102 // Note that promo_payload_ isn't currently used for desktop promos. | 98 // Note that promo_payload_ isn't currently used for desktop promos. |
103 scoped_ptr<const base::DictionaryValue> promo_payload_; | 99 scoped_ptr<const base::DictionaryValue> promo_payload_; |
104 | 100 |
105 double start_; | 101 double start_; |
106 double end_; | 102 double end_; |
107 | 103 |
108 int num_groups_; | 104 int num_groups_; |
109 int initial_segment_; | 105 int initial_segment_; |
110 int increment_; | 106 int increment_; |
111 int time_slice_; | 107 int time_slice_; |
112 int max_group_; | 108 int max_group_; |
113 | 109 |
114 // When max_views_ is 0, we don't cap the number of views. | 110 // When max_views_ is 0, we don't cap the number of views. |
115 int max_views_; | 111 int max_views_; |
116 | 112 |
117 int group_; | 113 int group_; |
118 int views_; | 114 int views_; |
119 bool closed_; | 115 bool closed_; |
120 | 116 |
121 bool gplus_required_; | |
122 | |
123 bool new_notification_; | 117 bool new_notification_; |
124 | 118 |
125 DISALLOW_COPY_AND_ASSIGN(NotificationPromo); | 119 DISALLOW_COPY_AND_ASSIGN(NotificationPromo); |
126 }; | 120 }; |
127 | 121 |
128 #endif // CHROME_BROWSER_WEB_RESOURCE_NOTIFICATION_PROMO_H_ | 122 #endif // CHROME_BROWSER_WEB_RESOURCE_NOTIFICATION_PROMO_H_ |
OLD | NEW |