[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].*
This commit is contained in:
Maurits van Beusekom
2023-09-07 17:36:04 +02:00
committed by GitHub
parent b8b84b2304
commit 1598ccd896
9 changed files with 84 additions and 3 deletions

View File

@ -1,3 +1,8 @@
## 2.6.0
* Adds support to register a callback to intercept messages that are written to
the JavaScript console. See `PlatformWebViewController.setOnConsoleMessage`.
## 2.5.1
* Adds pub topics to package metadata.

View File

@ -277,6 +277,15 @@ abstract class PlatformWebViewController extends PlatformInterface {
'getUserAgent is not implemented on the current platform',
);
}
/// Sets a callback that notifies the host application of any console messages
/// written to the JavaScript console.
Future<void> setOnConsoleMessage(
void Function(JavaScriptConsoleMessage consoleMessage) onConsoleMessage) {
throw UnimplementedError(
'setOnConsoleMessage is not implemented on the current platform',
);
}
}
/// Describes the parameters necessary for registering a JavaScript channel.

View File

@ -0,0 +1,23 @@
// 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:meta/meta.dart';
import 'javascript_log_level.dart';
/// Represents a console message written to the JavaScript console.
@immutable
class JavaScriptConsoleMessage {
/// Creates a [JavaScriptConsoleMessage].
const JavaScriptConsoleMessage({
required this.level,
required this.message,
});
/// The severity of a JavaScript log message.
final JavaScriptLogLevel level;
/// The message written to the console.
final String message;
}

View File

@ -0,0 +1,24 @@
// 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.
/// Represents the severity of a JavaScript log message.
enum JavaScriptLogLevel {
/// Indicates an error message was logged via an "error" event of the
/// `console.error` method.
error,
/// Indicates a warning message was logged using the `console.warning`
/// method.
warning,
/// Indicates a debug message was logged using the `console.debug` method.
debug,
/// Indicates an informational message was logged using the `console.info`
/// method.
info,
/// Indicates a log message was logged using the `console.log` method.
log,
}

View File

@ -3,6 +3,8 @@
// found in the LICENSE file.
export 'http_response_error.dart';
export 'javascript_console_message.dart';
export 'javascript_log_level.dart';
export 'javascript_message.dart';
export 'javascript_mode.dart';
export 'load_request_params.dart';

View File

@ -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.5.1
version: 2.6.0
environment:
sdk: ">=2.19.0 <4.0.0"

View File

@ -400,6 +400,20 @@ void main() {
throwsUnimplementedError,
);
});
test(
'Default implementation of setOnConsoleMessage should throw unimplemented error',
() {
final PlatformWebViewController controller =
ExtendsPlatformWebViewController(
const PlatformWebViewControllerCreationParams());
expect(
() =>
controller.setOnConsoleMessage((JavaScriptConsoleMessage message) {}),
throwsUnimplementedError,
);
});
}
class MockWebViewPlatformWithMixin extends MockWebViewPlatform

View File

@ -1,7 +1,9 @@
// Mocks generated by Mockito 5.4.0 from annotations
// Mocks generated by Mockito 5.4.1 from annotations
// in webview_flutter_platform_interface/test/platform_webview_controller_test.dart.
// Do not manually edit this file.
// @dart=2.19
// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i4;

View File

@ -1,7 +1,9 @@
// Mocks generated by Mockito 5.4.0 from annotations
// Mocks generated by Mockito 5.4.1 from annotations
// in webview_flutter_platform_interface/test/webview_platform_test.dart.
// Do not manually edit this file.
// @dart=2.19
// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'package:mockito/mockito.dart' as _i1;
import 'package:webview_flutter_platform_interface/src/platform_navigation_delegate.dart'