Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Closed
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
25 changes: 25 additions & 0 deletions lib/web_ui/lib/src/engine/dom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3027,6 +3027,31 @@ extension DomMessageEventExtension on DomMessageEvent {
@JS('origin')
external JSString get _origin;
String get origin => _origin.toDart;

/// The source may be a `WindowProxy`, a `MessagePort`, or a `ServiceWorker`.
///
/// When a message is sent from an iframe through `window.parent.postMessage`
/// the source will be a `WindowProxy` which has the same methods as [Window].
DomMessageEventSource get source => js_util.getProperty(this, 'source');

List<DomMessagePort> get ports =>
js_util.getProperty<List<Object?>>(this, 'ports').cast<DomMessagePort>();
}

@JS()
@staticInterop
class DomMessageEventSource {}

extension DomMEssageEventSourceExtension on DomMessageEventSource {
external DomMessageEventLocation? get location;
}

@JS()
@staticInterop
class DomMessageEventLocation {}

extension DomMessageEventSourceExtension on DomMessageEventLocation {
external String? get href;
}

@JS()
Expand Down
4 changes: 2 additions & 2 deletions lib/web_ui/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dev_dependencies:
http: 1.1.0
http_multi_server: any
image: 3.0.1
matcher: 0.12.14
matcher: 0.12.16
package_config: any
path: 1.8.0
pool: any
Expand All @@ -38,7 +38,7 @@ dev_dependencies:
shelf_web_socket: any
stack_trace: any
stream_channel: 2.1.1
test: 1.22.1
test: 1.24.8
test_api: any
test_core: any
typed_data: any
Expand Down
2 changes: 1 addition & 1 deletion web_sdk/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dev_dependencies:
# up-to-date instead of a version from pub, which may not have the latest
# language features enabled.
analyzer: any
test: 1.22.1
test: 1.24.8

dependency_overrides: # Must include all transitive dependencies from the "any" packages above.
_fe_analyzer_shared:
Expand Down
43 changes: 21 additions & 22 deletions web_sdk/web_engine_tester/lib/static/host.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,8 @@ StreamChannel<dynamic> _connectToIframe(String url, int id) {
..height = '1000';
domDocument.body!.appendChild(iframe);

// Use this to communicate securely with the iframe.
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<DomSubscription> domSubscriptions = <DomSubscription>[];
final List<StreamSubscription<dynamic>> streamSubscriptions = <StreamSubscription<dynamic>>[];
_domSubscriptions[id] = domSubscriptions;
Expand All @@ -229,37 +223,42 @@ StreamChannel<dynamic> _connectToIframe(String url, int id) {
if (message.origin != domWindow.location.origin) {
return;
}

if (message.data['href'] != iframe.src) {
if (message.source.location?.href != iframe.src) {
return;
}

message.stopPropagation();

if (message.data['ready'] == true) {
if (message.data == 'port') {
final DomMessagePort port = message.ports.first;
domSubscriptions.add(
DomSubscription(port, 'message',(DomEvent event) {
controller.local.sink.add((event as DomMessageEvent).data);
}));
port.start();
streamSubscriptions.add(controller.local.stream.listen(port.postMessage));
} else 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.
final DomMessageChannel channel = createDomMessageChannel();
channel.port1.start();
channel.port2.start();
iframe.contentWindow.postMessage('port', domWindow.location.origin,
<DomMessagePort>[channel.port2]);
readyCompleter.complete();
iframe.contentWindow.postMessage(
'port', domWindow.location.origin, <DomMessagePort>[channel.port2]);
domSubscriptions
.add(DomSubscription(channel.port1, 'message', (DomEvent message) {
controller.local.sink.add((message as DomMessageEvent).data['data']);
}));

streamSubscriptions
.add(controller.local.stream.listen(channel.port1.postMessage));
} 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']);
}
}));

channel.port1.start();
domSubscriptions.add(DomSubscription(channel.port1, 'message',
(DomEvent message) {
controller.local.sink.add((message as DomMessageEvent).data['data']);
}));

streamSubscriptions.add(controller.local.stream.listen((dynamic message) async {
await readyCompleter.future;
channel.port1.postMessage(message);
}));

return controller.foreign;
}
2 changes: 1 addition & 1 deletion web_sdk/web_engine_tester/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ environment:
dependencies:
js: 0.6.4
stream_channel: 2.1.1
test: 1.22.1
test: 1.24.8
webkit_inspection_protocol: 1.0.0
stack_trace: 1.10.0
ui:
Expand Down