Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / components / web_resource / notification_promo.h
blob69a81a8539329d538b7be4ecb3900d41ea3afa07
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.
5 #ifndef COMPONENTS_WEB_RESOURCE_NOTIFICATION_PROMO_H_
6 #define COMPONENTS_WEB_RESOURCE_NOTIFICATION_PROMO_H_
8 #include <string>
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"
16 class PrefRegistrySimple;
17 class PrefService;
19 namespace base {
20 class DictionaryValue;
21 class ListValue;
24 namespace user_prefs {
25 class PrefRegistrySyncable;
28 namespace version_info {
29 enum class Channel;
32 namespace web_resource {
34 // Helper class for PromoResourceService that parses promo notification info
35 // from json or prefs.
36 class NotificationPromo {
37 public:
38 static GURL PromoServerURL(version_info::Channel channel);
40 enum PromoType {
41 NO_PROMO,
42 NTP_NOTIFICATION_PROMO,
43 NTP_BUBBLE_PROMO,
44 MOBILE_NTP_SYNC_PROMO,
45 MOBILE_NTP_WHATS_NEW_PROMO,
48 explicit NotificationPromo(PrefService* local_state);
49 ~NotificationPromo();
51 // Initialize from json/prefs.
52 void InitFromJson(const base::DictionaryValue& json, PromoType promo_type);
53 void InitFromPrefs(PromoType promo_type);
55 // Can this promo be shown?
56 bool CanShow() const;
58 // Calculates promo notification start time with group-based time slice
59 // offset.
60 double StartTimeForGroup() const;
61 double EndTime() const;
63 // Helpers for NewTabPageHandler.
64 // Mark the promo as closed when the user dismisses it.
65 static void HandleClosed(PromoType promo_type, PrefService* local_state);
66 // Mark the promo has having been viewed. This returns true if views
67 // exceeds the maximum allowed.
68 static bool HandleViewed(PromoType promo_type, PrefService* local_state);
70 bool new_notification() const { return new_notification_; }
72 const std::string& promo_text() const { return promo_text_; }
73 PromoType promo_type() const { return promo_type_; }
74 const base::DictionaryValue* promo_payload() const {
75 return promo_payload_.get();
78 // Register preferences.
79 static void RegisterPrefs(PrefRegistrySimple* registry);
80 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
81 static void MigrateUserPrefs(PrefService* user_prefs);
83 private:
84 // For testing.
85 friend class NotificationPromoTest;
87 // Check if this promo notification is new based on start/end times,
88 // and trigger events accordingly.
89 void CheckForNewNotification();
91 // Actions on receiving a new promo notification.
92 void OnNewNotification();
94 // Flush data members to prefs for storage.
95 void WritePrefs();
97 // Tests group_ against max_group_.
98 // When max_group_ is 0, all groups pass.
99 bool ExceedsMaxGroup() const;
101 // Tests views_ against max_views_.
102 // When max_views_ is 0, we don't cap the number of views.
103 bool ExceedsMaxViews() const;
105 // Tests |first_view_time_| + |max_seconds_| and -now().
106 // When either is 0, we don't cap the number of seconds.
107 bool ExceedsMaxSeconds() const;
109 PrefService* local_state_;
111 PromoType promo_type_;
112 std::string promo_text_;
114 scoped_ptr<const base::DictionaryValue> promo_payload_;
116 double start_;
117 double end_;
119 int num_groups_;
120 int initial_segment_;
121 int increment_;
122 int time_slice_;
123 int max_group_;
125 // When max_views_ is 0, we don't cap the number of views.
126 int max_views_;
128 // When max_seconds_ is 0 or not set, we don't cap the number of seconds a
129 // promo can be visible.
130 int max_seconds_;
132 // Set when the promo is viewed for the first time.
133 double first_view_time_;
135 int group_;
136 int views_;
137 bool closed_;
139 bool new_notification_;
141 DISALLOW_COPY_AND_ASSIGN(NotificationPromo);
144 } // namespace web_resource
146 #endif // COMPONENTS_WEB_RESOURCE_NOTIFICATION_PROMO_H_