OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/app/application_delegate/background_activity.h" |
| 6 |
| 7 #import "base/ios/weak_nsobject.h" |
| 8 #include "base/metrics/histogram_macros.h" |
| 9 #include "base/metrics/user_metrics.h" |
| 10 #include "base/metrics/user_metrics_action.h" |
| 11 #include "base/time/time.h" |
| 12 #import "ios/chrome/app/application_delegate/browser_launcher.h" |
| 13 #import "ios/chrome/app/application_delegate/metrics_mediator.h" |
| 14 #import "ios/chrome/browser/crash_report/crash_report_background_uploader.h" |
| 15 #include "ios/chrome/browser/metrics/first_user_action_recorder.h" |
| 16 #import "ios/chrome/browser/metrics/previous_session_info.h" |
| 17 #include "url/gurl.h" |
| 18 |
| 19 namespace { |
| 20 // Key of the UMA Startup.BackgroundFetchTimeInterval histogram. |
| 21 const char* const kUMAMobileBackgroundFetchTimeInterval = |
| 22 "Startup.BackgroundFetchTimeInterval"; |
| 23 // Key in the UserDefaults to store the date/time that the application was last |
| 24 // launched for a background fetch. |
| 25 NSString* const kLastBackgroundFetch = @"LastBackgroundFetch"; |
| 26 } // namespace |
| 27 |
| 28 @implementation BackgroundActivity |
| 29 |
| 30 - (instancetype)init { |
| 31 NOTREACHED(); |
| 32 return nil; |
| 33 } |
| 34 |
| 35 #pragma mark - Public class methods. |
| 36 |
| 37 + (void)application:(UIApplication*)application |
| 38 performFetchWithCompletionHandler: |
| 39 (void (^)(UIBackgroundFetchResult))completionHandler |
| 40 metricsMediator:(MetricsMediator*)metricsMediator |
| 41 browserLauncher:(id<BrowserLauncher>)browserLauncher { |
| 42 [browserLauncher startUpBrowserToStage:INITIALIZATION_STAGE_BACKGROUND]; |
| 43 |
| 44 // TODO(crbug.com/616436): Check if this can be moved to MetricsMediator. |
| 45 base::RecordAction(base::UserMetricsAction("BackgroundFetchCalled")); |
| 46 NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults]; |
| 47 base::Time lastTime = |
| 48 base::Time::FromDoubleT([userDefaults doubleForKey:kLastBackgroundFetch]); |
| 49 base::Time now = base::Time::Now(); |
| 50 if (!lastTime.is_null()) { |
| 51 UMA_HISTOGRAM_LONG_TIMES_100(kUMAMobileBackgroundFetchTimeInterval, |
| 52 now - lastTime); |
| 53 } |
| 54 [userDefaults setDouble:now.ToDoubleT() forKey:kLastBackgroundFetch]; |
| 55 |
| 56 // The crashes concerning an old version are obsolete. To reduce data usage, |
| 57 // these crash reports are not uploaded. |
| 58 if (![metricsMediator areMetricsEnabled] || |
| 59 [[PreviousSessionInfo sharedInstance] isFirstSessionAfterUpgrade]) { |
| 60 [application setMinimumBackgroundFetchInterval: |
| 61 UIApplicationBackgroundFetchIntervalNever]; |
| 62 completionHandler(UIBackgroundFetchResultNoData); |
| 63 return; |
| 64 } |
| 65 |
| 66 if (![metricsMediator isUploadingEnabled]) { |
| 67 completionHandler(UIBackgroundFetchResultFailed); |
| 68 return; |
| 69 } |
| 70 [CrashReportBackgroundUploader |
| 71 performFetchWithCompletionHandler:completionHandler]; |
| 72 } |
| 73 |
| 74 + (void)handleEventsForBackgroundURLSession:(NSString*)identifier |
| 75 completionHandler:(void (^)(void))completionHandler |
| 76 browserLauncher: |
| 77 (id<BrowserLauncher>)browserLauncher { |
| 78 [browserLauncher startUpBrowserToStage:INITIALIZATION_STAGE_BACKGROUND]; |
| 79 if ([CrashReportBackgroundUploader |
| 80 canHandleBackgroundURLSession:identifier]) { |
| 81 [CrashReportBackgroundUploader |
| 82 handleEventsForBackgroundURLSession:identifier |
| 83 completionHandler:completionHandler]; |
| 84 } else { |
| 85 completionHandler(); |
| 86 } |
| 87 } |
| 88 |
| 89 + (void)foregroundStarted { |
| 90 [[NSUserDefaults standardUserDefaults] |
| 91 removeObjectForKey:kLastBackgroundFetch]; |
| 92 } |
| 93 |
| 94 @end |
OLD | NEW |