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.8k
[url_launcher] Add a new launchUrl to platform interface #5966
Merged
stuartmorgan-g
merged 2 commits into
flutter:main
from
stuartmorgan-g:url-launcher-updated-platform-interface
Jun 17, 2022
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
4 changes: 4 additions & 0 deletions
4
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
69 changes: 69 additions & 0 deletions
69
packages/url_launcher/url_launcher_platform_interface/lib/src/types.dart
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,69 @@ | ||
// 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 'package:flutter/foundation.dart'; | ||
|
||
/// The desired mode to launch a URL. | ||
/// | ||
/// Support for these modes varies by platform. Platforms that do not support | ||
/// the requested mode may substitute another mode. | ||
enum PreferredLaunchMode { | ||
/// Leaves the decision of how to launch the URL to the platform | ||
/// implementation. | ||
platformDefault, | ||
|
||
/// Loads the URL in an in-app web view (e.g., Safari View Controller). | ||
inAppWebView, | ||
|
||
/// Passes the URL to the OS to be handled by another application. | ||
externalApplication, | ||
|
||
/// Passes the URL to the OS to be handled by another non-browser application. | ||
externalNonBrowserApplication, | ||
} | ||
|
||
/// Additional configuration options for [PreferredLaunchMode.inAppWebView]. | ||
/// | ||
/// Not all options are supported on all platforms. This is a superset of | ||
/// available options exposed across all implementations. | ||
@immutable | ||
class InAppWebViewConfiguration { | ||
/// Creates a new WebViewConfiguration with the given settings. | ||
const InAppWebViewConfiguration({ | ||
this.enableJavaScript = true, | ||
this.enableDomStorage = true, | ||
this.headers = const <String, String>{}, | ||
}); | ||
|
||
/// Whether or not JavaScript is enabled for the web content. | ||
final bool enableJavaScript; | ||
|
||
/// Whether or not DOM storage is enabled for the web content. | ||
final bool enableDomStorage; | ||
|
||
/// Additional headers to pass in the load request. | ||
final Map<String, String> headers; | ||
} | ||
|
||
/// Options for [launchUrl]. | ||
@immutable | ||
class LaunchOptions { | ||
/// Creates a new parameter object with the given options. | ||
const LaunchOptions({ | ||
this.mode = PreferredLaunchMode.platformDefault, | ||
this.webViewConfiguration = const InAppWebViewConfiguration(), | ||
this.webOnlyWindowName, | ||
}); | ||
|
||
/// The requested launch mode. | ||
final PreferredLaunchMode mode; | ||
|
||
/// Configuration for the web view in [PreferredLaunchMode.inAppWebView] mode. | ||
final InAppWebViewConfiguration webViewConfiguration; | ||
|
||
/// A web-platform-specific option to set the link target. | ||
/// | ||
/// Default behaviour when unset should be to open the url in a new tab. | ||
final String? webOnlyWindowName; | ||
} |
94 changes: 94 additions & 0 deletions
94
packages/url_launcher/url_launcher_platform_interface/lib/src/url_launcher_platform.dart
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,94 @@ | ||
// 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 'dart:async'; | ||
|
||
import 'package:plugin_platform_interface/plugin_platform_interface.dart'; | ||
import 'package:url_launcher_platform_interface/link.dart'; | ||
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart'; | ||
|
||
import '../method_channel_url_launcher.dart'; | ||
|
||
/// The interface that implementations of url_launcher must implement. | ||
/// | ||
/// 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 extends PlatformInterface { | ||
/// Constructs a UrlLauncherPlatform. | ||
UrlLauncherPlatform() : super(token: _token); | ||
|
||
static final Object _token = Object(); | ||
|
||
static UrlLauncherPlatform _instance = MethodChannelUrlLauncher(); | ||
|
||
/// The default instance of [UrlLauncherPlatform] to use. | ||
/// | ||
/// Defaults to [MethodChannelUrlLauncher]. | ||
static UrlLauncherPlatform get instance => _instance; | ||
|
||
/// Platform-specific plugins should set this with their own platform-specific | ||
/// class that extends [UrlLauncherPlatform] when they register themselves. | ||
// TODO(amirh): Extract common platform interface logic. | ||
// https://github.com/flutter/flutter/issues/43368 | ||
static set instance(UrlLauncherPlatform instance) { | ||
PlatformInterface.verify(instance, _token); | ||
_instance = instance; | ||
} | ||
|
||
/// The delegate used by the Link widget to build itself. | ||
LinkDelegate? get linkDelegate; | ||
|
||
/// Returns `true` if this platform is able to launch [url]. | ||
Future<bool> canLaunch(String url) { | ||
throw UnimplementedError('canLaunch() has not been implemented.'); | ||
} | ||
|
||
/// Passes [url] to the underlying platform for handling. | ||
/// | ||
/// Returns `true` if the given [url] was successfully launched. | ||
/// | ||
/// For documentation on the other arguments, see the `launch` documentation | ||
/// in `package:url_launcher/url_launcher.dart`. | ||
Future<bool> launch( | ||
String url, { | ||
required bool useSafariVC, | ||
required bool useWebView, | ||
required bool enableJavaScript, | ||
required bool enableDomStorage, | ||
required bool universalLinksOnly, | ||
required Map<String, String> headers, | ||
String? webOnlyWindowName, | ||
}) { | ||
throw UnimplementedError('launch() has not been implemented.'); | ||
} | ||
|
||
/// Passes [url] to the underlying platform for handling. | ||
/// | ||
/// Returns `true` if the given [url] was successfully launched. | ||
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. Same comment as method above. 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. Same fix here. |
||
Future<bool> launchUrl(String url, LaunchOptions options) { | ||
final bool isWebURL = url.startsWith('http:') || url.startsWith('https:'); | ||
final bool useWebView = options.mode == PreferredLaunchMode.inAppWebView || | ||
(isWebURL && options.mode == PreferredLaunchMode.platformDefault); | ||
|
||
return launch( | ||
url, | ||
useSafariVC: useWebView, | ||
useWebView: useWebView, | ||
enableJavaScript: options.webViewConfiguration.enableJavaScript, | ||
enableDomStorage: options.webViewConfiguration.enableDomStorage, | ||
universalLinksOnly: | ||
options.mode == PreferredLaunchMode.externalNonBrowserApplication, | ||
headers: options.webViewConfiguration.headers, | ||
webOnlyWindowName: options.webOnlyWindowName, | ||
); | ||
} | ||
|
||
/// Closes the WebView, if one was opened earlier by [launch]. | ||
Future<void> closeWebView() { | ||
throw UnimplementedError('closeWebView() has not been implemented.'); | ||
} | ||
} |
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
121 changes: 121 additions & 0 deletions
121
packages/url_launcher/url_launcher_platform_interface/test/url_launcher_platform_test.dart
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,121 @@ | ||
// 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 'package:flutter_test/flutter_test.dart'; | ||
import 'package:url_launcher_platform_interface/link.dart'; | ||
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart'; | ||
|
||
class CapturingUrlLauncher extends UrlLauncherPlatform { | ||
String? url; | ||
bool? useSafariVC; | ||
bool? useWebView; | ||
bool? enableJavaScript; | ||
bool? enableDomStorage; | ||
bool? universalLinksOnly; | ||
Map<String, String> headers = <String, String>{}; | ||
String? webOnlyWindowName; | ||
|
||
@override | ||
final LinkDelegate? linkDelegate = null; | ||
|
||
@override | ||
Future<bool> launch( | ||
String url, { | ||
required bool useSafariVC, | ||
required bool useWebView, | ||
required bool enableJavaScript, | ||
required bool enableDomStorage, | ||
required bool universalLinksOnly, | ||
required Map<String, String> headers, | ||
String? webOnlyWindowName, | ||
}) async { | ||
this.url = url; | ||
this.useSafariVC = useSafariVC; | ||
this.useWebView = useWebView; | ||
this.enableJavaScript = enableJavaScript; | ||
this.enableDomStorage = enableDomStorage; | ||
this.universalLinksOnly = universalLinksOnly; | ||
this.headers = headers; | ||
this.webOnlyWindowName = webOnlyWindowName; | ||
|
||
return true; | ||
} | ||
} | ||
|
||
void main() { | ||
test('launchUrl calls through to launch with default options for web URL', | ||
() async { | ||
final CapturingUrlLauncher launcher = CapturingUrlLauncher(); | ||
|
||
await launcher.launchUrl('https://flutter.dev', const LaunchOptions()); | ||
|
||
expect(launcher.url, 'https://flutter.dev'); | ||
expect(launcher.useSafariVC, true); | ||
expect(launcher.useWebView, true); | ||
expect(launcher.enableJavaScript, true); | ||
expect(launcher.enableDomStorage, true); | ||
expect(launcher.universalLinksOnly, false); | ||
expect(launcher.headers, isEmpty); | ||
expect(launcher.webOnlyWindowName, null); | ||
}); | ||
|
||
test('launchUrl calls through to launch with default options for non-web URL', | ||
() async { | ||
final CapturingUrlLauncher launcher = CapturingUrlLauncher(); | ||
|
||
await launcher.launchUrl('tel:123456789', const LaunchOptions()); | ||
|
||
expect(launcher.url, 'tel:123456789'); | ||
expect(launcher.useSafariVC, false); | ||
expect(launcher.useWebView, false); | ||
expect(launcher.enableJavaScript, true); | ||
expect(launcher.enableDomStorage, true); | ||
expect(launcher.universalLinksOnly, false); | ||
expect(launcher.headers, isEmpty); | ||
expect(launcher.webOnlyWindowName, null); | ||
}); | ||
|
||
test('launchUrl calls through to launch with universal links', () async { | ||
final CapturingUrlLauncher launcher = CapturingUrlLauncher(); | ||
|
||
await launcher.launchUrl( | ||
'https://flutter.dev', | ||
const LaunchOptions( | ||
mode: PreferredLaunchMode.externalNonBrowserApplication)); | ||
|
||
expect(launcher.url, 'https://flutter.dev'); | ||
expect(launcher.useSafariVC, false); | ||
expect(launcher.useWebView, false); | ||
expect(launcher.enableJavaScript, true); | ||
expect(launcher.enableDomStorage, true); | ||
expect(launcher.universalLinksOnly, true); | ||
expect(launcher.headers, isEmpty); | ||
expect(launcher.webOnlyWindowName, null); | ||
}); | ||
|
||
test('launchUrl calls through to launch with all non-default options', | ||
() async { | ||
final CapturingUrlLauncher launcher = CapturingUrlLauncher(); | ||
|
||
await launcher.launchUrl( | ||
'https://flutter.dev', | ||
const LaunchOptions( | ||
mode: PreferredLaunchMode.externalApplication, | ||
webViewConfiguration: InAppWebViewConfiguration( | ||
enableJavaScript: false, | ||
enableDomStorage: false, | ||
headers: <String, String>{'foo': 'bar'}), | ||
webOnlyWindowName: 'a_name', | ||
)); | ||
|
||
expect(launcher.url, 'https://flutter.dev'); | ||
expect(launcher.useSafariVC, false); | ||
expect(launcher.useWebView, false); | ||
expect(launcher.enableJavaScript, false); | ||
expect(launcher.enableDomStorage, false); | ||
expect(launcher.universalLinksOnly, false); | ||
expect(launcher.headers['foo'], 'bar'); | ||
expect(launcher.webOnlyWindowName, 'a_name'); | ||
}); | ||
} |
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.
nit: This could probably updated to a description of what the method is doing and not just the return value
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.
Added the initial sentence overview from the app-facing API.