OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 #import "ios/chrome/browser/ui/first_run/first_run_util.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback.h" |
| 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "base/time/time.h" |
| 11 |
| 12 #include "components/signin/core/browser/signin_manager.h" |
| 13 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 14 #import "ios/chrome/browser/crash_report/breakpad_helper.h" |
| 15 #include "ios/chrome/browser/first_run/first_run.h" |
| 16 #import "ios/chrome/browser/first_run/first_run_configuration.h" |
| 17 #include "ios/chrome/browser/first_run/first_run_metrics.h" |
| 18 #include "ios/chrome/browser/signin/signin_manager_factory.h" |
| 19 #include "ios/chrome/browser/tabs/tab.h" |
| 20 #include "ios/chrome/browser/ui/first_run/first_run_histograms.h" |
| 21 #import "ios/chrome/browser/ui/settings/settings_utils.h" |
| 22 #import "ios/chrome/browser/ui/sync/sync_util.h" |
| 23 #include "ios/chrome/browser/ui/ui_util.h" |
| 24 #include "ios/web/public/web_thread.h" |
| 25 #import "ui/gfx/ios/NSString+CrStringDrawing.h" |
| 26 NSString* const kChromeFirstRunUIWillFinishNotification = |
| 27 @"kChromeFirstRunUIWillFinishNotification"; |
| 28 |
| 29 NSString* const kChromeFirstRunUIDidFinishNotification = |
| 30 @"kChromeFirstRunUIDidFinishNotification"; |
| 31 |
| 32 namespace { |
| 33 |
| 34 NSString* RemoveLastWord(NSString* text) { |
| 35 __block NSRange range = NSMakeRange(0, [text length]); |
| 36 NSStringEnumerationOptions options = NSStringEnumerationByWords | |
| 37 NSStringEnumerationReverse | |
| 38 NSStringEnumerationSubstringNotRequired; |
| 39 |
| 40 // Enumerate backwards through the words in |text| to get the range of the |
| 41 // last word. |
| 42 [text |
| 43 enumerateSubstringsInRange:range |
| 44 options:options |
| 45 usingBlock:^(NSString* substring, NSRange substringRange, |
| 46 NSRange enclosingRange, BOOL* stop) { |
| 47 range = substringRange; |
| 48 *stop = YES; |
| 49 }]; |
| 50 return [text substringToIndex:range.location]; |
| 51 } |
| 52 |
| 53 NSString* InsertNewlineBeforeNthToLastWord(NSString* text, int index) { |
| 54 __block NSRange range = NSMakeRange(0, [text length]); |
| 55 __block int count = 0; |
| 56 NSStringEnumerationOptions options = NSStringEnumerationByWords | |
| 57 NSStringEnumerationReverse | |
| 58 NSStringEnumerationSubstringNotRequired; |
| 59 [text |
| 60 enumerateSubstringsInRange:range |
| 61 options:options |
| 62 usingBlock:^(NSString* substring, NSRange substringRange, |
| 63 NSRange enclosingRange, BOOL* stop) { |
| 64 range = substringRange; |
| 65 count++; |
| 66 *stop = count == index; |
| 67 }]; |
| 68 NSMutableString* textWithNewline = [[text mutableCopy] autorelease]; |
| 69 [textWithNewline insertString:@"\n" atIndex:range.location]; |
| 70 return textWithNewline; |
| 71 } |
| 72 |
| 73 // Trampoline method for Bind to create the sentinel file. |
| 74 bool CreateSentinel() { |
| 75 return FirstRun::CreateSentinel(); |
| 76 } |
| 77 |
| 78 // Helper function for recording first run metrics. Takes an additional |
| 79 // |to_record| argument which is the returned value from CreateSentinel(). |
| 80 void RecordFirstRunMetricsInternal(ios::ChromeBrowserState* browserState, |
| 81 bool sign_in_attempted, |
| 82 bool has_sso_accounts, |
| 83 bool to_record) { |
| 84 // |to_record| is false if the sentinel file was not created which indicates |
| 85 // that the sentinel already exists and metrics were already recorded. |
| 86 // Note: If the user signs in and then signs out during first run, it will be |
| 87 // recorded as a successful sign in. |
| 88 if (!to_record) |
| 89 return; |
| 90 |
| 91 bool user_signed_in = |
| 92 ios::SigninManagerFactory::GetForBrowserState(browserState) |
| 93 ->IsAuthenticated(); |
| 94 first_run::SignInStatus sign_in_status; |
| 95 |
| 96 if (user_signed_in) { |
| 97 sign_in_status = has_sso_accounts |
| 98 ? first_run::HAS_SSO_ACCOUNT_SIGNIN_SUCCESSFUL |
| 99 : first_run::SIGNIN_SUCCESSFUL; |
| 100 } else { |
| 101 if (sign_in_attempted) { |
| 102 sign_in_status = has_sso_accounts |
| 103 ? first_run::HAS_SSO_ACCOUNT_SIGNIN_SKIPPED_GIVEUP |
| 104 : first_run::SIGNIN_SKIPPED_GIVEUP; |
| 105 } else { |
| 106 sign_in_status = has_sso_accounts |
| 107 ? first_run::HAS_SSO_ACCOUNT_SIGNIN_SKIPPED_QUICK |
| 108 : first_run::SIGNIN_SKIPPED_QUICK; |
| 109 } |
| 110 } |
| 111 UMA_HISTOGRAM_ENUMERATION("FirstRun.SignIn", sign_in_status, |
| 112 first_run::SIGNIN_SIZE); |
| 113 } |
| 114 |
| 115 } // namespace |
| 116 |
| 117 namespace ios_internal { |
| 118 |
| 119 BOOL FixOrphanWord(UILabel* label) { |
| 120 // Calculate the height of the label's text. |
| 121 NSString* text = label.text; |
| 122 CGSize textSize = |
| 123 [text cr_boundingSizeWithSize:label.frame.size font:label.font]; |
| 124 CGFloat textHeight = AlignValueToPixel(textSize.height); |
| 125 |
| 126 // Remove the last word and calculate the height of the new text. |
| 127 NSString* textMinusLastWord = RemoveLastWord(text); |
| 128 CGSize minusLastWordSize = |
| 129 [textMinusLastWord cr_boundingSizeWithSize:label.frame.size |
| 130 font:label.font]; |
| 131 CGFloat minusLastWordHeight = AlignValueToPixel(minusLastWordSize.height); |
| 132 |
| 133 // Check if removing the last word results in a smaller height. |
| 134 if (minusLastWordHeight < textHeight) { |
| 135 // The last word was the only word on its line. Add a newline before the |
| 136 // second to last word. |
| 137 label.text = InsertNewlineBeforeNthToLastWord(text, 2); |
| 138 return true; |
| 139 } |
| 140 return false; |
| 141 } |
| 142 |
| 143 void WriteFirstRunSentinelAndRecordMetrics( |
| 144 ios::ChromeBrowserState* browserState, |
| 145 BOOL sign_in_attempted, |
| 146 BOOL has_sso_account) { |
| 147 // Call CreateSentinel() and pass the result into RecordFirstRunMetrics(). |
| 148 base::Callback<bool(void)> task = base::Bind(&CreateSentinel); |
| 149 base::Callback<void(bool)> reply = |
| 150 base::Bind(&RecordFirstRunMetricsInternal, browserState, |
| 151 sign_in_attempted, has_sso_account); |
| 152 base::PostTaskAndReplyWithResult(web::WebThread::GetBlockingPool(), FROM_HERE, |
| 153 task, reply); |
| 154 } |
| 155 |
| 156 void FinishFirstRun(ios::ChromeBrowserState* browserState, |
| 157 Tab* tab, |
| 158 FirstRunConfiguration* config) { |
| 159 [[NSNotificationCenter defaultCenter] |
| 160 postNotificationName:kChromeFirstRunUIWillFinishNotification |
| 161 object:nil]; |
| 162 WriteFirstRunSentinelAndRecordMetrics(browserState, config.signInAttempted, |
| 163 config.hasSSOAccount); |
| 164 |
| 165 // Display the sync errors infobar. |
| 166 ios_internal::sync::displaySyncErrors(browserState, tab); |
| 167 } |
| 168 |
| 169 void RecordProductTourTimingMetrics(NSString* timer_name, |
| 170 base::TimeTicks start_time) { |
| 171 base::TimeDelta delta = base::TimeTicks::Now() - start_time; |
| 172 NSString* histogramName = |
| 173 [NSString stringWithFormat:@"ProductTour.IOSScreens%@", timer_name]; |
| 174 UMA_HISTOGRAM_CUSTOM_TIMES_FIRST_RUN(base::SysNSStringToUTF8(histogramName), |
| 175 delta, |
| 176 base::TimeDelta::FromMilliseconds(10), |
| 177 base::TimeDelta::FromMinutes(3), 50); |
| 178 } |
| 179 |
| 180 void FirstRunDismissed() { |
| 181 [[NSNotificationCenter defaultCenter] |
| 182 postNotificationName:kChromeFirstRunUIDidFinishNotification |
| 183 object:nil]; |
| 184 } |
| 185 |
| 186 } // namespace ios_internal |
OLD | NEW |