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