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/main_application_delegate.h" |
| 6 |
| 7 #include "base/mac/foundation_util.h" |
| 8 #import "base/mac/scoped_nsobject.h" |
| 9 #import "ios/chrome/app/application_delegate/app_navigation.h" |
| 10 #import "ios/chrome/app/application_delegate/app_state.h" |
| 11 #import "ios/chrome/app/application_delegate/background_activity.h" |
| 12 #import "ios/chrome/app/application_delegate/browser_launcher.h" |
| 13 #import "ios/chrome/app/application_delegate/memory_warning_helper.h" |
| 14 #import "ios/chrome/app/application_delegate/metrics_mediator.h" |
| 15 #import "ios/chrome/app/application_delegate/startup_information.h" |
| 16 #import "ios/chrome/app/application_delegate/tab_opening.h" |
| 17 #import "ios/chrome/app/application_delegate/tab_switching.h" |
| 18 #import "ios/chrome/app/application_delegate/url_opener.h" |
| 19 #import "ios/chrome/app/application_delegate/user_activity_handler.h" |
| 20 #import "ios/chrome/app/chrome_overlay_window.h" |
| 21 #import "ios/chrome/app/main_application_delegate_testing.h" |
| 22 #import "ios/chrome/app/main_controller.h" |
| 23 #include "ios/public/provider/chrome/browser/chrome_browser_provider.h" |
| 24 #include "ios/public/provider/chrome/browser/signin/chrome_identity_service.h" |
| 25 |
| 26 @interface MainApplicationDelegate () { |
| 27 base::scoped_nsobject<MainController> _mainController; |
| 28 // Memory helper used to log the number of memory warnings received. |
| 29 base::scoped_nsobject<MemoryWarningHelper> _memoryHelper; |
| 30 // Metrics mediator used to check and update the metrics accordingly to |
| 31 // to the user preferences. |
| 32 base::scoped_nsobject<MetricsMediator> _metricsMediator; |
| 33 // Browser launcher to have a global launcher. |
| 34 base::scoped_nsprotocol<id<BrowserLauncher>> _browserLauncher; |
| 35 // Container for startup information. |
| 36 base::scoped_nsprotocol<id<StartupInformation>> _startupInformation; |
| 37 // Helper to open new tabs. |
| 38 base::scoped_nsprotocol<id<TabOpening>> _tabOpener; |
| 39 // Handles the application stage changes. |
| 40 base::scoped_nsobject<AppState> _appState; |
| 41 // Handles tab switcher. |
| 42 base::scoped_nsprotocol<id<AppNavigation>> _appNavigation; |
| 43 // Handles tab switcher. |
| 44 base::scoped_nsprotocol<id<TabSwitching>> _tabSwitcherProtocol; |
| 45 } |
| 46 |
| 47 @end |
| 48 |
| 49 @implementation MainApplicationDelegate |
| 50 |
| 51 - (instancetype)init { |
| 52 if (self = [super init]) { |
| 53 _memoryHelper.reset([[MemoryWarningHelper alloc] init]); |
| 54 _mainController.reset([[MainController alloc] init]); |
| 55 _metricsMediator.reset([[MetricsMediator alloc] init]); |
| 56 [_mainController setMetricsMediator:_metricsMediator]; |
| 57 _browserLauncher.reset([_mainController retain]); |
| 58 _startupInformation.reset([_mainController retain]); |
| 59 _tabOpener.reset([_mainController retain]); |
| 60 _appState.reset([[AppState alloc] |
| 61 initWithBrowserLauncher:_browserLauncher |
| 62 startupInformation:_startupInformation |
| 63 applicationDelegate:self]); |
| 64 _tabSwitcherProtocol.reset([_mainController retain]); |
| 65 _appNavigation.reset([_mainController retain]); |
| 66 [_mainController setAppState:_appState]; |
| 67 } |
| 68 return self; |
| 69 } |
| 70 |
| 71 - (UIWindow*)window { |
| 72 return [_mainController window]; |
| 73 } |
| 74 |
| 75 - (void)setWindow:(UIWindow*)newWindow { |
| 76 DCHECK(newWindow); |
| 77 [_mainController setWindow:newWindow]; |
| 78 // self.window has been set by this time. _appState window can now be set. |
| 79 [_appState setWindow:newWindow]; |
| 80 } |
| 81 |
| 82 #pragma mark - UIApplicationDelegate methods - |
| 83 |
| 84 #pragma mark Responding to App State Changes and System Events |
| 85 |
| 86 // Called by the OS to create the UI for display. The UI will not be displayed, |
| 87 // even if it is ready, until this function returns. |
| 88 // The absolute minimum work should be done here, to ensure that the application |
| 89 // startup is fast, and the UI appears as soon as possible. |
| 90 - (BOOL)application:(UIApplication*)application |
| 91 didFinishLaunchingWithOptions:(NSDictionary*)launchOptions { |
| 92 // Main window must be ChromeOverlayWindow or a subclass of it. |
| 93 self.window = [[[ChromeOverlayWindow alloc] |
| 94 initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; |
| 95 |
| 96 BOOL inBackground = |
| 97 [application applicationState] == UIApplicationStateBackground; |
| 98 return [_appState requiresHandlingAfterLaunchWithOptions:launchOptions |
| 99 stateBackground:inBackground]; |
| 100 } |
| 101 |
| 102 - (void)applicationDidBecomeActive:(UIApplication*)application { |
| 103 if ([_appState isInSafeMode]) |
| 104 return; |
| 105 |
| 106 [_appState resumeSessionWithTabOpener:_tabOpener |
| 107 tabSwitcher:_tabSwitcherProtocol]; |
| 108 } |
| 109 |
| 110 - (void)applicationWillResignActive:(UIApplication*)application { |
| 111 if ([_appState isInSafeMode]) |
| 112 return; |
| 113 |
| 114 [_appState willResignActiveTabModel]; |
| 115 } |
| 116 |
| 117 // Called when going into the background. iOS already broadcasts, so |
| 118 // stakeholders can register for it directly. |
| 119 - (void)applicationDidEnterBackground:(UIApplication*)application { |
| 120 [_appState |
| 121 applicationDidEnterBackground:application |
| 122 memoryHelper:_memoryHelper |
| 123 tabSwitcherIsActive:[_mainController isTabSwitcherActive]]; |
| 124 } |
| 125 |
| 126 // Called when returning to the foreground. |
| 127 - (void)applicationWillEnterForeground:(UIApplication*)application { |
| 128 [_appState applicationWillEnterForeground:application |
| 129 metricsMediator:_metricsMediator |
| 130 memoryHelper:_memoryHelper |
| 131 tabOpener:_tabOpener |
| 132 appNavigation:_appNavigation]; |
| 133 } |
| 134 |
| 135 - (void)applicationWillTerminate:(UIApplication*)application { |
| 136 if ([_appState isInSafeMode]) |
| 137 return; |
| 138 |
| 139 // Instead of adding code here, consider if it could be handled by listening |
| 140 // for UIApplicationWillterminate. |
| 141 [_appState applicationWillTerminate:application |
| 142 applicationNavigation:_appNavigation]; |
| 143 } |
| 144 |
| 145 - (void)applicationDidReceiveMemoryWarning:(UIApplication*)application { |
| 146 if ([_appState isInSafeMode]) |
| 147 return; |
| 148 |
| 149 [_memoryHelper handleMemoryPressure]; |
| 150 } |
| 151 |
| 152 #pragma mark Downloading Data in the Background |
| 153 |
| 154 - (void)application:(UIApplication*)application |
| 155 performFetchWithCompletionHandler: |
| 156 (void (^)(UIBackgroundFetchResult))completionHandler { |
| 157 if ([_appState isInSafeMode]) |
| 158 return; |
| 159 |
| 160 if ([application applicationState] != UIApplicationStateBackground) { |
| 161 // If this handler is called in foreground, it means it has to be activated. |
| 162 // Returning |UIBackgroundFetchResultNewData| means that the handler will be |
| 163 // called again in case of a crash. |
| 164 completionHandler(UIBackgroundFetchResultNewData); |
| 165 return; |
| 166 } |
| 167 |
| 168 [BackgroundActivity application:application |
| 169 performFetchWithCompletionHandler:completionHandler |
| 170 metricsMediator:_metricsMediator |
| 171 browserLauncher:_browserLauncher]; |
| 172 } |
| 173 |
| 174 - (void)application:(UIApplication*)application |
| 175 handleEventsForBackgroundURLSession:(NSString*)identifier |
| 176 completionHandler:(void (^)(void))completionHandler { |
| 177 if ([_appState isInSafeMode]) |
| 178 return; |
| 179 |
| 180 [BackgroundActivity handleEventsForBackgroundURLSession:identifier |
| 181 completionHandler:completionHandler |
| 182 browserLauncher:_browserLauncher]; |
| 183 } |
| 184 |
| 185 #pragma mark Continuing User Activity and Handling Quick Actions |
| 186 |
| 187 - (BOOL)application:(UIApplication*)application |
| 188 willContinueUserActivityWithType:(NSString*)userActivityType { |
| 189 if ([_appState isInSafeMode]) |
| 190 return NO; |
| 191 |
| 192 return |
| 193 [UserActivityHandler willContinueUserActivityWithType:userActivityType]; |
| 194 } |
| 195 |
| 196 - (BOOL)application:(UIApplication*)application |
| 197 continueUserActivity:(NSUserActivity*)userActivity |
| 198 restorationHandler:(void (^)(NSArray*))restorationHandler { |
| 199 if ([_appState isInSafeMode]) |
| 200 return NO; |
| 201 |
| 202 BOOL applicationIsActive = |
| 203 [application applicationState] == UIApplicationStateActive; |
| 204 |
| 205 return [UserActivityHandler continueUserActivity:userActivity |
| 206 applicationIsActive:applicationIsActive |
| 207 tabOpener:_tabOpener |
| 208 startupInformation:_startupInformation]; |
| 209 } |
| 210 |
| 211 - (void)application:(UIApplication*)application |
| 212 performActionForShortcutItem:(UIApplicationShortcutItem*)shortcutItem |
| 213 completionHandler:(void (^)(BOOL succeeded))completionHandler { |
| 214 if ([_appState isInSafeMode]) |
| 215 return; |
| 216 |
| 217 [UserActivityHandler |
| 218 performActionForShortcutItem:shortcutItem |
| 219 completionHandler:completionHandler |
| 220 tabOpener:_tabOpener |
| 221 startupInformation:_startupInformation |
| 222 browserViewInformation:[_mainController browserViewInformation]]; |
| 223 } |
| 224 |
| 225 #pragma mark Opening a URL-Specified Resource |
| 226 |
| 227 // Handles open URL. The registered URL Schemes are defined in project |
| 228 // variables ${CHROMIUM_URL_SCHEME_x}. |
| 229 // The url can either be empty, in which case the app is simply opened or |
| 230 // can contain an URL that will be opened in a new tab. |
| 231 - (BOOL)application:(UIApplication*)application |
| 232 openURL:(NSURL*)url |
| 233 options:(NSDictionary<NSString*, id>*)options { |
| 234 if ([_appState isInSafeMode]) |
| 235 return NO; |
| 236 |
| 237 if (ios::GetChromeBrowserProvider() |
| 238 ->GetChromeIdentityService() |
| 239 ->HandleApplicationOpenURL(application, url, options)) { |
| 240 return YES; |
| 241 } |
| 242 |
| 243 BOOL applicationActive = |
| 244 [application applicationState] == UIApplicationStateActive; |
| 245 return [URLOpener openURL:url |
| 246 applicationActive:applicationActive |
| 247 options:options |
| 248 tabOpener:_tabOpener |
| 249 startupInformation:_startupInformation]; |
| 250 } |
| 251 |
| 252 #pragma mark - chromeExecuteCommand |
| 253 |
| 254 - (void)chromeExecuteCommand:(id)sender { |
| 255 [_mainController chromeExecuteCommand:sender]; |
| 256 } |
| 257 |
| 258 #pragma mark - Testing methods |
| 259 |
| 260 - (MainController*)mainController { |
| 261 return _mainController; |
| 262 } |
| 263 |
| 264 - (AppState*)appState { |
| 265 return _appState; |
| 266 } |
| 267 |
| 268 + (MainController*)sharedMainController { |
| 269 return base::mac::ObjCCast<MainApplicationDelegate>( |
| 270 [[UIApplication sharedApplication] delegate]) |
| 271 .mainController; |
| 272 } |
| 273 |
| 274 @end |
OLD | NEW |