-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[url_launcher] Add url_launcher_platform_interface package
#2217
Changes from all commits
a440899
d1f0ca2
d58d524
bada3fe
7cba506
3607ed6
9c33b61
e6119ee
732afcd
36d1455
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Copyright 2017 The Chromium Authors. All rights reserved. | ||
| // | ||
| // Redistribution and use in source and binary forms, with or without | ||
| // modification, are permitted provided that the following conditions are | ||
| // met: | ||
| // | ||
| // * Redistributions of source code must retain the above copyright | ||
| // notice, this list of conditions and the following disclaimer. | ||
| // * Redistributions in binary form must reproduce the above | ||
| // copyright notice, this list of conditions and the following disclaimer | ||
| // in the documentation and/or other materials provided with the | ||
| // distribution. | ||
| // * Neither the name of Google Inc. nor the names of its | ||
| // contributors may be used to endorse or promote products derived from | ||
| // this software without specific prior written permission. | ||
| // | ||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| # url_launcher_platform_interface | ||
|
|
||
| A common platform interface for the [`url_launcher`][1] plugin. | ||
|
|
||
| This interface allows platform-specific implementations of the `url_launcher` | ||
| plugin, as well as the plugin itself, to ensure they are supporting the | ||
| same interface. | ||
|
|
||
| # Usage | ||
|
|
||
| To implement a new platform-specific implementation of `url_launcher`, extend | ||
| [`UrlLauncherPlatform`][2] with an implementation that performs the | ||
| platform-specific behavior, and when you register your plugin, set the default | ||
| `UrlLauncherPlatform` by calling | ||
| `UrlLauncherPlatform.instance = MyPlatformUrlLauncher()`. | ||
|
|
||
| # Note on breaking changes | ||
|
|
||
| Strongly prefer non-breaking changes (such as adding a method to the interface) | ||
| over breaking changes for this package. | ||
|
|
||
| See https://flutter.dev/go/platform-interface-breaking-changes for a discussion | ||
| on why a less-clean interface is preferable to a breaking change. | ||
|
|
||
| [1]: ../url_launcher | ||
| [2]: lib/url_launcher_platform_interface.dart |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Copyright 2017 The Chromium 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:flutter/services.dart'; | ||
| import 'package:meta/meta.dart' show required; | ||
|
|
||
| import 'url_launcher_platform_interface.dart'; | ||
|
|
||
| const MethodChannel _channel = MethodChannel('plugins.flutter.io/url_launcher'); | ||
|
|
||
| /// An implementation of [UrlLauncherPlatform] that uses method channels. | ||
| class MethodChannelUrlLauncher extends UrlLauncherPlatform { | ||
| @override | ||
| Future<bool> canLaunch(String url) { | ||
| return _channel.invokeMethod<bool>( | ||
| 'canLaunch', | ||
| <String, Object>{'url': url}, | ||
| ); | ||
| } | ||
|
|
||
| @override | ||
| Future<void> closeWebView() { | ||
| return _channel.invokeMethod<void>('closeWebView'); | ||
| } | ||
|
|
||
| @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, | ||
| }) { | ||
| return _channel.invokeMethod<bool>( | ||
| 'launch', | ||
| <String, Object>{ | ||
| 'url': url, | ||
| 'useSafariVC': useSafariVC, | ||
| 'useWebView': useWebView, | ||
| 'enableJavaScript': enableJavaScript, | ||
| 'enableDomStorage': enableDomStorage, | ||
| 'universalLinksOnly': universalLinksOnly, | ||
| 'headers': headers, | ||
| }, | ||
| ); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Copyright 2017 The Chromium 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:meta/meta.dart' show required; | ||
|
|
||
| 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. | ||
| abstract class UrlLauncherPlatform { | ||
|
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. The static platform field should be on this class and not on the app-facing plugin (so that platform implementations don't need to depend on the app-facing package)
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. Done. |
||
| /// 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(); | ||
|
|
||
| /// Returns `true` if this platform is able to launch [url]. | ||
| Future<bool> canLaunch(String url) { | ||
| throw UnimplementedError('canLaunch() has not been implemented.'); | ||
| } | ||
|
|
||
| /// 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( | ||
|
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. These should have default implementations that throw unimplemented.
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. Done. |
||
| String url, { | ||
| @required bool useSafariVC, | ||
| @required bool useWebView, | ||
| @required bool enableJavaScript, | ||
| @required bool enableDomStorage, | ||
| @required bool universalLinksOnly, | ||
| @required Map<String, String> headers, | ||
| }) { | ||
| throw UnimplementedError('launch() has not been implemented.'); | ||
| } | ||
|
|
||
| /// Closes the WebView, if one was opened earlier by [launch]. | ||
| Future<void> closeWebView() { | ||
| throw UnimplementedError('closeWebView() has not been implemented.'); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| name: url_launcher_platform_interface | ||
| description: A common platform interface for the url_launcher plugin. | ||
| author: Flutter Team <flutter-dev@googlegroups.com> | ||
| homepage: https://github.com/flutter/plugins/tree/master/packages/url_launcher/url_launcher_platform_interface | ||
| # NOTE: We strongly prefer non-breaking changes, even at the expense of a | ||
| # less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes | ||
| version: 1.0.0 | ||
|
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. I'd keep the comment here as well, I feel like ongoing maintainers are less likely to page in the README before publishing, but they will visit this line for sure if they want to bump the major version.
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. Done. |
||
|
|
||
| dependencies: | ||
| flutter: | ||
| sdk: flutter | ||
| meta: ^1.0.5 | ||
|
|
||
| dev_dependencies: | ||
| flutter_test: | ||
| sdk: flutter | ||
|
|
||
| environment: | ||
| sdk: ">=2.0.0-dev.28.0 <3.0.0" | ||
| flutter: ">=1.9.1+hotfix.4 <2.0.0" | ||
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.
Add to the class doc that implementers should extend this class and not implement it, otherwise we won't be able to add new methods.
See similar comment on webview:
plugins/packages/webview_flutter/lib/platform_interface.dart
Line 35 in fe9b559
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.
Done.