1 // Copyright (c) 2010 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 #include "chrome/browser/instant/promo_counter.h" 6 7 #include "base/metrics/histogram.h" 8 #include "base/values.h" 9 #include "chrome/browser/prefs/pref_service.h" 10 #include "chrome/browser/profiles/profile.h" 11 12 // Pref keys. These are relative to pref_key_. 13 static const char* kShowKey = ".show"; 14 static const char* kNumSessionsKey = ".num_sessions"; 15 static const char* kInitialTimeKey = ".initial_time"; 16 17 // Values used for histograms. These are relative to histogram_key_. 18 static const char* kHistogramHide = ".hide"; 19 static const char* kHistogramMaxSessions = ".max_sessions"; 20 static const char* kHistogramMaxTime = ".max_time"; 21 22 PromoCounter::PromoCounter(Profile* profile, 23 const std::string& pref_key, 24 const std::string& histogram_key, 25 int max_sessions, 26 int max_days) 27 : profile_(profile), 28 pref_key_(pref_key), 29 histogram_key_(histogram_key), 30 max_sessions_(max_sessions), 31 max_days_(max_days), 32 did_init_(false), 33 show_(false) { 34 } 35 36 PromoCounter::~PromoCounter() { 37 } 38 39 // static 40 void PromoCounter::RegisterUserPrefs(PrefService* prefs, 41 const std::string& base_key) { 42 prefs->RegisterBooleanPref((base_key + kShowKey).c_str(), true); 43 prefs->RegisterIntegerPref((base_key + kNumSessionsKey).c_str(), 0); 44 prefs->RegisterInt64Pref((base_key + kInitialTimeKey).c_str(), 0); 45 } 46 47 bool PromoCounter::ShouldShow(base::Time current_time) { 48 if (!did_init_) { 49 did_init_ = true; 50 Init(current_time); 51 } 52 53 if (show_ && (current_time - initial_show_).InDays() >= max_days_) 54 MaxTimeLapsed(current_time); 55 56 return show_; 57 } 58 59 void PromoCounter::Hide() { 60 show_ = false; 61 did_init_ = true; 62 UMA_HISTOGRAM_CUSTOM_COUNTS(histogram_key_ + kHistogramHide, 63 (base::Time::Now() - initial_show_).InHours(), 64 1, max_days_ * 24, 24); 65 if (profile_->GetPrefs()) 66 profile_->GetPrefs()->SetBoolean((pref_key_ + kShowKey).c_str(), false); 67 } 68 69 void PromoCounter::Init(base::Time current_time) { 70 PrefService* prefs = profile_->GetPrefs(); 71 if (!prefs) 72 return; 73 74 show_ = prefs->GetBoolean((pref_key_ + kShowKey).c_str()); 75 if (!show_) 76 return; 77 78 // The user hasn't chosen to opt in or out. Only show the opt-in if it's 79 // less than max_days_ since we first showed the opt-in, or the user hasn't 80 // launched the profile max_sessions_ times. 81 int session_count = prefs->GetInteger((pref_key_ + kNumSessionsKey).c_str()); 82 int64 initial_show_int = 83 prefs->GetInt64((pref_key_ + kInitialTimeKey).c_str()); 84 initial_show_ = base::Time(base::Time::FromInternalValue(initial_show_int)); 85 if (initial_show_int == 0 || initial_show_ > current_time) { 86 initial_show_ = base::Time::Now(); 87 prefs->SetInt64((pref_key_ + kInitialTimeKey).c_str(), 88 initial_show_.ToInternalValue()); 89 } 90 if (session_count >= max_sessions_) { 91 // Time check is handled in ShouldShow. 92 MaxSessionsEncountered(current_time); 93 } else { 94 // Up the session count. 95 prefs->SetInteger((pref_key_ + kNumSessionsKey).c_str(), session_count + 1); 96 } 97 } 98 99 void PromoCounter::MaxSessionsEncountered(base::Time current_time) { 100 show_ = false; 101 UMA_HISTOGRAM_CUSTOM_COUNTS(histogram_key_ + kHistogramMaxSessions, 102 (current_time - initial_show_).InHours(), 1, 103 max_days_ * 24, 24); 104 if (profile_->GetPrefs()) 105 profile_->GetPrefs()->SetBoolean((pref_key_ + kShowKey).c_str(), false); 106 } 107 108 void PromoCounter::MaxTimeLapsed(base::Time current_time) { 109 show_ = false; 110 UMA_HISTOGRAM_CUSTOM_COUNTS(histogram_key_ + kHistogramMaxTime, 111 (current_time - initial_show_).InHours(), 112 1, max_days_ * 24, 24); 113 if (profile_->GetPrefs()) 114 profile_->GetPrefs()->SetBoolean((pref_key_ + kShowKey).c_str(), false); 115 } 116