Skip to content

Commit 65e0a81

Browse files
authored
[webview_flutter] Implement platform interface for JavaScript dialog (flutter#5670)
Adds the platform interface implementation for JavaScript dailog. This PR is part of a series of PRs that aim to close flutter#30358 (comment) The PR that contains all changes can be found at flutter/packages#4704
1 parent 3273017 commit 65e0a81

File tree

6 files changed

+136
-1
lines changed

6 files changed

+136
-1
lines changed

packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.9.0
2+
3+
* Adds support to show JavaScript dialog. See `PlatformWebViewController.setOnJavaScriptAlertDialog`, `PlatformWebViewController.setOnJavaScriptConfirmDialog` and `PlatformWebViewController.setOnJavaScriptTextInputDialog`.
4+
15
## 2.8.0
26

37
* Adds support to track scroll position changes. See `PlatformWebViewController.setOnScrollPositionChange`.

packages/webview_flutter/webview_flutter_platform_interface/lib/src/platform_webview_controller.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,36 @@ abstract class PlatformWebViewController extends PlatformInterface {
294294
throw UnimplementedError(
295295
'setOnScrollPositionChange is not implemented on the current platform');
296296
}
297+
298+
/// Sets a callback that notifies the host application that the web page
299+
/// wants to display a JavaScript alert() dialog.
300+
Future<void> setOnJavaScriptAlertDialog(
301+
Future<void> Function(JavaScriptAlertDialogRequest request)
302+
onJavaScriptAlertDialog) async {
303+
throw UnimplementedError(
304+
'setOnJavaScriptAlertDialog is not implemented on the current platform',
305+
);
306+
}
307+
308+
/// Sets a callback that notifies the host application that the web page
309+
/// wants to display a JavaScript confirm() dialog.
310+
Future<void> setOnJavaScriptConfirmDialog(
311+
Future<bool> Function(JavaScriptConfirmDialogRequest request)
312+
onJavaScriptConfirmDialog) async {
313+
throw UnimplementedError(
314+
'setOnJavaScriptConfirmDialog is not implemented on the current platform',
315+
);
316+
}
317+
318+
/// Sets a callback that notifies the host application that the web page
319+
/// wants to display a JavaScript prompt() dialog.
320+
Future<void> setOnJavaScriptTextInputDialog(
321+
Future<String> Function(JavaScriptTextInputDialogRequest request)
322+
onJavaScriptTextInputDialog) async {
323+
throw UnimplementedError(
324+
'setOnJavaScriptTextInputDialog is not implemented on the current platform',
325+
);
326+
}
297327
}
298328

299329
/// Describes the parameters necessary for registering a JavaScript channel.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Copyright 2013 The Flutter 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 'package:flutter/foundation.dart';
6+
7+
/// Defines the parameters that support `PlatformWebViewController.setOnJavaScriptAlertDialog`.
8+
@immutable
9+
class JavaScriptAlertDialogRequest {
10+
/// Creates a [JavaScriptAlertDialogRequest].
11+
const JavaScriptAlertDialogRequest({
12+
required this.message,
13+
required this.url,
14+
});
15+
16+
/// The message to be displayed in the window.
17+
final String message;
18+
19+
/// The URL of the page requesting the dialog.
20+
final String url;
21+
}
22+
23+
/// Defines the parameters that support `PlatformWebViewController.setOnJavaScriptConfirmDialog`.
24+
@immutable
25+
class JavaScriptConfirmDialogRequest {
26+
/// Creates a [JavaScriptConfirmDialogRequest].
27+
const JavaScriptConfirmDialogRequest({
28+
required this.message,
29+
required this.url,
30+
});
31+
32+
/// The message to be displayed in the window.
33+
final String message;
34+
35+
/// The URL of the page requesting the dialog.
36+
final String url;
37+
}
38+
39+
/// Defines the parameters that support `PlatformWebViewController.setOnJavaScriptTextInputDialog`.
40+
@immutable
41+
class JavaScriptTextInputDialogRequest {
42+
/// Creates a [JavaScriptAlertDialogRequest].
43+
const JavaScriptTextInputDialogRequest({
44+
required this.message,
45+
required this.url,
46+
required this.defaultText,
47+
});
48+
49+
/// The message to be displayed in the window.
50+
final String message;
51+
52+
/// The URL of the page requesting the dialog.
53+
final String url;
54+
55+
/// The initial text to display in the text entry field.
56+
final String? defaultText;
57+
}

packages/webview_flutter/webview_flutter_platform_interface/lib/src/types/types.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
export 'http_auth_request.dart';
66
export 'http_response_error.dart';
77
export 'javascript_console_message.dart';
8+
export 'javascript_dialog_request.dart';
89
export 'javascript_log_level.dart';
910
export 'javascript_message.dart';
1011
export 'javascript_mode.dart';

packages/webview_flutter/webview_flutter_platform_interface/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ repository: https://github.com/flutter/packages/tree/main/packages/webview_flutt
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview_flutter%22
55
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
66
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
7-
version: 2.8.0
7+
version: 2.9.0
88

99
environment:
1010
sdk: ">=3.0.0 <4.0.0"

packages/webview_flutter/webview_flutter_platform_interface/test/platform_webview_controller_test.dart

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,49 @@ void main() {
414414
throwsUnimplementedError,
415415
);
416416
});
417+
418+
test(
419+
'Default implementation of setOnJavaScriptAlertDialog should throw unimplemented error',
420+
() {
421+
final PlatformWebViewController controller =
422+
ExtendsPlatformWebViewController(
423+
const PlatformWebViewControllerCreationParams());
424+
425+
expect(
426+
() => controller.setOnJavaScriptAlertDialog((_) async {}),
427+
throwsUnimplementedError,
428+
);
429+
});
430+
431+
test(
432+
'Default implementation of setOnJavaScriptConfirmDialog should throw unimplemented error',
433+
() {
434+
final PlatformWebViewController controller =
435+
ExtendsPlatformWebViewController(
436+
const PlatformWebViewControllerCreationParams());
437+
438+
expect(
439+
() => controller.setOnJavaScriptConfirmDialog((_) async {
440+
return false;
441+
}),
442+
throwsUnimplementedError,
443+
);
444+
});
445+
446+
test(
447+
'Default implementation of setOnJavaScriptTextInputDialog should throw unimplemented error',
448+
() {
449+
final PlatformWebViewController controller =
450+
ExtendsPlatformWebViewController(
451+
const PlatformWebViewControllerCreationParams());
452+
453+
expect(
454+
() => controller.setOnJavaScriptTextInputDialog((_) async {
455+
return '';
456+
}),
457+
throwsUnimplementedError,
458+
);
459+
});
417460
}
418461

419462
class MockWebViewPlatformWithMixin extends MockWebViewPlatform

0 commit comments

Comments
 (0)