Skip to content

Commit 1598ccd

Browse files
authored
[webview_flutter_platform_interface] Adds option to override console log (#4701)
Adds an option to the `webview_flutter_platform_interface` to register a JavaScript console callback. This will allow developers to receive JavaScript console messages in a Dart callback. This PR contains the `webview_flutter_platform_interface` changes from PR #4541. Related issue: flutter/flutter#32908 *If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].*
1 parent b8b84b2 commit 1598ccd

9 files changed

+84
-3
lines changed

packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.6.0
2+
3+
* Adds support to register a callback to intercept messages that are written to
4+
the JavaScript console. See `PlatformWebViewController.setOnConsoleMessage`.
5+
16
## 2.5.1
27

38
* Adds pub topics to package metadata.

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,15 @@ abstract class PlatformWebViewController extends PlatformInterface {
277277
'getUserAgent is not implemented on the current platform',
278278
);
279279
}
280+
281+
/// Sets a callback that notifies the host application of any console messages
282+
/// written to the JavaScript console.
283+
Future<void> setOnConsoleMessage(
284+
void Function(JavaScriptConsoleMessage consoleMessage) onConsoleMessage) {
285+
throw UnimplementedError(
286+
'setOnConsoleMessage is not implemented on the current platform',
287+
);
288+
}
280289
}
281290

282291
/// Describes the parameters necessary for registering a JavaScript channel.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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:meta/meta.dart';
6+
7+
import 'javascript_log_level.dart';
8+
9+
/// Represents a console message written to the JavaScript console.
10+
@immutable
11+
class JavaScriptConsoleMessage {
12+
/// Creates a [JavaScriptConsoleMessage].
13+
const JavaScriptConsoleMessage({
14+
required this.level,
15+
required this.message,
16+
});
17+
18+
/// The severity of a JavaScript log message.
19+
final JavaScriptLogLevel level;
20+
21+
/// The message written to the console.
22+
final String message;
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
/// Represents the severity of a JavaScript log message.
6+
enum JavaScriptLogLevel {
7+
/// Indicates an error message was logged via an "error" event of the
8+
/// `console.error` method.
9+
error,
10+
11+
/// Indicates a warning message was logged using the `console.warning`
12+
/// method.
13+
warning,
14+
15+
/// Indicates a debug message was logged using the `console.debug` method.
16+
debug,
17+
18+
/// Indicates an informational message was logged using the `console.info`
19+
/// method.
20+
info,
21+
22+
/// Indicates a log message was logged using the `console.log` method.
23+
log,
24+
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// found in the LICENSE file.
44

55
export 'http_response_error.dart';
6+
export 'javascript_console_message.dart';
7+
export 'javascript_log_level.dart';
68
export 'javascript_message.dart';
79
export 'javascript_mode.dart';
810
export 'load_request_params.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.5.1
7+
version: 2.6.0
88

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

packages/webview_flutter/webview_flutter_platform_interface/test/platform_webview_controller_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,20 @@ void main() {
400400
throwsUnimplementedError,
401401
);
402402
});
403+
404+
test(
405+
'Default implementation of setOnConsoleMessage should throw unimplemented error',
406+
() {
407+
final PlatformWebViewController controller =
408+
ExtendsPlatformWebViewController(
409+
const PlatformWebViewControllerCreationParams());
410+
411+
expect(
412+
() =>
413+
controller.setOnConsoleMessage((JavaScriptConsoleMessage message) {}),
414+
throwsUnimplementedError,
415+
);
416+
});
403417
}
404418

405419
class MockWebViewPlatformWithMixin extends MockWebViewPlatform

packages/webview_flutter/webview_flutter_platform_interface/test/platform_webview_controller_test.mocks.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
// Mocks generated by Mockito 5.4.0 from annotations
1+
// Mocks generated by Mockito 5.4.1 from annotations
22
// in webview_flutter_platform_interface/test/platform_webview_controller_test.dart.
33
// Do not manually edit this file.
44

5+
// @dart=2.19
6+
57
// ignore_for_file: no_leading_underscores_for_library_prefixes
68
import 'dart:async' as _i4;
79

packages/webview_flutter/webview_flutter_platform_interface/test/webview_platform_test.mocks.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
// Mocks generated by Mockito 5.4.0 from annotations
1+
// Mocks generated by Mockito 5.4.1 from annotations
22
// in webview_flutter_platform_interface/test/webview_platform_test.dart.
33
// Do not manually edit this file.
44

5+
// @dart=2.19
6+
57
// ignore_for_file: no_leading_underscores_for_library_prefixes
68
import 'package:mockito/mockito.dart' as _i1;
79
import 'package:webview_flutter_platform_interface/src/platform_navigation_delegate.dart'

0 commit comments

Comments
 (0)