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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.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 } | 18 } |
19 | 19 |
20 class PrefService; | 20 class PrefService; |
21 class Profile; | 21 class Profile; |
22 | 22 |
23 // Helper class for PromoResourceService that parses promo notification info | 23 // Helper class for PromoResourceService that parses promo notification info |
24 // from json or prefs. | 24 // from json or prefs. |
25 class NotificationPromo | 25 class NotificationPromo { |
26 : public base::RefCountedThreadSafe<NotificationPromo> { | |
27 public: | 26 public: |
28 class Delegate { | 27 static GURL PromoServerURL(); |
29 public: | |
30 virtual ~Delegate() {} | |
31 virtual void OnNotificationParsed(double start, double end, | |
32 bool new_notification) = 0; | |
33 // For testing. | |
34 virtual bool IsBuildAllowed(int builds_targeted) const { return false; } | |
35 virtual int CurrentPlatform() const { return PLATFORM_NONE; } | |
36 }; | |
37 | 28 |
38 // Static factory for creating new notification promos. | 29 explicit NotificationPromo(Profile* profile); |
39 static NotificationPromo* Create(Profile* profile, Delegate* delegate); | 30 ~NotificationPromo(); |
40 | |
41 static GURL PromoServerURL(); | |
42 | 31 |
43 // Initialize from json/prefs. | 32 // Initialize from json/prefs. |
44 void InitFromJson(const base::DictionaryValue& json); | 33 void InitFromJson(const base::DictionaryValue& json); |
45 void InitFromPrefs(); | 34 void InitFromPrefs(); |
46 // TODO(achuith): This legacy method parses json from the tip server. | |
47 // This code will be deleted very soon. http://crbug.com/126317. | |
48 void InitFromJsonLegacy(const base::DictionaryValue& json); | |
49 | 35 |
50 // Can this promo be shown? | 36 // Can this promo be shown? |
51 bool CanShow() const; | 37 bool CanShow() const; |
52 | 38 |
53 // Calculates promo notification start time with group-based time slice | 39 // Calculates promo notification start time with group-based time slice |
54 // offset. | 40 // offset. |
55 double StartTimeForGroup() const; | 41 double StartTimeForGroup() const; |
| 42 double EndTime() const; |
56 | 43 |
57 // Helpers for NewTabPageHandler. | 44 // Helpers for NewTabPageHandler. |
58 void HandleClosed(); | 45 void HandleClosed(); |
59 bool HandleViewed(); // returns true if views exceeds maximum allowed. | 46 bool HandleViewed(); // returns true if views exceeds maximum allowed. |
60 | 47 |
| 48 bool new_notification() const { return new_notification_; } |
| 49 |
61 // Register preferences. | 50 // Register preferences. |
62 static void RegisterUserPrefs(PrefService* prefs); | 51 static void RegisterUserPrefs(PrefService* prefs); |
63 | 52 |
64 private: | 53 private: |
65 friend class base::RefCountedThreadSafe<NotificationPromo>; | |
66 NotificationPromo(Profile* profile, Delegate* delegate); | |
67 virtual ~NotificationPromo(); | |
68 | |
69 // For testing. | 54 // For testing. |
70 friend class NotificationPromoTestDelegate; | 55 friend class NotificationPromoTest; |
71 FRIEND_TEST_ALL_PREFIXES(PromoResourceServiceTest, GetNextQuestionValueTest); | |
72 | |
73 enum PlatformType { | |
74 PLATFORM_NONE = 0, | |
75 PLATFORM_WIN = 1, | |
76 PLATFORM_MAC = 1 << 1, | |
77 PLATFORM_LINUX = 1 << 2, | |
78 PLATFORM_CHROMEOS = 1 << 3, | |
79 PLATFORM_ALL = (1 << 4) -1, | |
80 }; | |
81 | |
82 // Parse the answers array element. Set the data members of this instance | |
83 // and trigger OnNewNotification callback if necessary. | |
84 void Parse(const base::DictionaryValue* dict); | |
85 | |
86 // Set promo notification params from a question string, which is of the form | |
87 // <build_type>:<time_slice>:<max_group>:<max_views>:<platform> | |
88 void ParseParams(const base::DictionaryValue* dict); | |
89 | 56 |
90 // Check if this promo notification is new based on start/end times, | 57 // Check if this promo notification is new based on start/end times, |
91 // and trigger events accordingly. | 58 // and trigger events accordingly. |
92 void CheckForNewNotification(); | 59 void CheckForNewNotification(); |
93 | 60 |
94 // Actions on receiving a new promo notification. | 61 // Actions on receiving a new promo notification. |
95 void OnNewNotification(); | 62 void OnNewNotification(); |
96 | 63 |
97 // Returns an int converted from the question substring starting at index | |
98 // till the next colon. Sets index to the location right after the colon. | |
99 // Returns 0 if *err is true, and sets *err to true upon error. | |
100 static int GetNextQuestionValue(const std::string& question, | |
101 size_t* index, | |
102 bool* err); | |
103 | |
104 // Flush data members to prefs for storage. | 64 // Flush data members to prefs for storage. |
105 void WritePrefs(); | 65 void WritePrefs(); |
106 | 66 |
107 // Tests views_ against max_views_. | 67 // Tests views_ against max_views_. |
108 // When max_views_ is 0, we don't cap the number of views. | 68 // When max_views_ is 0, we don't cap the number of views. |
109 bool ExceedsMaxViews() const; | 69 bool ExceedsMaxViews() const; |
110 | 70 |
111 // Match our channel with specified build type. | |
112 bool IsBuildAllowed(int builds_allowed) const; | |
113 | |
114 // Match our platform with the specified platform bitfield. | |
115 bool IsPlatformAllowed(int target_platform) const; | |
116 | |
117 // True if this promo is not targeted to G+ users, or if this is a G+ user. | 71 // True if this promo is not targeted to G+ users, or if this is a G+ user. |
118 bool IsGPlusRequired() const; | 72 bool IsGPlusRequired() const; |
119 | 73 |
120 // Current platform. | |
121 static int CurrentPlatform(); | |
122 | |
123 Profile* profile_; | 74 Profile* profile_; |
124 Delegate* delegate_; | |
125 PrefService* prefs_; | 75 PrefService* prefs_; |
126 | 76 |
127 std::string promo_text_; | 77 std::string promo_text_; |
128 | 78 |
129 double start_; | 79 double start_; |
130 double end_; | 80 double end_; |
131 | 81 |
132 int num_groups_; | 82 int num_groups_; |
133 int initial_segment_; | 83 int initial_segment_; |
134 int increment_; | 84 int increment_; |
135 int time_slice_; | 85 int time_slice_; |
136 int max_group_; | 86 int max_group_; |
137 | 87 |
138 // When max_views_ is 0, we don't cap the number of views. | 88 // When max_views_ is 0, we don't cap the number of views. |
139 int max_views_; | 89 int max_views_; |
140 | 90 |
141 int group_; | 91 int group_; |
142 int views_; | 92 int views_; |
143 bool closed_; | 93 bool closed_; |
144 | 94 |
145 int build_; | 95 bool gplus_required_; |
146 int platform_; | |
147 | 96 |
148 bool gplus_required_; | 97 bool new_notification_; |
149 | 98 |
150 DISALLOW_COPY_AND_ASSIGN(NotificationPromo); | 99 DISALLOW_COPY_AND_ASSIGN(NotificationPromo); |
151 }; | 100 }; |
152 | 101 |
153 #endif // CHROME_BROWSER_WEB_RESOURCE_NOTIFICATION_PROMO_H_ | 102 #endif // CHROME_BROWSER_WEB_RESOURCE_NOTIFICATION_PROMO_H_ |
OLD | NEW |