Skip to content

Commit b2b1ddd

Browse files
Added PlatformInAppLocalhostServer.onData parameter to set a custom on data server callback #2188
1 parent 143ebd1 commit b2b1ddd

File tree

8 files changed

+60
-9
lines changed

8 files changed

+60
-9
lines changed

flutter_inappwebview/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#### Platform Interface
1212
- Updated static `fromMap` implementation for some classes
13+
- Added `PlatformInAppLocalhostServer.onData` parameter to set a custom on data server callback
1314

1415
#### Android Platform
1516
- Added `InAppWebViewController.enableSlowWholeDocumentDraw` static method
@@ -21,6 +22,9 @@
2122
- Moved `WKUserContentController` initialization on `preWKWebViewConfiguration` to fix possible `undefined is not an object (evaluating 'window.webkit.messageHandlers')` javascript error
2223
- Merged "change priority of DispatchQueue" [#2322](https://github.com/pichillilorenzo/flutter_inappwebview/pull/2322) (thanks to [nnnlog](https://github.com/nnnlog))
2324

25+
#### Web Platform
26+
- Merged "[web] support iframe role and aria-hidden attributes" [2293](https://github.com/pichillilorenzo/flutter_inappwebview/pull/2293) (thanks to [p-mazhnik](https://github.com/p-mazhnik))
27+
2428
## 6.1.5
2529

2630
- Updated dependencies to the latest versions for all platform implementations:

flutter_inappwebview/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
![InAppWebView-logo](https://user-images.githubusercontent.com/5956938/195422744-bdcfed16-73f0-4bc9-94ab-ecf10771a1c4.png)
66

77
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
8-
[![All Contributors](https://img.shields.io/badge/all_contributors-85-orange.svg?style=flat-square)](#contributors-)
8+
[![All Contributors](https://img.shields.io/badge/all_contributors-87-orange.svg?style=flat-square)](#contributors-)
99
<!-- ALL-CONTRIBUTORS-BADGE:END -->
1010

1111
[![flutter_inappwebview version](https://img.shields.io/pub/v/flutter_inappwebview?include_prereleases)](https://pub.dartlang.org/packages/flutter_inappwebview)
@@ -193,6 +193,8 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
193193
</tr>
194194
<tr>
195195
<td align="center" valign="top" width="14.28%"><a href="https://nlog.dev"><img src="https://avatars.githubusercontent.com/u/20399222?v=4?s=100" width="100px;" alt="nlog (solrin)"/><br /><sub><b>nlog (solrin)</b></sub></a><br /><a href="https://github.com/pichillilorenzo/flutter_inappwebview/commits?author=nnnlog" title="Code">💻</a></td>
196+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/Murmurl912"><img src="https://avatars.githubusercontent.com/u/36264246?v=4?s=100" width="100px;" alt="Murmurl912"/><br /><sub><b>Murmurl912</b></sub></a><br /><a href="https://github.com/pichillilorenzo/flutter_inappwebview/commits?author=Murmurl912" title="Code">💻</a></td>
197+
<td align="center" valign="top" width="14.28%"><a href="https://github.com/bschulz87"><img src="https://avatars.githubusercontent.com/u/30199362?v=4?s=100" width="100px;" alt="Benjamin Schulz"/><br /><sub><b>Benjamin Schulz</b></sub></a><br /><a href="#ideas-bschulz87" title="Ideas, Planning, & Feedback">🤔</a></td>
196198
</tr>
197199
</tbody>
198200
</table>

flutter_inappwebview/example/lib/main.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ import 'dart:async';
33
import 'package:flutter/foundation.dart';
44
import 'package:flutter/material.dart';
55
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
6-
76
import 'package:flutter_inappwebview_example/chrome_safari_browser_example.screen.dart';
87
import 'package:flutter_inappwebview_example/headless_in_app_webview.screen.dart';
9-
import 'package:flutter_inappwebview_example/in_app_webiew_example.screen.dart';
108
import 'package:flutter_inappwebview_example/in_app_browser_example.screen.dart';
9+
import 'package:flutter_inappwebview_example/in_app_webiew_example.screen.dart';
1110
import 'package:flutter_inappwebview_example/web_authentication_session_example.screen.dart';
1211
import 'package:pointer_interceptor/pointer_interceptor.dart';
1312

@@ -23,6 +22,8 @@ Future main() async {
2322
// await Permission.microphone.request();
2423
// await Permission.storage.request();
2524

25+
await localhostServer.start();
26+
2627
if (!kIsWeb && defaultTargetPlatform == TargetPlatform.windows) {
2728
final availableVersion = await WebViewEnvironment.getAvailableVersion();
2829
assert(availableVersion != null,

flutter_inappwebview/lib/src/in_app_localhost_server.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:async';
2+
import 'dart:io';
23
import 'package:flutter_inappwebview_platform_interface/flutter_inappwebview_platform_interface.dart';
34

45
///{@macro flutter_inappwebview_platform_interface.PlatformInAppLocalhostServer}
@@ -9,12 +10,14 @@ class InAppLocalhostServer {
910
String directoryIndex = 'index.html',
1011
String documentRoot = './',
1112
bool shared = false,
13+
Future<bool> Function(HttpRequest request)? onData,
1214
}) : this.fromPlatformCreationParams(
1315
PlatformInAppLocalhostServerCreationParams(
1416
port: port,
1517
directoryIndex: directoryIndex,
1618
documentRoot: documentRoot,
17-
shared: shared),
19+
shared: shared,
20+
onData: onData),
1821
);
1922

2023
/// Constructs a [InAppLocalhostServer] from creation params for a specific
@@ -42,6 +45,9 @@ class InAppLocalhostServer {
4245
///{@macro flutter_inappwebview_platform_interface.PlatformInAppLocalhostServer.shared}
4346
bool get shared => platform.shared;
4447

48+
///{@macro flutter_inappwebview_platform_interface.PlatformInAppLocalhostServer.onData}
49+
Future<bool> Function(HttpRequest request)? get onData => platform.onData;
50+
4551
///{@macro flutter_inappwebview_platform_interface.PlatformInAppLocalhostServer.start}
4652
Future<void> start() => platform.start();
4753

flutter_inappwebview_platform_interface/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#### 1.4.0
22

33
- Updated static `fromMap` implementation for some classes
4+
- Added `PlatformInAppLocalhostServer.onData` parameter to set a custom on data server callback
45

56
## 1.3.0+1
67

flutter_inappwebview_platform_interface/lib/src/in_app_localhost_server.dart

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class DefaultInAppLocalhostServer extends PlatformInAppLocalhostServer {
3838
bool _shared = false;
3939
String _directoryIndex = 'index.html';
4040
String _documentRoot = './';
41+
Future<bool> Function(HttpRequest)? _customOnData;
4142

4243
/// Creates a new [DefaultInAppLocalhostServer].
4344
DefaultInAppLocalhostServer(PlatformInAppLocalhostServerCreationParams params)
@@ -53,6 +54,7 @@ class DefaultInAppLocalhostServer extends PlatformInAppLocalhostServer {
5354
? params.documentRoot
5455
: '${params.documentRoot}/';
5556
this._shared = params.shared;
57+
this._customOnData = params.onData;
5658
}
5759

5860
@override
@@ -67,6 +69,9 @@ class DefaultInAppLocalhostServer extends PlatformInAppLocalhostServer {
6769
@override
6870
bool get shared => _shared;
6971

72+
@override
73+
Future<bool> Function(HttpRequest request)? get onData => _customOnData;
74+
7075
@override
7176
Future<void> start() async {
7277
if (this._started) {
@@ -78,11 +83,19 @@ class DefaultInAppLocalhostServer extends PlatformInAppLocalhostServer {
7883

7984
runZonedGuarded(() {
8085
HttpServer.bind('127.0.0.1', _port, shared: _shared).then((server) {
81-
print('Server running on http://localhost:' + _port.toString());
86+
if (kDebugMode) {
87+
print('Server running on http://localhost:' + _port.toString());
88+
}
8289

8390
this._server = server;
8491

8592
server.listen((HttpRequest request) async {
93+
if (await _customOnData?.call(request) ?? false) {
94+
// if _customOnData returns true,
95+
// it means that the request has been handled
96+
return;
97+
}
98+
8699
Uint8List body = Uint8List(0);
87100

88101
var path = request.requestedUri.path;
@@ -99,8 +112,10 @@ class DefaultInAppLocalhostServer extends PlatformInAppLocalhostServer {
99112
.buffer
100113
.asUint8List();
101114
} catch (e) {
102-
print(Uri.decodeFull(path));
103-
print(e.toString());
115+
if (kDebugMode) {
116+
print(Uri.decodeFull(path));
117+
print(e.toString());
118+
}
104119
request.response.close();
105120
return;
106121
}
@@ -115,13 +130,18 @@ class DefaultInAppLocalhostServer extends PlatformInAppLocalhostServer {
115130
}
116131

117132
request.response.headers.contentType = contentType;
133+
print(request.response.headers);
118134
request.response.add(body);
119135
request.response.close();
120136
});
121137

122138
completer.complete();
123139
});
124-
}, (e, stackTrace) => print('Error: $e $stackTrace'));
140+
}, (e, stackTrace) {
141+
if (kDebugMode) {
142+
print('Error: $e $stackTrace');
143+
}
144+
});
125145

126146
return completer.future;
127147
}
@@ -132,7 +152,9 @@ class DefaultInAppLocalhostServer extends PlatformInAppLocalhostServer {
132152
return;
133153
}
134154
await this._server!.close(force: true);
135-
print('Server running on http://localhost:$_port closed');
155+
if (kDebugMode) {
156+
print('Server running on http://localhost:$_port closed');
157+
}
136158
this._started = false;
137159
this._server = null;
138160
}

flutter_inappwebview_platform_interface/lib/src/platform_in_app_localhost_server.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:async';
2+
import 'dart:io';
23

34
import 'package:flutter/foundation.dart';
45
import 'package:flutter_inappwebview_platform_interface/flutter_inappwebview_platform_interface.dart';
@@ -16,6 +17,7 @@ class PlatformInAppLocalhostServerCreationParams {
1617
this.directoryIndex = 'index.html',
1718
this.documentRoot = './',
1819
this.shared = false,
20+
this.onData = null,
1921
});
2022

2123
///{@macro flutter_inappwebview_platform_interface.PlatformInAppLocalhostServer.port}
@@ -29,6 +31,9 @@ class PlatformInAppLocalhostServerCreationParams {
2931

3032
///{@macro flutter_inappwebview_platform_interface.PlatformInAppLocalhostServer.shared}
3133
final bool shared;
34+
35+
///{@macro flutter_inappwebview_platform_interface.PlatformInAppLocalhostServer.onData}
36+
final Future<bool> Function(HttpRequest request)? onData;
3237
}
3338

3439
///{@template flutter_inappwebview_platform_interface.PlatformInAppLocalhostServer}
@@ -40,6 +45,7 @@ class PlatformInAppLocalhostServerCreationParams {
4045
///- Android native WebView
4146
///- iOS
4247
///- MacOS
48+
///- Windows
4349
///{@endtemplate}
4450
abstract class PlatformInAppLocalhostServer extends PlatformInterface {
4551
/// Creates a new [PlatformInAppLocalhostServer]
@@ -98,6 +104,14 @@ abstract class PlatformInAppLocalhostServer extends PlatformInterface {
98104
///{@endtemplate}
99105
bool get shared => params.shared;
100106

107+
///{@template flutter_inappwebview_platform_interface.PlatformInAppLocalhostServer.onData}
108+
///A custom callback that is called when a new request is received by the server
109+
///that can be used to send or modify the response, for example adding custom headers.
110+
///If this callback returns `true`, it means that the request has been handled by this callback.
111+
///Otherwise, if this callback returns `false`, the server will continue to process the request using the default implementation.
112+
///{@endtemplate}
113+
Future<bool> Function(HttpRequest request)? get onData => params.onData;
114+
101115
///{@template flutter_inappwebview_platform_interface.PlatformInAppLocalhostServer.start}
102116
///Starts the server on `http://localhost:[port]/`.
103117
///

flutter_inappwebview_web/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 1.2.0
22

33
- Updated flutter_inappwebview_platform_interface version to ^1.4.0
4+
- Merged "[web] support iframe role and aria-hidden attributes" [2293](https://github.com/pichillilorenzo/flutter_inappwebview/pull/2293) (thanks to [p-mazhnik](https://github.com/p-mazhnik))
45

56
## 1.1.2
67

0 commit comments

Comments
 (0)