This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Add application:openURLs: forwarding on macOS #44689
Merged
auto-submit
merged 2 commits into
flutter:main
from
stuartmorgan-g:macos-plugin-open-urls-delegation
Aug 14, 2023
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
shell/platform/darwin/macos/framework/Source/FlutterAppDelegateTest.mm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // 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 "flutter/shell/platform/darwin/macos/framework/Headers/FlutterAppDelegate.h" | ||
|
|
||
| #import "flutter/testing/testing.h" | ||
| #include "third_party/googletest/googletest/include/gtest/gtest.h" | ||
|
|
||
| @interface AppDelegateNoopFlutterAppLifecycleDelegate : NSObject <FlutterAppLifecycleDelegate> | ||
| @property(nonatomic, copy, nullable) NSArray<NSURL*>* receivedURLs; | ||
| @end | ||
|
|
||
| @implementation AppDelegateNoopFlutterAppLifecycleDelegate | ||
| @end | ||
|
|
||
| @interface AppDelegateTestFlutterAppLifecycleDelegate : NSObject <FlutterAppLifecycleDelegate> | ||
| @property(nonatomic, copy, nullable) NSArray<NSURL*>* receivedURLs; | ||
| @end | ||
|
|
||
| @implementation AppDelegateTestFlutterAppLifecycleDelegate | ||
|
|
||
| - (BOOL)handleOpenURLs:(NSArray<NSURL*>*)urls { | ||
| self.receivedURLs = [urls copy]; | ||
| return YES; | ||
| } | ||
|
|
||
| @end | ||
|
|
||
| namespace flutter::testing { | ||
|
|
||
| TEST(FlutterAppDelegateTest, DoesNotCallDelegatesWithoutHandler) { | ||
| FlutterAppDelegate* appDelegate = [[FlutterAppDelegate alloc] init]; | ||
| AppDelegateNoopFlutterAppLifecycleDelegate* noopDelegate = | ||
| [[AppDelegateNoopFlutterAppLifecycleDelegate alloc] init]; | ||
| [appDelegate addApplicationLifecycleDelegate:noopDelegate]; | ||
|
|
||
| [appDelegate application:NSApplication.sharedApplication openURLs:@[]]; | ||
| // No EXPECT, since the test is that the call doesn't throw due to calling without checking that | ||
| // the method is implemented. | ||
| } | ||
|
|
||
| TEST(FlutterAppDelegateTest, ReceivesOpenURLs) { | ||
| FlutterAppDelegate* appDelegate = [[FlutterAppDelegate alloc] init]; | ||
| AppDelegateTestFlutterAppLifecycleDelegate* delegate = | ||
| [[AppDelegateTestFlutterAppLifecycleDelegate alloc] init]; | ||
| [appDelegate addApplicationLifecycleDelegate:delegate]; | ||
|
|
||
| NSArray<NSURL*>* URLs = @[ [NSURL URLWithString:@"https://flutter.dev"] ]; | ||
| [appDelegate application:NSApplication.sharedApplication openURLs:URLs]; | ||
|
|
||
| EXPECT_EQ([delegate receivedURLs], URLs); | ||
| } | ||
|
|
||
| TEST(FlutterAppDelegateTest, OperURLsStopsAfterHandled) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice - thanks for adding this one. |
||
| FlutterAppDelegate* appDelegate = [[FlutterAppDelegate alloc] init]; | ||
| AppDelegateTestFlutterAppLifecycleDelegate* firstDelegate = | ||
| [[AppDelegateTestFlutterAppLifecycleDelegate alloc] init]; | ||
| AppDelegateTestFlutterAppLifecycleDelegate* secondDelegate = | ||
| [[AppDelegateTestFlutterAppLifecycleDelegate alloc] init]; | ||
| [appDelegate addApplicationLifecycleDelegate:firstDelegate]; | ||
| [appDelegate addApplicationLifecycleDelegate:secondDelegate]; | ||
|
|
||
| NSArray<NSURL*>* URLs = @[ [NSURL URLWithString:@"https://flutter.dev"] ]; | ||
| [appDelegate application:NSApplication.sharedApplication openURLs:URLs]; | ||
|
|
||
| EXPECT_EQ([firstDelegate receivedURLs], URLs); | ||
| EXPECT_EQ([secondDelegate receivedURLs], nil); | ||
| } | ||
|
|
||
| } // namespace flutter::testing | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| // found in the LICENSE file. | ||
|
|
||
| #import "flutter/shell/platform/darwin/macos/framework/Headers/FlutterAppLifecycleDelegate.h" | ||
| #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterAppLifecycleDelegate_Internal.h" | ||
|
|
||
| #include <AppKit/AppKit.h> | ||
| #include <AppKit/NSApplication.h> | ||
|
|
@@ -12,14 +13,8 @@ | |
| #include "flutter/fml/logging.h" | ||
| #include "flutter/fml/paths.h" | ||
|
|
||
| @interface FlutterAppLifecycleRegistrar () | ||
| @end | ||
|
|
||
| @implementation FlutterAppLifecycleRegistrar { | ||
| NSMutableArray* _notificationUnsubscribers; | ||
|
|
||
| // Weak references to registered plugins. | ||
| NSPointerArray* _delegates; | ||
| } | ||
|
|
||
| - (void)addObserverFor:(NSString*)name selector:(SEL)selector { | ||
|
|
@@ -87,7 +82,7 @@ - (void)addDelegate:(NSObject<FlutterAppLifecycleDelegate>*)delegate { | |
|
|
||
| - (void)removeDelegate:(NSObject<FlutterAppLifecycleDelegate>*)delegate { | ||
| NSUInteger index = [[_delegates allObjects] indexOfObject:delegate]; | ||
| if (index >= 0) { | ||
| if (index != NSNotFound) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noticed while I was looking at the existing structure; the previous code was wrong since
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice catch. |
||
| [_delegates removePointerAtIndex:index]; | ||
| } | ||
| } | ||
|
|
@@ -101,9 +96,6 @@ - (void)removeDelegate:(NSObject<FlutterAppLifecycleDelegate>*)delegate { | |
| #define DISTRIBUTE_NOTIFICATION(SELECTOR) \ | ||
| -(void)handle##SELECTOR : (NSNotification*)notification { \ | ||
| for (NSObject<FlutterAppLifecycleDelegate> * delegate in _delegates) { \ | ||
| if (!delegate) { \ | ||
cbracken marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| continue; \ | ||
| } \ | ||
| if ([delegate respondsToSelector:@selector(handle##SELECTOR:)]) { \ | ||
| [delegate handle##SELECTOR:notification]; \ | ||
| } \ | ||
|
|
||
18 changes: 18 additions & 0 deletions
18
shell/platform/darwin/macos/framework/Source/FlutterAppLifecycleDelegate_Internal.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // 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. | ||
|
|
||
| #ifndef SHELL_PLATFORM_DARWIN_MACOS_FRAMEWORK_SOURCE_FLUTTERAPPLIFECYCLEDELEGATE_INTERNAL_H_ | ||
| #define SHELL_PLATFORM_DARWIN_MACOS_FRAMEWORK_SOURCE_FLUTTERAPPLIFECYCLEDELEGATE_INTERNAL_H_ | ||
|
|
||
| #import "flutter/shell/platform/darwin/macos/framework/Headers/FlutterAppLifecycleDelegate.h" | ||
|
|
||
| @interface FlutterAppLifecycleRegistrar () | ||
| /** | ||
| * Registered delegates. Exposed to allow FlutterAppDelegate to share the delegate list for | ||
| * handling non-notification delegation. | ||
| */ | ||
| @property(nonatomic, strong) NSPointerArray* delegates; | ||
| @end | ||
|
|
||
| #endif // SHELL_PLATFORM_DARWIN_MACOS_FRAMEWORK_SOURCE_FLUTTERAPPLIFECYCLEDELEGATE_INTERNAL_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I missed adding these when I added nullability annotations to the split-out delegate above, which causes warnings about not having full nullability annotation.