OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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" | |
14 #include "base/memory/scoped_ptr.h" | |
13 | 15 |
14 namespace base { | 16 namespace base { |
15 class DictionaryValue; | 17 class DictionaryValue; |
16 } | 18 } |
17 | 19 |
20 namespace net { | |
21 class URLRequestContextGetter; | |
22 } | |
23 | |
18 class PrefService; | 24 class PrefService; |
25 class Profile; | |
19 | 26 |
20 // Helper class for PromoResourceService that parses promo notification info | 27 // Helper class for PromoResourceService that parses promo notification info |
21 // from json or prefs. | 28 // from json or prefs. |
22 class NotificationPromo { | 29 class NotificationPromo |
30 : public base::RefCountedThreadSafe<NotificationPromo> { | |
23 public: | 31 public: |
24 class Delegate { | 32 class Delegate { |
25 public: | 33 public: |
26 virtual ~Delegate() {} | 34 virtual ~Delegate() {} |
27 virtual void OnNewNotification(double start, double end) = 0; | 35 virtual void OnNotificationParsed(double start, double end, |
36 bool new_notification) = 0; | |
28 // For testing. | 37 // For testing. |
29 virtual bool IsBuildAllowed(int builds_targeted) const { return false; } | 38 virtual bool IsBuildAllowed(int builds_targeted) const { return false; } |
30 virtual int CurrentPlatform() const { return PLATFORM_NONE; } | 39 virtual int CurrentPlatform() const { return PLATFORM_NONE; } |
31 }; | 40 }; |
32 | 41 |
33 explicit NotificationPromo(PrefService* prefs, Delegate* delegate); | 42 // Static factory for creating new notification promos. |
43 static NotificationPromo* Create(Profile* profile, Delegate* delegate); | |
34 | 44 |
35 // Initialize from json/prefs. | 45 // Initialize from json/prefs. |
36 void InitFromJson(const base::DictionaryValue& json); | 46 void InitFromJson(const base::DictionaryValue& json, bool do_cookie_check); |
37 void InitFromPrefs(); | 47 void InitFromPrefs(); |
38 | 48 |
39 // Can this promo be shown? | 49 // Can this promo be shown? |
40 bool CanShow() const; | 50 bool CanShow() const; |
41 | 51 |
42 // Calculates promo notification start time with group-based time slice | 52 // Calculates promo notification start time with group-based time slice |
43 // offset. | 53 // offset. |
44 double StartTimeWithOffset() const; | 54 double StartTimeWithOffset() const; |
45 | 55 |
46 // Helpers for NewTabPageHandler. | 56 // Helpers for NewTabPageHandler. |
47 void HandleClosed(); | 57 void HandleClosed(); |
48 bool HandleViewed(); // returns true if views exceeds maximum allowed. | 58 bool HandleViewed(); // returns true if views exceeds maximum allowed. |
49 | 59 |
50 // Register preferences. | 60 // Register preferences. |
51 static void RegisterUserPrefs(PrefService* prefs); | 61 static void RegisterUserPrefs(PrefService* prefs); |
52 | 62 |
53 private: | 63 private: |
64 friend class base::RefCountedThreadSafe<NotificationPromo>; | |
65 NotificationPromo(Profile* profile, Delegate* delegate); | |
66 virtual ~NotificationPromo(); | |
67 | |
54 // For testing. | 68 // For testing. |
55 friend class NotificationPromoTestDelegate; | 69 friend class NotificationPromoTestDelegate; |
56 FRIEND_TEST_ALL_PREFIXES(PromoResourceServiceTest, GetNextQuestionValueTest); | 70 FRIEND_TEST_ALL_PREFIXES(PromoResourceServiceTest, GetNextQuestionValueTest); |
57 FRIEND_TEST_ALL_PREFIXES(PromoResourceServiceTest, NewGroupTest); | 71 FRIEND_TEST_ALL_PREFIXES(PromoResourceServiceTest, NewGroupTest); |
58 | 72 |
59 enum PlatformType { | 73 enum PlatformType { |
60 PLATFORM_NONE = 0, | 74 PLATFORM_NONE = 0, |
61 PLATFORM_WIN = 1, | 75 PLATFORM_WIN = 1, |
62 PLATFORM_MAC = 1 << 1, | 76 PLATFORM_MAC = 1 << 1, |
63 PLATFORM_LINUX = 1 << 2, | 77 PLATFORM_LINUX = 1 << 2, |
64 PLATFORM_CHROMEOS = 1 << 3, | 78 PLATFORM_CHROMEOS = 1 << 3, |
65 PLATFORM_ALL = (1 << 4) -1, | 79 PLATFORM_ALL = (1 << 4) -1, |
66 }; | 80 }; |
67 | 81 |
68 // Users are randomly assigned to one of kMaxGroupSize + 1 buckets, in order | 82 // Users are randomly assigned to one of kMaxGroupSize + 1 buckets, in order |
69 // to be able to roll out promos slowly, or display different promos to | 83 // to be able to roll out promos slowly, or display different promos to |
70 // different groups. | 84 // different groups. |
71 static const int kMaxGroupSize = 99; | 85 static const int kMaxGroupSize = 99; |
72 | 86 |
87 // Flags for feature_mask_. | |
88 enum FEATURE { | |
achuithb
2011/11/28 21:21:22
I believe enums should appear before static data m
Cait (Slow)
2011/11/29 00:12:27
Done.
| |
89 NO_FEATURE = 0, | |
90 FEATURE_GPLUS = 1, | |
91 }; | |
92 | |
73 // Parse the answers array element. Set the data members of this instance | 93 // Parse the answers array element. Set the data members of this instance |
74 // and trigger OnNewNotification callback if necessary. | 94 // and trigger OnNewNotification callback if necessary. |
75 void Parse(const base::DictionaryValue* dict); | 95 void Parse(const base::DictionaryValue* dict); |
76 | 96 |
77 // Set promo notification params from a question string, which is of the form | 97 // Set promo notification params from a question string, which is of the form |
78 // <build_type>:<time_slice>:<max_group>:<max_views> | 98 // <build_type>:<time_slice>:<max_group>:<max_views>:<platform>:<feature_mask> |
79 void ParseParams(const base::DictionaryValue* dict); | 99 void ParseParams(const base::DictionaryValue* dict); |
80 | 100 |
81 // Check if this promo notification is new based on start/end times, | 101 // Check if this promo notification is new based on start/end times, |
82 // and trigger events accordingly. | 102 // and trigger events accordingly. |
83 void CheckForNewNotification(); | 103 void CheckForNewNotification(bool found_cookie); |
84 | 104 |
85 // Actions on receiving a new promo notification. | 105 // Actions on receiving a new promo notification. |
86 void OnNewNotification(); | 106 void OnNewNotification(); |
87 | 107 |
108 // Async method to get cookies from GPlus url. Used to check if user is | |
109 // logged in to GPlus. | |
110 void GetCookies(net::URLRequestContextGetter* getter); | |
111 | |
112 // Callback for GetCookies. | |
113 void GetCookiesCallback(const std::string& cookies); | |
114 | |
115 // Parse cookies in search of a SID= value. | |
achuithb
2011/11/28 21:21:22
nit: extra space before Parse.
Cait (Slow)
2011/11/29 00:12:27
Done.
| |
116 static bool CheckForGPlusCookie(const std::string& cookies); | |
117 | |
88 // Create a new promo notification group. | 118 // Create a new promo notification group. |
89 static int NewGroup(); | 119 static int NewGroup(); |
90 | 120 |
91 // Returns an int converted from the question substring starting at index | 121 // Returns an int converted from the question substring starting at index |
92 // till the next colon. Sets index to the location right after the colon. | 122 // till the next colon. Sets index to the location right after the colon. |
93 // Returns 0 if *err is true, and sets *err to true upon error. | 123 // Returns 0 if *err is true, and sets *err to true upon error. |
94 static int GetNextQuestionValue(const std::string& question, | 124 static int GetNextQuestionValue(const std::string& question, |
95 size_t* index, | 125 size_t* index, |
96 bool* err); | 126 bool* err); |
97 | 127 |
98 // Flush data members to prefs for storage. | 128 // Flush data members to prefs for storage. |
99 void WritePrefs(); | 129 void WritePrefs(); |
100 | 130 |
101 // Match our channel with specified build type. | 131 // Match our channel with specified build type. |
102 bool IsBuildAllowed(int builds_allowed) const; | 132 bool IsBuildAllowed(int builds_allowed) const; |
103 | 133 |
104 // Match our platform with the specified platform bitfield. | 134 // Match our platform with the specified platform bitfield. |
105 bool IsPlatformAllowed(int target_platform) const; | 135 bool IsPlatformAllowed(int target_platform) const; |
106 | 136 |
107 // Current platform. | 137 // Current platform. |
108 static int CurrentPlatform(); | 138 static int CurrentPlatform(); |
109 | 139 |
110 // For testing. | 140 // For testing. |
111 bool operator==(const NotificationPromo& other) const; | 141 bool operator==(const NotificationPromo& other) const; |
112 | 142 |
143 Profile* profile_; | |
113 PrefService* prefs_; | 144 PrefService* prefs_; |
114 Delegate* delegate_; | 145 Delegate* delegate_; |
115 | 146 |
116 double start_; | 147 double start_; |
117 double end_; | 148 double end_; |
118 | 149 |
119 int build_; | 150 int build_; |
120 int time_slice_; | 151 int time_slice_; |
121 int max_group_; | 152 int max_group_; |
122 int max_views_; | 153 int max_views_; |
123 int platform_; | 154 int platform_; |
155 int feature_mask_; | |
124 | 156 |
125 int group_; | 157 int group_; |
126 int views_; | 158 int views_; |
127 std::string text_; | 159 std::string text_; |
128 bool closed_; | 160 bool closed_; |
161 bool gplus_; | |
129 | 162 |
130 DISALLOW_COPY_AND_ASSIGN(NotificationPromo); | 163 DISALLOW_COPY_AND_ASSIGN(NotificationPromo); |
131 }; | 164 }; |
132 | 165 |
133 #endif // CHROME_BROWSER_WEB_RESOURCE_NOTIFICATION_PROMO_H_ | 166 #endif // CHROME_BROWSER_WEB_RESOURCE_NOTIFICATION_PROMO_H_ |
134 | 167 |
OLD | NEW |