Skip to content
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

revert bundle request #660

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion webf/lib/src/css/background.dart
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ class CSSBackgroundImage {
// Increment count when request.
controller.view.document.incrementRequestCount();

ImageLoadResponse data = await request.obtainImage(controller);
ImageLoadResponse data = await request.obtainImage(controller.view.contextId);

// Decrement count when response.
controller.view.document.decrementRequestCount();
Expand Down
3 changes: 1 addition & 2 deletions webf/lib/src/css/font_face.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ class CSSFontFace {
if (uri == null) return;
final WebFController controller = WebFController.getControllerOfJSContextId(contextId)!;
WebFBundle bundle = controller.getPreloadBundleFromUrl(uri.toString()) ?? WebFBundle.fromUrl(uri.toString());
await bundle.resolve(baseUrl: controller.url, uriParser: controller.uriParser);
await bundle.obtainData(controller.view.contextId);
await bundle.resolve(contextId);
assert(bundle.isResolved, 'Failed to obtain $url');
FontLoader loader = FontLoader(removeQuotationMark(fontFamily));
Future<ByteData> bytes = Future.value(bundle.data?.buffer.asByteData());
Expand Down
24 changes: 11 additions & 13 deletions webf/lib/src/foundation/bundle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:webf/foundation.dart';
import 'package:webf/launcher.dart';
import 'package:webf/module.dart';
import 'package:webf/bridge.dart';

Expand Down Expand Up @@ -97,7 +98,7 @@ abstract class WebFBundle {
Uint8List? data;

// Indicate the bundle is resolved.
bool get isResolved => _uri != null;
bool get isResolved => _uri != null && data != null;
bool get isDataObtained => data != null;

bool _hitCache = false;
Expand Down Expand Up @@ -134,20 +135,20 @@ abstract class WebFBundle {
}

@mustCallSuper
Future<void> resolve({ String? baseUrl, UriParser? uriParser }) async {
Future<void> resolve(double? contextId) async {
if (isResolved) return;

// Source is input by user, do not trust it's a valid URL.
_uri = Uri.tryParse(url);

if (baseUrl != null && _uri != null) {
uriParser ??= UriParser();
_uri = uriParser.resolve(Uri.parse(baseUrl), _uri!);
if (contextId != null && _uri != null) {
WebFController? controller = WebFController.getControllerOfJSContextId(contextId);
if (controller != null) {
_uri = controller.uriParser!.resolve(Uri.parse(controller.url), _uri!);
}
}
}

Future<void> obtainData([double contextId]);

// Dispose the memory obtained by bundle.
@mustCallSuper
void dispose() {
Expand Down Expand Up @@ -234,9 +235,6 @@ class DataBundle extends WebFBundle {
data = uriData.contentAsBytes();
_contentType = contentType ?? ContentType.parse('${uriData.mimeType}; charset=${uriData.charset}');
}

@override
Future<void> obtainData([double contextId = 0]) async {}
}

// The bundle that source from http or https.
Expand All @@ -250,8 +248,8 @@ class NetworkBundle extends WebFBundle {
Map<String, String>? additionalHttpHeaders = {};

@override
Future<void> obtainData([double contextId = 0]) async {
if (data != null) return;
Future<void> resolve(double? contextId) async {
super.resolve(contextId);

NetworkOpItem? currentProfileOp;
if (enableWebFProfileTracking) {
Expand All @@ -269,7 +267,7 @@ class NetworkBundle extends WebFBundle {
// Prepare request headers.
request.headers.set('Accept', _acceptHeader());
additionalHttpHeaders?.forEach(request.headers.set);
WebFHttpOverrides.setContextHeader(request.headers, contextId);
WebFHttpOverrides.setContextHeader(request.headers, contextId!);

if (enableWebFProfileTracking) {
WebFProfiler.instance.startTrackNetworkStep(currentProfileOp!, 'request.close()');
Expand Down
3 changes: 1 addition & 2 deletions webf/lib/src/html/head.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ class LinkElement extends Element {
// Increment count when request.
ownerDocument.incrementRequestCount();

await bundle.resolve(baseUrl: ownerDocument.controller.url, uriParser: ownerDocument.controller.uriParser);
await bundle.obtainData(ownerView.contextId);
await bundle.resolve(contextId);
assert(bundle.isResolved, 'Failed to obtain $url');
_loading = false;
// Decrement count when response.
Expand Down
10 changes: 4 additions & 6 deletions webf/lib/src/html/img.dart
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ class ImageElement extends Element {
// Increment count when request.
ownerDocument.incrementRequestCount();

final data = await request.obtainImage(ownerDocument.controller);
final data = await request.obtainImage(contextId);

// Decrement count when response.
ownerDocument.decrementRequestCount();
Expand Down Expand Up @@ -774,11 +774,9 @@ class ImageRequest {
state == _ImageRequestState.completelyAvailable ||
state == _ImageRequestState.partiallyAvailable;

Future<ImageLoadResponse> obtainImage(WebFController controller) async {
final WebFBundle bundle =
controller.getPreloadBundleFromUrl(currentUri.toString()) ?? WebFBundle.fromUrl(currentUri.toString());
await bundle.resolve(baseUrl: controller.url, uriParser: controller.uriParser);
await bundle.obtainData(controller.view.contextId);
Future<ImageLoadResponse> obtainImage(double? contextId) async {
final WebFBundle bundle = WebFBundle.fromUrl(currentUri.toString());
await bundle.resolve(contextId);

if (!bundle.isResolved) {
throw FlutterError('Failed to load $currentUri');
Expand Down
3 changes: 1 addition & 2 deletions webf/lib/src/html/script.dart
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ class ScriptRunner {
// Increment count when request.
_document.incrementDOMContentLoadedEventDelayCount();
try {
await bundle.resolve(baseUrl: _document.controller.url, uriParser: _document.controller.uriParser);
await bundle.obtainData(_contextId);
await bundle.resolve(_contextId);

if (!bundle.isResolved) {
throw FlutterError('Network error.');
Expand Down
3 changes: 1 addition & 2 deletions webf/lib/src/launcher/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1568,8 +1568,7 @@ class WebFController {

// Resolve the bundle, including network download or other fetching ways.
try {
await bundleToLoad.resolve(baseUrl: url, uriParser: uriParser);
await bundleToLoad.obtainData(view.contextId);
await bundleToLoad.resolve(view.contextId);
} catch (e, stack) {
if (onLoadError != null) {
onLoadError!(FlutterError(e.toString()), stack);
Expand Down
Loading