Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/url_launcher/url_launcher_web/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 0.0.2

- Switch to using `url_launcher_platform_interface`.

# 0.0.1

- Initial open-source release.
61 changes: 29 additions & 32 deletions packages/url_launcher/url_launcher_web/lib/url_launcher_web.dart
Original file line number Diff line number Diff line change
@@ -1,49 +1,46 @@
import 'dart:async';
import 'dart:html' as html;

import 'package:flutter/services.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:meta/meta.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';

class UrlLauncherPlugin {
/// The web implementation of [UrlLauncherPlatform].
///
/// This class implements the `package:url_launcher` functionality for the web.
class UrlLauncherPlugin extends UrlLauncherPlatform {
/// Registers this class as the default instance of [UrlLauncherPlatform].
static void registerWith(Registrar registrar) {
final MethodChannel channel = MethodChannel(
'plugins.flutter.io/url_launcher',
const StandardMethodCodec(),
registrar.messenger);
final UrlLauncherPlugin instance = UrlLauncherPlugin();
channel.setMethodCallHandler(instance.handleMethodCall);
UrlLauncherPlatform.instance = UrlLauncherPlugin();
}

Future<dynamic> handleMethodCall(MethodCall call) async {
switch (call.method) {
case 'canLaunch':
final String url = call.arguments['url'];
return _canLaunch(url);
case 'launch':
final String url = call.arguments['url'];
return _launch(url);
default:
throw PlatformException(
code: 'Unimplemented',
details: "The url_launcher plugin for web doesn't implement "
"the method '${call.method}'");
}
/// Opens the given [url] in a new window.
///
/// Returns the newly created window.
@visibleForTesting
html.WindowBase openNewWindow(String url) {
return html.window.open(url, '');
}

bool _canLaunch(String url) {
@override
Future<bool> canLaunch(String url) {
final Uri parsedUrl = Uri.tryParse(url);
if (parsedUrl == null) return false;

return parsedUrl.isScheme('http') || parsedUrl.isScheme('https');
}
if (parsedUrl == null) return Future<bool>.value(false);

bool _launch(String url) {
return openNewWindow(url) != null;
return Future<bool>.value(
parsedUrl.isScheme('http') || parsedUrl.isScheme('https'));
}

@visibleForTesting
html.WindowBase openNewWindow(String url) {
return html.window.open(url, '');
@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 Future<bool>.value(openNewWindow(url) != null);
}
}
3 changes: 2 additions & 1 deletion packages/url_launcher/url_launcher_web/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: url_launcher_web
description: Web platform implementation of url_launcher
author: Flutter Team <flutter-dev@googlegroups.com>
homepage: https://github.com/flutter/plugins/tree/master/packages/url_launcher/url_launcher_web
version: 0.0.1
version: 0.0.2

flutter:
plugin:
Expand All @@ -17,6 +17,7 @@ dependencies:
flutter_web_plugins:
sdk: flutter
meta: ^1.1.7
url_launcher_platform_interface: ^1.0.1

dev_dependencies:
flutter_test:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@
import 'dart:html' as html;

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter_web_plugins/flutter_web_plugins.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:url_launcher_web/url_launcher_web.dart';
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';

void main() {
group('URL Launcher for Web', () {
setUp(() {
TestWidgetsFlutterBinding.ensureInitialized();
webPluginRegistry.registerMessageHandler();
final Registrar registrar =
webPluginRegistry.registrarFor(UrlLauncherPlugin);
UrlLauncherPlugin.registerWith(registrar);
UrlLauncherPlatform.instance = UrlLauncherPlugin();
});

test('$UrlLauncherPlugin is the live instance', () {
expect(UrlLauncherPlatform.instance, isA<UrlLauncherPlugin>());
});

test('can launch "http" URLs', () {
Expand All @@ -45,5 +45,9 @@ void main() {
expect(newWindow, isNot(equals(html.window)));
expect(newWindow.opener, equals(html.window));
});

test('does not implement closeWebView()', () {
expect(closeWebView(), throwsUnimplementedError);
});
});
}