Chromium Code Reviews
|
| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_WEB_RESOURCE_PROMO_NOTIFICATION_H_ | |
| 6 #define CHROME_BROWSER_WEB_RESOURCE_PROMO_NOTIFICATION_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/gtest_prod_util.h" | |
| 13 | |
| 14 namespace base { | |
| 15 class DictionaryValue; | |
| 16 } | |
| 17 | |
| 18 class PrefService; | |
| 19 | |
| 20 // Helper class for PromoResourceService that parses promo notification info | |
| 21 // from json or prefs. | |
| 22 class PromoNotification { | |
|
jstritar
2011/09/28 15:28:21
naming nit: Do you think NotificationPromo would m
| |
| 23 public: | |
| 24 class Delegate { | |
| 25 public: | |
| 26 virtual ~Delegate() {} | |
| 27 virtual void OnNewNotification(double start, double end) = 0; | |
| 28 // For testing. | |
| 29 virtual bool IsBuildAllowed(int builds_targeted) const { return false; } | |
| 30 }; | |
| 31 | |
| 32 explicit PromoNotification(PrefService* prefs, Delegate* delegate); | |
| 33 | |
| 34 // Initialize from json/prefs. | |
| 35 void InitFromJson(const base::DictionaryValue& json); | |
| 36 void InitFromPrefs(); | |
| 37 | |
| 38 // Can this promo be shown? | |
| 39 bool CanShow() const; | |
| 40 | |
| 41 static void RegisterUserPrefs(PrefService* prefs); | |
|
jstritar
2011/09/28 15:28:21
nit: static methods should be defined above regula
achuithb
2011/09/29 02:00:50
I don't see the rule in the Declaration Order link
jstritar
2011/09/30 19:57:57
Ah, interesting. Maybe this is just something we d
| |
| 42 | |
| 43 // Calculates promo notification start time with group-based time slice | |
| 44 // offset. | |
| 45 double StartTimeWithOffset() const; | |
| 46 | |
| 47 private: | |
| 48 // For testing. | |
| 49 friend class PromoNotificationTestDelegate; | |
| 50 FRIEND_TEST_ALL_PREFIXES(PromoResourceServiceTest, GetNextQuestionValueTest); | |
| 51 FRIEND_TEST_ALL_PREFIXES(PromoResourceServiceTest, NewGroupTest); | |
| 52 | |
| 53 // Users are randomly assigned to one of kMaxGroupSize + 1 buckets, in order | |
| 54 // to be able to roll out promos slowly, or display different promos to | |
| 55 // different groups. | |
| 56 static const int kMaxGroupSize = 99; | |
|
jstritar
2011/09/28 15:28:21
Depending on what's needed for testing, you can pr
achuithb
2011/09/29 02:00:50
Yup, I'm using this variable in the unit tests so
| |
| 57 | |
| 58 // Maximum number of views. | |
| 59 static const int kMaxViews = 1000; | |
| 60 | |
| 61 // Maximum number of hours for each time slice (4 weeks). | |
| 62 static const int kMaxTimeSliceHours = 24 * 7 * 4; | |
| 63 | |
| 64 // Parse the answers array element. Set the data members of this instance | |
| 65 // and trigger OnNewNotification callback if necessary. | |
| 66 void Parse(const base::DictionaryValue* dict); | |
| 67 | |
| 68 // Set promo notification params from a question string, which is of the form | |
| 69 // <build_type>:<time_slice>:<max_group>:<max_views> | |
| 70 void ParseParams(const base::DictionaryValue* dict); | |
| 71 | |
| 72 // Check if this promo notification is new based on start/end times, | |
| 73 // and trigger events accordingly. | |
| 74 void CheckForNewNotification(); | |
| 75 | |
| 76 // Actions on receiving a new promo notification. | |
| 77 void OnNewNotification(); | |
| 78 | |
| 79 // Create a new promo notification group. | |
| 80 static int NewGroup(); | |
| 81 | |
| 82 // Returns an int converted from the question substring starting at index | |
| 83 // till the next colon. Sets index to the location right after the colon. | |
| 84 // Returns 0 if *err is true, and sets *err to true upon error. | |
| 85 static int GetNextQuestionValue(const std::string& question, | |
| 86 size_t* index, | |
| 87 bool* err); | |
| 88 | |
| 89 // Time getters. | |
| 90 static double GetTimeFromDict(const base::DictionaryValue* dict); | |
| 91 double GetTimeFromPrefs(const char* pref) const; | |
|
jstritar
2011/09/28 15:28:21
These could also be in the anonymous namespace. So
achuithb
2011/09/29 02:00:50
Done.
I was in half a mind whether these needed
| |
| 92 | |
| 93 // Flush data members to prefs for storage. | |
| 94 void WritePrefs(); | |
| 95 | |
| 96 // Match our channel with specified build type. | |
| 97 bool IsBuildAllowed(int builds_allowed) const; | |
| 98 | |
| 99 // For testing. | |
| 100 bool operator==(const PromoNotification& other) const; | |
| 101 | |
| 102 PrefService* prefs_; | |
| 103 Delegate* delegate_; | |
| 104 | |
| 105 double start_; | |
| 106 double end_; | |
| 107 | |
| 108 int build_; | |
| 109 int time_slice_; | |
| 110 int max_group_; | |
| 111 int max_views_; | |
| 112 | |
| 113 int group_; | |
| 114 int views_; | |
| 115 std::string text_; | |
| 116 bool closed_; | |
| 117 | |
| 118 DISALLOW_COPY_AND_ASSIGN(PromoNotification); | |
| 119 }; | |
| 120 | |
| 121 #endif // CHROME_BROWSER_WEB_RESOURCE_PROMO_NOTIFICATION_H_ | |
| 122 | |
| OLD | NEW |