Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit f04b3c8

Browse files
author
Harry Terkelsen
authored
[url_launcher] Add url_launcher_platform_interface package (#2217)
* [url_launcher] Add overridable platform * Fix analyzer lints * Run dartfmt * Add a url_launcher_platform_interface package * Disable tests temporarily * Add license header * Fix typo in test * Add LICENSE file * Respond to review comments * Respond to review comments
1 parent fe9b559 commit f04b3c8

File tree

6 files changed

+439
-0
lines changed

6 files changed

+439
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2017 The Chromium Authors. All rights reserved.
2+
//
3+
// Redistribution and use in source and binary forms, with or without
4+
// modification, are permitted provided that the following conditions are
5+
// met:
6+
//
7+
// * Redistributions of source code must retain the above copyright
8+
// notice, this list of conditions and the following disclaimer.
9+
// * Redistributions in binary form must reproduce the above
10+
// copyright notice, this list of conditions and the following disclaimer
11+
// in the documentation and/or other materials provided with the
12+
// distribution.
13+
// * Neither the name of Google Inc. nor the names of its
14+
// contributors may be used to endorse or promote products derived from
15+
// this software without specific prior written permission.
16+
//
17+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# url_launcher_platform_interface
2+
3+
A common platform interface for the [`url_launcher`][1] plugin.
4+
5+
This interface allows platform-specific implementations of the `url_launcher`
6+
plugin, as well as the plugin itself, to ensure they are supporting the
7+
same interface.
8+
9+
# Usage
10+
11+
To implement a new platform-specific implementation of `url_launcher`, extend
12+
[`UrlLauncherPlatform`][2] with an implementation that performs the
13+
platform-specific behavior, and when you register your plugin, set the default
14+
`UrlLauncherPlatform` by calling
15+
`UrlLauncherPlatform.instance = MyPlatformUrlLauncher()`.
16+
17+
# Note on breaking changes
18+
19+
Strongly prefer non-breaking changes (such as adding a method to the interface)
20+
over breaking changes for this package.
21+
22+
See https://flutter.dev/go/platform-interface-breaking-changes for a discussion
23+
on why a less-clean interface is preferable to a breaking change.
24+
25+
[1]: ../url_launcher
26+
[2]: lib/url_launcher_platform_interface.dart
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2017 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:async';
6+
7+
import 'package:flutter/services.dart';
8+
import 'package:meta/meta.dart' show required;
9+
10+
import 'url_launcher_platform_interface.dart';
11+
12+
const MethodChannel _channel = MethodChannel('plugins.flutter.io/url_launcher');
13+
14+
/// An implementation of [UrlLauncherPlatform] that uses method channels.
15+
class MethodChannelUrlLauncher extends UrlLauncherPlatform {
16+
@override
17+
Future<bool> canLaunch(String url) {
18+
return _channel.invokeMethod<bool>(
19+
'canLaunch',
20+
<String, Object>{'url': url},
21+
);
22+
}
23+
24+
@override
25+
Future<void> closeWebView() {
26+
return _channel.invokeMethod<void>('closeWebView');
27+
}
28+
29+
@override
30+
Future<bool> launch(
31+
String url, {
32+
@required bool useSafariVC,
33+
@required bool useWebView,
34+
@required bool enableJavaScript,
35+
@required bool enableDomStorage,
36+
@required bool universalLinksOnly,
37+
@required Map<String, String> headers,
38+
}) {
39+
return _channel.invokeMethod<bool>(
40+
'launch',
41+
<String, Object>{
42+
'url': url,
43+
'useSafariVC': useSafariVC,
44+
'useWebView': useWebView,
45+
'enableJavaScript': enableJavaScript,
46+
'enableDomStorage': enableDomStorage,
47+
'universalLinksOnly': universalLinksOnly,
48+
'headers': headers,
49+
},
50+
);
51+
}
52+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2017 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:async';
6+
7+
import 'package:meta/meta.dart' show required;
8+
9+
import 'method_channel_url_launcher.dart';
10+
11+
/// The interface that implementations of url_launcher must implement.
12+
///
13+
/// Platform implementations that live in a separate package should extend this
14+
/// class rather than implement it as `url_launcher` does not consider newly
15+
/// added methods to be breaking changes. Extending this class (using `extends`)
16+
/// ensures that the subclass will get the default implementation, while
17+
/// platform implementations that `implements` this interface will be broken by
18+
/// newly added [UrlLauncherPlatform] methods.
19+
abstract class UrlLauncherPlatform {
20+
/// The default instance of [UrlLauncherPlatform] to use.
21+
///
22+
/// Platform-specific plugins should override this with their own
23+
/// platform-specific class that extends [UrlLauncherPlatform] when they
24+
/// register themselves.
25+
///
26+
/// Defaults to [MethodChannelUrlLauncher].
27+
static UrlLauncherPlatform instance = MethodChannelUrlLauncher();
28+
29+
/// Returns `true` if this platform is able to launch [url].
30+
Future<bool> canLaunch(String url) {
31+
throw UnimplementedError('canLaunch() has not been implemented.');
32+
}
33+
34+
/// Returns `true` if the given [url] was successfully launched.
35+
///
36+
/// For documentation on the other arguments, see the `launch` documentation
37+
/// in `package:url_launcher/url_launcher.dart`.
38+
Future<bool> launch(
39+
String url, {
40+
@required bool useSafariVC,
41+
@required bool useWebView,
42+
@required bool enableJavaScript,
43+
@required bool enableDomStorage,
44+
@required bool universalLinksOnly,
45+
@required Map<String, String> headers,
46+
}) {
47+
throw UnimplementedError('launch() has not been implemented.');
48+
}
49+
50+
/// Closes the WebView, if one was opened earlier by [launch].
51+
Future<void> closeWebView() {
52+
throw UnimplementedError('closeWebView() has not been implemented.');
53+
}
54+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: url_launcher_platform_interface
2+
description: A common platform interface for the url_launcher plugin.
3+
author: Flutter Team <[email protected]>
4+
homepage: https://github.com/flutter/plugins/tree/master/packages/url_launcher/url_launcher_platform_interface
5+
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
6+
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
7+
version: 1.0.0
8+
9+
dependencies:
10+
flutter:
11+
sdk: flutter
12+
meta: ^1.0.5
13+
14+
dev_dependencies:
15+
flutter_test:
16+
sdk: flutter
17+
18+
environment:
19+
sdk: ">=2.0.0-dev.28.0 <3.0.0"
20+
flutter: ">=1.9.1+hotfix.4 <2.0.0"

0 commit comments

Comments
 (0)