OLD | NEW |
1 {{+bindTo:partials.standard_nacl_api}} | 1 {{+bindTo:partials.standard_nacl_api}} |
2 | 2 |
3 <section id="progress-events"> | 3 <section id="progress-events"> |
4 <h1 id="progress-events">Progress Events</h1> | 4 <h1 id="progress-events">Progress Events</h1> |
5 <div class="contents local" id="contents" style="display: none"> | 5 <div class="contents local" id="contents" style="display: none"> |
6 <ul class="small-gap"> | 6 <ul class="small-gap"> |
7 <li><a class="reference internal" href="#module-loading-and-progress-events" id=
"id3">Module loading and progress events</a></li> | 7 <li><a class="reference internal" href="#module-loading-and-progress-events" id=
"id3">Module loading and progress events</a></li> |
8 <li><a class="reference internal" href="#handling-progress-events" id="id4">Hand
ling progress events</a></li> | 8 <li><a class="reference internal" href="#handling-progress-events" id="id4">Hand
ling progress events</a></li> |
9 <li><a class="reference internal" href="#displaying-load-status" id="id5">Displa
ying load status</a></li> | 9 <li><a class="reference internal" href="#displaying-load-status" id="id5">Displa
ying load status</a></li> |
10 <li><a class="reference internal" href="#the-lasterror-attribute" id="id6">The <
code>lastError</code> attribute</a></li> | 10 <li><a class="reference internal" href="#the-lasterror-attribute" id="id6">The <
code>lastError</code> attribute</a></li> |
11 <li><a class="reference internal" href="#the-readystate-attribute" id="id7">The
<code>readyState</code> attribute</a></li> | 11 <li><a class="reference internal" href="#the-readystate-attribute" id="id7">The
<code>readyState</code> attribute</a></li> |
12 <li><a class="reference internal" href="#the-exitstatus-attribute" id="id8">The
<code>exitStatus</code> attribute</a></li> | 12 <li><a class="reference internal" href="#the-exitstatus-attribute" id="id8">The
<code>exitStatus</code> attribute</a></li> |
13 </ul> | 13 </ul> |
14 | 14 |
15 </div><p>There are five types of events that developers can respond to in Native
Client: | 15 </div><p>There are five types of events that developers can respond to in Native
Client: |
16 progress, message, view change, focus, and input events (each described in the | 16 progress, message, view change, focus, and input events (each described in the |
17 glossary below). This chapter describes how to monitor progress events (events | 17 glossary below). This chapter describes how to monitor progress events (events |
18 that occur during the loading and execution of a Native Client module). This | 18 that occur during the loading and execution of a Native Client module). This |
19 chapter assumes you are familiar with the material presented in the | 19 chapter assumes you are familiar with the material presented in the |
20 <a class="reference internal" href="/native-client/overview.html"><em>Technical
Overview</em></a>.</p> | 20 <a class="reference internal" href="/native-client/overview.html"><em>Technical
Overview</em></a>.</p> |
21 <aside class="note"> | 21 <aside class="note"> |
22 The load_progress example illustrates progress event handling. You can find | 22 The load_progress example illustrates progress event handling. You can find |
23 this code in the <code>/examples/tutorial/load_progress/</code> directory in the
Native | 23 this code in the <code>/examples/tutorial/load_progress/</code> directory in the
Native |
24 Client SDK download. | 24 Client SDK download. |
25 </aside> | 25 </aside> |
26 <section id="module-loading-and-progress-events"> | |
27 <h2 id="module-loading-and-progress-events">Module loading and progress events</
h2> | 26 <h2 id="module-loading-and-progress-events">Module loading and progress events</
h2> |
28 <p>The Native Client runtime reports a set of state changes during the module | 27 <p>The Native Client runtime reports a set of state changes during the module |
29 loading process by means of DOM progress events. This set of events is a direct | 28 loading process by means of DOM progress events. This set of events is a direct |
30 port of the proposed W3C <a class="reference external" href="http://www.w3.org/T
R/progress-events/">Progress Events</a> standard (except for the <code>crash</co
de> | 29 port of the proposed W3C <a class="reference external" href="http://www.w3.org/T
R/progress-events/">Progress Events</a> standard (except for the <code>crash</co
de> |
31 event which is an extension of the W3C standard). The following table lists the | 30 event which is an extension of the W3C standard). The following table lists the |
32 events types reported by the Native Client runtime:</p> | 31 events types reported by the Native Client runtime:</p> |
33 <table border="1" class="docutils"> | 32 <table border="1" class="docutils"> |
34 <colgroup> | 33 <colgroup> |
35 </colgroup> | 34 </colgroup> |
36 <thead valign="bottom"> | 35 <thead valign="bottom"> |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 <tr class="row-odd"><td><code>load</code></td> | 249 <tr class="row-odd"><td><code>load</code></td> |
251 <td>start executing the module</td> | 250 <td>start executing the module</td> |
252 </tr> | 251 </tr> |
253 <tr class="row-even"><td><code>loadend</code></td> | 252 <tr class="row-even"><td><code>loadend</code></td> |
254 <td> </td> | 253 <td> </td> |
255 </tr> | 254 </tr> |
256 </tbody> | 255 </tbody> |
257 </table> | 256 </table> |
258 <p>Errors that occur during loading are logged to the JavaScript console in Goog
le | 257 <p>Errors that occur during loading are logged to the JavaScript console in Goog
le |
259 Chrome (select the menu icon <img alt="menu-icon" src="/native-client/images/men
u-icon.png" /> > Tools > JavaScript console).</p> | 258 Chrome (select the menu icon <img alt="menu-icon" src="/native-client/images/men
u-icon.png" /> > Tools > JavaScript console).</p> |
260 </section><section id="handling-progress-events"> | |
261 <h2 id="handling-progress-events">Handling progress events</h2> | 259 <h2 id="handling-progress-events">Handling progress events</h2> |
262 <p>You should add event listeners in a <code><script></code> element to li
sten for these | 260 <p>You should add event listeners in a <code><script></code> element to li
sten for these |
263 events before the <code><embed></code> element is parsed. For example, the
following code | 261 events before the <code><embed></code> element is parsed. For example, the
following code |
264 adds a listener for the <code>load</code> event to a parent <code><div></c
ode> element that also | 262 adds a listener for the <code>load</code> event to a parent <code><div></c
ode> element that also |
265 contains the Native Client <code><embed></code> element. First, the listen
er is | 263 contains the Native Client <code><embed></code> element. First, the listen
er is |
266 attached. Then, when the listener <code><div></code> receives the <code>lo
ad</code> event, the | 264 attached. Then, when the listener <code><div></code> receives the <code>lo
ad</code> event, the |
267 JavaScript <code>moduleDidLoad()</code> function is called. The following code i
s | 265 JavaScript <code>moduleDidLoad()</code> function is called. The following code i
s |
268 excerpted from the example in <code>getting_started/part1/</code>:</p> | 266 excerpted from the example in <code>getting_started/part1/</code>:</p> |
269 <pre class="prettyprint"> | 267 <pre class="prettyprint"> |
270 <!-- | 268 <!-- |
(...skipping 21 matching lines...) Expand all Loading... |
292 src="hello_tutorial.nmf" | 290 src="hello_tutorial.nmf" |
293 type="application/x-pnacl" /> | 291 type="application/x-pnacl" /> |
294 </div> | 292 </div> |
295 </pre> | 293 </pre> |
296 <p>Event listeners can be added to any DOM object. Since listeners set at the | 294 <p>Event listeners can be added to any DOM object. Since listeners set at the |
297 outermost scope capture events for their contained elements, you can set | 295 outermost scope capture events for their contained elements, you can set |
298 listeners on outer elements (including the <code><body></code> element) to
handle events | 296 listeners on outer elements (including the <code><body></code> element) to
handle events |
299 from inner elements. For more information, see the W3 specifications for <a clas
s="reference external" href="http://www.w3.org/TR/DOM-Level-2-Events/events.html
#Events-flow-capture">event | 297 from inner elements. For more information, see the W3 specifications for <a clas
s="reference external" href="http://www.w3.org/TR/DOM-Level-2-Events/events.html
#Events-flow-capture">event |
300 flow capture</a> and | 298 flow capture</a> and |
301 <a class="reference external" href="http://www.w3.org/TR/DOM-Level-2-Events/even
ts.html#Events-registration">event listener registration</a>.</p> | 299 <a class="reference external" href="http://www.w3.org/TR/DOM-Level-2-Events/even
ts.html#Events-registration">event listener registration</a>.</p> |
302 </section><section id="displaying-load-status"> | |
303 <h2 id="displaying-load-status">Displaying load status</h2> | 300 <h2 id="displaying-load-status">Displaying load status</h2> |
304 <p>One common response to progress events is to display the percentage of the | 301 <p>One common response to progress events is to display the percentage of the |
305 module that has been loaded. In the load_progress example, when the <code>progre
ss</code> | 302 module that has been loaded. In the load_progress example, when the <code>progre
ss</code> |
306 event is triggered the <code>moduleLoadProgress</code> function is called. This
function | 303 event is triggered the <code>moduleLoadProgress</code> function is called. This
function |
307 uses the <code>lengthComputable</code>, <code>loaded</code>, and <code>total</co
de> attributes (described | 304 uses the <code>lengthComputable</code>, <code>loaded</code>, and <code>total</co
de> attributes (described |
308 in the proposed W3C <a class="reference external" href="http://www.w3.org/TR/pro
gress-events/">Progress Events</a> | 305 in the proposed W3C <a class="reference external" href="http://www.w3.org/TR/pro
gress-events/">Progress Events</a> |
309 standard) of the event to calculate the percentage of the module that has | 306 standard) of the event to calculate the percentage of the module that has |
310 loaded.</p> | 307 loaded.</p> |
311 <pre class="prettyprint"> | 308 <pre class="prettyprint"> |
312 function moduleLoadProgress(event) { | 309 function moduleLoadProgress(event) { |
313 var loadPercent = 0.0; | 310 var loadPercent = 0.0; |
314 var loadPercentString; | 311 var loadPercentString; |
315 if (event.lengthComputable && event.total > 0) { | 312 if (event.lengthComputable && event.total > 0) { |
316 loadPercent = event.loaded / event.total * 100.0; | 313 loadPercent = event.loaded / event.total * 100.0; |
317 loadPercentString = loadPercent + '%'; | 314 loadPercentString = loadPercent + '%'; |
318 common.logMessage('progress: ' + event.url + ' ' + loadPercentString + | 315 common.logMessage('progress: ' + event.url + ' ' + loadPercentString + |
319 ' (' + event.loaded + ' of ' + event.total + ' bytes)'); | 316 ' (' + event.loaded + ' of ' + event.total + ' bytes)'); |
320 } else { | 317 } else { |
321 // The total length is not yet known. | 318 // The total length is not yet known. |
322 common.logMessage('progress: Computing...'); | 319 common.logMessage('progress: Computing...'); |
323 } | 320 } |
324 } | 321 } |
325 </pre> | 322 </pre> |
326 </section><section id="the-lasterror-attribute"> | |
327 <h2 id="the-lasterror-attribute">The <code>lastError</code> attribute</h2> | 323 <h2 id="the-lasterror-attribute">The <code>lastError</code> attribute</h2> |
328 <p>The <code><embed></code> element has a <code>lastError</code> attribute
that is set to an | 324 <p>The <code><embed></code> element has a <code>lastError</code> attribute
that is set to an |
329 informative string whenever a load failure (an <code>error</code> or <code>abort
</code> event) | 325 informative string whenever a load failure (an <code>error</code> or <code>abort
</code> event) |
330 occurs.</p> | 326 occurs.</p> |
331 <p>The following code adds an event listener before the <code><embed></cod
e> element to | 327 <p>The following code adds an event listener before the <code><embed></cod
e> element to |
332 capture and handle an error in loading the Native Client module. The | 328 capture and handle an error in loading the Native Client module. The |
333 <code>handleError()</code> function listens for an <code>error</code> event. Whe
n an error occurs, | 329 <code>handleError()</code> function listens for an <code>error</code> event. Whe
n an error occurs, |
334 this function prints the contents of the <code>lastError</code> attribute | 330 this function prints the contents of the <code>lastError</code> attribute |
335 (<code>embed_element.lastError</code>) as an alert.</p> | 331 (<code>embed_element.lastError</code>) as an alert.</p> |
336 <pre class="prettyprint"> | 332 <pre class="prettyprint"> |
337 function domContentLoaded(name, tc, config, width, height) { | 333 function domContentLoaded(name, tc, config, width, height) { |
338 var listener = document.getElementById('listener'); | 334 var listener = document.getElementById('listener'); |
339 ... | 335 ... |
340 listener.addEventListener('error', moduleLoadError, true); | 336 listener.addEventListener('error', moduleLoadError, true); |
341 ... | 337 ... |
342 common.createNaClModule(name, tc, config, width, height); | 338 common.createNaClModule(name, tc, config, width, height); |
343 } | 339 } |
344 | 340 |
345 function moduleLoadError() { | 341 function moduleLoadError() { |
346 common.logMessage('error: ' + common.naclModule.lastError); | 342 common.logMessage('error: ' + common.naclModule.lastError); |
347 } | 343 } |
348 </pre> | 344 </pre> |
349 </section><section id="the-readystate-attribute"> | |
350 <h2 id="the-readystate-attribute">The <code>readyState</code> attribute</h2> | 345 <h2 id="the-readystate-attribute">The <code>readyState</code> attribute</h2> |
351 <p>You can use the <code>readyState</code> attribute to monitor the loading proc
ess. This | 346 <p>You can use the <code>readyState</code> attribute to monitor the loading proc
ess. This |
352 attribute is particularly useful if you don’t care about the details of | 347 attribute is particularly useful if you don’t care about the details of |
353 individual progress events or when you want to poll for current load state | 348 individual progress events or when you want to poll for current load state |
354 without registering listeners. The value of <code>readyState</code> progresses a
s follows | 349 without registering listeners. The value of <code>readyState</code> progresses a
s follows |
355 for a successful load:</p> | 350 for a successful load:</p> |
356 <table border="1" class="docutils"> | 351 <table border="1" class="docutils"> |
357 <colgroup> | 352 <colgroup> |
358 </colgroup> | 353 </colgroup> |
359 <thead valign="bottom"> | 354 <thead valign="bottom"> |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 <embed | 401 <embed |
407 name="naclModule" | 402 name="naclModule" |
408 id="my_embed" | 403 id="my_embed" |
409 width=0 height=0 | 404 width=0 height=0 |
410 src="my_example.nmf" | 405 src="my_example.nmf" |
411 type="application/x-pnacl" /> | 406 type="application/x-pnacl" /> |
412 </div> | 407 </div> |
413 </body> | 408 </body> |
414 </html> | 409 </html> |
415 </pre> | 410 </pre> |
416 </section><section id="the-exitstatus-attribute"> | |
417 <h2 id="the-exitstatus-attribute">The <code>exitStatus</code> attribute</h2> | 411 <h2 id="the-exitstatus-attribute">The <code>exitStatus</code> attribute</h2> |
418 <p>This read-only attribute is set if the application calls <code>exit(n)</code>
, | 412 <p>This read-only attribute is set if the application calls <code>exit(n)</code>
, |
419 <code>abort()</code>, or crashes. Since NaCl modules are event handlers, there i
s no | 413 <code>abort()</code>, or crashes. Since NaCl modules are event handlers, there i
s no |
420 need to call <code>exit(n)</code> in normal execution. If the module does exit o
r | 414 need to call <code>exit(n)</code> in normal execution. If the module does exit o
r |
421 crash, the <code>crash</code> progress event is issued and the <code>exitStatus<
/code> attribute | 415 crash, the <code>crash</code> progress event is issued and the <code>exitStatus<
/code> attribute |
422 will contain the numeric value of the exit status:</p> | 416 will contain the numeric value of the exit status:</p> |
423 <ul class="small-gap"> | 417 <ul class="small-gap"> |
424 <li>In the case of explicit calls to <code>exit(n)</code>, the numeric value wil
l be | 418 <li>In the case of explicit calls to <code>exit(n)</code>, the numeric value wil
l be |
425 <code>n</code> (between 0 and 255).</li> | 419 <code>n</code> (between 0 and 255).</li> |
426 <li>In the case of crashes and calls to <code>abort()</code>, the numeric value
will | 420 <li>In the case of crashes and calls to <code>abort()</code>, the numeric value
will |
427 be non-zero, but the exact value will depend on the chosen libc and the | 421 be non-zero, but the exact value will depend on the chosen libc and the |
428 target architecture, and may change in the future. Applications should not | 422 target architecture, and may change in the future. Applications should not |
429 rely on the <code>exitStatus</code> value being stable in these cases, but the v
alue | 423 rely on the <code>exitStatus</code> value being stable in these cases, but the v
alue |
430 may nevertheless be useful for temporary debugging.</li> | 424 may nevertheless be useful for temporary debugging.</li> |
431 </ul> | 425 </ul> |
432 </section></section> | 426 </section> |
433 | 427 |
434 {{/partials.standard_nacl_api}} | 428 {{/partials.standard_nacl_api}} |
OLD | NEW |