This repository was archived by the owner on Feb 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Forbid ... implements UrlLauncherPlatform
#2230
Merged
Merged
Changes from all commits
Commits
Show all changes
3 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
7 changes: 7 additions & 0 deletions
7
packages/url_launcher/url_launcher_platform_interface/CHANGELOG.md
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,7 @@ | ||
| ## 1.0.1 | ||
|
|
||
| * Enforce that UrlLauncherPlatform isn't implemented with `implements`. | ||
|
|
||
| ## 1.0.0 | ||
|
|
||
| * Initial release. |
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 |
|---|---|---|
|
|
@@ -4,27 +4,50 @@ | |
|
|
||
| import 'dart:async'; | ||
|
|
||
| import 'package:meta/meta.dart' show required; | ||
| import 'package:meta/meta.dart' show required, visibleForTesting; | ||
|
|
||
| import 'method_channel_url_launcher.dart'; | ||
|
|
||
| /// The interface that implementations of url_launcher must implement. | ||
| /// | ||
| /// Platform implementations that live in a separate package should extend this | ||
| /// class rather than implement it as `url_launcher` does not consider newly | ||
| /// added methods to be breaking changes. Extending this class (using `extends`) | ||
| /// ensures that the subclass will get the default implementation, while | ||
| /// platform implementations that `implements` this interface will be broken by | ||
| /// newly added [UrlLauncherPlatform] methods. | ||
| /// Platform implementations should extend this class rather than implement it as `url_launcher` | ||
| /// does not consider newly added methods to be breaking changes. Extending this class | ||
| /// (using `extends`) ensures that the subclass will get the default implementation, while | ||
| /// platform implementations that `implements` this interface will be broken by newly added | ||
| /// [UrlLauncherPlatform] methods. | ||
| abstract class UrlLauncherPlatform { | ||
| /// Only mock implementations should set this to true. | ||
| /// | ||
| /// Mockito mocks are implementing this class with `implements` which is forbidden for anything | ||
| /// other than mocks (see class docs). This property provides a backdoor for mockito mocks to | ||
| /// skip the verification that the class isn't implemented with `implements`. | ||
| @visibleForTesting | ||
| bool get isMock => false; | ||
|
|
||
| /// The default instance of [UrlLauncherPlatform] to use. | ||
| /// | ||
| /// Platform-specific plugins should override this with their own | ||
| /// platform-specific class that extends [UrlLauncherPlatform] when they | ||
| /// register themselves. | ||
| /// | ||
| /// Defaults to [MethodChannelUrlLauncher]. | ||
| static UrlLauncherPlatform instance = MethodChannelUrlLauncher(); | ||
| static UrlLauncherPlatform _instance = MethodChannelUrlLauncher(); | ||
|
|
||
| static UrlLauncherPlatform get instance => _instance; | ||
|
|
||
| // TODO(amirh): Extract common platform interface logic. | ||
| // https://github.com/flutter/flutter/issues/43368 | ||
| static set instance(UrlLauncherPlatform instance) { | ||
|
Contributor
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. nit: style guide gives the order for getters/setters as "getter/field/setter" with no whitespace between them |
||
| if (!instance.isMock) { | ||
| try { | ||
| instance._verifyProvidesDefaultImplementations(); | ||
|
Contributor
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. this should be in an assert so it doesn't run in release builds |
||
| } on NoSuchMethodError catch (_) { | ||
| throw AssertionError( | ||
| 'Platform interfaces must not be implemented with `implements`'); | ||
| } | ||
| } | ||
| _instance = instance; | ||
| } | ||
|
|
||
| /// Returns `true` if this platform is able to launch [url]. | ||
| Future<bool> canLaunch(String url) { | ||
|
|
@@ -51,4 +74,12 @@ abstract class UrlLauncherPlatform { | |
| Future<void> closeWebView() { | ||
| throw UnimplementedError('closeWebView() has not been implemented.'); | ||
| } | ||
|
|
||
| // This method makes sure that UrlLauncher isn't implemented with `implements`. | ||
| // | ||
| // See class doc for more details on why implementing this class is forbidden. | ||
| // | ||
| // This private method is called by the instance setter, which fails if the class is | ||
| // implemented with `implements`. | ||
| void _verifyProvidesDefaultImplementations() {} | ||
| } | ||
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
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.
A lot of this functionality will be shared across many plugins. Should it be a mixin or base class?
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.
Filed flutter/flutter#43368 and added a TODO (we will have to wait for a stable release before we can use common framework code for this)