| Index: lib/src/runner/browser/post_message_channel.dart
|
| diff --git a/lib/src/runner/browser/post_message_channel.dart b/lib/src/runner/browser/post_message_channel.dart
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..865467536d6b77866d44d24dfe86c5e9adec3b95
|
| --- /dev/null
|
| +++ b/lib/src/runner/browser/post_message_channel.dart
|
| @@ -0,0 +1,44 @@
|
| +// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
|
| +// for details. All rights reserved. Use of this source code is governed by a
|
| +// BSD-style license that can be found in the LICENSE file.
|
| +
|
| +import 'dart:convert';
|
| +import 'dart:html';
|
| +
|
| +import 'package:stream_channel/stream_channel.dart';
|
| +
|
| +/// Constructs a [StreamChannel] wrapping `postMessage` communication with the
|
| +/// host page.
|
| +StreamChannel postMessageChannel() {
|
| + var controller = new StreamChannelController(sync: true);
|
| +
|
| + window.onMessage.listen((message) {
|
| + // 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) return;
|
| + message.stopPropagation();
|
| +
|
| + // See host.dart for why we have to explicitly decode here.
|
| + controller.local.sink.add(JSON.decode(message.data));
|
| + });
|
| +
|
| + controller.local.stream.listen((data) {
|
| + // TODO(nweiz): Stop manually adding href here once issue 22554 is
|
| + // fixed.
|
| + window.parent.postMessage({
|
| + "href": window.location.href,
|
| + "data": data
|
| + }, window.location.origin);
|
| + });
|
| +
|
| + // Send a ready message once we're listening so the host knows it's safe to
|
| + // start sending events.
|
| + window.parent.postMessage({
|
| + "href": window.location.href,
|
| + "ready": true
|
| + }, window.location.origin);
|
| +
|
| + return controller.foreign;
|
| +}
|
|
|