blob: 7ef72257999f3a5f4e875b8958c35d8d58b806ef [file] [log] [blame]
// 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 'dart:convert';
import 'dart:html';
import 'package:flutter/cupertino.dart';
import 'package:webview_flutter_platform_interface/webview_flutter_platform_interface.dart';
import 'http_request_factory.dart';
import 'shims/dart_ui.dart' as ui;
/// An implementation of [PlatformWebViewControllerCreationParams] using Flutter
/// for Web API.
@immutable
class WebWebViewControllerCreationParams
extends PlatformWebViewControllerCreationParams {
/// Creates a new [AndroidWebViewControllerCreationParams] instance.
WebWebViewControllerCreationParams({
@visibleForTesting this.httpRequestFactory = const HttpRequestFactory(),
}) : super();
/// Creates a [WebWebViewControllerCreationParams] instance based on [PlatformWebViewControllerCreationParams].
WebWebViewControllerCreationParams.fromPlatformWebViewControllerCreationParams(
// Recommended placeholder to prevent being broken by platform interface.
// ignore: avoid_unused_constructor_parameters
PlatformWebViewControllerCreationParams params, {
@visibleForTesting
HttpRequestFactory httpRequestFactory = const HttpRequestFactory(),
}) : this(httpRequestFactory: httpRequestFactory);
static int _nextIFrameId = 0;
/// Handles creating and sending URL requests.
final HttpRequestFactory httpRequestFactory;
/// The underlying element used as the WebView.
@visibleForTesting
final IFrameElement iFrame = IFrameElement()
..id = 'webView${_nextIFrameId++}'
..width = '100%'
..height = '100%'
..style.border = 'none';
}
/// An implementation of [PlatformWebViewController] using Flutter for Web API.
class WebWebViewController extends PlatformWebViewController {
/// Constructs a [WebWebViewController].
WebWebViewController(PlatformWebViewControllerCreationParams params)
: super.implementation(params is WebWebViewControllerCreationParams
? params
: WebWebViewControllerCreationParams
.fromPlatformWebViewControllerCreationParams(params));
WebWebViewControllerCreationParams get _webWebViewParams =>
params as WebWebViewControllerCreationParams;
@override
Future<void> loadHtmlString(String html, {String? baseUrl}) async {
// ignore: unsafe_html
_webWebViewParams.iFrame.src = Uri.dataFromString(
html,
mimeType: 'text/html',
encoding: utf8,
).toString();
}
@override
Future<void> loadRequest(LoadRequestParams params) async {
if (!params.uri.hasScheme) {
throw ArgumentError(
'LoadRequestParams#uri is required to have a scheme.');
}
final HttpRequest httpReq =
await _webWebViewParams.httpRequestFactory.request(
params.uri.toString(),
method: params.method.serialize(),
requestHeaders: params.headers,
sendData: params.body,
);
final String contentType =
httpReq.getResponseHeader('content-type') ?? 'text/html';
// ignore: unsafe_html
_webWebViewParams.iFrame.src = Uri.dataFromString(
httpReq.responseText ?? '',
mimeType: contentType,
encoding: utf8,
).toString();
}
}
/// An implementation of [PlatformWebViewWidget] using Flutter the for Web API.
class WebWebViewWidget extends PlatformWebViewWidget {
/// Constructs a [WebWebViewWidget].
WebWebViewWidget(PlatformWebViewWidgetCreationParams params)
: super.implementation(params) {
final WebWebViewController controller =
params.controller as WebWebViewController;
ui.platformViewRegistry.registerViewFactory(
controller._webWebViewParams.iFrame.id,
(int viewId) => controller._webWebViewParams.iFrame,
);
}
@override
Widget build(BuildContext context) {
return HtmlElementView(
key: params.key,
viewType: (params.controller as WebWebViewController)
._webWebViewParams
.iFrame
.id,
);
}
}