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