Skip to content

Commit fe5615f

Browse files
authored
[webview_flutter_platform_interface] Adds platform interface for onHttpError callback (flutter#3645)
[webview_flutter_platform_interface] Adds platform interface for onHttpError callback
1 parent aad90e7 commit fe5615f

File tree

7 files changed

+87
-2
lines changed

7 files changed

+87
-2
lines changed

packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
## NEXT
1+
## 2.2.0
22

33
* Updates minimum Flutter version to 3.3.
44
* Fixes common typos in tests and documentation.
5+
* Adds support for listening to HTTP errors. See
6+
`PlatformNavigationDelegate.setOnHttpError`.
57

68
## 2.1.0
79

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ typedef PageEventCallback = void Function(String url);
1919
/// Signature for callbacks that report loading progress of a page.
2020
typedef ProgressCallback = void Function(int progress);
2121

22+
/// Signature for callbacks that report http errors during loading a page.
23+
typedef HttpResponseErrorCallback = void Function(HttpResponseError error);
24+
2225
/// Signature for callbacks that report a resource loading error.
2326
typedef WebResourceErrorCallback = void Function(WebResourceError error);
2427

@@ -90,6 +93,16 @@ abstract class PlatformNavigationDelegate extends PlatformInterface {
9093
'setOnPageFinished is not implemented on the current platform.');
9194
}
9295

96+
/// Invoked when an HTTP error has occurred during loading.
97+
///
98+
/// See [PlatformWebViewController.setPlatformNavigationDelegate].
99+
Future<void> setOnHttpError(
100+
HttpResponseErrorCallback onHttpError,
101+
) {
102+
throw UnimplementedError(
103+
'setOnHttpError is not implemented on the current platform.');
104+
}
105+
93106
/// Invoked when a page is loading to report the progress.
94107
///
95108
/// See [PlatformWebViewController.setPlatformNavigationDelegate].
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
/// Error returned in `PlatformNavigationDelegate.setOnHttpError` when an HTTP
8+
/// response error has been received.
9+
///
10+
/// Platform specific implementations can add additional fields by extending
11+
/// this class.
12+
///
13+
/// This example demonstrates how to extend the [HttpResponseError] to
14+
/// provide additional platform specific parameters.
15+
///
16+
/// When extending [HttpResponseError] additional parameters should always
17+
/// accept `null` or have a default value to prevent breaking changes.
18+
///
19+
/// ```dart
20+
/// class IOSHttpResponseError extends HttpResponseError {
21+
/// IOSHttpResponseError._(HttpResponseError error, {required this.domain})
22+
/// : super(
23+
/// statusCode: error.statusCode,
24+
/// );
25+
///
26+
/// factory IOSHttpResponseError.fromHttpResponseError(
27+
/// HttpResponseError error, {
28+
/// required String? domain,
29+
/// }) {
30+
/// return IOSHttpResponseError._(error, domain: domain);
31+
/// }
32+
///
33+
/// final String? domain;
34+
/// }
35+
/// ```
36+
@immutable
37+
class HttpResponseError {
38+
/// Used by the platform implementation to create a new [HttpResponseError].
39+
const HttpResponseError({
40+
required this.statusCode,
41+
});
42+
43+
/// The HTTP status code.
44+
final int statusCode;
45+
}

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
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
export 'http_response_error.dart';
56
export 'javascript_message.dart';
67
export 'javascript_mode.dart';
78
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.1.0
7+
version: 2.2.0
88

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

packages/webview_flutter/webview_flutter_platform_interface/test/platform_navigation_delegate_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,20 @@ void main() {
9191
});
9292

9393
test(
94+
'Default implementation of setOnHttpError should throw unimplemented error',
95+
() {
96+
final PlatformNavigationDelegate callbackDelegate =
97+
ExtendsPlatformNavigationDelegate(
98+
const PlatformNavigationDelegateCreationParams());
99+
100+
expect(
101+
() => callbackDelegate.setOnHttpError((HttpResponseError error) {}),
102+
throwsUnimplementedError,
103+
);
104+
});
105+
106+
test(
107+
// ignore: lines_longer_than_80_chars
94108
'Default implementation of setOnProgress should throw unimplemented error',
95109
() {
96110
final PlatformNavigationDelegate callbackDelegate =

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ class MockPlatformNavigationDelegate extends _i1.Mock
8383
returnValueForMissingStub: _i4.Future<void>.value(),
8484
) as _i4.Future<void>);
8585
@override
86+
_i4.Future<void> setOnHttpError(_i3.HttpResponseErrorCallback? onHttpError) =>
87+
(super.noSuchMethod(
88+
Invocation.method(
89+
#setOnHttpError,
90+
[onHttpError],
91+
),
92+
returnValue: _i4.Future<void>.value(),
93+
returnValueForMissingStub: _i4.Future<void>.value(),
94+
) as _i4.Future<void>);
95+
@override
8696
_i4.Future<void> setOnProgress(_i3.ProgressCallback? onProgress) =>
8797
(super.noSuchMethod(
8898
Invocation.method(

0 commit comments

Comments
 (0)