OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/desktop_ios_promotion/desktop_ios_promotion_controll
er.h" | 5 #include "chrome/browser/ui/desktop_ios_promotion/desktop_ios_promotion_controll
er.h" |
6 | 6 |
7 DesktopIOSPromotionController::DesktopIOSPromotionController() {} | 7 #include "base/bind.h" |
| 8 #include "base/metrics/histogram_macros.h" |
| 9 #include "base/time/time.h" |
| 10 #include "chrome/browser/browser_process.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/ui/desktop_ios_promotion/desktop_ios_promotion.h" |
| 13 #include "chrome/browser/ui/desktop_ios_promotion/desktop_ios_promotion_util.h" |
| 14 #include "chrome/browser/ui/desktop_ios_promotion/sms_service.h" |
| 15 #include "chrome/browser/ui/desktop_ios_promotion/sms_service_factory.h" |
| 16 #include "components/prefs/pref_service.h" |
| 17 |
| 18 DesktopIOSPromotionController::DesktopIOSPromotionController( |
| 19 Profile* profile, |
| 20 desktop_ios_promotion::PromotionEntryPoint entry_point) |
| 21 : profile_prefs_(profile->GetPrefs()), |
| 22 entry_point_(entry_point), |
| 23 sms_service_(SMSServiceFactory::GetForProfile(profile)), |
| 24 weak_ptr_factory_(this) { |
| 25 sms_service_->QueryPhoneNumber( |
| 26 base::Bind(&DesktopIOSPromotionController::OnQueryPhoneNumber, |
| 27 weak_ptr_factory_.GetWeakPtr())); |
| 28 } |
8 | 29 |
9 DesktopIOSPromotionController::~DesktopIOSPromotionController() {} | 30 DesktopIOSPromotionController::~DesktopIOSPromotionController() {} |
10 | 31 |
| 32 desktop_ios_promotion::PromotionEntryPoint |
| 33 DesktopIOSPromotionController::GetEntryPoint() { |
| 34 return entry_point_; |
| 35 } |
| 36 |
| 37 void DesktopIOSPromotionController::SetPromotionView( |
| 38 DesktopIOSPromotion* promotion) { |
| 39 promotion_view_ = promotion; |
| 40 } |
| 41 |
11 void DesktopIOSPromotionController::OnSendSMSClicked() { | 42 void DesktopIOSPromotionController::OnSendSMSClicked() { |
12 // TODO(crbug.com/676655): Call the growth api to send sms. | 43 // TODO(crbug.com/676655): Get the SMS message id from the finch group. |
| 44 std::string sms_message_id = "19001507"; |
| 45 sms_service_->SendSMS(sms_message_id, |
| 46 base::Bind(&DesktopIOSPromotionController::OnSendSMS, |
| 47 weak_ptr_factory_.GetWeakPtr())); |
| 48 |
| 49 // Update Profile prefs. |
| 50 profile_prefs_->SetInteger(prefs::kIOSPromotionSMSEntryPoint, |
| 51 static_cast<int>(entry_point_)); |
| 52 |
| 53 // Update histograms. |
| 54 desktop_ios_promotion::LogDismissalReason( |
| 55 desktop_ios_promotion::PromotionDismissalReason::SEND_SMS, entry_point_); |
| 56 } |
| 57 |
| 58 void DesktopIOSPromotionController::OnPromotionShown() { |
| 59 // update the impressions count. |
| 60 PrefService* local_state = g_browser_process->local_state(); |
| 61 int impressions = local_state->GetInteger( |
| 62 desktop_ios_promotion::kEntryPointLocalPrefs |
| 63 [static_cast<int>(entry_point_) + |
| 64 desktop_ios_promotion::kPromotionEntrypointIndexOffset] |
| 65 [static_cast<int>( |
| 66 desktop_ios_promotion::EntryPointLocalPrefType::IMPRESSIONS)]); |
| 67 impressions++; |
| 68 local_state->SetInteger( |
| 69 desktop_ios_promotion::kEntryPointLocalPrefs |
| 70 [static_cast<int>(entry_point_) + |
| 71 desktop_ios_promotion::kPromotionEntrypointIndexOffset] |
| 72 [static_cast<int>( |
| 73 desktop_ios_promotion::EntryPointLocalPrefType::IMPRESSIONS)], |
| 74 impressions); |
| 75 |
| 76 // Update synced profile prefs. |
| 77 int shown_entrypoints = |
| 78 profile_prefs_->GetInteger(prefs::kIOSPromotionShownEntryPoints); |
| 79 shown_entrypoints |= 1 << static_cast<int>(entry_point_); |
| 80 profile_prefs_->SetInteger(prefs::kIOSPromotionShownEntryPoints, |
| 81 shown_entrypoints); |
| 82 |
| 83 // If the promo is seen then it means the SMS was not sent on the last 7 days, |
| 84 // reset the pref. |
| 85 profile_prefs_->SetInteger(prefs::kIOSPromotionSMSEntryPoint, 0); |
| 86 |
| 87 double last_impression = base::Time::NowFromSystemTime().ToDoubleT(); |
| 88 profile_prefs_->SetDouble(prefs::kIOSPromotionLastImpression, |
| 89 last_impression); |
| 90 // Update histograms. |
| 91 UMA_HISTOGRAM_ENUMERATION( |
| 92 "DesktopIOSPromotion.ImpressionFromEntryPoint", |
| 93 static_cast<int>(entry_point_), |
| 94 static_cast<int>( |
| 95 desktop_ios_promotion::PromotionEntryPoint::ENTRY_POINT_MAX_VALUE)); |
13 } | 96 } |
14 | 97 |
15 void DesktopIOSPromotionController::OnNoThanksClicked() { | 98 void DesktopIOSPromotionController::OnNoThanksClicked() { |
16 // TODO(crbug.com/676655): Handle logging & update sync. | 99 PrefService* local_state = g_browser_process->local_state(); |
| 100 local_state->SetBoolean( |
| 101 desktop_ios_promotion::kEntryPointLocalPrefs |
| 102 [static_cast<int>(entry_point_) + |
| 103 desktop_ios_promotion::kPromotionEntrypointIndexOffset] |
| 104 [static_cast<int>( |
| 105 desktop_ios_promotion::EntryPointLocalPrefType::DISMISSED)], |
| 106 true); |
| 107 desktop_ios_promotion::LogDismissalReason( |
| 108 desktop_ios_promotion::PromotionDismissalReason::NO_THANKS, entry_point_); |
17 } | 109 } |
| 110 |
| 111 std::string DesktopIOSPromotionController::GetUsersRecoveryPhoneNumber() { |
| 112 return recovery_number_; |
| 113 } |
| 114 |
| 115 void DesktopIOSPromotionController::OnQueryPhoneNumber( |
| 116 SMSService::Request* request, |
| 117 bool success, |
| 118 const std::string& number) { |
| 119 if (success) |
| 120 recovery_number_ = number; |
| 121 if (promotion_view_) |
| 122 promotion_view_->UpdateRecoveryPhoneLabel(); |
| 123 VLOG(0) << "Recovery phone number arrived " << recovery_number_; |
| 124 UMA_HISTOGRAM_BOOLEAN("DesktopIOSPromotion.QueryPhoneNumberSucceeded", |
| 125 success); |
| 126 } |
| 127 |
| 128 void DesktopIOSPromotionController::OnSendSMS(SMSService::Request* request, |
| 129 bool success, |
| 130 const std::string& number) { |
| 131 UMA_HISTOGRAM_BOOLEAN("DesktopIOSPromotion.SendSMSSucceeded", success); |
| 132 } |
OLD | NEW |