Skip to content

Commit 7819cee

Browse files
authored
[web] DRY up access to headers required for multi-threaded WebAssembly (#163555)
1 parent e0617ac commit 7819cee

4 files changed

Lines changed: 20 additions & 19 deletions

File tree

packages/flutter_tools/lib/src/isolated/devfs_web.dart

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import '../web/chrome.dart';
4141
import '../web/compile.dart';
4242
import '../web/memory_fs.dart';
4343
import '../web/module_metadata.dart';
44+
import '../web/web_constants.dart';
4445
import '../web_template.dart';
4546

4647
typedef DwdsLauncher =
@@ -1315,11 +1316,8 @@ class ReleaseAssetServer {
13151316
'Content-Type': mimeType,
13161317
'Cross-Origin-Resource-Policy': 'cross-origin',
13171318
'Access-Control-Allow-Origin': '*',
1318-
if (_needsCoopCoep &&
1319-
_fileSystem.path.extension(file.path) == '.html') ...<String, String>{
1320-
'Cross-Origin-Opener-Policy': 'same-origin',
1321-
'Cross-Origin-Embedder-Policy': 'credentialless',
1322-
},
1319+
if (_needsCoopCoep && _fileSystem.path.extension(file.path) == '.html')
1320+
...kMultiThreadedHeaders,
13231321
},
13241322
);
13251323
}
@@ -1329,10 +1327,7 @@ class ReleaseAssetServer {
13291327
file.readAsBytesSync(),
13301328
headers: <String, String>{
13311329
'Content-Type': 'text/html',
1332-
if (_needsCoopCoep) ...<String, String>{
1333-
'Cross-Origin-Opener-Policy': 'same-origin',
1334-
'Cross-Origin-Embedder-Policy': 'credentialless',
1335-
},
1330+
if (_needsCoopCoep) ...kMultiThreadedHeaders,
13361331
},
13371332
);
13381333
}

packages/flutter_tools/lib/src/test/flutter_web_platform.dart

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import '../web/bootstrap.dart';
3232
import '../web/chrome.dart';
3333
import '../web/compile.dart';
3434
import '../web/memory_fs.dart';
35+
import '../web/web_constants.dart';
3536
import 'test_compiler.dart';
3637
import 'test_golden_comparator.dart';
3738
import 'test_time_recorder.dart';
@@ -57,10 +58,7 @@ shelf.Handler createDirectoryHandler(Directory directory, {required bool crossOr
5758
file.openRead(),
5859
headers: <String, String>{
5960
if (contentType != null) 'Content-Type': contentType,
60-
if (needsCrossOriginIsolated) ...<String, String>{
61-
'Cross-Origin-Opener-Policy': 'same-origin',
62-
'Cross-Origin-Embedder-Policy': 'credentialless',
63-
},
61+
if (needsCrossOriginIsolated) ...kMultiThreadedHeaders,
6462
},
6563
);
6664
};
@@ -551,10 +549,7 @@ class FlutterWebPlatform extends PlatformPlugin {
551549
''',
552550
headers: <String, String>{
553551
'Content-Type': 'text/html',
554-
if (webRenderer == WebRendererMode.skwasm) ...<String, String>{
555-
'Cross-Origin-Opener-Policy': 'same-origin',
556-
'Cross-Origin-Embedder-Policy': 'credentialless',
557-
},
552+
if (webRenderer == WebRendererMode.skwasm) ...kMultiThreadedHeaders,
558553
},
559554
);
560555
}

packages/flutter_tools/lib/src/web/web_constants.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,12 @@
33
// found in the LICENSE file.
44

55
const String kWasmMoreInfo = 'See https://flutter.dev/to/wasm for more information.';
6+
7+
/// Headers required to run Wasm-compiled applications with multi-threading.
8+
///
9+
/// See https://developer.chrome.com/blog/coep-credentialless-origin-trial
10+
/// for more information.
11+
const Map<String, String> kMultiThreadedHeaders = <String, String>{
12+
'Cross-Origin-Opener-Policy': 'same-origin',
13+
'Cross-Origin-Embedder-Policy': 'credentialless',
14+
};

packages/flutter_tools/test/general.shard/web/web_asset_server_test.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import 'package:flutter_tools/src/base/file_system.dart';
77
import 'package:flutter_tools/src/base/io.dart';
88
import 'package:flutter_tools/src/base/platform.dart';
99
import 'package:flutter_tools/src/isolated/devfs_web.dart';
10+
import 'package:flutter_tools/src/web/web_constants.dart';
1011
import 'package:shelf/shelf.dart';
1112

1213
import '../../src/common.dart';
@@ -236,8 +237,9 @@ void main() {
236237

237238
expect(response.statusCode, HttpStatus.ok);
238239
final Map<String, String> headers = response.headers;
239-
expect(headers['Cross-Origin-Opener-Policy'], 'same-origin');
240-
expect(headers['Cross-Origin-Embedder-Policy'], 'credentialless');
240+
for (final MapEntry<String, String> entry in kMultiThreadedHeaders.entries) {
241+
expect(headers, containsPair(entry.key, entry.value));
242+
}
241243
},
242244
);
243245

0 commit comments

Comments
 (0)