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_util.h" |
| 13 #include "chrome/browser/ui/desktop_ios_promotion/desktop_ios_promotion_view.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" |
8 | 17 |
9 DesktopIOSPromotionController::~DesktopIOSPromotionController() {} | 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 dismissal_reason_( |
| 25 desktop_ios_promotion::PromotionDismissalReason::FOCUS_LOST), |
| 26 weak_ptr_factory_(this) { |
| 27 sms_service_->QueryPhoneNumber( |
| 28 base::Bind(&DesktopIOSPromotionController::OnGotPhoneNumber, |
| 29 weak_ptr_factory_.GetWeakPtr())); |
| 30 } |
| 31 |
| 32 DesktopIOSPromotionController::~DesktopIOSPromotionController() { |
| 33 desktop_ios_promotion::LogDismissalReason(dismissal_reason_, entry_point_); |
| 34 } |
| 35 |
| 36 void DesktopIOSPromotionController::SetPromotionView( |
| 37 DesktopIOSPromotionView* promotion_view) { |
| 38 promotion_view_ = promotion_view; |
| 39 } |
10 | 40 |
11 void DesktopIOSPromotionController::OnSendSMSClicked() { | 41 void DesktopIOSPromotionController::OnSendSMSClicked() { |
12 // TODO(crbug.com/676655): Call the growth api to send sms. | 42 // TODO(crbug.com/676655): Get the SMS message id from the finch group. |
| 43 std::string sms_message_id = "19001507"; |
| 44 sms_service_->SendSMS(sms_message_id, |
| 45 base::Bind(&DesktopIOSPromotionController::OnSendSMS, |
| 46 weak_ptr_factory_.GetWeakPtr())); |
| 47 |
| 48 // Update Profile prefs. |
| 49 profile_prefs_->SetInteger(prefs::kIOSPromotionSMSEntryPoint, |
| 50 static_cast<int>(entry_point_)); |
| 51 |
| 52 // Update dismissal reason. |
| 53 dismissal_reason_ = desktop_ios_promotion::PromotionDismissalReason::SEND_SMS; |
| 54 } |
| 55 |
| 56 void DesktopIOSPromotionController::OnPromotionShown() { |
| 57 // update the impressions count. |
| 58 PrefService* local_state = g_browser_process->local_state(); |
| 59 int impressions = local_state->GetInteger( |
| 60 desktop_ios_promotion::kEntryPointLocalPrefs |
| 61 [static_cast<int>(entry_point_)][static_cast<int>( |
| 62 desktop_ios_promotion::EntryPointLocalPrefType::IMPRESSIONS)]); |
| 63 impressions++; |
| 64 local_state->SetInteger( |
| 65 desktop_ios_promotion::kEntryPointLocalPrefs |
| 66 [static_cast<int>(entry_point_)][static_cast<int>( |
| 67 desktop_ios_promotion::EntryPointLocalPrefType::IMPRESSIONS)], |
| 68 impressions); |
| 69 |
| 70 // Update synced profile prefs. |
| 71 int shown_entrypoints = |
| 72 profile_prefs_->GetInteger(prefs::kIOSPromotionShownEntryPoints); |
| 73 shown_entrypoints |= 1 << static_cast<int>(entry_point_); |
| 74 profile_prefs_->SetInteger(prefs::kIOSPromotionShownEntryPoints, |
| 75 shown_entrypoints); |
| 76 |
| 77 // If the promo is seen then it means the SMS was not sent on the last 7 days, |
| 78 // reset the pref. |
| 79 profile_prefs_->SetInteger(prefs::kIOSPromotionSMSEntryPoint, 0); |
| 80 |
| 81 double last_impression = base::Time::NowFromSystemTime().ToDoubleT(); |
| 82 profile_prefs_->SetDouble(prefs::kIOSPromotionLastImpression, |
| 83 last_impression); |
| 84 // Update histograms. |
| 85 UMA_HISTOGRAM_ENUMERATION( |
| 86 "DesktopIOSPromotion.ImpressionFromEntryPoint", |
| 87 static_cast<int>(entry_point_), |
| 88 static_cast<int>( |
| 89 desktop_ios_promotion::PromotionEntryPoint::ENTRY_POINT_MAX_VALUE)); |
13 } | 90 } |
14 | 91 |
15 void DesktopIOSPromotionController::OnNoThanksClicked() { | 92 void DesktopIOSPromotionController::OnNoThanksClicked() { |
16 // TODO(crbug.com/676655): Handle logging & update sync. | 93 PrefService* local_state = g_browser_process->local_state(); |
| 94 local_state->SetBoolean( |
| 95 desktop_ios_promotion::kEntryPointLocalPrefs |
| 96 [static_cast<int>(entry_point_)][static_cast<int>( |
| 97 desktop_ios_promotion::EntryPointLocalPrefType::DISMISSED)], |
| 98 true); |
| 99 dismissal_reason_ = |
| 100 desktop_ios_promotion::PromotionDismissalReason::NO_THANKS; |
17 } | 101 } |
| 102 |
| 103 std::string DesktopIOSPromotionController::GetUsersRecoveryPhoneNumber() { |
| 104 return recovery_number_; |
| 105 } |
| 106 |
| 107 void DesktopIOSPromotionController::OnGotPhoneNumber( |
| 108 SMSService::Request* request, |
| 109 bool success, |
| 110 const std::string& number) { |
| 111 if (success) |
| 112 recovery_number_ = number; |
| 113 if (promotion_view_) |
| 114 promotion_view_->UpdateRecoveryPhoneLabel(); |
| 115 UMA_HISTOGRAM_BOOLEAN("DesktopIOSPromotion.QueryPhoneNumberSucceeded", |
| 116 success); |
| 117 } |
| 118 |
| 119 void DesktopIOSPromotionController::OnSendSMS(SMSService::Request* request, |
| 120 bool success, |
| 121 const std::string& number) { |
| 122 UMA_HISTOGRAM_BOOLEAN("DesktopIOSPromotion.SendSMSSucceeded", success); |
| 123 } |
OLD | NEW |