From 28154a9ded0729ec928e7d9d96ae7f9bfc2a4f01 Mon Sep 17 00:00:00 2001 From: Aaron Clarke Date: Fri, 6 Nov 2020 10:08:36 -0800 Subject: [PATCH 1/3] Reland Added the ability to set the initial route via launch urls. (#21336) --- ci/licenses_golden/licenses_flutter | 2 + shell/platform/darwin/ios/BUILD.gn | 1 + .../framework/Source/FlutterAppDelegate.mm | 63 ++++++++++++++----- .../Source/FlutterAppDelegateTest.mm | 41 ++++++++++++ .../Source/FlutterAppDelegate_Test.h | 9 +++ .../ios/framework/Source/FlutterEngine.mm | 13 ++++ .../ios/framework/Source/FlutterEngineTest.mm | 14 +++++ .../framework/Source/FlutterEngine_Internal.h | 1 + .../ios/framework/Source/FlutterEngine_Test.h | 3 + 9 files changed, 130 insertions(+), 17 deletions(-) create mode 100644 shell/platform/darwin/ios/framework/Source/FlutterAppDelegateTest.mm create mode 100644 shell/platform/darwin/ios/framework/Source/FlutterAppDelegate_Test.h diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index 245980e77e921..520eca83268b9 100755 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -942,6 +942,8 @@ FILE: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterPlugin FILE: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Info.plist FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm +FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegateTest.mm +FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate_Test.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterBinaryMessengerRelay.h FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterBinaryMessengerRelay.mm FILE: ../../../flutter/shell/platform/darwin/ios/framework/Source/FlutterBinaryMessengerRelayTest.mm diff --git a/shell/platform/darwin/ios/BUILD.gn b/shell/platform/darwin/ios/BUILD.gn index e06275c3cfa59..99acf3960c825 100644 --- a/shell/platform/darwin/ios/BUILD.gn +++ b/shell/platform/darwin/ios/BUILD.gn @@ -208,6 +208,7 @@ shared_library("ios_test_flutter") { "//build/config:symbol_visibility_hidden", ] sources = [ + "framework/Source/FlutterAppDelegateTest.mm", "framework/Source/FlutterBinaryMessengerRelayTest.mm", "framework/Source/FlutterDartProjectTest.mm", "framework/Source/FlutterEngineTest.mm", diff --git a/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm b/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm index d1834c3a41d4a..37ab9c46c6f38 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm @@ -4,15 +4,21 @@ #import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterAppDelegate.h" -#include "flutter/fml/logging.h" +#import "flutter/fml/logging.h" #import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterPluginAppLifeCycleDelegate.h" #import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h" +#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate_Test.h" +#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterPluginAppLifeCycleDelegate_internal.h" static NSString* kUIBackgroundMode = @"UIBackgroundModes"; static NSString* kRemoteNotificationCapabitiliy = @"remote-notification"; static NSString* kBackgroundFetchCapatibility = @"fetch"; +@interface FlutterAppDelegate () +@property(nonatomic, copy) FlutterViewController* (^rootFlutterViewControllerGetter)(void); +@end + @implementation FlutterAppDelegate { FlutterPluginAppLifeCycleDelegate* _lifeCycleDelegate; } @@ -26,6 +32,7 @@ - (instancetype)init { - (void)dealloc { [_lifeCycleDelegate release]; + [_rootFlutterViewControllerGetter release]; [super dealloc]; } @@ -41,10 +48,13 @@ - (BOOL)application:(UIApplication*)application // Returns the key window's rootViewController, if it's a FlutterViewController. // Otherwise, returns nil. -+ (FlutterViewController*)rootFlutterViewController { - UIViewController* viewController = [UIApplication sharedApplication].keyWindow.rootViewController; - if ([viewController isKindOfClass:[FlutterViewController class]]) { - return (FlutterViewController*)viewController; +- (FlutterViewController*)rootFlutterViewController { + if (_rootFlutterViewControllerGetter != nil) { + return _rootFlutterViewControllerGetter(); + } + UIViewController* rootViewController = _window.rootViewController; + if ([rootViewController isKindOfClass:[FlutterViewController class]]) { + return (FlutterViewController*)rootViewController; } return nil; } @@ -124,7 +134,28 @@ - (void)userNotificationCenter:(UNUserNotificationCenter*)center - (BOOL)application:(UIApplication*)application openURL:(NSURL*)url options:(NSDictionary*)options { - return [_lifeCycleDelegate application:application openURL:url options:options]; + if ([_lifeCycleDelegate application:application openURL:url options:options]) { + return YES; + } else { + FlutterViewController* flutterViewController = [self rootFlutterViewController]; + if (flutterViewController) { + [flutterViewController.engine + waitForFirstFrame:3.0 + callback:^(BOOL didTimeout) { + if (didTimeout) { + FML_LOG(ERROR) + << "Timeout waiting for the first frame when launching an URL."; + } else { + [flutterViewController.engine.navigationChannel invokeMethod:@"pushRoute" + arguments:url.path]; + } + }]; + return YES; + } else { + FML_LOG(ERROR) << "Attempting to open an URL without a Flutter RootViewController."; + return NO; + } + } } - (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)url { @@ -175,27 +206,25 @@ - (BOOL)application:(UIApplication*)application #pragma mark - FlutterPluginRegistry methods. All delegating to the rootViewController - (NSObject*)registrarForPlugin:(NSString*)pluginKey { - UIViewController* rootViewController = _window.rootViewController; - if ([rootViewController isKindOfClass:[FlutterViewController class]]) { - return - [[(FlutterViewController*)rootViewController pluginRegistry] registrarForPlugin:pluginKey]; + FlutterViewController* flutterRootViewController = [self rootFlutterViewController]; + if (flutterRootViewController) { + return [[flutterRootViewController pluginRegistry] registrarForPlugin:pluginKey]; } return nil; } - (BOOL)hasPlugin:(NSString*)pluginKey { - UIViewController* rootViewController = _window.rootViewController; - if ([rootViewController isKindOfClass:[FlutterViewController class]]) { - return [[(FlutterViewController*)rootViewController pluginRegistry] hasPlugin:pluginKey]; + FlutterViewController* flutterRootViewController = [self rootFlutterViewController]; + if (flutterRootViewController) { + return [[flutterRootViewController pluginRegistry] hasPlugin:pluginKey]; } return false; } - (NSObject*)valuePublishedByPlugin:(NSString*)pluginKey { - UIViewController* rootViewController = _window.rootViewController; - if ([rootViewController isKindOfClass:[FlutterViewController class]]) { - return [[(FlutterViewController*)rootViewController pluginRegistry] - valuePublishedByPlugin:pluginKey]; + FlutterViewController* flutterRootViewController = [self rootFlutterViewController]; + if (flutterRootViewController) { + return [[flutterRootViewController pluginRegistry] valuePublishedByPlugin:pluginKey]; } return nil; } diff --git a/shell/platform/darwin/ios/framework/Source/FlutterAppDelegateTest.mm b/shell/platform/darwin/ios/framework/Source/FlutterAppDelegateTest.mm new file mode 100644 index 0000000000000..69dc0e016a2ac --- /dev/null +++ b/shell/platform/darwin/ios/framework/Source/FlutterAppDelegateTest.mm @@ -0,0 +1,41 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import +#import + +#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterAppDelegate.h" +#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterEngine.h" +#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h" +#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate_Test.h" +#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Test.h" + +FLUTTER_ASSERT_ARC + +@interface FlutterAppDelegateTest : XCTestCase +@end + +@implementation FlutterAppDelegateTest + +- (void)testLaunchUrl { + FlutterAppDelegate* appDelegate = [[FlutterAppDelegate alloc] init]; + FlutterViewController* viewController = OCMClassMock([FlutterViewController class]); + FlutterEngine* engine = OCMClassMock([FlutterEngine class]); + FlutterMethodChannel* navigationChannel = OCMClassMock([FlutterMethodChannel class]); + OCMStub([engine navigationChannel]).andReturn(navigationChannel); + OCMStub([viewController engine]).andReturn(engine); + OCMStub([engine waitForFirstFrame:3.0 callback:([OCMArg invokeBlockWithArgs:@(NO), nil])]); + appDelegate.rootFlutterViewControllerGetter = ^{ + return viewController; + }; + NSURL* url = [NSURL URLWithString:@"http://example.com"]; + NSDictionary* options = @{}; + BOOL result = [appDelegate application:[UIApplication sharedApplication] + openURL:url + options:options]; + XCTAssertTrue(result); + OCMVerify([navigationChannel invokeMethod:@"pushRoute" arguments:url.path]); +} + +@end diff --git a/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate_Test.h b/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate_Test.h new file mode 100644 index 0000000000000..1b10384058db5 --- /dev/null +++ b/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate_Test.h @@ -0,0 +1,9 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +@class FlutterViewController; + +@interface FlutterAppDelegate (Test) +@property(nonatomic, copy) FlutterViewController* (^rootFlutterViewControllerGetter)(void); +@end diff --git a/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm b/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm index d19c696f07ec3..9c26b05081ccc 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm @@ -914,6 +914,19 @@ - (void)onLocaleUpdated:(NSNotification*)notification { [self.localizationChannel invokeMethod:@"setLocale" arguments:localeData]; } +- (void)waitForFirstFrame:(NSTimeInterval)timeout + callback:(void (^_Nonnull)(BOOL didTimeout))callback { + dispatch_queue_t queue = dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0); + dispatch_async(queue, ^{ + fml::TimeDelta waitTime = fml::TimeDelta::FromMilliseconds(timeout * 1000); + BOOL didTimeout = + self.shell.WaitForFirstFrame(waitTime).code() == fml::StatusCode::kDeadlineExceeded; + dispatch_async(dispatch_get_main_queue(), ^{ + callback(didTimeout); + }); + }); +} + @end @implementation FlutterEngineRegistrar { diff --git a/shell/platform/darwin/ios/framework/Source/FlutterEngineTest.mm b/shell/platform/darwin/ios/framework/Source/FlutterEngineTest.mm index e51bb06162a77..3684ea5374a60 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterEngineTest.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterEngineTest.mm @@ -120,4 +120,18 @@ - (void)testPlatformViewsControllerRenderingSoftware { XCTAssertEqual(renderingApi, flutter::IOSRenderingAPI::kSoftware); } + +- (void)testWaitForFirstFrameTimeout { + FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"foobar"]; + [engine run]; + XCTestExpectation* timeoutFirstFrame = [self expectationWithDescription:@"timeoutFirstFrame"]; + [engine waitForFirstFrame:0.1 + callback:^(BOOL didTimeout) { + if (timeoutFirstFrame) { + [timeoutFirstFrame fulfill]; + } + }]; + [self waitForExpectationsWithTimeout:1 handler:nil]; +} + @end diff --git a/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h b/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h index 739138dcf0099..85a82f3891530 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h +++ b/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h @@ -48,6 +48,7 @@ - (void)notifyLowMemory; - (flutter::PlatformViewIOS*)iosPlatformView; +- (void)waitForFirstFrame:(NSTimeInterval)timeout callback:(void (^)(BOOL didTimeout))callback; @end #endif // FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTERENGINE_INTERNAL_H_ diff --git a/shell/platform/darwin/ios/framework/Source/FlutterEngine_Test.h b/shell/platform/darwin/ios/framework/Source/FlutterEngine_Test.h index 82516b1e77389..50db832916ea4 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterEngine_Test.h +++ b/shell/platform/darwin/ios/framework/Source/FlutterEngine_Test.h @@ -5,8 +5,11 @@ #import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterEngine.h" #import "flutter/shell/platform/darwin/ios/rendering_api_selection.h" +@class FlutterBinaryMessengerRelay; + // Category to add test-only visibility. @interface FlutterEngine (Test) - (void)setBinaryMessenger:(FlutterBinaryMessengerRelay*)binaryMessenger; - (flutter::IOSRenderingAPI)platformViewsRenderingAPI; +- (void)waitForFirstFrame:(NSTimeInterval)timeout callback:(void (^)(BOOL didTimeout))callback; @end From 354a360d55671416b9f5aa6a72c3101603bea5b1 Mon Sep 17 00:00:00 2001 From: Aaron Clarke Date: Fri, 6 Nov 2020 10:09:41 -0800 Subject: [PATCH 2/3] added check in info.plist --- .../ios/framework/Source/FlutterAppDelegate.mm | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm b/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm index 37ab9c46c6f38..2beef220391be 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm @@ -131,11 +131,23 @@ - (void)userNotificationCenter:(UNUserNotificationCenter*)center } } +static BOOL IsDeepLinkingEnabled() { + NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary]; + NSNumber* isEnabled = [infoDict objectForKey:@"FlutterDeepLinkingEnabled"]; + if (isEnabled) { + return [isEnabled boolValue]; + } else { + return NO; + } +} + - (BOOL)application:(UIApplication*)application openURL:(NSURL*)url options:(NSDictionary*)options { if ([_lifeCycleDelegate application:application openURL:url options:options]) { return YES; + } else if (!IsDeepLinkingEnabled()) { + return NO; } else { FlutterViewController* flutterViewController = [self rootFlutterViewController]; if (flutterViewController) { From 60b373315e625ff3ad9dde7631b5ca135f720f01 Mon Sep 17 00:00:00 2001 From: Aaron Clarke Date: Mon, 9 Nov 2020 09:54:02 -0800 Subject: [PATCH 3/3] mocked out getting the infoplist --- .../framework/Source/FlutterAppDelegate.mm | 21 ++++++++++++++----- .../Source/FlutterAppDelegateTest.mm | 5 ++++- .../Source/FlutterAppDelegate_Test.h | 6 ++++++ 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm b/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm index 2beef220391be..1e574aec6343d 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate.mm @@ -131,9 +131,8 @@ - (void)userNotificationCenter:(UNUserNotificationCenter*)center } } -static BOOL IsDeepLinkingEnabled() { - NSDictionary* infoDict = [[NSBundle mainBundle] infoDictionary]; - NSNumber* isEnabled = [infoDict objectForKey:@"FlutterDeepLinkingEnabled"]; +static BOOL IsDeepLinkingEnabled(NSDictionary* infoDictionary) { + NSNumber* isEnabled = [infoDictionary objectForKey:@"FlutterDeepLinkingEnabled"]; if (isEnabled) { return [isEnabled boolValue]; } else { @@ -143,10 +142,11 @@ static BOOL IsDeepLinkingEnabled() { - (BOOL)application:(UIApplication*)application openURL:(NSURL*)url - options:(NSDictionary*)options { + options:(NSDictionary*)options + infoPlistGetter:(NSDictionary* (^)())infoPlistGetter { if ([_lifeCycleDelegate application:application openURL:url options:options]) { return YES; - } else if (!IsDeepLinkingEnabled()) { + } else if (!IsDeepLinkingEnabled(infoPlistGetter())) { return NO; } else { FlutterViewController* flutterViewController = [self rootFlutterViewController]; @@ -170,6 +170,17 @@ - (BOOL)application:(UIApplication*)application } } +- (BOOL)application:(UIApplication*)application + openURL:(NSURL*)url + options:(NSDictionary*)options { + return [self application:application + openURL:url + options:options + infoPlistGetter:^NSDictionary*() { + return [[NSBundle mainBundle] infoDictionary]; + }]; +} + - (BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)url { return [_lifeCycleDelegate application:application handleOpenURL:url]; } diff --git a/shell/platform/darwin/ios/framework/Source/FlutterAppDelegateTest.mm b/shell/platform/darwin/ios/framework/Source/FlutterAppDelegateTest.mm index 69dc0e016a2ac..5e3fd48371912 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterAppDelegateTest.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterAppDelegateTest.mm @@ -33,7 +33,10 @@ - (void)testLaunchUrl { NSDictionary* options = @{}; BOOL result = [appDelegate application:[UIApplication sharedApplication] openURL:url - options:options]; + options:options + infoPlistGetter:^NSDictionary*() { + return @{@"FlutterDeepLinkingEnabled" : @(YES)}; + }]; XCTAssertTrue(result); OCMVerify([navigationChannel invokeMethod:@"pushRoute" arguments:url.path]); } diff --git a/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate_Test.h b/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate_Test.h index 1b10384058db5..a4394fde9fdac 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate_Test.h +++ b/shell/platform/darwin/ios/framework/Source/FlutterAppDelegate_Test.h @@ -6,4 +6,10 @@ @interface FlutterAppDelegate (Test) @property(nonatomic, copy) FlutterViewController* (^rootFlutterViewControllerGetter)(void); + +- (BOOL)application:(UIApplication*)application + openURL:(NSURL*)url + options:(NSDictionary*)options + infoPlistGetter:(NSDictionary* (^)())infoPlistGetter; + @end