Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
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
21 changes: 16 additions & 5 deletions lib/web_ui/lib/src/engine/platform_dispatcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,20 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
final MethodCall decoded = codec.decodeMethodCall(data);
switch (decoded.method) {
case 'Skia.setResourceCacheMaxBytes':
if (decoded.arguments is int) {
rasterizer?.setSkiaResourceCacheMaxBytes(decoded.arguments);
if (useCanvasKit) {
// If we're in CanvasKit mode, we must also have a rasterizer.
assert(rasterizer != null);
assert(
decoded.arguments is int,
'Argument to Skia.setResourceCacheMaxBytes must be an int, but was ${decoded.arguments.runtimeType}',
);
final int cacheSizeInBytes = decoded.arguments as int;
rasterizer!.setSkiaResourceCacheMaxBytes(cacheSizeInBytes);
}

// Also respond in HTML mode. Otherwise, apps would have to detect
// CanvasKit vs HTML before invoking this method.
_replyToPlatformMessage(callback, codec.encodeSuccessEnvelope([true]));
break;
}
return;
Expand Down Expand Up @@ -896,9 +907,9 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
/// messages and responses have to be exchanged asynchronously. We simulate
/// that by adding a zero-length delay to the reply.
void _replyToPlatformMessage(
ui.PlatformMessageResponseCallback? callback,
ByteData? data,
) {
ui.PlatformMessageResponseCallback? callback,
ByteData? data,
) {
Future<void>.delayed(Duration.zero).then((_) {
if (callback != null) {
callback(data);
Expand Down
46 changes: 46 additions & 0 deletions lib/web_ui/test/canvaskit/platform_dispatcher_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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.

// @dart = 2.12
import 'dart:async';
import 'dart:typed_data';

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

import 'package:test/bootstrap/browser.dart';
import 'package:test/test.dart';

import 'common.dart';

void main() {
internalBootstrapBrowserTest(() => testMain);
}

void testMain() {
group('PlatformDispatcher', () {
setUpCanvasKitTest();

test('responds to flutter/skia Skia.setResourceCacheMaxBytes', () async {
const MethodCodec codec = JSONMethodCodec();
final Completer<ByteData?> completer = Completer<ByteData?>();
ui.PlatformDispatcher.instance.sendPlatformMessage(
'flutter/skia',
codec.encodeMethodCall(MethodCall(
'Skia.setResourceCacheMaxBytes',
512 * 1000 * 1000,
)),
completer.complete,
);

final ByteData? response = await completer.future;
expect(response, isNotNull);
expect(
codec.decodeEnvelope(response!),
[true],
);
});
// TODO: https://github.com/flutter/flutter/issues/60040
}, skip: isIosSafari);
}
41 changes: 41 additions & 0 deletions lib/web_ui/test/engine/platform_dispatcher_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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.

// @dart = 2.12
import 'dart:async';
import 'dart:typed_data';

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

import 'package:test/bootstrap/browser.dart';
import 'package:test/test.dart';

void main() {
internalBootstrapBrowserTest(() => testMain);
}

void testMain() {
group('PlatformDispatcher', () {
test('responds to flutter/skia Skia.setResourceCacheMaxBytes', () async {
const MethodCodec codec = JSONMethodCodec();
final Completer<ByteData?> completer = Completer<ByteData?>();
ui.PlatformDispatcher.instance.sendPlatformMessage(
'flutter/skia',
codec.encodeMethodCall(MethodCall(
'Skia.setResourceCacheMaxBytes',
512 * 1000 * 1000,
)),
completer.complete,
);

final ByteData? response = await completer.future;
expect(response, isNotNull);
expect(
codec.decodeEnvelope(response!),
[true],
);
});
});
}