• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
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 
8 #include <string>
9 
10 #include "base/basictypes.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "url/gurl.h"
15 
16 class PrefRegistrySimple;
17 class PrefService;
18 
19 namespace base {
20 class DictionaryValue;
21 class ListValue;
22 }
23 
24 namespace user_prefs {
25 class PrefRegistrySyncable;
26 }
27 
28 // Helper class for PromoResourceService that parses promo notification info
29 // from json or prefs.
30 class NotificationPromo {
31  public:
32   static GURL PromoServerURL();
33 
34   enum PromoType {
35     NO_PROMO,
36     NTP_NOTIFICATION_PROMO,
37     NTP_BUBBLE_PROMO,
38     MOBILE_NTP_SYNC_PROMO,
39     MOBILE_NTP_WHATS_NEW_PROMO,
40   };
41 
42   NotificationPromo();
43   ~NotificationPromo();
44 
45   // Initialize from json/prefs.
46   void InitFromJson(const base::DictionaryValue& json, PromoType promo_type);
47   void InitFromPrefs(PromoType promo_type);
48 
49   // Can this promo be shown?
50   bool CanShow() const;
51 
52   // Calculates promo notification start time with group-based time slice
53   // offset.
54   double StartTimeForGroup() const;
55   double EndTime() const;
56 
57   // Helpers for NewTabPageHandler.
58   // Mark the promo as closed when the user dismisses it.
59   static void HandleClosed(PromoType promo_type);
60   // Mark the promo has having been viewed. This returns true if views
61   // exceeds the maximum allowed.
62   static bool HandleViewed(PromoType promo_type);
63 
new_notification()64   bool new_notification() const { return new_notification_; }
65 
promo_text()66   const std::string& promo_text() const { return promo_text_; }
promo_type()67   PromoType promo_type() const { return promo_type_; }
promo_payload()68   const base::DictionaryValue* promo_payload() const {
69     return promo_payload_.get();
70   }
71 
72   // Register preferences.
73   static void RegisterPrefs(PrefRegistrySimple* registry);
74   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
75   static void MigrateUserPrefs(PrefService* user_prefs);
76 
77  private:
78   // For testing.
79   friend class NotificationPromoTest;
80 
81   // Check if this promo notification is new based on start/end times,
82   // and trigger events accordingly.
83   void CheckForNewNotification();
84 
85   // Actions on receiving a new promo notification.
86   void OnNewNotification();
87 
88   // Flush data members to prefs for storage.
89   void WritePrefs();
90 
91   // Tests group_ against max_group_.
92   // When max_group_ is 0, all groups pass.
93   bool ExceedsMaxGroup() const;
94 
95   // Tests views_ against max_views_.
96   // When max_views_ is 0, we don't cap the number of views.
97   bool ExceedsMaxViews() const;
98 
99   // Tests |first_view_time_| + |max_seconds_| and -now().
100   // When either is 0, we don't cap the number of seconds.
101   bool ExceedsMaxSeconds() const;
102 
103   // Returns false if this promo should not be displayed because it is a promo
104   // for the app launcher, and the user has already enabled the app launcher.
105   bool CheckAppLauncher() const;
106 
107   PrefService* prefs_;
108 
109   PromoType promo_type_;
110   std::string promo_text_;
111 
112   scoped_ptr<const base::DictionaryValue> promo_payload_;
113 
114   double start_;
115   double end_;
116 
117   int num_groups_;
118   int initial_segment_;
119   int increment_;
120   int time_slice_;
121   int max_group_;
122 
123   // When max_views_ is 0, we don't cap the number of views.
124   int max_views_;
125 
126   // When max_seconds_ is 0 or not set, we don't cap the number of seconds a
127   // promo can be visible.
128   int max_seconds_;
129 
130   // Set when the promo is viewed for the first time.
131   double first_view_time_;
132 
133   int group_;
134   int views_;
135   bool closed_;
136 
137   bool new_notification_;
138 
139   DISALLOW_COPY_AND_ASSIGN(NotificationPromo);
140 };
141 
142 #endif  // CHROME_BROWSER_WEB_RESOURCE_NOTIFICATION_PROMO_H_
143