Skip to content

[webview_flutter] Implement platform interface for JavaScript dialog #5670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.9.0

* Adds support to show JavaScript dialog. See `PlatformWebViewController.setOnJavaScriptAlertDialog`, `PlatformWebViewController.setOnJavaScriptConfirmDialog` and `PlatformWebViewController.setOnJavaScriptTextInputDialog`.

## 2.8.0

* Adds support to track scroll position changes. See `PlatformWebViewController.setOnScrollPositionChange`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,36 @@ abstract class PlatformWebViewController extends PlatformInterface {
throw UnimplementedError(
'setOnScrollPositionChange is not implemented on the current platform');
}

/// Sets a callback that notifies the host application that the web page
/// wants to display a JavaScript alert() dialog.
Future<void> setOnJavaScriptAlertDialog(
Future<void> Function(JavaScriptAlertDialogRequest request)
onJavaScriptAlertDialog) async {
throw UnimplementedError(
'setOnJavaScriptAlertDialog is not implemented on the current platform',
);
}

/// Sets a callback that notifies the host application that the web page
/// wants to display a JavaScript confirm() dialog.
Future<void> setOnJavaScriptConfirmDialog(
Future<bool> Function(JavaScriptConfirmDialogRequest request)
onJavaScriptConfirmDialog) async {
throw UnimplementedError(
'setOnJavaScriptConfirmDialog is not implemented on the current platform',
);
}

/// Sets a callback that notifies the host application that the web page
/// wants to display a JavaScript prompt() dialog.
Future<void> setOnJavaScriptTextInputDialog(
Future<String> Function(JavaScriptTextInputDialogRequest request)
onJavaScriptTextInputDialog) async {
throw UnimplementedError(
'setOnJavaScriptTextInputDialog is not implemented on the current platform',
);
}
}

/// Describes the parameters necessary for registering a JavaScript channel.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// 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';

/// Defines the parameters that support `PlatformWebViewController.setOnJavaScriptAlertDialog`.
@immutable
class JavaScriptAlertDialogRequest {
/// Creates a [JavaScriptAlertDialogRequest].
const JavaScriptAlertDialogRequest({
required this.message,
required this.url,
});

/// The message to be displayed in the window.
final String message;

/// The URL of the page requesting the dialog.
final String url;
}

/// Defines the parameters that support `PlatformWebViewController.setOnJavaScriptConfirmDialog`.
@immutable
class JavaScriptConfirmDialogRequest {
/// Creates a [JavaScriptConfirmDialogRequest].
const JavaScriptConfirmDialogRequest({
required this.message,
required this.url,
});

/// The message to be displayed in the window.
final String message;

/// The URL of the page requesting the dialog.
final String url;
}

/// Defines the parameters that support `PlatformWebViewController.setOnJavaScriptTextInputDialog`.
@immutable
class JavaScriptTextInputDialogRequest {
/// Creates a [JavaScriptAlertDialogRequest].
const JavaScriptTextInputDialogRequest({
required this.message,
required this.url,
required this.defaultText,
});

/// The message to be displayed in the window.
final String message;

/// The URL of the page requesting the dialog.
final String url;

/// The initial text to display in the text entry field.
final String? defaultText;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
export 'http_auth_request.dart';
export 'http_response_error.dart';
export 'javascript_console_message.dart';
export 'javascript_dialog_request.dart';
export 'javascript_log_level.dart';
export 'javascript_message.dart';
export 'javascript_mode.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ repository: https://github.com/flutter/packages/tree/main/packages/webview_flutt
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview_flutter%22
# 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: 2.8.0
version: 2.9.0

environment:
sdk: ">=3.0.0 <4.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,49 @@ void main() {
throwsUnimplementedError,
);
});

test(
'Default implementation of setOnJavaScriptAlertDialog should throw unimplemented error',
() {
final PlatformWebViewController controller =
ExtendsPlatformWebViewController(
const PlatformWebViewControllerCreationParams());

expect(
() => controller.setOnJavaScriptAlertDialog((_) async {}),
throwsUnimplementedError,
);
});

test(
'Default implementation of setOnJavaScriptConfirmDialog should throw unimplemented error',
() {
final PlatformWebViewController controller =
ExtendsPlatformWebViewController(
const PlatformWebViewControllerCreationParams());

expect(
() => controller.setOnJavaScriptConfirmDialog((_) async {
return false;
}),
throwsUnimplementedError,
);
});

test(
'Default implementation of setOnJavaScriptTextInputDialog should throw unimplemented error',
() {
final PlatformWebViewController controller =
ExtendsPlatformWebViewController(
const PlatformWebViewControllerCreationParams());

expect(
() => controller.setOnJavaScriptTextInputDialog((_) async {
return '';
}),
throwsUnimplementedError,
);
});
}

class MockWebViewPlatformWithMixin extends MockWebViewPlatform
Expand Down