Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[web] Move webOnlyAssetManager to dart:ui_web #42642

Merged
merged 4 commits into from
Jun 16, 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
4 changes: 2 additions & 2 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -1893,7 +1893,6 @@ ORIGIN: ../../../flutter/lib/web_ui/lib/semantics.dart + ../../../flutter/LICENS
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/alarm_clock.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/app_bootstrap.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/assets.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/browser_detection.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/canvas_pool.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/src/engine/canvaskit/canvas.dart + ../../../flutter/LICENSE
Expand Down Expand Up @@ -2109,6 +2108,7 @@ ORIGIN: ../../../flutter/lib/web_ui/lib/text.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/tile_mode.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/ui.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/ui_web/src/ui_web.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/ui_web/src/ui_web/asset_manager.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/ui_web/src/ui_web/navigation/platform_location.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/ui_web/src/ui_web/navigation/url_strategy.dart + ../../../flutter/LICENSE
ORIGIN: ../../../flutter/lib/web_ui/lib/ui_web/src/ui_web/platform_view_registry.dart + ../../../flutter/LICENSE
Expand Down Expand Up @@ -4572,7 +4572,6 @@ FILE: ../../../flutter/lib/web_ui/lib/semantics.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/alarm_clock.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/app_bootstrap.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/assets.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/browser_detection.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/canvas_pool.dart
FILE: ../../../flutter/lib/web_ui/lib/src/engine/canvaskit/canvas.dart
Expand Down Expand Up @@ -4788,6 +4787,7 @@ FILE: ../../../flutter/lib/web_ui/lib/text.dart
FILE: ../../../flutter/lib/web_ui/lib/tile_mode.dart
FILE: ../../../flutter/lib/web_ui/lib/ui.dart
FILE: ../../../flutter/lib/web_ui/lib/ui_web/src/ui_web.dart
FILE: ../../../flutter/lib/web_ui/lib/ui_web/src/ui_web/asset_manager.dart
FILE: ../../../flutter/lib/web_ui/lib/ui_web/src/ui_web/navigation/platform_location.dart
FILE: ../../../flutter/lib/web_ui/lib/ui_web/src/ui_web/navigation/url_strategy.dart
FILE: ../../../flutter/lib/web_ui/lib/ui_web/src/ui_web/platform_view_registry.dart
Expand Down
17 changes: 12 additions & 5 deletions lib/web_ui/lib/initialization.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,18 @@ set debugEmulateFlutterTesterEnvironment(bool value) {
bool _debugEmulateFlutterTesterEnvironment = false;

/// Provides the asset manager.
// TODO(yjbanov): this function should not return a private type. Instead, we
// should create a public interface for the returned value that's
// implemented by the engine.
// https://github.com/flutter/flutter/issues/100394
engine.AssetManager get webOnlyAssetManager => engine.assetManager;
// TODO(mdebbar): Deprecate this and remove it.
// https://github.com/flutter/flutter/issues/127395
ui_web.AssetManager get webOnlyAssetManager {
assert(() {
engine.printWarning(
'The webOnlyAssetManager getter is deprecated and will be removed in a '
'future release. Please use `assetManager` from `dart:ui_web` instead.',
);
return true;
}());
return ui_web.assetManager;
}

/// Sets the handler that forwards platform messages to web plugins.
///
Expand Down
1 change: 0 additions & 1 deletion lib/web_ui/lib/src/engine.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ library engine;

export 'engine/alarm_clock.dart';
export 'engine/app_bootstrap.dart';
export 'engine/assets.dart';
export 'engine/browser_detection.dart';
export 'engine/canvas_pool.dart';
export 'engine/canvaskit/canvas.dart';
Expand Down
3 changes: 2 additions & 1 deletion lib/web_ui/lib/src/engine/canvaskit/fonts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:async';
import 'dart:typed_data';

import 'package:ui/src/engine.dart';
import 'package:ui/ui_web/src/ui_web.dart' as ui_web;

// This URL was found by using the Google Fonts Developer API to find the URL
// for Roboto. The API warns that this URL is not stable. In order to update
Expand Down Expand Up @@ -109,7 +110,7 @@ class SkiaFontCollection implements FlutterFontCollection {
loadedRoboto = true;
}
for (final FontAsset fontAsset in family.fontAssets) {
final String url = assetManager.getAssetUrl(fontAsset.asset);
final String url = ui_web.assetManager.getAssetUrl(fontAsset.asset);
pendingDownloads.add(_downloadFont(fontAsset.asset, url, family.name));
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/web_ui/lib/src/engine/canvaskit/renderer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'dart:typed_data';

import 'package:ui/src/engine.dart';
import 'package:ui/ui.dart' as ui;
import 'package:ui/ui_web/src/ui_web.dart' as ui_web;

enum CanvasKitVariant {
/// The appropriate variant is chosen based on the browser.
Expand Down Expand Up @@ -384,7 +385,7 @@ class CanvasKitRenderer implements Renderer {
if (_programs.containsKey(assetKey)) {
return _programs[assetKey]!;
}
return _programs[assetKey] = assetManager.load(assetKey).then((ByteData data) {
return _programs[assetKey] = ui_web.assetManager.load(assetKey).then((ByteData data) {
return CkFragmentProgram.fromBytes(assetKey, data.buffer.asUint8List());
});
}
Expand Down
5 changes: 3 additions & 2 deletions lib/web_ui/lib/src/engine/fonts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'dart:js_interop';
import 'dart:typed_data';

import 'package:ui/src/engine.dart';
import 'package:ui/ui_web/src/ui_web.dart' as ui_web;

class FontAsset {
FontAsset(this.asset, this.descriptors);
Expand All @@ -29,8 +30,8 @@ class FontManifest {
final List<FontFamily> families;
}

Future<FontManifest> fetchFontManifest(AssetManager assetManager) async {
final HttpFetchResponse response = await assetManager.loadAsset('FontManifest.json');
Future<FontManifest> fetchFontManifest(ui_web.AssetManager assetManager) async {
final HttpFetchResponse response = await assetManager.loadAsset('FontManifest.json') as HttpFetchResponse;
if (!response.hasPayload) {
printWarning('Font manifest does not exist at `${response.url}` - ignoring.');
return FontManifest(<FontFamily>[]);
Expand Down
15 changes: 8 additions & 7 deletions lib/web_ui/lib/src/engine/initialization.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'dart:js_interop';

import 'package:ui/src/engine.dart';
import 'package:ui/ui.dart' as ui;
import 'package:ui/ui_web/src/ui_web.dart' as ui_web;
import 'package:web_test_fonts/web_test_fonts.dart';

/// The mode the app is running in.
Expand Down Expand Up @@ -56,7 +57,7 @@ void debugEmulateHotRestart() {

/// Fully initializes the engine, including services and UI.
Future<void> initializeEngine({
AssetManager? assetManager,
ui_web.AssetManager? assetManager,
}) async {
await initializeEngineServices(assetManager: assetManager);
await initializeEngineUi();
Expand Down Expand Up @@ -114,7 +115,7 @@ void debugResetEngineInitializationState() {
/// * [initializeEngineUi], which is typically called after this function, and
/// puts UI elements on the page.
Future<void> initializeEngineServices({
AssetManager? assetManager,
ui_web.AssetManager? assetManager,
JsFlutterConfiguration? jsConfiguration
}) async {
if (_initializationState != DebugEngineInitializationState.uninitialized) {
Expand Down Expand Up @@ -193,7 +194,7 @@ Future<void> initializeEngineServices({
}
};

assetManager ??= AssetManager(assetBase: configuration.assetBase);
assetManager ??= ui_web.AssetManager(assetBase: configuration.assetBase);
_setAssetManager(assetManager);

Future<void> initializeRendererCallback () async => renderer.initialize();
Expand Down Expand Up @@ -229,10 +230,10 @@ Future<void> initializeEngineUi() async {
_initializationState = DebugEngineInitializationState.initialized;
}

AssetManager get assetManager => _assetManager!;
AssetManager? _assetManager;
ui_web.AssetManager get engineAssetManager => _assetManager!;
ui_web.AssetManager? _assetManager;

void _setAssetManager(AssetManager assetManager) {
void _setAssetManager(ui_web.AssetManager assetManager) {
if (assetManager == _assetManager) {
return;
}
Expand All @@ -253,7 +254,7 @@ Future<void> _downloadAssetFonts() async {
}

if (_assetManager != null) {
await renderer.fontCollection.loadAssetFonts(await fetchFontManifest(assetManager));
await renderer.fontCollection.loadAssetFonts(await fetchFontManifest(ui_web.assetManager));
}
}

Expand Down
18 changes: 3 additions & 15 deletions lib/web_ui/lib/src/engine/platform_dispatcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,10 @@ import 'dart:convert';
import 'dart:js_interop';
import 'dart:typed_data';

import 'package:ui/src/engine/canvaskit/renderer.dart';
import 'package:ui/src/engine/renderer.dart';
import 'package:ui/ui.dart' as ui;
import 'package:ui/ui_web/src/ui_web.dart' as ui_web;

import '../engine.dart' show flutterViewEmbedder, platformViewManager, registerHotRestartListener;
import 'clipboard.dart';
import 'dom.dart';
import 'mouse_cursor.dart';
import 'platform_views/message_handler.dart';
import 'plugins.dart';
import 'safe_browser_api.dart';
import 'semantics.dart';
import 'services.dart';
import 'text_editing/text_editing.dart';
import 'util.dart';
import 'window.dart';
import '../engine.dart';

/// Requests that the browser schedule a frame.
///
Expand Down Expand Up @@ -688,7 +676,7 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {

Future<void> _handleFlutterAssetsMessage(String url, ui.PlatformMessageResponseCallback? callback) async {
try {
final HttpFetchResponse response = await ui.webOnlyAssetManager.loadAsset(url);
final HttpFetchResponse response = await ui_web.assetManager.loadAsset(url) as HttpFetchResponse;
final ByteBuffer assetData = await response.asByteBuffer();
replyToPlatformMessage(callback, assetData.asByteData());
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'dart:typed_data';

import 'package:ui/src/engine.dart';
import 'package:ui/src/engine/skwasm/skwasm_impl.dart';
import 'package:ui/ui_web/src/ui_web.dart' as ui_web;

// This URL was found by using the Google Fonts Developer API to find the URL
// for Roboto. The API warns that this URL is not stable. In order to update
Expand Down Expand Up @@ -99,12 +100,12 @@ class SkwasmFontCollection implements FlutterFontCollection {
Future<FontLoadError?> _downloadFontAsset(FontAsset asset, String family) async {
final HttpFetchResponse response;
try {
response = await assetManager.loadAsset(asset.asset);
response = await ui_web.assetManager.loadAsset(asset.asset) as HttpFetchResponse;
} catch (error) {
return FontDownloadError(assetManager.getAssetUrl(asset.asset), error);
return FontDownloadError(ui_web.assetManager.getAssetUrl(asset.asset), error);
}
if (!response.hasPayload) {
return FontNotFoundError(assetManager.getAssetUrl(asset.asset));
return FontNotFoundError(ui_web.assetManager.getAssetUrl(asset.asset));
}
int length = 0;
final List<JSUint8Array> chunks = <JSUint8Array>[];
Expand All @@ -128,7 +129,7 @@ class SkwasmFontCollection implements FlutterFontCollection {
skStringFree(familyNameHandle);
return null;
} else {
return FontInvalidDataError(assetManager.getAssetUrl(asset.asset));
return FontInvalidDataError(ui_web.assetManager.getAssetUrl(asset.asset));
}
}

Expand Down
3 changes: 2 additions & 1 deletion lib/web_ui/lib/src/engine/skwasm/skwasm_impl/renderer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'dart:typed_data';
import 'package:ui/src/engine.dart';
import 'package:ui/src/engine/skwasm/skwasm_impl.dart';
import 'package:ui/ui.dart' as ui;
import 'package:ui/ui_web/src/ui_web.dart' as ui_web;

class SkwasmRenderer implements Renderer {
late DomCanvasElement sceneElement;
Expand Down Expand Up @@ -438,7 +439,7 @@ class SkwasmRenderer implements Renderer {
if (_programs.containsKey(assetKey)) {
return _programs[assetKey]!;
}
return _programs[assetKey] = assetManager.load(assetKey).then((ByteData data) {
return _programs[assetKey] = ui_web.assetManager.load(assetKey).then((ByteData data) {
return SkwasmFragmentProgram.fromBytes(assetKey, data.buffer.asUint8List());
});
}
Expand Down
3 changes: 2 additions & 1 deletion lib/web_ui/lib/src/engine/skwasm/skwasm_stub/renderer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'dart:typed_data';

import 'package:ui/src/engine.dart';
import 'package:ui/ui.dart' as ui;
import 'package:ui/ui_web/src/ui_web.dart' as ui_web;

class SkwasmRenderer implements Renderer {
@override
Expand Down Expand Up @@ -171,7 +172,7 @@ class SkwasmRenderer implements Renderer {
if (_programs.containsKey(assetKey)) {
return _programs[assetKey]!;
}
return _programs[assetKey] = assetManager.load(assetKey).then((ByteData data) {
return _programs[assetKey] = ui_web.assetManager.load(assetKey).then((ByteData data) {
return CkFragmentProgram.fromBytes(assetKey, data.buffer.asUint8List());
});
}
Expand Down
5 changes: 3 additions & 2 deletions lib/web_ui/lib/src/engine/text/font_collection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'dart:async';
import 'dart:typed_data';

import 'package:ui/src/engine.dart';
import 'package:ui/ui_web/src/ui_web.dart' as ui_web;

/// This class is responsible for registering and loading fonts.
///
Expand All @@ -14,7 +15,7 @@ import 'package:ui/src/engine.dart';
/// font manifest. If test fonts are enabled, then call
/// [debugDownloadTestFonts] as well.
class HtmlFontCollection implements FlutterFontCollection {
/// Reads the font manifest using the [assetManager] and downloads all of the
/// Reads the font manifest using the [ui_web.assetManager] and downloads all of the
/// fonts declared within.
@override
Future<AssetFontsResult> loadAssetFonts(FontManifest manifest) async {
Expand Down Expand Up @@ -149,7 +150,7 @@ class HtmlFontCollection implements FlutterFontCollection {
) async {
// try/catch because `new FontFace` can crash with an improper font family.
try {
final DomFontFace fontFace = createDomFontFace(family, 'url(${assetManager.getAssetUrl(asset)})', descriptors);
final DomFontFace fontFace = createDomFontFace(family, 'url(${ui_web.assetManager.getAssetUrl(asset)})', descriptors);
return await fontFace.load();
} catch (e) {
printWarning('Error while loading font family "$family":\n$e');
Expand Down
1 change: 1 addition & 0 deletions lib/web_ui/lib/ui_web/src/ui_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// ignore: unnecessary_library_directive
library ui_web;

export 'ui_web/asset_manager.dart';
export 'ui_web/navigation/platform_location.dart';
export 'ui_web/navigation/url_strategy.dart';
export 'ui_web/platform_view_registry.dart';
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import 'dart:convert';
import 'dart:typed_data';

import 'dom.dart';
import 'util.dart';
import 'package:ui/src/engine.dart';

/// Provides the [AssetManager] used by the Flutter Engine.
AssetManager get assetManager => engineAssetManager;

/// This class downloads assets over the network.
///
Expand Down Expand Up @@ -79,11 +81,11 @@ class AssetManager {
}

/// Loads an asset and returns the server response.
Future<HttpFetchResponse> loadAsset(String asset) {
Future<Object> loadAsset(String asset) {
return httpFetch(getAssetUrl(asset));
}

/// Loads an asset using an [DomXMLHttpRequest] and returns data as [ByteData].
/// Loads an asset using an [XMLHttpRequest] and returns data as [ByteData].
Future<ByteData> load(String asset) async {
final String url = getAssetUrl(asset);
final HttpFetchResponse response = await httpFetch(url);
Expand Down
3 changes: 2 additions & 1 deletion lib/web_ui/test/common/fake_asset_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import 'dart:convert';
import 'dart:typed_data';

import 'package:ui/src/engine.dart';
import 'package:ui/ui_web/src/ui_web.dart' as ui_web;

class FakeAssetManager implements AssetManager {
class FakeAssetManager implements ui_web.AssetManager {
FakeAssetManager();

@override
Expand Down
Loading