[web] Migrate Flutter Web DOM usage to JS static interop - 44. (flutter/engine#34379)
This commit is contained in:
@@ -49,6 +49,13 @@ extension DomWindowExtension on DomWindow {
|
||||
]) as DomCSSStyleDeclaration;
|
||||
external DomScreen? get screen;
|
||||
external int requestAnimationFrame(DomRequestAnimationFrameCallback callback);
|
||||
void postMessage(Object message, String targetOrigin,
|
||||
[List<DomMessagePort>? messagePorts]) =>
|
||||
js_util.callMethod(this, 'postMessage', <Object>[
|
||||
message,
|
||||
targetOrigin,
|
||||
if (messagePorts != null) js_util.jsify(messagePorts)
|
||||
]);
|
||||
}
|
||||
|
||||
typedef DomRequestAnimationFrameCallback = void Function(num highResTime);
|
||||
@@ -936,6 +943,8 @@ extension DomLocationExtension on DomLocation {
|
||||
external String? get search;
|
||||
// We have to change the name here because 'hash' is inherited from [Object].
|
||||
String get locationHash => js_util.getProperty(this, 'hash');
|
||||
external String get origin;
|
||||
external String get href;
|
||||
}
|
||||
|
||||
@JS()
|
||||
@@ -1132,8 +1141,8 @@ extension DomCompositionEventExtension on DomCompositionEvent {
|
||||
external String? get data;
|
||||
}
|
||||
|
||||
DomCompositionEvent createDomCompositionEvent(String type, [Map<dynamic,
|
||||
dynamic>? options]) =>
|
||||
DomCompositionEvent createDomCompositionEvent(String type,
|
||||
[Map<dynamic, dynamic>? options]) =>
|
||||
js_util.callConstructor(domGetConstructor('CompositionEvent')!,
|
||||
<Object>[type, if (options != null) js_util.jsify(options)]);
|
||||
|
||||
@@ -1174,6 +1183,7 @@ class DomTokenList {}
|
||||
|
||||
extension DomTokenListExtension on DomTokenList {
|
||||
external void add(String value);
|
||||
external void remove(String value);
|
||||
external bool contains(String token);
|
||||
}
|
||||
|
||||
@@ -1317,6 +1327,64 @@ class DomPoint {
|
||||
DomPoint(this.x, this.y);
|
||||
}
|
||||
|
||||
@JS()
|
||||
@staticInterop
|
||||
class DomWebSocket extends DomEventTarget {}
|
||||
|
||||
extension DomWebSocketExtension on DomWebSocket {
|
||||
external void send(Object? data);
|
||||
}
|
||||
|
||||
DomWebSocket createDomWebSocket(String url) =>
|
||||
domCallConstructorString('WebSocket', <Object>[url])! as DomWebSocket;
|
||||
|
||||
@JS()
|
||||
@staticInterop
|
||||
class DomMessageEvent extends DomEvent {}
|
||||
|
||||
extension DomMessageEventExtension on DomMessageEvent {
|
||||
dynamic get data => js_util.dartify(js_util.getProperty(this, 'data'));
|
||||
external String get origin;
|
||||
}
|
||||
|
||||
@JS()
|
||||
@staticInterop
|
||||
class DomHTMLIFrameElement extends DomHTMLElement {}
|
||||
|
||||
extension DomHTMLIFrameElementExtension on DomHTMLIFrameElement {
|
||||
external set src(String? value);
|
||||
external String? get src;
|
||||
external set height(String? value);
|
||||
external set width(String? value);
|
||||
external DomWindow get contentWindow;
|
||||
}
|
||||
|
||||
DomHTMLIFrameElement createDomHTMLIFrameElement() =>
|
||||
domDocument.createElement('iframe') as DomHTMLIFrameElement;
|
||||
|
||||
@JS()
|
||||
@staticInterop
|
||||
class DomMessagePort extends DomEventTarget {}
|
||||
|
||||
extension DomMessagePortExtension on DomMessagePort {
|
||||
void postMessage(Object? message) => js_util.callMethod(this, 'postMessage',
|
||||
<Object>[if (message != null) js_util.jsify(message)]);
|
||||
external void start();
|
||||
}
|
||||
|
||||
@JS()
|
||||
@staticInterop
|
||||
class DomMessageChannel {}
|
||||
|
||||
extension DomMessageChannelExtension on DomMessageChannel {
|
||||
external DomMessagePort get port1;
|
||||
external DomMessagePort get port2;
|
||||
}
|
||||
|
||||
DomMessageChannel createDomMessageChannel() =>
|
||||
domCallConstructorString('MessageChannel', <Object>[])!
|
||||
as DomMessageChannel;
|
||||
|
||||
Object? domGetConstructor(String constructorName) =>
|
||||
js_util.getProperty(domWindow, constructorName);
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:html' as html;
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'package:meta/meta.dart';
|
||||
@@ -47,14 +46,16 @@ class HighContrastSupport {
|
||||
final List<HighContrastListener> _listeners = <HighContrastListener>[];
|
||||
|
||||
/// Reference to css media query that indicates whether high contrast is on.
|
||||
final html.MediaQueryList _highContrastMediaQuery = html.window.matchMedia(_highContrastMediaQueryString);
|
||||
final DomMediaQueryList _highContrastMediaQuery = domWindow.matchMedia(_highContrastMediaQueryString);
|
||||
late final DomEventListener _onHighContrastChangeListener =
|
||||
allowInterop(_onHighContrastChange);
|
||||
|
||||
bool get isHighContrastEnabled => _highContrastMediaQuery.matches;
|
||||
|
||||
/// Adds function to the list of listeners on high contrast changes
|
||||
void addListener(HighContrastListener listener) {
|
||||
if (_listeners.isEmpty) {
|
||||
_highContrastMediaQuery.addListener(_onHighContrastChange);
|
||||
_highContrastMediaQuery.addListener(_onHighContrastChangeListener);
|
||||
}
|
||||
_listeners.add(listener);
|
||||
}
|
||||
@@ -63,12 +64,12 @@ class HighContrastSupport {
|
||||
void removeListener(HighContrastListener listener) {
|
||||
_listeners.remove(listener);
|
||||
if (_listeners.isEmpty) {
|
||||
_highContrastMediaQuery.removeListener(_onHighContrastChange);
|
||||
_highContrastMediaQuery.removeListener(_onHighContrastChangeListener);
|
||||
}
|
||||
}
|
||||
|
||||
void _onHighContrastChange(html.Event event) {
|
||||
final html.MediaQueryListEvent mqEvent = event as html.MediaQueryListEvent;
|
||||
void _onHighContrastChange(DomEvent event) {
|
||||
final DomMediaQueryListEvent mqEvent = event as DomMediaQueryListEvent;
|
||||
final bool isHighContrastEnabled = mqEvent.matches!;
|
||||
for (final HighContrastListener listener in _listeners) {
|
||||
listener(isHighContrastEnabled);
|
||||
@@ -542,7 +543,6 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
|
||||
_platformViewMessageHandler ??= PlatformViewMessageHandler(
|
||||
contentManager: platformViewManager,
|
||||
contentHandler: (DomElement content) {
|
||||
// Remove cast to [html.Element] after migration.
|
||||
flutterViewEmbedder.glassPaneElement!.append(content);
|
||||
},
|
||||
);
|
||||
|
||||
@@ -42,7 +42,6 @@ import 'dart:async';
|
||||
import 'dart:collection';
|
||||
import 'dart:convert' hide Codec;
|
||||
import 'dart:developer' as developer;
|
||||
import 'dart:html' as html;
|
||||
import 'dart:js' as js;
|
||||
import 'dart:js_util' as js_util;
|
||||
import 'dart:_js_annotations';
|
||||
|
||||
@@ -64,8 +64,6 @@ export 'foo.dart';
|
||||
final _CheckResult result = _checkFile(
|
||||
File('lib/web_ui/lib/src/engine/alarm_clock.dart'),
|
||||
'''
|
||||
import 'dart:html'
|
||||
show HtmlElement;
|
||||
import 'dart:async';
|
||||
import 'package:ui/ui.dart'
|
||||
as ui;
|
||||
@@ -73,8 +71,7 @@ import 'package:ui/ui.dart'
|
||||
);
|
||||
expect(result.failed, isTrue);
|
||||
expect(result.violations, <String>[
|
||||
"on line 1: import is broken up into multiple lines: import 'dart:html'",
|
||||
"on line 4: import is broken up into multiple lines: import 'package:ui/ui.dart'",
|
||||
"on line 2: import is broken up into multiple lines: import 'package:ui/ui.dart'",
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
@@ -30,7 +30,6 @@ import 'dart:async';
|
||||
import 'dart:collection';
|
||||
import 'dart:convert' hide Codec;
|
||||
import 'dart:developer' as developer;
|
||||
import 'dart:html' as html;
|
||||
import 'dart:js' as js;
|
||||
import 'dart:js_util' as js_util;
|
||||
import 'dart:_js_annotations';
|
||||
|
||||
@@ -4,14 +4,16 @@
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:html' as html;
|
||||
|
||||
import 'package:test/test.dart';
|
||||
// ignore: implementation_imports
|
||||
import 'package:ui/src/engine.dart' show operatingSystem, OperatingSystem, useCanvasKit;
|
||||
// ignore: implementation_imports
|
||||
import 'package:ui/src/engine/dom.dart';
|
||||
import 'package:ui/ui.dart';
|
||||
|
||||
Future<dynamic> _callScreenshotServer(dynamic requestData) async {
|
||||
final html.HttpRequest request = await html.HttpRequest.request(
|
||||
final DomXMLHttpRequest request = await domHttpRequest(
|
||||
'screenshot',
|
||||
method: 'POST',
|
||||
sendData: json.encode(requestData),
|
||||
|
||||
@@ -7,11 +7,12 @@ library test.host;
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:html';
|
||||
|
||||
import 'package:js/js.dart';
|
||||
import 'package:stack_trace/stack_trace.dart';
|
||||
import 'package:stream_channel/stream_channel.dart';
|
||||
// ignore: implementation_imports
|
||||
import 'package:ui/src/engine/dom.dart';
|
||||
|
||||
/// A class defined in content shell, used to control its behavior.
|
||||
@JS()
|
||||
@@ -46,13 +47,14 @@ class _JSApi {
|
||||
external set _jsApi(_JSApi api);
|
||||
|
||||
/// The iframes created for each loaded test suite, indexed by the suite id.
|
||||
final Map<int, IFrameElement> _iframes = <int, IFrameElement>{};
|
||||
final Map<int, DomHTMLIFrameElement> _iframes = <int, DomHTMLIFrameElement>{};
|
||||
|
||||
/// Subscriptions created for each loaded test suite, indexed by the suite id.
|
||||
final Map<int, List<StreamSubscription<dynamic>>> _subscriptions = <int, List<StreamSubscription<dynamic>>>{};
|
||||
final Map<int, List<DomSubscription>> _domSubscriptions = <int, List<DomSubscription>>{};
|
||||
final Map<int, List<StreamSubscription<dynamic>>> _streamSubscriptions =<int, List<StreamSubscription<dynamic>>>{};
|
||||
|
||||
/// The URL for the current page.
|
||||
final Uri _currentUrl = Uri.parse(window.location.href);
|
||||
final Uri _currentUrl = Uri.parse(domWindow.location.href);
|
||||
|
||||
/// Code that runs in the browser and loads test suites at the server's behest.
|
||||
///
|
||||
@@ -111,7 +113,7 @@ void main() {
|
||||
testRunner?.waitUntilDone();
|
||||
|
||||
if (_currentUrl.queryParameters['debug'] == 'true') {
|
||||
document.body!.classes.add('debug');
|
||||
domDocument.body!.classList.add('debug');
|
||||
}
|
||||
|
||||
runZonedGuarded(
|
||||
@@ -126,14 +128,17 @@ void main() {
|
||||
final StreamChannel<dynamic> iframeChannel = _connectToIframe(url, messageId);
|
||||
suiteChannel.pipe(iframeChannel);
|
||||
} else if (message['command'] == 'displayPause') {
|
||||
document.body!.classes.add('paused');
|
||||
domDocument.body!.classList.add('paused');
|
||||
} else if (message['command'] == 'resume') {
|
||||
document.body!.classes.remove('paused');
|
||||
domDocument.body!.classList.remove('paused');
|
||||
} else {
|
||||
assert(message['command'] == 'closeSuite');
|
||||
_iframes.remove(message['id'])!.remove();
|
||||
|
||||
for (final StreamSubscription<dynamic> subscription in _subscriptions.remove(message['id'])!) {
|
||||
for (final DomSubscription subscription in _domSubscriptions.remove(message['id'])!) {
|
||||
subscription.cancel();
|
||||
}
|
||||
for (final StreamSubscription<dynamic> subscription in _streamSubscriptions.remove(message['id'])!) {
|
||||
subscription.cancel();
|
||||
}
|
||||
}
|
||||
@@ -145,9 +150,10 @@ void main() {
|
||||
(_) => serverChannel.sink.add(<String, String>{'command': 'ping'}));
|
||||
|
||||
_jsApi = _JSApi(resume: allowInterop(() {
|
||||
if (!document.body!.classes.remove('paused')) {
|
||||
if (!domDocument.body!.classList.contains('paused')) {
|
||||
return;
|
||||
}
|
||||
domDocument.body!.classList.remove('paused');
|
||||
serverChannel.sink.add(<String, String>{'command': 'resume'});
|
||||
}), restartCurrent: allowInterop(() {
|
||||
serverChannel.sink.add(<String, String>{'command': 'restart'});
|
||||
@@ -164,13 +170,13 @@ void main() {
|
||||
MultiChannel<dynamic> _connectToServer() {
|
||||
// The `managerUrl` query parameter contains the WebSocket URL of the remote
|
||||
// [BrowserManager] with which this communicates.
|
||||
final WebSocket webSocket = WebSocket(_currentUrl.queryParameters['managerUrl']!);
|
||||
final DomWebSocket webSocket = createDomWebSocket(_currentUrl.queryParameters['managerUrl']!);
|
||||
|
||||
final StreamChannelController<dynamic> controller = StreamChannelController<dynamic>(sync: true);
|
||||
webSocket.onMessage.listen((MessageEvent message) {
|
||||
final String data = message.data as String;
|
||||
webSocket.addEventListener('message', allowInterop((DomEvent message) {
|
||||
final String data = (message as DomMessageEvent).data as String;
|
||||
controller.local.sink.add(jsonDecode(data));
|
||||
});
|
||||
}));
|
||||
|
||||
controller.local.stream
|
||||
.listen((dynamic message) => webSocket.send(jsonEncode(message)));
|
||||
@@ -183,31 +189,34 @@ MultiChannel<dynamic> _connectToServer() {
|
||||
///
|
||||
/// [id] identifies the suite loaded in this iframe.
|
||||
StreamChannel<dynamic> _connectToIframe(String url, int id) {
|
||||
final IFrameElement iframe = IFrameElement();
|
||||
final DomHTMLIFrameElement iframe = createDomHTMLIFrameElement();
|
||||
_iframes[id] = iframe;
|
||||
iframe
|
||||
..src = url
|
||||
..width = '1000'
|
||||
..height = '1000';
|
||||
document.body!.children.add(iframe);
|
||||
domDocument.body!.appendChild(iframe);
|
||||
|
||||
// Use this to communicate securely with the iframe.
|
||||
final MessageChannel channel = MessageChannel();
|
||||
final DomMessageChannel channel = createDomMessageChannel();
|
||||
final StreamChannelController<dynamic> controller = StreamChannelController<dynamic>(sync: true);
|
||||
|
||||
// Use this to avoid sending a message to the iframe before it's sent a
|
||||
// message to us. This ensures that no messages get dropped on the floor.
|
||||
final Completer<dynamic> readyCompleter = Completer<dynamic>();
|
||||
|
||||
final List<StreamSubscription<dynamic>> subscriptions = <StreamSubscription<dynamic>>[];
|
||||
_subscriptions[id] = subscriptions;
|
||||
|
||||
subscriptions.add(window.onMessage.listen((dynamic message) {
|
||||
final List<DomSubscription> domSubscriptions = <DomSubscription>[];
|
||||
final List<StreamSubscription<dynamic>> streamSubscriptions = <StreamSubscription<dynamic>>[];
|
||||
_domSubscriptions[id] = domSubscriptions;
|
||||
_streamSubscriptions[id] = streamSubscriptions;
|
||||
domSubscriptions.add(DomSubscription(domWindow, 'message',
|
||||
allowInterop((DomEvent event) {
|
||||
final DomMessageEvent message = event as DomMessageEvent;
|
||||
// A message on the Window can theoretically come from any website. It's
|
||||
// very unlikely that a malicious site would care about hacking someone's
|
||||
// unit tests, let alone be able to find the test server while it's
|
||||
// running, but it's good practice to check the origin anyway.
|
||||
if (message.origin != window.location.origin) {
|
||||
if (message.origin != domWindow.location.origin) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -220,20 +229,24 @@ StreamChannel<dynamic> _connectToIframe(String url, int id) {
|
||||
if (message.data['ready'] == true) {
|
||||
// This message indicates that the iframe is actively listening for
|
||||
// events, so the message channel's second port can now be transferred.
|
||||
iframe.contentWindow!.postMessage('port', window.location.origin, <MessagePort>[channel.port2]);
|
||||
channel.port2.start();
|
||||
iframe.contentWindow.postMessage('port', domWindow.location.origin,
|
||||
<DomMessagePort>[channel.port2]);
|
||||
readyCompleter.complete();
|
||||
} else if (message.data['exception'] == true) {
|
||||
// This message from `dart.js` indicates that an exception occurred
|
||||
// loading the test.
|
||||
controller.local.sink.add(message.data['data']);
|
||||
}
|
||||
}));
|
||||
})));
|
||||
|
||||
subscriptions.add(channel.port1.onMessage.listen((dynamic message) {
|
||||
controller.local.sink.add(message.data['data']);
|
||||
}));
|
||||
channel.port1.start();
|
||||
domSubscriptions.add(DomSubscription(channel.port1, 'message',
|
||||
allowInterop((DomEvent message) {
|
||||
controller.local.sink.add((message as DomMessageEvent).data['data']);
|
||||
})));
|
||||
|
||||
subscriptions.add(controller.local.stream.listen((dynamic message) async {
|
||||
streamSubscriptions.add(controller.local.stream.listen((dynamic message) async {
|
||||
await readyCompleter.future;
|
||||
channel.port1.postMessage(message);
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user