notification_promo.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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 "googleurl/src/gurl.h"
15
16namespace base {
17class DictionaryValue;
18class ListValue;
19}
20
21class PrefService;
22class Profile;
23
24// Helper class for PromoResourceService that parses promo notification info
25// from json or prefs.
26class NotificationPromo {
27 public:
28  static GURL PromoServerURL();
29
30  enum PromoType {
31    NO_PROMO,
32    NTP_NOTIFICATION_PROMO,
33    NTP_BUBBLE_PROMO,
34    MOBILE_NTP_SYNC_PROMO,
35  };
36
37  explicit NotificationPromo(Profile* profile);
38  ~NotificationPromo();
39
40  // Initialize from json/prefs.
41  void InitFromJson(const base::DictionaryValue& json, PromoType promo_type);
42  void InitFromPrefs(PromoType promo_type);
43
44  // Can this promo be shown?
45  bool CanShow() const;
46
47  // Calculates promo notification start time with group-based time slice
48  // offset.
49  double StartTimeForGroup() const;
50  double EndTime() const;
51
52  // Helpers for NewTabPageHandler.
53  // Mark the promo as closed when the user dismisses it.
54  static void HandleClosed(Profile* profile, PromoType promo_type);
55  // Mark the promo has having been viewed. This returns true if views
56  // exceeds the maximum allowed.
57  static bool HandleViewed(Profile* profile, PromoType promo_type);
58
59  bool new_notification() const { return new_notification_; }
60
61  const std::string& promo_text() const { return promo_text_; }
62  PromoType promo_type() const { return promo_type_; }
63  const base::DictionaryValue* promo_payload() const {
64    return promo_payload_.get();
65  }
66
67  // Register preferences.
68  static void RegisterUserPrefs(PrefService* prefs);
69
70 private:
71  // For testing.
72  friend class NotificationPromoTest;
73
74  // Check if this promo notification is new based on start/end times,
75  // and trigger events accordingly.
76  void CheckForNewNotification();
77
78  // Actions on receiving a new promo notification.
79  void OnNewNotification();
80
81  // Flush data members to prefs for storage.
82  void WritePrefs();
83
84  // Tests group_ against max_group_.
85  // When max_group_ is 0, all groups pass.
86  bool ExceedsMaxGroup() const;
87
88  // Tests views_ against max_views_.
89  // When max_views_ is 0, we don't cap the number of views.
90  bool ExceedsMaxViews() const;
91
92  // True if this promo is not targeted to G+ users, or if this is a G+ user.
93  bool IsGPlusRequired() const;
94
95  Profile* profile_;
96  PrefService* prefs_;
97
98  PromoType promo_type_;
99  std::string promo_text_;
100
101  // Note that promo_payload_ isn't currently used for desktop promos.
102  scoped_ptr<const base::DictionaryValue> promo_payload_;
103
104  double start_;
105  double end_;
106
107  int num_groups_;
108  int initial_segment_;
109  int increment_;
110  int time_slice_;
111  int max_group_;
112
113  // When max_views_ is 0, we don't cap the number of views.
114  int max_views_;
115
116  int group_;
117  int views_;
118  bool closed_;
119
120  bool gplus_required_;
121
122  bool new_notification_;
123
124  DISALLOW_COPY_AND_ASSIGN(NotificationPromo);
125};
126
127#endif  // CHROME_BROWSER_WEB_RESOURCE_NOTIFICATION_PROMO_H_
128