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_NOTIFICATION_PROMO_H_ |
| 6 #define CHROME_BROWSER_WEB_RESOURCE_NOTIFICATION_PROMO_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 NotificationPromo { |
| 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 NotificationPromo(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 // Calculates promo notification start time with group-based time slice |
| 42 // offset. |
| 43 double StartTimeWithOffset() const; |
| 44 |
| 45 // Helpers for NewTabPageHandler. |
| 46 void HandleClosed(); |
| 47 bool HandleViewed(); // returns true if views exceeds maximum allowed. |
| 48 |
| 49 // Register preferences. |
| 50 static void RegisterUserPrefs(PrefService* prefs); |
| 51 |
| 52 private: |
| 53 // For testing. |
| 54 friend class NotificationPromoTestDelegate; |
| 55 FRIEND_TEST_ALL_PREFIXES(PromoResourceServiceTest, GetNextQuestionValueTest); |
| 56 FRIEND_TEST_ALL_PREFIXES(PromoResourceServiceTest, NewGroupTest); |
| 57 |
| 58 // Users are randomly assigned to one of kMaxGroupSize + 1 buckets, in order |
| 59 // to be able to roll out promos slowly, or display different promos to |
| 60 // different groups. |
| 61 static const int kMaxGroupSize = 99; |
| 62 |
| 63 // Parse the answers array element. Set the data members of this instance |
| 64 // and trigger OnNewNotification callback if necessary. |
| 65 void Parse(const base::DictionaryValue* dict); |
| 66 |
| 67 // Set promo notification params from a question string, which is of the form |
| 68 // <build_type>:<time_slice>:<max_group>:<max_views> |
| 69 void ParseParams(const base::DictionaryValue* dict); |
| 70 |
| 71 // Check if this promo notification is new based on start/end times, |
| 72 // and trigger events accordingly. |
| 73 void CheckForNewNotification(); |
| 74 |
| 75 // Actions on receiving a new promo notification. |
| 76 void OnNewNotification(); |
| 77 |
| 78 // Create a new promo notification group. |
| 79 static int NewGroup(); |
| 80 |
| 81 // Returns an int converted from the question substring starting at index |
| 82 // till the next colon. Sets index to the location right after the colon. |
| 83 // Returns 0 if *err is true, and sets *err to true upon error. |
| 84 static int GetNextQuestionValue(const std::string& question, |
| 85 size_t* index, |
| 86 bool* err); |
| 87 |
| 88 // Flush data members to prefs for storage. |
| 89 void WritePrefs(); |
| 90 |
| 91 // Match our channel with specified build type. |
| 92 bool IsBuildAllowed(int builds_allowed) const; |
| 93 |
| 94 // For testing. |
| 95 bool operator==(const NotificationPromo& other) const; |
| 96 |
| 97 PrefService* prefs_; |
| 98 Delegate* delegate_; |
| 99 |
| 100 double start_; |
| 101 double end_; |
| 102 |
| 103 int build_; |
| 104 int time_slice_; |
| 105 int max_group_; |
| 106 int max_views_; |
| 107 |
| 108 int group_; |
| 109 int views_; |
| 110 std::string text_; |
| 111 bool closed_; |
| 112 |
| 113 DISALLOW_COPY_AND_ASSIGN(NotificationPromo); |
| 114 }; |
| 115 |
| 116 #endif // CHROME_BROWSER_WEB_RESOURCE_NOTIFICATION_PROMO_H_ |
| 117 |
OLD | NEW |