Skip to content

[webview_flutter_platform_interface] Adds platform interface for onHttpError callback #3645

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
## NEXT
## 2.2.0

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

## 2.1.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ typedef PageEventCallback = void Function(String url);
/// Signature for callbacks that report loading progress of a page.
typedef ProgressCallback = void Function(int progress);

/// Signature for callbacks that report http errors during loading a page.
typedef HttpResponseErrorCallback = void Function(HttpResponseError error);

/// Signature for callbacks that report a resource loading error.
typedef WebResourceErrorCallback = void Function(WebResourceError error);

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

/// Invoked when an HTTP error has occurred during loading.
///
/// See [PlatformWebViewController.setPlatformNavigationDelegate].
Future<void> setOnHttpError(
HttpResponseErrorCallback onHttpError,
) {
throw UnimplementedError(
'setOnHttpError is not implemented on the current platform.');
}

/// Invoked when a page is loading to report the progress.
///
/// See [PlatformWebViewController.setPlatformNavigationDelegate].
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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';

/// Error returned in `PlatformNavigationDelegate.setOnHttpError` when an HTTP
/// response error has been received.
///
/// Platform specific implementations can add additional fields by extending
/// this class.
///
/// This example demonstrates how to extend the [HttpResponseError] to
/// provide additional platform specific parameters.
///
/// When extending [HttpResponseError] additional parameters should always
/// accept `null` or have a default value to prevent breaking changes.
///
/// ```dart
/// class IOSHttpResponseError extends HttpResponseError {
/// IOSHttpResponseError._(HttpResponseError error, {required this.domain})
/// : super(
/// statusCode: error.statusCode,
/// );
///
/// factory IOSHttpResponseError.fromHttpResponseError(
/// HttpResponseError error, {
/// required String? domain,
/// }) {
/// return IOSHttpResponseError._(error, domain: domain);
/// }
///
/// final String? domain;
/// }
/// ```
@immutable
class HttpResponseError {
/// Used by the platform implementation to create a new [HttpResponseError].
const HttpResponseError({
required this.statusCode,
});

/// The HTTP status code.
final int statusCode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

export 'http_response_error.dart';
export 'javascript_message.dart';
export 'javascript_mode.dart';
export 'load_request_params.dart';
Expand Down
Original file line number Diff line number Diff line change
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.1.0
version: 2.2.0

environment:
sdk: ">=2.18.0 <4.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@ void main() {
});

test(
'Default implementation of setOnHttpError should throw unimplemented error',
() {
final PlatformNavigationDelegate callbackDelegate =
ExtendsPlatformNavigationDelegate(
const PlatformNavigationDelegateCreationParams());

expect(
() => callbackDelegate.setOnHttpError((HttpResponseError error) {}),
throwsUnimplementedError,
);
});

test(
// ignore: lines_longer_than_80_chars
'Default implementation of setOnProgress should throw unimplemented error',
() {
final PlatformNavigationDelegate callbackDelegate =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ class MockPlatformNavigationDelegate extends _i1.Mock
returnValueForMissingStub: _i4.Future<void>.value(),
) as _i4.Future<void>);
@override
_i4.Future<void> setOnHttpError(_i3.HttpResponseErrorCallback? onHttpError) =>
(super.noSuchMethod(
Invocation.method(
#setOnHttpError,
[onHttpError],
),
returnValue: _i4.Future<void>.value(),
returnValueForMissingStub: _i4.Future<void>.value(),
) as _i4.Future<void>);
@override
_i4.Future<void> setOnProgress(_i3.ProgressCallback? onProgress) =>
(super.noSuchMethod(
Invocation.method(
Expand Down