OLD | NEW |
---|---|
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library test.runner.browser.host; | 5 library test.runner.browser.host; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:html'; | 9 import 'dart:html'; |
10 | 10 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
99 | 99 |
100 /// Creates an iframe with `src` [url] and establishes a connection to it using | 100 /// Creates an iframe with `src` [url] and establishes a connection to it using |
101 /// `postMessage`. | 101 /// `postMessage`. |
102 StreamChannel _connectToIframe(String url) { | 102 StreamChannel _connectToIframe(String url) { |
103 var iframe = new IFrameElement(); | 103 var iframe = new IFrameElement(); |
104 iframe.src = url; | 104 iframe.src = url; |
105 document.body.children.add(iframe); | 105 document.body.children.add(iframe); |
106 | 106 |
107 var inputController = new StreamController(sync: true); | 107 var inputController = new StreamController(sync: true); |
108 var outputController = new StreamController(sync: true); | 108 var outputController = new StreamController(sync: true); |
109 iframe.onLoad.first.then((_) { | |
110 // TODO(nweiz): use MessageChannel once Firefox supports it | |
111 // (http://caniuse.com/#search=MessageChannel). | |
112 | 109 |
113 // Send an initial command to give the iframe something to reply to. | 110 // Use this to avoid sending a message to the iframe before it's sent a |
114 iframe.contentWindow.postMessage( | 111 // message to us. This ensures that no messages get dropped on the floor. |
115 {"command": "connect"}, | 112 var readyCompleter = new Completer(); |
116 window.location.origin); | |
117 | 113 |
118 window.onMessage.listen((message) { | 114 // TODO(nweiz): use MessageChannel once Firefox supports it |
119 // A message on the Window can theoretically come from any website. It's | 115 // (http://caniuse.com/#search=MessageChannel). |
120 // very unlikely that a malicious site would care about hacking someone's | 116 window.onMessage.listen((message) { |
121 // unit tests, let alone be able to find the test server while it's | 117 // A message on the Window can theoretically come from any website. It's |
122 // running, but it's good practice to check the origin anyway. | 118 // very unlikely that a malicious site would care about hacking someone's |
123 if (message.origin != window.location.origin) return; | 119 // unit tests, let alone be able to find the test server while it's |
120 // running, but it's good practice to check the origin anyway. | |
121 if (message.origin != window.location.origin) return; | |
124 | 122 |
125 // TODO(nweiz): Stop manually checking href here once issue 22554 is | 123 // TODO(nweiz): Stop manually checking href here once issue 22554 is |
126 // fixed. | 124 // fixed. |
127 if (message.data["href"] != iframe.src) return; | 125 if (message.data["href"] != iframe.src) return; |
128 | 126 |
129 message.stopPropagation(); | 127 message.stopPropagation(); |
130 inputController.add(message.data["data"]); | 128 inputController.add(message.data["data"]); |
131 }); | 129 readyCompleter.complete(); |
130 }); | |
132 | 131 |
133 outputController.stream.listen((message) => | 132 outputController.stream.listen((message) { |
kevmoo
2015/04/13 20:36:24
readyCompleter.future.then?
nweiz
2015/04/13 20:39:08
Done.
| |
133 readyCompleter.then((_) => | |
134 iframe.contentWindow.postMessage(message, window.location.origin)); | 134 iframe.contentWindow.postMessage(message, window.location.origin)); |
135 }); | 135 }); |
136 | 136 |
137 return new StreamChannel(inputController.stream, outputController.sink); | 137 return new StreamChannel(inputController.stream, outputController.sink); |
138 } | 138 } |
OLD | NEW |