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