Annotation of html5/postmsg/Overview.html, revision 1.9
1.1 ihickson 1: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><!-- when publishing, change bits marked ZZZ --><html lang="en-US-x-Hixie"><title>HTML5 Communications</title><style type="text/css">
2: pre { margin-left: 2em; white-space: pre-wrap; }
3: h2 { margin: 3em 0 1em 0; }
4: h3 { margin: 2.5em 0 1em 0; }
5: h4 { margin: 2.5em 0 0.75em 0; }
6: h5, h6 { margin: 2.5em 0 1em; }
7: h1 + h2, h1 + h2 + h2 { margin: 0.75em 0 0.75em; }
8: h2 + h3, h3 + h4, h4 + h5, h5 + h6 { margin-top: 0.5em; }
9: p { margin: 1em 0; }
10: hr:not(.top) { display: block; background: none; border: none; padding: 0; margin: 2em 0; height: auto; }
11: dl, dd { margin-top: 0; margin-bottom: 0; }
12: dt { margin-top: 0.75em; margin-bottom: 0.25em; clear: left; }
13: dt + dt { margin-top: 0; }
14: dd dt { margin-top: 0.25em; margin-bottom: 0; }
15: dd p { margin-top: 0; }
16: dd dl + p { margin-top: 1em; }
17: dd table + p { margin-top: 1em; }
18: p + * > li, dd li { margin: 1em 0; }
19: dt, dfn { font-weight: bold; font-style: normal; }
20: dt dfn { font-style: italic; }
21: pre, code { font-size: inherit; font-family: monospace; font-variant: normal; }
22: pre strong { color: black; font: inherit; font-weight: bold; background: yellow; }
23: pre em { font-weight: bolder; font-style: normal; }
24: @media screen { code { color: orangered; } code :link, code :visited { color: inherit; } }
25: var sub { vertical-align: bottom; font-size: smaller; position: relative; top: 0.1em; }
26: table { border-collapse: collapse; border-style: hidden hidden none hidden; }
27: table thead { border-bottom: solid; }
28: table tbody th:first-child { border-left: solid; }
29: table tbody th { text-align: left; }
30: table td, table th { border-left: solid; border-right: solid; border-bottom: solid thin; vertical-align: top; padding: 0.2em; }
31: blockquote { margin: 0 0 0 2em; border: 0; padding: 0; font-style: italic; }
32:
33: .bad, .bad *:not(.XXX) { color: gray; border-color: gray; background: transparent; }
34: .matrix, .matrix td { border: none; text-align: right; }
35: .matrix { margin-left: 2em; }
36: .dice-example { border-collapse: collapse; border-style: hidden solid solid hidden; border-width: thin; margin-left: 3em; }
37: .dice-example caption { width: 30em; font-size: smaller; font-style: italic; padding: 0.75em 0; text-align: left; }
38: .dice-example td, .dice-example th { border: solid thin; width: 1.35em; height: 1.05em; text-align: center; padding: 0; }
39:
40: .toc dfn, h1 dfn, h2 dfn, h3 dfn, h4 dfn, h5 dfn, h6 dfn { font: inherit; }
41: img.extra { float: right; }
42: pre.idl { border: solid thin; background: #EEEEEE; color: black; padding: 0.5em 1em; }
43: pre.idl :link, pre.idl :visited { color: inherit; background: transparent; }
44: pre.css { border: solid thin; background: #FFFFEE; color: black; padding: 0.5em 1em; }
45: pre.css:first-line { color: #AAAA50; }
46: dl.domintro { color: green; margin: 2em 0 2em 2em; padding: 0.5em 1em; border: none; background: #EEFFEE; }
47: hr + dl.domintro, div.impl + dl.domintro { margin-top: 2.5em; margin-bottom: 1.5em; }
48: dl.domintro dt, dl.domintro dt * { color: black; text-decoration: none; }
49: dl.domintro dd { margin: 0.5em 0 1em 2em; padding: 0; }
50: dl.domintro dd p { margin: 0.5em 0; }
51: dl.switch { padding-left: 2em; }
52: dl.switch > dt { text-indent: -1.5em; }
53: dl.switch > dt:before { content: '\21AA'; padding: 0 0.5em 0 0; display: inline-block; width: 1em; text-align: right; line-height: 0.5em; }
54: dl.triple { padding: 0 0 0 1em; }
55: dl.triple dt, dl.triple dd { margin: 0; display: inline }
56: dl.triple dt:after { content: ':'; }
57: dl.triple dd:after { content: '\A'; white-space: pre; }
58: .diff-old { text-decoration: line-through; color: silver; background: transparent; }
59: .diff-chg, .diff-new { text-decoration: underline; color: green; background: transparent; }
60: a .diff-new { border-bottom: 1px blue solid; }
61:
62: h2 { page-break-before: always; }
63: h1, h2, h3, h4, h5, h6 { page-break-after: avoid; }
64: h1 + h2, hr + h2.no-toc { page-break-before: auto; }
65:
66: p > span:not([title=""]):not([class="XXX"]):not([class="impl"]), li > span:not([title=""]):not([class="XXX"]):not([class="impl"]) { border-bottom: solid #9999CC; }
67:
68: div.head { margin: 0 0 1em; padding: 1em 0 0 0; }
69: div.head p { margin: 0; }
70: div.head h1 { margin: 0; }
71: div.head .logo { float: right; margin: 0 1em; }
72: div.head .logo img { border: none } /* remove border from top image */
73: div.head dl { margin: 1em 0; }
74: div.head p.copyright, div.head p.alt { font-size: x-small; font-style: oblique; margin: 0; }
75:
76: body > .toc > li { margin-top: 1em; margin-bottom: 1em; }
77: body > .toc.brief > li { margin-top: 0.35em; margin-bottom: 0.35em; }
78: body > .toc > li > * { margin-bottom: 0.5em; }
79: body > .toc > li > * > li > * { margin-bottom: 0.25em; }
80: .toc, .toc li { list-style: none; }
81:
82: .brief { margin-top: 1em; margin-bottom: 1em; line-height: 1.1; }
83: .brief li { margin: 0; padding: 0; }
84: .brief li p { margin: 0; padding: 0; }
85:
86: .category-list { margin-top: -0.75em; margin-bottom: 1em; line-height: 1.5; }
87: .category-list::before { content: '\21D2\A0'; font-size: 1.2em; font-weight: 900; }
88: .category-list li { display: inline; }
89: .category-list li:not(:last-child)::after { content: ', '; }
90: .category-list li > span, .category-list li > a { text-transform: lowercase; }
91: .category-list li * { text-transform: none; } /* don't affect <code> nested in <a> */
92:
93: .XXX { color: #E50000; background: white; border: solid red; padding: 0.5em; margin: 1em 0; }
94: .XXX > :first-child { margin-top: 0; }
95: p .XXX { line-height: 3em; }
96: .annotation { border: solid thin black; background: #0C479D; color: white; position: relative; margin: 8px 0 20px 0; }
97: .annotation:before { position: absolute; left: 0; top: 0; width: 100%; height: 100%; margin: 6px -6px -6px 6px; background: #333333; z-index: -1; content: ''; }
98: .annotation :link, .annotation :visited { color: inherit; }
99: .annotation :link:hover, .annotation :visited:hover { background: transparent; }
100: .annotation span { border: none ! important; }
101: .note { color: green; background: transparent; font-family: sans-serif; }
102: .warning { color: red; background: transparent; }
103: .note, .warning { font-weight: bolder; font-style: italic; }
104: p.note, div.note { padding: 0.5em 2em; }
105: span.note { padding: 0 2em; }
106: .note p:first-child, .warning p:first-child { margin-top: 0; }
107: .note p:last-child, .warning p:last-child { margin-bottom: 0; }
108: .warning:before { font-style: normal; }
109: p.note:before { content: 'Note: '; }
110: p.warning:before { content: '\26A0 Warning! '; }
111:
112: .bookkeeping:before { display: block; content: 'Bookkeeping details'; font-weight: bolder; font-style: italic; }
113: .bookkeeping { font-size: 0.8em; margin: 2em 0; }
114: .bookkeeping p { margin: 0.5em 2em; display: list-item; list-style: square; }
115:
116: h4 { position: relative; z-index: 3; }
117: h4 + .element, h4 + div + .element { margin-top: -2.5em; padding-top: 2em; }
118: .element {
119: background: #EEEEFF;
120: color: black;
121: margin: 0 0 1em 0.15em;
122: padding: 0 1em 0.25em 0.75em;
123: border-left: solid #9999FF 0.25em;
124: position: relative;
125: z-index: 1;
126: }
127: .element:before {
128: position: absolute;
129: z-index: 2;
130: top: 0;
131: left: -1.15em;
132: height: 2em;
133: width: 0.9em;
134: background: #EEEEFF;
135: content: ' ';
136: border-style: none none solid solid;
137: border-color: #9999FF;
138: border-width: 0.25em;
139: }
140:
141: .example { display: block; color: #222222; background: #FCFCFC; border-left: double; margin-left: 2em; padding-left: 1em; }
142: td > .example:only-child { margin: 0 0 0 0.1em; }
143:
144: .tall-and-narrow {
145: font-size: 0.6em;
146: column-width: 25em;
147: column-gap: 1em;
148: -moz-column-width: 25em;
149: -moz-column-gap: 1em;
150: -webkit-column-width: 25em;
151: -webkit-column-gap: 1em;
152: }
153:
154: ul.domTree, ul.domTree ul { padding: 0 0 0 1em; margin: 0; }
155: ul.domTree li { padding: 0; margin: 0; list-style: none; position: relative; }
156: ul.domTree li li { list-style: none; }
157: ul.domTree li:first-child::before { position: absolute; top: 0; height: 0.6em; left: -0.75em; width: 0.5em; border-style: none none solid solid; content: ''; border-width: 0.1em; }
158: ul.domTree li:not(:last-child)::after { position: absolute; top: 0; bottom: -0.6em; left: -0.75em; width: 0.5em; border-style: none none solid solid; content: ''; border-width: 0.1em; }
159: ul.domTree span { font-style: italic; font-family: serif; }
160: ul.domTree .t1 code { color: purple; font-weight: bold; }
161: ul.domTree .t2 { font-style: normal; font-family: monospace; }
162: ul.domTree .t2 .name { color: black; font-weight: bold; }
163: ul.domTree .t2 .value { color: blue; font-weight: normal; }
164: ul.domTree .t3 code, .domTree .t4 code, .domTree .t5 code { color: gray; }
165: ul.domTree .t7 code, .domTree .t8 code { color: green; }
166: ul.domTree .t10 code { color: teal; }
167:
1.7 ihickson 168: body.dfnEnabled dfn { cursor: pointer; }
169: .dfnPanel {
170: display: inline;
171: position: absolute;
172: z-index: 10;
173: height: auto;
174: width: auto;
175: padding: 0.5em 0.75em;
176: font: small sans-serif, Droid Sans Fallback;
177: background: #DDDDDD;
178: color: black;
179: border: outset 0.2em;
180: }
181: .dfnPanel * { margin: 0; padding: 0; font: inherit; text-indent: 0; }
182: .dfnPanel :link, .dfnPanel :visited { color: black; }
183: .dfnPanel p { font-weight: bolder; }
184: .dfnPanel * + p { margin-top: 0.25em; }
185: .dfnPanel li { list-style-position: inside; }
186:
1.1 ihickson 187: #configUI { position: absolute; z-index: 20; top: 10em; right: 1em; width: 11em; font-size: small; }
188: #configUI p { margin: 0.5em 0; padding: 0.3em; background: #EEEEEE; color: black; border: inset thin; }
189: #configUI p label { display: block; }
190: #configUI #updateUI, #configUI .loginUI { text-align: center; }
191: #configUI input[type=button] { display: block; margin: auto; }
192: </style><style type="text/css">
193:
194: .applies thead th > * { display: block; }
195: .applies thead code { display: block; }
196: .applies tbody th { whitespace: nowrap; }
197: .applies td { text-align: center; }
198: .applies .yes { background: yellow; }
199:
200: .matrix, .matrix td { border: none; text-align: right; }
201: .matrix { margin-left: 2em; }
202:
203: .dice-example { border-collapse: collapse; border-style: hidden solid solid hidden; border-width: thin; margin-left: 3em; }
204: .dice-example caption { width: 30em; font-size: smaller; font-style: italic; padding: 0.75em 0; text-align: left; }
205: .dice-example td, .dice-example th { border: solid thin; width: 1.35em; height: 1.05em; text-align: center; padding: 0; }
206:
207: #table-example-1 { border: solid thin; border-collapse: collapse; margin-left: 3em; }
208: #table-example-1 * { font-family: "Essays1743", serif; line-height: 1.01em; }
209: #table-example-1 caption { padding-bottom: 0.5em; }
210: #table-example-1 thead, #table-example-1 tbody { border: none; }
211: #table-example-1 th, #table-example-1 td { border: solid thin; }
212: #table-example-1 th { font-weight: normal; }
213: #table-example-1 td { border-style: none solid; vertical-align: top; }
214: #table-example-1 th { padding: 0.5em; vertical-align: middle; text-align: center; }
215: #table-example-1 tbody tr:first-child td { padding-top: 0.5em; }
216: #table-example-1 tbody tr:last-child td { padding-bottom: 1.5em; }
217: #table-example-1 tbody td:first-child { padding-left: 2.5em; padding-right: 0; width: 9em; }
218: #table-example-1 tbody td:first-child::after { content: leader(". "); }
219: #table-example-1 tbody td { padding-left: 2em; padding-right: 2em; }
220: #table-example-1 tbody td:first-child + td { width: 10em; }
221: #table-example-1 tbody td:first-child + td ~ td { width: 2.5em; }
222: #table-example-1 tbody td:first-child + td + td + td ~ td { width: 1.25em; }
223:
224: .apple-table-examples { border: none; border-collapse: separate; border-spacing: 1.5em 0em; width: 40em; margin-left: 3em; }
225: .apple-table-examples * { font-family: "Times", serif; }
226: .apple-table-examples td, .apple-table-examples th { border: none; white-space: nowrap; padding-top: 0; padding-bottom: 0; }
227: .apple-table-examples tbody th:first-child { border-left: none; width: 100%; }
228: .apple-table-examples thead th:first-child ~ th { font-size: smaller; font-weight: bolder; border-bottom: solid 2px; text-align: center; }
229: .apple-table-examples tbody th::after, .apple-table-examples tfoot th::after { content: leader(". ") }
230: .apple-table-examples tbody th, .apple-table-examples tfoot th { font: inherit; text-align: left; }
231: .apple-table-examples td { text-align: right; vertical-align: top; }
232: .apple-table-examples.e1 tbody tr:last-child td { border-bottom: solid 1px; }
233: .apple-table-examples.e1 tbody + tbody tr:last-child td { border-bottom: double 3px; }
234: .apple-table-examples.e2 th[scope=row] { padding-left: 1em; }
235: .apple-table-examples sup { line-height: 0; }
236:
237: .details-example img { vertical-align: top; }
238:
1.6 ihickson 239: </style><link href="data:text/css," id="complete" rel="stylesheet" title="Complete specification"><link href="data:text/css,.impl%20{%20display:%20none;%20}" id="author" rel="alternate stylesheet" title="Author documentation only"><link href="data:text/css,.impl%20{%20background:%20%23FFEEEE;%20}" id="highlight" rel="alternate stylesheet" title="Highlight implementation requirements"><script type="text/javascript">
1.1 ihickson 240: function getCookie(name) {
241: var params = location.search.substr(1).split("&");
242: for (var index = 0; index < params.length; index++) {
243: if (params[index] == name)
244: return "1";
245: var data = params[index].split("=");
246: if (data[0] == name)
247: return unescape(data[1]);
248: }
249: var cookies = document.cookie.split("; ");
250: for (var index = 0; index < cookies.length; index++) {
251: var data = cookies[index].split("=");
252: if (data[0] == name)
253: return unescape(data[1]);
254: }
255: return null;
256: }
257: function load(script) {
258: var e = document.createElement('script');
259: e.setAttribute('src', 'http://www.whatwg.org/specs/web-apps/current-work/' + script + '?' + encodeURIComponent(location) + '&' + encodeURIComponent(document.referrer));
260: document.body.appendChild(e);
261: }
262: function init() {
263: if (location.search == '?slow-browser')
264: return;
265: var configUI = document.createElement('div');
266: configUI.id = 'configUI';
267: document.body.appendChild(configUI);
268: // load('reviewer.js'); // would need cross-site XHR
269: if (document.getElementById('head'))
270: load('toc.js');
271: load('styler.js');
272: // load('updater.js'); // would need cross-site XHR
273: load('dfn.js'); // doesn't support split-out specs, but, oh well.
274: // load('status.js'); // would need cross-site XHR
275: if (getCookie('profile') == '1')
276: document.getElementsByTagName('h2')[0].textContent += '; load: ' + (new Date() - loadTimer) + 'ms';
277: fixBrokenLink();
278: }
279: </script><link href="http://www.w3.org/StyleSheets/TR/W3C-ED" rel="stylesheet" type="text/css"><body onload="init()"><div class="head" id="head">
280: <p><a href="http://www.w3.org/"><img alt="W3C" height="48" src="http://www.w3.org/Icons/w3c_home" width="72"></a></p>
281: <h1>HTML5 Communications</h1>
282: <h2 class="no-num no-toc" id="generatedID"></h2>
283:
1.9 ! ihickson 284: <h2 class="no-num no-toc" id="editor-s-draft-11-february-2010">Editor's Draft 11 February 2010</h2>
1.1 ihickson 285: <dl><dt>Latest Published Version:</dt>
286: <dd><a href="http://www.w3.org/TR/postmsg/">http://www.w3.org/TR/postmsg/</a></dd>
287: <dt>Latest Editor's Draft:</dt>
288: <dd><a href="http://dev.w3.org/html5/postmsg/">http://dev.w3.org/html5/postmsg/</a></dd>
289: <!-- ZZZ: add the new version after it has shipped -->
290: <!--
291: <dt>Previous Versions:</dt>
292: -->
293: <!-- :ZZZ -->
294: <dt>Editors:</dt>
295: <dd><a href="mailto:ian@hixie.ch">Ian Hickson</a>, Google, Inc.</dd>
1.3 ihickson 296: </dl><p class="copyright"><a href="http://www.w3.org/Consortium/Legal/ipr-notice#Copyright">Copyright</a>
1.1 ihickson 297: © 2009 <a href="http://www.w3.org/"><abbr title="World Wide
298: Web Consortium">W3C</abbr></a><sup>®</sup> (<a href="http://www.csail.mit.edu/"><abbr title="Massachusetts
299: Institute of Technology">MIT</abbr></a>, <a href="http://www.ercim.org/"><abbr title="European Research
300: Consortium for Informatics and Mathematics">ERCIM</abbr></a>, <a href="http://www.keio.ac.jp/">Keio</a>), All Rights Reserved. W3C
301: <a href="http://www.w3.org/Consortium/Legal/ipr-notice#Legal_Disclaimer">liability</a>,
302: <a href="http://www.w3.org/Consortium/Legal/ipr-notice#W3C_Trademarks">trademark</a>
303: and <a href="http://www.w3.org/Consortium/Legal/copyright-documents">document
304: use</a> rules apply.</p>
1.6 ihickson 305:
306: <!-- UNDER NO CIRCUMSTANCES IS THE FOLLOWING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
307: <!-- temporarily removed after discussion with plh in October 2009 -->
308: <!-- will be moved back to below W3C copyright in April 2009 -->
309: <!--
310: <p class="alt">The <a
311: href="http://www.whatwg.org/specs/web-apps/current-work/multipage/">WHATWG
312: version</a> of this specification is available under a license that
313: permits reuse of the specification text.</p>
314: -->
315: <!-- UNDER NO CIRCUMSTANCES IS THE PRECEDING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
316:
1.1 ihickson 317: </div><hr class="top"><h2 class="no-num no-toc" id="abstract">Abstract</h2><p>This specification defines two mechanism for communicating
1.6 ihickson 318: between browsing contexts in HTML documents.<h2 class="no-num no-toc" id="status-of-this-document">Status of This document</h2><!-- intro boilerplate (required) --><p><em>This section describes the status of this document at the
1.1 ihickson 319: time of its publication. Other documents may supersede this
320: document. A list of current W3C publications and the most recently
321: formally published revision of this technical report can be found in
322: the <a href="http://www.w3.org/TR/">W3C technical reports index</a>
323: at http://www.w3.org/TR/.</em></p><!-- where to send feedback (required) --><p>If you wish to make comments regarding this document, please send
324: them to <a href="mailto:public-html-comments@w3.org">public-html-comments@w3.org</a>
325: (<a href="mailto:public-html-comments-request@w3.org?subject=subscribe">subscribe</a>,
326: <a href="http://lists.w3.org/Archives/Public/public-html-comments/">archives</a>)
327:
328: <!-- UNDER NO CIRCUMSTANCES IS THE FOLLOWING SENTENCE TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
329: or <a href="mailto:whatwg@whatwg.org">whatwg@whatwg.org</a> (<a href="http://lists.whatwg.org/listinfo.cgi/whatwg-whatwg.org">subscribe</a>,
330: <a href="http://lists.whatwg.org/pipermail/whatwg-whatwg.org/">archives</a>),
331: <!-- UNDER NO CIRCUMSTANCES IS THE PRECEDING SENTENCE TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST -->
332:
333: or submit them using <a href="http://www.w3.org/Bugs/Public/enter_bug.cgi?product=HTML%20WG">our
334: public bug database</a>.
335:
336: All feedback is welcome.</p><!-- UNDER NO CIRCUMSTANCES IS THE FOLLOWING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST --><p>The working groups maintains <a href="http://www.w3.org/Bugs/Public/buglist.cgi?component=Spec%20bugs&component=Spec%20proposals&product=HTML%20WG&resolution=NEEDSINFO&resolution=LATER&resolution=REMIND&resolution=---&order=bugs.resolution%2Cbugs.priority%2C%20bugs.bug_severity">a
337: list of all bug reports that the editor has not yet tried to
338: address</a> and <a href="http://www.w3.org/html/wg/tracker/products/1">a list of issues
339: for which the chairs have not yet declared a decision</a>. The
340: editor also maintains <a href="http://www.whatwg.org/issues/">a list
341: of all e-mails that he has not yet tried to address</a>. These bugs,
342: issues, and e-mails apply to all HTML specifications, not just this
343: one.</p><!-- UNDER NO CIRCUMSTANCES IS THE PRECEDING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST --><!-- stability (required) --><p>Implementors should be aware that this specification is not
344: stable. <strong>Implementors who are not taking part in the
345: discussions are likely to find the specification changing out from
346: under them in incompatible ways.</strong> Vendors interested in
347: implementing this specification before it eventually reaches the
348: Candidate Recommendation stage should join the aforementioned
1.6 ihickson 349: mailing lists and take part in the discussions.</p><!-- not everyone agrees with this (requested before html5 fpwd) --><p>The publication of this document by the W3C as a W3C Working
1.1 ihickson 350: Draft does not imply that all of the participants in the W3C HTML
351: working group endorse the contents of the specification. Indeed, for
352: any section of the specification, one can usually find many members
353: of the working group or of the W3C as a whole who object strongly to
354: the current text, the existence of the section at all, or the idea
355: that the working group should even spend time discussing the concept
356: of that section.</p><!-- UNDER NO CIRCUMSTANCES IS THE FOLLOWING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST --><!-- version history or list of changes (required) --><p>The latest stable version of the editor's draft of this
357: specification is always available on <a href="http://dev.w3.org/html5/">the W3C CVS server</a> and in the <a href="http://svn.whatwg.org/webapps/">WHATWG Subversion
358: repository</a>. The <a href="http://www.whatwg.org/specs/web-apps/current-work/complete.html">latest
359: editor's working copy</a> (which may contain unfinished text in the
360: process of being prepared) contains the latest draft text of this
361: specification (amongst others). For more details, please see the <a href="http://wiki.whatwg.org/wiki/FAQ#What_are_the_various_versions_of_the_spec.3F">WHATWG
362: FAQ</a>.</p><!-- UNDER NO CIRCUMSTANCES IS THE PRECEDING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST --><!-- UNDER NO CIRCUMSTANCES IS THE FOLLOWING LIST TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST --><p>There are various ways to follow the change history for the
363: HTML specifications:<dl><dt>E-mail notifications of changes</dt>
364: <dd>HTML-Diffs mailing list (diff-marked HTML versions for each change): <a href="http://lists.w3.org/Archives/Public/public-html-diffs/latest">http://lists.w3.org/Archives/Public/public-html-diffs/latest</a></dd>
365: <dd>Commit-Watchers mailing list (complete source diffs): <a href="http://lists.whatwg.org/listinfo.cgi/commit-watchers-whatwg.org">http://lists.whatwg.org/listinfo.cgi/commit-watchers-whatwg.org</a></dd>
366: <dt>Real-time notifications of changes:</dt>
367: <dd>Generated diff-marked HTML versions for each change: <a href="http://twitter.com/HTML5">http://twitter.com/HTML5</a></dd>
368: <dd>All (non-editorial) changes to the spec source: <a href="http://twitter.com/WHATWG">http://twitter.com/WHATWG</a></dd>
369: <dt>Browsable version-control record of all changes:</dt>
370: <dd>CVSWeb interface with side-by-side diffs: <a href="http://dev.w3.org/cvsweb/html5/">http://dev.w3.org/cvsweb/html5/</a></dd>
371: <dd>Annotated summary with unified diffs: <a href="http://html5.org/tools/web-apps-tracker">http://html5.org/tools/web-apps-tracker</a></dd>
372: <dd>Raw Subversion interface: <code>svn checkout http://svn.whatwg.org/webapps/</code></dd>
373: </dl><!-- UNDER NO CIRCUMSTANCES IS THE PRECEDING LIST TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST --><!-- status of document, group responsible (required) --><p>The W3C <a href="http://www.w3.org/html/wg/">HTML Working
374: Group</a> is the W3C working group responsible for this
375: specification's progress along the W3C Recommendation
376: track.
377:
1.9 ! ihickson 378: This specification is the 11 February 2010 Editor's Draft.
1.1 ihickson 379: </p><!-- UNDER NO CIRCUMSTANCES IS THE PRECEDING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST --><!-- relationship to other work (required) --><p>This specification is part of <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/">a
1.6 ihickson 380: larger specification</a> being produced by the <a href="http://www.whatwg.org/">WHATWG</a>, which is available under a
381: license that permits reuse of the specification text.</p><!-- UNDER NO CIRCUMSTANCES IS THE FOLLOWING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST --><!-- UNDER NO CIRCUMSTANCES IS THE PRECEDING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST --><!-- context and rationale (required) --><!-- (this text is from the RDFa+HTML spec --><p>This specification is an extension to the HTML5 language. All
382: normative content in the HTML5 specification, unless specifically
383: overridden by this specification, is intended to be the basis for
384: this specification.</p><!-- UNDER NO CIRCUMSTANCES IS THE FOLLOWING PARAGRAPH TO BE REMOVED OR EDITED WITHOUT TALKING TO IAN FIRST --><!-- required patent boilerplate --><p>This document was produced by a group operating under the <a href="http://www.w3.org/Consortium/Patent-Policy-20040205/">5
1.1 ihickson 385: February 2004 W3C Patent Policy</a>. W3C maintains a <a href="http://www.w3.org/2004/01/pp-impl/40318/status" rel="disclosure">public list of
386: any patent disclosures</a> made in connection with the deliverables
387: of the group; that page also includes instructions for disclosing a
388: patent. An individual who has actual knowledge of a patent which the
389: individual believes contains <a href="http://www.w3.org/Consortium/Patent-Policy-20040205/#def-essential">Essential
390: Claim(s)</a> must disclose the information in accordance with <a href="http://www.w3.org/Consortium/Patent-Policy-20040205/#sec-Disclosure">section
1.6 ihickson 391: 6 of the W3C Patent Policy</a>.<h2 class="no-num no-toc" id="contents">Table of Contents</h2>
1.1 ihickson 392: <!--begin-toc-->
393: <ol class="toc">
394: <li><a href="#conformance-requirements"><span class="secno">1 </span>Conformance requirements</a></li>
395: <li><a href="#idl"><span class="secno">2 </span>IDL</a></li>
396: <li><a href="#event-definitions"><span class="secno">3 </span>Event definitions</a></li>
397: <li><a href="#crossDocumentMessages"><span class="secno">4 </span>Cross-document messaging</a>
398: <ol>
399: <li><a href="#introduction"><span class="secno">4.1 </span>Introduction</a></li>
400: <li><a href="#security"><span class="secno">4.2 </span>Security</a>
401: <ol>
402: <li><a href="#authors"><span class="secno">4.2.1 </span>Authors</a></li>
403: <li><a href="#user-agents"><span class="secno">4.2.2 </span>User agents</a></ol></li>
404: <li><a href="#posting-messages"><span class="secno">4.3 </span>Posting messages</a></li>
405: <li><a href="#posting-messages-with-message-ports"><span class="secno">4.4 </span>Posting messages with message ports</a></ol></li>
406: <li><a href="#channel-messaging"><span class="secno">5 </span>Channel messaging</a>
407: <ol>
408: <li><a href="#introduction-0"><span class="secno">5.1 </span>Introduction</a></li>
409: <li><a href="#message-channels"><span class="secno">5.2 </span>Message channels</a></li>
410: <li><a href="#message-ports"><span class="secno">5.3 </span>Message ports</a>
411: <ol>
412: <li><a href="#ports-and-garbage-collection"><span class="secno">5.3.1 </span>Ports and garbage collection</a></ol></ol></li>
413: <li><a class="no-num" href="#references">References</a></li>
414: <li><a class="no-num" href="#acknowledgements">Acknowledgements</a></ol>
415: <!--end-toc--><hr><h2 id="conformance-requirements"><span class="secno">1 </span>Conformance requirements</h2><p>This specification is an HTML specification. All the conformance
416: requirements, conformance classes, definitions, dependencies,
1.4 ihickson 417: terminology, and typographical conventions described in the core
1.6 ihickson 418: HTML5 specification apply to this specification. <a href="#refsHTML5">[HTML5]</a><h2 id="idl"><span class="secno">2 </span>IDL</h2><h2 id="event-definitions"><span class="secno">3 </span>Event definitions</h2><p>Messages in <span>server-sent events</span>, <span>Web
1.1 ihickson 419: sockets</span>, <a href="#crossDocumentMessages">cross-document messaging</a>, and
420: <a href="#channel-messaging">channel messaging</a> use the <dfn id="event-message" title="event-message"><code>message</code></dfn> event.
421:
422: <a href="#refsEVENTSOURCE">[EVENTSOURCE]</a>
423: <a href="#refsWEBSOCKET">[WEBSOCKET]</a>
424:
425: <p>The following interface is defined for this event:<pre class="idl">interface <dfn id="messageevent">MessageEvent</dfn> : Event {
426: readonly attribute any <a href="#dom-messageevent-data" title="dom-MessageEvent-data">data</a>;
427: readonly attribute DOMString <a href="#dom-messageevent-origin" title="dom-MessageEvent-origin">origin</a>;
428: readonly attribute DOMString <a href="#dom-messageevent-lasteventid" title="dom-MessageEvent-lastEventId">lastEventId</a>;
429: readonly attribute <span>WindowProxy</span> <a href="#dom-messageevent-source" title="dom-MessageEvent-source">source</a>;
430: readonly attribute <a href="#messageportarray">MessagePortArray</a> <a href="#dom-messageevent-ports" title="dom-MessageEvent-ports">ports</a>;
431: void <a href="#dom-messageevent-initmessageevent" title="dom-MessageEvent-initMessageEvent">initMessageEvent</a>(in DOMString typeArg, in boolean canBubbleArg, in boolean cancelableArg, in any dataArg, in DOMString originArg, in DOMString lastEventIdArg, in <span>WindowProxy</span> sourceArg, in <a href="#messageportarray">MessagePortArray</a> portsArg);
432: };</pre><dl class="domintro"><dt><var title="">event</var> . <code title="dom-MessageEvent-data"><a href="#dom-messageevent-data">data</a></code></dt>
433:
434: <dd>
435:
436: <p>Returns the data of the message.</p>
437:
438: </dd>
439:
440: <dt><var title="">event</var> . <code title="dom-MessageEvent-origin"><a href="#dom-messageevent-origin">origin</a></code></dt>
441:
442: <dd>
443:
444: <p>Returns the origin of the message, for <span>server-sent
445: events</span> and <a href="#crossDocumentMessages">cross-document messaging</a>.</p>
446:
447: </dd>
448:
449: <dt><var title="">event</var> . <code title="dom-MessageEvent-lastEventId"><a href="#dom-messageevent-lasteventid">lastEventId</a></code></dt>
450:
451: <dd>
452:
453: <p>Returns the last event ID, for <span>server-sent
454: events</span>.</p>
455:
456: </dd>
457:
458: <dt><var title="">event</var> . <code title="dom-MessageEvent-source"><a href="#dom-messageevent-source">source</a></code></dt>
459:
460: <dd>
461:
462: <p>Returns the <code>WindowProxy</code> of the source window, for
463: <a href="#crossDocumentMessages">cross-document messaging</a>.</p>
464:
465: </dd>
466:
467: <dt><var title="">event</var> . <code title="dom-MessageEvent-ports"><a href="#dom-messageevent-ports">ports</a></code></dt>
468:
469: <dd>
470:
471: <p>Returns the <code><a href="#messageportarray">MessagePortArray</a></code> sent with the
472: message, for <a href="#crossDocumentMessages">cross-document messaging</a> and
473: <a href="#channel-messaging">channel messaging</a>.</p>
474:
475: </dd>
476:
477: </dl><div class="impl">
478:
479: <p>The <dfn id="dom-messageevent-initmessageevent" title="dom-MessageEvent-initMessageEvent"><code>initMessageEvent()</code></dfn>
480: method must initialize the event in a manner analogous to the
481: similarly-named method in the DOM Events interfaces. <a href="#refsDOMEVENTS">[DOMEVENTS]</a></p>
482:
483: <p>The <dfn id="dom-messageevent-data" title="dom-MessageEvent-data"><code>data</code></dfn>
484: attribute represents the message being sent.</p>
485:
486: <p>The <dfn id="dom-messageevent-origin" title="dom-MessageEvent-origin"><code>origin</code></dfn> attribute
487: represents, in <span>server-sent events</span> and
488: <a href="#crossDocumentMessages">cross-document messaging</a>, the <span>origin</span> of
489: the document that sent the message (typically the scheme, hostname,
490: and port of the document, but not its path or fragment
491: identifier).</p>
492:
493: <p>The <dfn id="dom-messageevent-lasteventid" title="dom-MessageEvent-lastEventId"><code>lastEventId</code></dfn>
494: attribute represents, in <span>server-sent events</span>, the <span title="concept-event-stream-last-event-id">last event ID
495: string</span> of the event source.</p>
496:
497: <p>The <dfn id="dom-messageevent-source" title="dom-MessageEvent-source"><code>source</code></dfn> attribute
498: represents, in <a href="#crossDocumentMessages">cross-document messaging</a>, the
499: <code>WindowProxy</code> of the <span>browsing context</span> of the
500: <code>Window</code> object from which the message came.</p>
501:
502: <p>The <dfn id="dom-messageevent-ports" title="dom-MessageEvent-ports"><code>ports</code></dfn>
503: attribute represents, in <a href="#crossDocumentMessages">cross-document messaging</a> and
504: <a href="#channel-messaging">channel messaging</a> the <code><a href="#messageportarray">MessagePortArray</a></code>
505: being sent, if any.</p>
506:
507: <p>Except where otherwise specified, when the user agent creates and
508: dispatches a <code title="event-message"><a href="#event-message">message</a></code> event in the
509: algorithms described in the following sections, the <code title="dom-MessageEvent-lastEventId"><a href="#dom-messageevent-lasteventid">lastEventId</a></code> attribute
510: must be the empty string, the <code title="dom-MessageEvent-origin"><a href="#dom-messageevent-origin">origin</a></code> attribute must be the
511: empty string, the <code title="dom-MessageEvent-source"><a href="#dom-messageevent-source">source</a></code> attribute must be
512: null, and the <code title="dom-MessageEvent-ports"><a href="#dom-messageevent-ports">ports</a></code>
513: attribute must be null.</p>
514:
515: </div><h2 id="crossDocumentMessages"><span class="secno">4 </span><dfn>Cross-document messaging</dfn></h2><p>Web browsers, for security and privacy reasons, prevent documents
516: in different domains from affecting each other; that is, cross-site
517: scripting is disallowed.<p>While this is an important security feature, it prevents pages
518: from different domains from communicating even when those pages are
519: not hostile. This section introduces a messaging system that allows
520: documents to communicate with each other regardless of their source
521: domain, in a way designed to not enable cross-site scripting
522: attacks.<div class="impl">
523:
524: <p>The <span>task source</span> for the <span title="concept-task">tasks</span> in <a href="#crossDocumentMessages">cross-document
525: messaging</a> is the <dfn id="posted-message-task-source">posted message task source</dfn>.</p>
526:
527: </div><h3 id="introduction"><span class="secno">4.1 </span>Introduction</h3><p><i>This section is non-normative.</i><div class="example">
528:
529: <p>For example, if document A contains an <code>iframe</code>
530: element that contains document B, and script in document A calls
531: <code title="dom-window-postMessage-2"><a href="#dom-window-postmessage-2">postMessage()</a></code> on the
532: <code>Window</code> object of document B, then a message event will
533: be fired on that object, marked as originating from the
534: <code>Window</code> of document A. The script in document A might
535: look like:</p>
536:
537: <pre>var o = document.getElementsByTagName('iframe')[0];
538: o.contentWindow.postMessage('Hello world', 'http://b.example.org/');</pre>
539:
540: <p>To register an event handler for incoming events, the script
541: would use <code title="">addEventListener()</code> (or similar
542: mechanisms). For example, the script in document B might look
543: like:</p>
544:
545: <pre>window.addEventListener('message', receiver, false);
546: function receiver(e) {
547: if (e.origin == 'http://example.com') {
548: if (e.data == 'Hello world') {
549: e.source.postMessage('Hello', e.origin);
550: } else {
551: alert(e.data);
552: }
553: }
554: }</pre>
555:
556: <p>This script first checks the domain is the expected domain, and
557: then looks at the message, which it either displays to the user, or
558: responds to by sending a message back to the document which sent
559: the message in the first place.</p>
560:
561: </div><h3 id="security"><span class="secno">4.2 </span>Security</h3><div class="impl">
562:
563: <h4 id="authors"><span class="secno">4.2.1 </span>Authors</h4>
564:
565: </div><p class="warning">Use of this API requires extra care to protect
566: users from hostile entities abusing a site for their own
567: purposes.<p>Authors should check the <code title="dom-MessageEvent-origin"><a href="#dom-messageevent-origin">origin</a></code> attribute to ensure
568: that messages are only accepted from domains that they expect to
569: receive messages from. Otherwise, bugs in the author's message
570: handling code could be exploited by hostile sites.<p>Furthermore, even after checking the <code title="dom-MessageEvent-origin"><a href="#dom-messageevent-origin">origin</a></code> attribute, authors
571: should also check that the data in question is of the expected
572: format. Otherwise, if the source of the event has been attacked
573: using a cross-site scripting flaw, further unchecked processing of
574: information sent using the <code title="dom-window-postMessage-2"><a href="#dom-window-postmessage-2">postMessage()</a></code> method could
575: result in the attack being propagated into the receiver.<p>Authors should not use the wildcard keyword (*) in the <var title="">targetOrigin</var> argument in messages that contain any
576: confidential information, as otherwise there is no way to guarantee
577: that the message is only delivered to the recipient to which it was
578: intended.<div class="impl">
579:
580: <h4 id="user-agents"><span class="secno">4.2.2 </span>User agents</h4>
581:
582: <p>The integrity of this API is based on the inability for scripts
583: of one <span>origin</span> to post arbitrary events (using <code title="">dispatchEvent()</code> or otherwise) to objects in other
584: origins (those that are not the <span title="same
585: origin">same</span>).</p>
586:
587: <p class="note">Implementors are urged to take extra care in the
588: implementation of this feature. It allows authors to transmit
589: information from one domain to another domain, which is normally
590: disallowed for security reasons. It also requires that UAs be
591: careful to allow access to certain properties but not others.</p>
592:
593: </div><h3 id="posting-messages"><span class="secno">4.3 </span>Posting messages</h3><dl class="domintro"><dt><var title="">window</var> . <code title="dom-window-postMessage-2"><a href="#dom-window-postmessage-2">postMessage</a></code>(<var title="">message</var>, [ <var title="">ports</var>, ] <var title="">targetOrigin</var>)</dt>
594:
595: <dd>
596:
597: <p>Posts a message, optionally with an array of ports, to the
598: given window.</p>
599:
600: <p>If the origin of the target window doesn't match the given
601: origin, the message is discarded, to avoid information leakage. To
602: send the message to the target regardless of origin, set the
603: target origin to "<code title="">*</code>".</p>
604:
605: <p>Throws an <code>INVALID_STATE_ERR</code> if the <var title="">ports</var> array is not null and it contains either null
606: entries or duplicate ports.</p>
607:
608: </dd>
609:
610: </dl><div class="impl">
611:
612: <p>When a script invokes the <dfn id="dom-window-postmessage-2" title="dom-window-postMessage-2"><code>postMessage(<var title="">message</var>, <var title="">targetOrigin</var>)</code></dfn> method (with only two
613: arguments) on a <code>Window</code> object, the user agent must
614: follow these steps:
615:
616: <ol><li>
617:
618: <p>If the value of the <var title="">targetOrigin</var> argument
619: is not a single U+002A ASTERISK character (*), and <span title="resolve a url">resolving</span> it relative to the
620: <span>entry script</span>'s <span title="script's base URL">base
621: URL</span> either fails or results in a <span>URL</span> with a
622: <code title="url-host-specific"><host-specific></code>
623: component that is neither empty nor a single U+002F SOLIDUS
624: character (/), then throw a <code>SYNTAX_ERR</code> exception and
625: abort the overall set of steps.</p>
626:
627: </li>
628:
629: <li>
630:
631: <p>Let <var title="">message clone</var> be the result of
632: obtaining a <span>structured clone</span> of the <var title="">message</var> argument. If this throws an exception, then
633: throw that exception and abort these steps.</p>
634:
635: </li>
636:
637: <li>
638:
639: <p>Return from the <code title="dom-window-postMessage-2"><a href="#dom-window-postmessage-2">postMessage()</a></code> method, but
640: asynchronously continue running these steps.</p>
641:
642: </li>
643:
644: <li>
645:
646: <p>If the <var title="">targetOrigin</var> argument has a value
647: other than a single literal U+002A ASTERISK character (*), and
648: the <code>Document</code> of the <code>Window</code> object on
649: which the method was invoked does not have the <span>same
650: origin</span> as <var title="">targetOrigin</var>, then abort
651: these steps silently.</p>
652:
653: </li>
654:
655: <li>
656:
657: <p>Create an event that uses the <code><a href="#messageevent">MessageEvent</a></code>
658: interface, with the event name <code title="event-message"><a href="#event-message">message</a></code>, which does not bubble, is
659: not cancelable, and has no default action. The <code title="dom-MessageEvent-data"><a href="#dom-messageevent-data">data</a></code> attribute must be set to
660: the value of <var title="">message clone</var>, the <code title="dom-MessageEvent-origin"><a href="#dom-messageevent-origin">origin</a></code> attribute must be
661: set to the <span title="Unicode serialization of an
662: origin">Unicode serialization</span> of the <span>origin</span> of
663: the script that invoked the method, and the <code title="dom-MessageEvent-source"><a href="#dom-messageevent-source">source</a></code> attribute must be
664: set to the <span>script's global object</span>.</p> <!--
665: invariant: the global object is always a Window if the script can
666: see this method -->
667:
668: </li>
669:
670: <li>
671:
672: <p><span>Queue a task</span> to dispatch the event created in the
673: previous step at the <code>Window</code> object on which the
674: method was invoked. The <span>task source</span> for this <span title="concept-task">task</span> is the <a href="#posted-message-task-source">posted message task
675: source</a>.</p>
676:
677: </li>
678:
679: </ol></div><div class="impl">
680:
681: <h3 id="posting-messages-with-message-ports"><span class="secno">4.4 </span>Posting messages with message ports</h3>
682:
683: <p>When a script invokes the <dfn id="dom-window-postmessage-3" title="dom-window-postMessage-3"><code>postMessage(<var title="">message</var>, <var title="">ports</var>, <var title="">targetOrigin</var>)</code></dfn> method (with three
684: arguments) on a <code>Window</code> object, the user agent must
685: follow these steps:
686:
687: <ol><!-- EXCEPT WHERE NOTED, THESE STEPS ARE IDENTICAL TO THE PREVIOUS SECTION --><!-- one exception is the use of -3 instead of -2 in the xrefs --><li>
688:
689: <p>If the value of the <var title="">targetOrigin</var> argument
690: is not a single U+002A ASTERISK character (*), and <span title="resolve a url">resolving</span> it relative to the
691: <span>entry script</span>'s <span title="script's base URL">base
692: URL</span> either fails or results in a <span>URL</span> with a
693: <code title="url-host-specific"><host-specific></code>
694: component that is neither empty nor a single U+002F SOLIDUS
695: character (/), then throw a <code>SYNTAX_ERR</code> exception and
696: abort the overall set of steps.</p>
697:
698: </li>
699:
700: <li>
701:
702: <p>Let <var title="">message clone</var> be the result of
703: obtaining a <span>structured clone</span> of the <var title="">message</var> argument. If this throws an exception, then
704: throw that exception and abort these steps.</p>
705:
706: </li>
707:
708: <li> <!-- NEW STEP -->
709:
710: <p>If the <var title="">ports</var> argument is empty<!-- or null
711: (if we make it nullable)-->, then act as if the method had just
712: been <a href="#dom-window-postmessage-2" title="dom-window-postMessage-2">called with two
713: arguments</a>, <var title="">message</var> and <var title="">targetOrigin</var>.</p>
714:
715: </li>
716:
717: <li> <!-- NEW STEP -->
718:
719: <p>If any of the entries in <var title="">ports</var> are null, if
720: any <code><a href="#messageport">MessagePort</a></code> object is listed in <var title="">ports</var> more than once, or if any of the
721: <code><a href="#messageport">MessagePort</a></code> objects listed in <var title="">ports</var> have already been cloned once before, then
722: throw an <code>INVALID_STATE_ERR</code> exception.</p>
723:
724: </li>
725:
726: <li> <!-- NEW STEP -->
727:
728: <p>Let <var title="">new ports</var> be an empty array.</p>
729:
730: <p>For each port in <var title="">ports</var> in turn,
731: obtain a new port by <a href="#clone-a-port" title="clone a port">cloning</a> the
732: port with the <code>Window</code> object on which the method was
733: invoked as the owner of the clone, and append the clone to the
734: <var title="">new ports</var> array.</p>
735: <!--
736: <p class="note">If the original <var title="">ports</var>
737: array was empty, then the <var title="">new ports</var> array will
738: also be empty.</p>
739: -->
740: </li>
741:
742: <li>
743:
744: <p>Return from the <code title="dom-window-postMessage-3"><a href="#dom-window-postmessage-3">postMessage()</a></code> method, but
745: asynchronously continue running these steps.</p>
746:
747: </li>
748:
749: <li>
750:
751: <p>If the <var title="">targetOrigin</var> argument has a value
752: other than a single literal U+002A ASTERISK character (*), and
753: the <code>Document</code> of the <code>Window</code> object on
754: which the method was invoked does not have the <span>same
755: origin</span> as <var title="">targetOrigin</var>, then abort
756: these steps silently.</p>
757:
758: </li>
759:
760: <li>
761:
762: <p>Create an event that uses the <code><a href="#messageevent">MessageEvent</a></code>
763: interface, with the event name <code title="event-message"><a href="#event-message">message</a></code>, which does not bubble, is
764: not cancelable, and has no default action. The <code title="dom-MessageEvent-data"><a href="#dom-messageevent-data">data</a></code> attribute must be set to
765: the value of <var title="">message clone</var>, the <code title="dom-MessageEvent-origin"><a href="#dom-messageevent-origin">origin</a></code> attribute must be
766: set to the <span title="Unicode serialization of an
767: origin">Unicode serialization</span> of the <span>origin</span> of
768: the script that invoked the method, and the <code title="dom-MessageEvent-source"><a href="#dom-messageevent-source">source</a></code> attribute must be
769: set to the <span>script's global object</span>.</p> <!--
770: invariant: the global object is always a Window if the script can
771: see this method -->
772:
773: </li>
774:
775: <li> <!-- NEW STEP -->
776:
777: <p>Let the <code title="dom-MessageEvent-ports"><a href="#dom-messageevent-ports">ports</a></code> attribute
778: of the event be the <var title="">new ports</var> array.</p>
779:
780: </li>
781:
782: <li>
783:
784: <p><span>Queue a task</span> to dispatch the event created in the
785: previous step at the <code>Window</code> object on which the
786: method was invoked. The <span>task source</span> for this <span title="concept-task">task</span> is the <a href="#posted-message-task-source">posted message task
787: source</a>.</p>
788:
789: </li>
790:
791: </ol><p class="note">These steps, with the exception of the second and
792: third steps and the penultimate step, are identical to those in the
793: previous section.</p>
794:
795: <!-- v2: we can merge this section and the previous section when
796: implementations have shipped postMessage(). Anne asked that these
797: sections be kept separate so that implementors can avoid getting
798: confused with the 'port' step. -->
799:
800: </div><h2 id="channel-messaging"><span class="secno">5 </span><dfn>Channel messaging</dfn></h2><h3 id="introduction-0"><span class="secno">5.1 </span>Introduction</h3><p><i>This section is non-normative.</i><p>To enable independent pieces of code (e.g. running in different
801: <span title="browsing context">browsing contexts</span>) to
802: communicate directly, authors can use <a href="#channel-messaging">channel
803: messaging</a>.<p>Communication channels in this mechanisms are implemented as
804: two-ways pipes, with a port at each end. Messages sent in one port
805: are delivered at the other port, and vice-versa. Messages are
806: asynchronous, and delivered as DOM events.<p>To create a connection (two "entangled" ports), the <code title="">MessageChannel()</code> constructor is called:<pre>var channel = new MessageChannel();</pre><p>One of the ports is kept as the local port, and the other port is
807: sent to the remote code, e.g. using <code title="dom-window-postMessage-3"><a href="#dom-window-postmessage-3">postMessage()</a></code>:<pre>otherWindow.postMessage('hello', [channel.port2], 'http://example.com');</pre><p>To send messages, the <code title="dom-MessagpePort-postMessage">postMessage()</code> method on
808: the port is used:<pre>channel.port1.postMessage('hello');</pre><p>To receive messages, one listens to <code title="event-message"><a href="#event-message">message</a></code> events:<pre>channel.port1.onmessage = handleMessage;
809: function handleMessage(event) {
810: // message is in event.data
811: // ...
812: }</pre><h3 id="message-channels"><span class="secno">5.2 </span>Message channels</h3><pre class="idl">[<a href="#dom-messagechannel" title="dom-MessageChannel">Constructor</a>]
813: interface <dfn id="messagechannel">MessageChannel</dfn> {
814: readonly attribute <a href="#messageport">MessagePort</a> <a href="#dom-channel-port1" title="dom-channel-port1">port1</a>;
815: readonly attribute <a href="#messageport">MessagePort</a> <a href="#dom-channel-port2" title="dom-channel-port2">port2</a>;
816: };</pre><dl class="domintro"><dt><var title="">channel</var> = new <code title="dom-MessageChannel"><a href="#dom-messagechannel">MessageChannel</a></code>()</dt>
817:
818: <dd>
819:
820: <p>Returns a new <code><a href="#messagechannel">MessageChannel</a></code> object with two new <code><a href="#messageport">MessagePort</a></code> objects.</p>
821:
822: </dd>
823:
824: <dt><var title="">channel</var> . <code title="dom-MessageChannel-port1">port1</code></dt>
825:
826: <dd>
827:
828: <p>Returns the first <code><a href="#messageport">MessagePort</a></code> object.</p>
829:
830: </dd>
831:
832: <dt><var title="">channel</var> . <code title="dom-MessageChannel-port2">port2</code></dt>
833:
834: <dd>
835:
836: <p>Returns the second <code><a href="#messageport">MessagePort</a></code> object.</p>
837:
838: </dd>
839:
840: </dl><div class="impl">
841:
842: <p>When the <dfn id="dom-messagechannel" title="dom-MessageChannel"><code>MessageChannel()</code></dfn>
843: constructor is called, it must run the following algorithm:</p>
844:
845: <ol><li><p><a href="#create-a-new-messageport-object">Create a new <code>MessagePort</code> object</a>
846: owned by the <span>script's global object</span>, and let <var title="">port1</var> be that object.</li>
847:
848: <li><p><a href="#create-a-new-messageport-object">Create a new <code>MessagePort</code> object</a>
849: owned by the <span>script's global object</span>, and let <var title="">port2</var> be that object.</li>
850:
851: <li><p><a href="#entangle">Entangle</a> the <var title="">port1</var> and <var title="">port2</var> objects.</li>
852:
853: <li><p>Instantiate a new <code><a href="#messagechannel">MessageChannel</a></code> object, and
854: let <var title="">channel</var> be that object.</li>
855:
856: <li><p>Let the <code title="dom-channel-port1"><a href="#dom-channel-port1">port1</a></code>
857: attribute of the <var title="">channel</var> object be <var title="">port1</var>.</p>
858:
859: <li><p>Let the <code title="dom-channel-port2"><a href="#dom-channel-port2">port2</a></code>
860: attribute of the <var title="">channel</var> object be <var title="">port2</var>.</p>
861:
862: <li><p>Return <var title="">channel</var>.</li>
863:
864: </ol><p>This constructor must be visible when the <span>script's global
865: object</span> is either a <code>Window</code> object or an object
866: implementing the <code>WorkerUtils</code> interface.</p>
867:
868: <p>The <dfn id="dom-channel-port1" title="dom-channel-port1"><code>port1</code></dfn> and
869: <dfn id="dom-channel-port2" title="dom-channel-port2"><code>port2</code></dfn> attributes
870: must return the values they were assigned when the
871: <code><a href="#messagechannel">MessageChannel</a></code> object was created.</p>
872:
873: </div><h3 id="message-ports"><span class="secno">5.3 </span>Message ports</h3><p>Each channel has two message ports. Data sent through one port is
874: received by the other port, and vice versa.<pre class="idl">typedef sequence<MessagePort> <dfn id="messageportarray">MessagePortArray</dfn>;
875:
876: interface <dfn id="messageport">MessagePort</dfn> {
877: <!-- v2-onclose readonly attribute boolean <span title="dom-MessagePort-active">active</span>;
878: --> void <a href="#dom-messageport-postmessage" title="dom-MessagePort-postMessage">postMessage</a>(in any message, in optional <a href="#messageportarray">MessagePortArray</a> ports);<!--
879: <span>MessagePort</span> <span title="dom-MessagePort-startConversation">startConversation</span>(in any message);-->
880: void <a href="#dom-messageport-start" title="dom-MessagePort-start">start</a>();
881: void <a href="#dom-messageport-close" title="dom-MessagePort-close">close</a>();
882:
883: // event handlers
884: attribute <span>Function</span> <a href="#handler-messageport-onmessage" title="handler-MessagePort-onmessage">onmessage</a>;
885: };
886: <a href="#messageport">MessagePort</a> implements <span>EventTarget</span>;</pre><dl class="domintro"><!-- v2-onclose
887: <dt><var title="">port</var> . <code title="dom-MessagePort-active">active</code></dt>
888:
889: <dd>
890:
891: <p>Returns true if the port is still active; otherwise, returns false.</p>
892:
893: </dd>
894: --><dt><var title="">port</var> . <code title="dom-MessagePort-poseMessage">postMessage</code>(<var title="">message</var> [, <var title="">ports</var>] )</dt>
895:
896: <dd>
897:
898: <p>Posts a message through the channel, optionally with the given
899: ports.</p>
900:
901: <p>Throws an <code>INVALID_STATE_ERR</code> if the <var title="">ports</var> array is not null and it contains either null
902: entries, duplicate ports, or the source or target port.</p>
903:
904: </dd>
905:
906: <dt><var title="">port</var> . <code title="dom-MessagePort-start"><a href="#dom-messageport-start">start</a></code>()</dt>
907:
908: <dd>
909:
910: <p>Begins dispatching messages received on the port.</p>
911:
912: </dd>
913:
914: <dt><var title="">port</var> . <code title="dom-MessagePort-close"><a href="#dom-messageport-close">close</a></code>()</dt>
915:
916: <dd>
917:
918: <p>Disconnects the port, so that it is no longer active.</p>
919:
920: </dd>
921:
922: </dl><div class="impl">
923:
924: <p>Each <code><a href="#messageport">MessagePort</a></code> object can be entangled with
925: another (a symmetric relationship). Each <code><a href="#messageport">MessagePort</a></code>
926: object also has a <span>task source</span> called the <dfn id="port-message-queue">port
927: message queue</dfn>, initial empty. A <a href="#port-message-queue">port message
928: queue</a> can be enabled or disabled, and is initially
929: disabled. Once enabled, a port can never be disabled again (though
930: messages in the queue can get moved to another queue or removed
931: altogether, which has much the same effect).</p>
932:
933: <p>When the user agent is to <dfn id="create-a-new-messageport-object">create a new
934: <code>MessagePort</code> object</dfn> owned by a <span>script's
935: global object</span> object <var title="">owner</var>, it must
936: instantiate a new <code><a href="#messageport">MessagePort</a></code> object, and let its owner
937: be <var title="">owner</var>.</p>
938:
939: <hr><p>When the user agent is to <dfn id="entangle">entangle</dfn> two
940: <code><a href="#messageport">MessagePort</a></code> objects, it must run the following
941: steps:</p>
942:
943: <ol><li>
944:
945: <p>If one of the ports is already entangled, then disentangle it
946: and the port that it was entangled with.</p>
947:
948: <p class="note">If those two previously entangled ports were the
949: two ports of a <code><a href="#messagechannel">MessageChannel</a></code> object, then that
950: <code><a href="#messagechannel">MessageChannel</a></code> object no longer represents an actual
951: channel: the two ports in that object are no longer entangled.</p>
952:
953: </li>
954:
955: <li><p>Associate the two ports to be entangled, so that they form
956: the two parts of a new channel. (There is no
957: <code><a href="#messagechannel">MessageChannel</a></code> object that represents this
958: channel.)</li>
959:
960: </ol><hr><p>When the user agent is to <dfn id="clone-a-port">clone a port</dfn> <var title="">original port</var>, with the clone being owned by <var title="">owner</var>, it must run the following steps, which return
961: a new <code><a href="#messageport">MessagePort</a></code> object. These steps must be run
962: atomically.</p>
963:
964: <ol><li><p><a href="#create-a-new-messageport-object">Create a new <code>MessagePort</code> object</a>
965: owned by <var title="">owner</var>, and let <var title="">new
966: port</var> be that object.</li>
967:
968: <li><p>Move all the events in the <a href="#port-message-queue">port message queue</a>
969: of <var title="">original port</var> to the <a href="#port-message-queue">port message
970: queue</a> of <var title="">new port</var>, if any, leaving the
971: <var title="">new port</var>'s <a href="#port-message-queue">port message queue</a> in
972: its initial disabled state.</li>
973:
974: <li>
975:
976: <p>If the <var title="">original port</var> is entangled with
977: another port, then run these substeps:</p>
978:
979: <ol><li><p>Let the <var title="">remote port</var> be the port with
980: which the <var title="">original port</var> is entangled.</li>
981:
982: <li><p><a href="#entangle">Entangle</a> the <var title="">remote port</var>
983: and <var title="">new port</var> objects. The <var title="">original port</var> object will be disentangled by this
984: process.</li>
985:
986: </ol></li>
987:
988: <li><p>Return <var title="">new port</var>. It is the
989: clone.</li>
990:
991: </ol><hr><!-- v2-onclose
992: <p>The <dfn title="dom-MessagePort-active"><code>active</code></dfn>
993: attribute must return true if the port is entangled, and false
994: otherwise.</p>
995:
996: <hr>
997: --><p>The <dfn id="dom-messageport-postmessage" title="dom-MessagePort-postMessage"><code>postMessage()</code></dfn>
998: method, when called on a port <var title="">source port</var>, must
999: cause the user agent to run the following steps:</p>
1000:
1001: <ol><li><p>Let <var title="">target port</var> be the port with which
1002: <var title="">source port</var> is entangled, if any.</li>
1003:
1004: <li><p>If the method was called with a second argument <var title="">ports</var> and that argument isn't null, then, if any of
1005: the entries in <var title="">ports</var> are null, if any
1006: <code><a href="#messageport">MessagePort</a></code> object is listed in <var title="">ports</var> more than once, if any of the
1007: <code><a href="#messageport">MessagePort</a></code> objects listed in <var title="">ports</var> have already been cloned once before, or if
1008: any of the entries in <var title="">ports</var> are either the <var title="">source port</var> or the <var title="">target port</var>
1009: (if any), then throw an <code>INVALID_STATE_ERR</code>
1010: exception.</li>
1011:
1012: <li><p>If there is no <var title="">target port</var> (i.e. if <var title="">source port</var> is not entangled), then abort these
1013: steps.</li>
1014: <!-- we don't raise an exception if there is no target port because
1015: this can happen at a moment's notice. we don't return false because
1016: if the port is _about_ to be closed, the message might not be
1017: listened for anyway. -->
1018:
1019: <li><p>Create an event that uses the <code><a href="#messageevent">MessageEvent</a></code>
1020: interface, with the name <code title="event-message"><a href="#event-message">message</a></code>, which does not bubble, is not
1021: cancelable, and has no default action.</li>
1022:
1023: <li><p>Let <var title="">message</var> be the method's first
1024: argument.</li>
1025:
1026: <li><p>Let <var title="">message clone</var> be the result of
1027: obtaining a <span>structured clone</span> of <var title="">message</var>. If this throws an exception, then throw
1028: that exception and abort these steps.</li>
1029:
1030: <li><p>Let the <code title="dom-MessageEvent-data"><a href="#dom-messageevent-data">data</a></code>
1031: attribute of the event have the value of <var title="">message
1032: clone</var>.</li>
1033:
1034: <li><p>If the method was called with a second argument <var title="">ports</var> and that argument isn't null, then run the
1035: following substeps:</p>
1036:
1037: <ol><li>
1038:
1039: <p>Let <var title="">new ports</var> be an empty array.</p>
1040:
1041: <p>For each port in <var title="">ports</var> in turn,
1042: obtain a new port by <a href="#clone-a-port" title="clone a port">cloning</a>
1043: the port with the owner of the <var title="">target port</var>
1044: as the owner of the clone, and append the clone to the <var title="">new ports</var> array.</p>
1045:
1046: <p class="note">If the original <var title="">ports</var>
1047: array was empty, then the <var title="">new ports</var> array will
1048: also be empty.</p>
1049:
1050: </li>
1051:
1052: <li><p>Let the <code title="dom-MessageEvent-ports"><a href="#dom-messageevent-ports">ports</a></code>
1053: attribute of the event be the <var title="">new ports</var>
1054: array.</li>
1055:
1056: </ol></li>
1057:
1058: <li><p>Add the event to the <a href="#port-message-queue">port message queue</a> of <var title="">target port</var>.</li>
1059:
1060: </ol><!--
1061: <hr>
1062:
1063: <p>The <dfn
1064: title="dom-MessagePort-startConversation"><code>startConversation(<var
1065: title="">message</var>)</code></dfn> method is a convenience method
1066: that simplifies create a new <code>MessageChannel</code> and
1067: invoking <code
1068: title="dom-MessagePort-postMessage">postMessage()</code> with one of
1069: the new ports. When invoked on a port <var title="">source
1070: port</var>, it must run the following steps:</p>
1071:
1072: <ol>
1073:
1074: <li><p>Let <var title="">message</var> be the method's first
1075: argument.</p></li>
1076:
1077: <li><p><span>Create a new <code>MessagePort</code> object</span>
1078: owned by the <span>script's global object</span>, and let <var
1079: title="">port1</var> be that object.</p></li>
1080:
1081: <li><p>If the <var title="">source port</var> is not entangled with
1082: another port, then return <var title="">port1</var> and abort these
1083: steps.</p></li>
1084: <!- - we don't raise an exception because this can happen moment's
1085: notice. we don't return null because then we'd end up with
1086: null derefs. better to just let the likely next postMessage call
1087: fall on the floor. - ->
1088:
1089: <li><p>Let <var title="">target port</var> be the port with which
1090: <var title="">source port</var> is entangled.</p></li>
1091:
1092: <li><p><span>Create a new <code>MessagePort</code> object</span>
1093: owned by the owner of the <var title="">target port</var>, and let
1094: <var title="">port2</var> be that object.</p></li>
1095:
1096: <li><p><span>Entangle</span> the <var title="">port1</var> and <var
1097: title="">port2</var> objects.</p></li>
1098:
1099: <li><p>Create an event that uses the <code>MessageEvent</code>
1100: interface, with the name <code
1101: title="event-message">message</code>, which does not bubble, is not
1102: cancelable, and has no default action.</p></li>
1103:
1104: <li><p>Let the <code title="dom-MessageEvent-data">data</code>
1105: attribute of the event have the value of <var
1106: title="">message</var>, the method's first argument.</p></li>
1107:
1108: <li><p>Let the <code
1109: title="dom-MessageEvent-ports">ports</code> attribute
1110: of the event be an array containing only <var
1111: title="">port2</var>.</p></li>
1112:
1113: <li><p>Return <var title="">port1</var> from the method, but
1114: continue with these steps.</p></li>
1115:
1116: <li><p>Add the event to the <span>port message queue</span> of <var
1117: title="">target port</var>.</p></li>
1118:
1119: </ol>
1120: --><hr><p>The <dfn id="dom-messageport-start" title="dom-MessagePort-start"><code>start()</code></dfn>
1121: method must enable its port's <a href="#port-message-queue">port message queue</a>, if it
1122: is not already enabled.</p>
1123:
1124: <p>When a port's <a href="#port-message-queue">port message queue</a> is enabled, the
1125: <span>event loop</span> must use it as one of its <span title="task
1126: source">task sources</span>.</p>
1127:
1128: <p class="note">If the <code>Document</code> of the port's event
1129: listeners' <span title="script's global object">global object</span>
1130: is not <span>fully active</span>, then the messages are lost.</p>
1131: <!-- because of the jump-to-entry-point algorithm first step -->
1132:
1133: <hr><p>The <dfn id="dom-messageport-close" title="dom-MessagePort-close"><code>close()</code></dfn>
1134: method, when called on a port <var title="">local port</var> that is
1135: entangled with another port, must cause the user agents to
1136: disentangle the two ports. If the method is called on a port that is
1137: not entangled, then the method must do nothing.</p>
1138:
1139: <hr><p>The following are the <span>event handlers</span> (and their
1140: corresponding <span title="event handler event type">event handler
1141: event types</span>) that must be supported, as IDL attributes, by
1142: all objects implementing the <code><a href="#messageport">MessagePort</a></code> interface:</p>
1143:
1144: <table><thead><tr><th><span title="event handlers">Event handler</span> <th><span>Event handler event type</span>
1145: <tbody><tr><td><dfn id="handler-messageport-onmessage" title="handler-MessagePort-onmessage"><code>onmessage</code></dfn> <td> <code title="event-message"><a href="#event-message">message</a></code>
1146: </table><p>The first time a <code><a href="#messageport">MessagePort</a></code> object's <code title="handler-MessagePort-onmessage"><a href="#handler-messageport-onmessage">onmessage</a></code> IDL attribute
1147: is set, the port's <a href="#port-message-queue">port message queue</a> must be enabled,
1148: as if the <code title="dom-MessagePort-start"><a href="#dom-messageport-start">start()</a></code> method
1149: had been called.</p>
1150:
1151: </div><h4 id="ports-and-garbage-collection"><span class="secno">5.3.1 </span>Ports and garbage collection</h4><div class="impl">
1152:
1153: <p>When a <code><a href="#messageport">MessagePort</a></code> object <var title="">o</var> is
1154: entangled, user agents must either act as if <var title="">o</var>'s
1155: entangled <code><a href="#messageport">MessagePort</a></code> object has a strong reference to
1156: <var title="">o</var>, or as if <var title="">o</var>'s owner has a
1157: strong reference to <var title="">o</var>.</p>
1158:
1159: <div class="note">
1160:
1161: <p>Thus, a message port can be received, given an event listener,
1162: and then forgotten, and so long as that event listener could
1163: receive a message, the channel will be maintained.</p>
1164:
1165: <p>Of course, if this was to occur on both sides of the channel,
1166: then both ports could be garbage collected, since they would not be
1167: reachable from live code, despite having a strong reference to each
1168: other.</p>
1169:
1170: </div>
1171:
1172: <p>Furthermore, a <code><a href="#messageport">MessagePort</a></code> object must not be
1173: garbage collected while there exists a message in a <span>task
1174: queue</span> that is to be dispatched on that
1175: <code><a href="#messageport">MessagePort</a></code> object, or while the
1176: <code><a href="#messageport">MessagePort</a></code> object's <a href="#port-message-queue">port message queue</a> is
1177: open and there exists a <code title="event-message"><a href="#event-message">message</a></code>
1178: event in that queue.</p>
1179: <!-- we might not need to explicitly say the first part if DOM
1180: Events is fixed to say that events on a task queue prevent GC -->
1181:
1182: <!-- ports in the ports attribute of a MessageEvent that isn't
1183: dispatched yet are safe because the MessageEvent is safe -->
1184:
1185: </div><p class="note">Authors are strongly encouraged to explicitly close
1186: <code><a href="#messageport">MessagePort</a></code> objects to disentangle them, so that their
1187: resources can be recollected. Creating many <code><a href="#messageport">MessagePort</a></code>
1188: objects and discarding them without closing them can lead to high
1.8 ihickson 1189: memory usage.<h2 class="no-num" id="references">References</h2><!--REFS--><p>All references are normative unless marked "Non-normative".</p><!-- Dates are only included for standards older than the Web,
1190: because the newer ones keep changing. --><dl><dt id="refsDOMEVENTS">[DOMEVENTS]</dt>
1.1 ihickson 1191: <!--
1192: <dd><cite><a
1193: href="http://www.w3.org/TR/DOM-Level-3-Events/">Document Object
1194: Model (DOM) Level 3 Events Specification</a></cite>,
1.8 ihickson 1195: B. Höhrmann, P. Le Hegaret, T. Pixley. W3C.</dd>
1.1 ihickson 1196: -->
1197: <dd><cite><a href="http://dev.w3.org/2006/webapi/DOM-Level-3-Events/html/DOM3-Events.html">Document
1198: Object Model (DOM) Level 3 Events Specification</a></cite>,
1.8 ihickson 1199: D. Schepers. W3C.</dd>
1.1 ihickson 1200:
1201: <dt id="refsEVENTSOURCE">[EVENTSOURCE]</dt>
1202: <!--
1203: <dd><cite><a href="http://www.w3.org/TR/eventsource/">Server-Sent
1.8 ihickson 1204: Events</a></cite>, I. Hickson. W3C.</dd>
1.1 ihickson 1205: -->
1206: <dd><cite><a href="http://dev.w3.org/html5/eventsource/">Server-Sent
1.8 ihickson 1207: Events</a></cite>, I. Hickson. W3C.</dd>
1.1 ihickson 1208:
1.6 ihickson 1209: <dt id="refsHTML5">[HTML5]</dt>
1.8 ihickson 1210: <dd><cite><a href="http://dev.w3.org/html5/spec/">HTML5</a></cite>,
1211: I. Hickson, D. Hyatt. W3C.</dd>
1.1 ihickson 1212:
1213: <dt id="refsWEBSOCKET">[WEBSOCKET]</dt>
1214: <!--
1215: <dd><cite><a href="http://www.w3.org/TR/websockets/">The Web Sockets
1.8 ihickson 1216: API</a></cite>, I. Hickson. W3C.</dd>
1.1 ihickson 1217: -->
1218: <dd><cite><a href="http://dev.w3.org/html5/websockets/">The Web Sockets
1.8 ihickson 1219: API</a></cite>, I. Hickson. W3C.</dd>
1.1 ihickson 1220:
1.6 ihickson 1221: </dl><h2 class="no-num" id="acknowledgements">Acknowledgements</h2><p>For a full list of acknowledgements, please see the HTML5
1222: specification. <a href="#refsHTML5">[HTML5]</a>
Webmaster