Chromium Code Reviews| Index: chrome/test/data/subresource_filter/websocket_connection.js |
| diff --git a/chrome/test/data/subresource_filter/websocket_connection.js b/chrome/test/data/subresource_filter/websocket_connection.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4b94c8dd0f43541e70da06f9139335c91ecb2015 |
| --- /dev/null |
| +++ b/chrome/test/data/subresource_filter/websocket_connection.js |
| @@ -0,0 +1,32 @@ |
| +// Note: This JS can run either in a worker or as a script in the main document |
| +// context. |
| +function connectWebSocket(url, inWorker) { |
| + function messageMainContext(data) { |
| + if (inWorker) |
| + self.postMessage(data); |
| + else |
| + self.postMessage(data, location); |
| + } |
| + var ws = new WebSocket(url); |
| + |
| + ws.onopen = function() { |
| + ws.send('hello world'); |
| + }; |
| + |
| + ws.onclose = function() { |
| + messageMainContext('onclose'); |
| + }; |
| + |
| + ws.onmessage = function() { |
| + messageMainContext('onmessage'); |
| + }; |
| +} |
| + |
| +self.addEventListener('message', function(e) { |
| + // Will only receive a message with a URL in it if this JS is executing in a |
|
engedy
2017/03/06 16:31:01
To make this a bit cleaner, what do you think of:
Charlie Harrison
2017/03/06 19:22:11
SGTM. Done
|
| + // worker. |
| + if (e.data.url) { |
| + connectWebSocket(e.data.url, true /* inWorker */); |
| + } |
| +}); |
| + |