Skip to content

Commit

Permalink
[webview_flutter] Implement platform interface for JavaScript dialog (#…
Browse files Browse the repository at this point in the history
…5670)

Adds the platform interface implementation for JavaScript dailog.

This PR is part of a series of PRs that aim to close flutter/flutter#30358 (comment)
The PR that contains all changes can be found at #4704
  • Loading branch information
jsharp83 committed Dec 19, 2023
1 parent 3273017 commit 65e0a81
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 1 deletion.
@@ -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
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
@@ -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;
}
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
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
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

0 comments on commit 65e0a81

Please sign in to comment.