1 {{+bindTo:partials.standard_nacl_article}}
3 <section id=
"progress-events">
4 <span id=
"devcycle-progress-events"></span><h1 id=
"progress-events"><span id=
"devcycle-progress-events"></span>Progress Events
</h1>
5 <div class=
"contents local" id=
"contents" style=
"display: none">
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">Handling progress events
</a></li>
9 <li><a class=
"reference internal" href=
"#displaying-load-status" id=
"id5">Displaying load status
</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>
12 <li><a class=
"reference internal" href=
"#the-exitstatus-attribute" id=
"id8">The
<code>exitStatus
</code> attribute
</a></li>
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
17 glossary below). This section describes how to monitor progress events (events
18 that occur during the loading and execution of a Native Client module). This
19 section 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>
22 The load_progress example illustrates progress event handling. You can find
23 this code in the
<code>/pepper_
<version
>/examples/tutorial/load_progress/
</code>
24 directory in the Native Client SDK download.
26 <h2 id=
"module-loading-and-progress-events">Module loading and progress events
</h2>
27 <p>The Native Client runtime reports a set of state changes during the module
28 loading process by means of DOM progress events. This set of events is a direct
29 port of the proposed W3C
<a class=
"reference external" href=
"http://www.w3.org/TR/progress-events/">Progress Events
</a> standard (except for the
<code>crash
</code>
30 event which is an extension of the W3C standard). The following table lists the
31 events types reported by the Native Client runtime:
</p>
32 <table border=
"1" class=
"docutils">
35 <thead valign=
"bottom">
36 <tr class=
"row-odd"><th class=
"head">Event
</th>
37 <th class=
"head">Times
41 <th class=
"head">How you might
46 <tr class=
"row-even"><td><dl class=
"first last docutils">
47 <dt><code>loadstart
</code></dt>
48 <dd>Native Client has started to
49 load a Native Client module.
</dd>
67 “Loading...
”</td>
69 <tr class=
"row-odd"><td><dl class=
"first last docutils">
70 <dt><code>progress
</code></dt>
71 <dd>Part of the module has been
78 <code>loadstart
</code>
84 <tr class=
"row-even"><td><dl class=
"first last docutils">
85 <dt><code>error
</code></dt>
86 <dd>The Native Client module failed
87 to start execution (includes any
88 error before or during
89 initialization of the module).
90 The
<code>lastError
</code> attribute
91 (mentioned later) provides
93 (initialization failed, sel_ldr
94 did not start, and so on).
</dd>
100 <code>progress
</code>
104 <code>loadstart
</code>
106 <code>progress
</code>
115 <tr class=
"row-odd"><td><dl class=
"first last docutils">
116 <dt><code>abort
</code></dt>
117 <dd>Loading of the NativeClient
118 module was aborted by the user.
</dd>
124 <code>progress
</code>
128 <code>loadstart
</code>
130 <code>progress
</code>
139 <tr class=
"row-even"><td><dl class=
"first last docutils">
140 <dt><code>load
</code></dt>
141 <dd>The Native Client module was
142 successfully loaded, and
143 execution was started.
144 (The module was initialized
152 <code>progress
</code>
156 <code>loadstart
</code>
158 <code>progress
</code>
164 <tr class=
"row-odd"><td><dl class=
"first last docutils">
165 <dt><code>loadend
</code></dt>
166 <dd>Loading of the Native Client
167 module has stopped. Load
168 succeeded (
<code>load
</code>), failed
169 (
<code>error
</code>), or was aborted
170 (
<code>abort
</code>).
</dd>
176 <code>abort
</code>, or
187 <tr class=
"row-even"><td><dl class=
"first last docutils">
188 <dt><code>crash
</code></dt>
189 <dd>The Native Client module is not
190 responding (died on an
191 <code>assert()
</code> or
<code>exit()
</code>)
192 after a successful load. This
193 event is unique to Native Client
194 and is not part of the W3C
195 Progress Events standard. The
196 <code>exitStatus
</code> attribute
197 provides the numeric exit
204 <code>loadend
</code>.
</td>
213 <p>The sequence of events for a successful module load is as follows:
</p>
214 <table border=
"1" class=
"docutils">
217 <thead valign=
"bottom">
218 <tr class=
"row-odd"><th class=
"head">Event is dispatched
</th>
219 <th class=
"head">... then this task is attempted
</th>
223 <tr class=
"row-even"><td><code>loadstart
</code></td>
224 <td>load the manifest file
</td>
226 <tr class=
"row-odd"><td><code>progress
</code> (first time)
</td>
227 <td>load the module
</td>
229 <tr class=
"row-even"><td><code>progress
</code> (subsequent times)
</td>
232 <tr class=
"row-odd"><td><code>load
</code></td>
233 <td>start executing the module
</td>
235 <tr class=
"row-even"><td><code>loadend
</code></td>
240 <p>Errors that occur during loading are logged to the JavaScript console in Google
241 Chrome (select the menu icon
<img alt=
"menu-icon" src=
"/native-client/images/menu-icon.png" /> > Tools
> JavaScript console).
</p>
242 <h2 id=
"handling-progress-events">Handling progress events
</h2>
243 <p>You should add event listeners in a
<code><script
></code> element to listen for these
244 events before the
<code><embed
></code> element is parsed. For example, the following code
245 adds a listener for the
<code>load
</code> event to a parent
<code><div
></code> element that also
246 contains the Native Client
<code><embed
></code> element. First, the listener is
247 attached. Then, when the listener
<code><div
></code> receives the
<code>load
</code> event, the
248 JavaScript
<code>moduleDidLoad()
</code> function is called. The following code is
249 excerpted from the example in
<code>getting_started/part1/
</code>:
</p>
250 <pre class=
"prettyprint">
252 Load the published pexe.
253 Note: Since this module does not use any real-estate in the browser, its
254 width and height are set to
0.
256 Note: The
<embed
> element is wrapped inside a
<div
>, which has both a 'load'
257 and a 'message' event listener attached. This wrapping method is used
258 instead of attaching the event listeners directly to the
<embed
> element to
259 ensure that the listeners are active before the NaCl module 'load' event
260 fires. This also allows you to use PPB_Messaging.PostMessage() (in C) or
261 pp::Instance.PostMessage() (in C++) from within the initialization code in
264 <div id=
"listener
">
265 <script type=
"text/javascript
">
266 var listener = document.getElementById('listener');
267 listener.addEventListener('load', moduleDidLoad, true);
268 listener.addEventListener('message', handleMessage, true);
271 <embed id=
"hello_tutorial
"
273 src=
"hello_tutorial.nmf
"
274 type=
"application/x-pnacl
" /
>
277 <p>Event listeners can be added to any DOM object. Since listeners set at the
278 outermost scope capture events for their contained elements, you can set
279 listeners on outer elements (including the
<code><body
></code> element) to handle events
280 from inner elements. For more information, see the W3 specifications for
<a class=
"reference external" href=
"http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow-capture">event
282 <a class=
"reference external" href=
"http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-registration">event listener registration
</a>.
</p>
283 <h2 id=
"displaying-load-status">Displaying load status
</h2>
284 <p>One common response to progress events is to display the percentage of the
285 module that has been loaded. In the load_progress example, when the
<code>progress
</code>
286 event is triggered the
<code>moduleLoadProgress
</code> function is called. This function
287 uses the
<code>lengthComputable
</code>,
<code>loaded
</code>, and
<code>total
</code> attributes (described
288 in the proposed W3C
<a class=
"reference external" href=
"http://www.w3.org/TR/progress-events/">Progress Events
</a>
289 standard) of the event to calculate the percentage of the module that has
291 <pre class=
"prettyprint">
292 function moduleLoadProgress(event) {
293 var loadPercent =
0.0;
294 var loadPercentString;
295 if (event.lengthComputable
&& event.total
> 0) {
296 loadPercent = event.loaded / event.total *
100.0;
297 loadPercentString = loadPercent + '%';
298 common.logMessage('progress: ' + event.url + ' ' + loadPercentString +
299 ' (' + event.loaded + ' of ' + event.total + ' bytes)');
301 // The total length is not yet known.
302 common.logMessage('progress: Computing...');
306 <h2 id=
"the-lasterror-attribute">The
<code>lastError
</code> attribute
</h2>
307 <p>The
<code><embed
></code> element has a
<code>lastError
</code> attribute that is set to an
308 informative string whenever a load failure (an
<code>error
</code> or
<code>abort
</code> event)
310 <p>The following code adds an event listener before the
<code><embed
></code> element to
311 capture and handle an error in loading the Native Client module. The
312 <code>handleError()
</code> function listens for an
<code>error
</code> event. When an error occurs,
313 this function prints the contents of the
<code>lastError
</code> attribute
314 (
<code>embed_element.lastError
</code>) as an alert.
</p>
315 <pre class=
"prettyprint">
316 function domContentLoaded(name, tc, config, width, height) {
317 var listener = document.getElementById('listener');
319 listener.addEventListener('error', moduleLoadError, true);
321 common.createNaClModule(name, tc, config, width, height);
324 function moduleLoadError() {
325 common.logMessage('error: ' + common.naclModule.lastError);
328 <h2 id=
"the-readystate-attribute">The
<code>readyState
</code> attribute
</h2>
329 <p>You can use the
<code>readyState
</code> attribute to monitor the loading process. This
330 attribute is particularly useful if you don
’t care about the details of
331 individual progress events or when you want to poll for current load state
332 without registering listeners. The value of
<code>readyState
</code> progresses as follows
333 for a successful load:
</p>
334 <table border=
"1" class=
"docutils">
337 <thead valign=
"bottom">
338 <tr class=
"row-odd"><th class=
"head">Event
</th>
339 <th class=
"head"><code>readyState
</code> value
</th>
343 <tr class=
"row-even"><td>(before any events)
</td>
344 <td><code>undefined
</code></td>
346 <tr class=
"row-odd"><td><code>loadstart
</code></td>
349 <tr class=
"row-even"><td><code>progress
</code></td>
352 <tr class=
"row-odd"><td><code>load
</code></td>
355 <tr class=
"row-even"><td><code>loadend
</code></td>
360 <p>The following code demonstrates how to monitor the loading process using the
361 <code>readyState
</code> attribute. As before, the script that adds the event listeners
362 precedes the
<code><embed
></code> element so that the event listeners are in place before
363 the progress events are generated.
</p>
364 <pre class=
"prettyprint">
367 <body id=
"body
">
368 <div id=
"status_div
">
370 <div id=
"listener_div
">
371 <script type=
"text/javascript
">
372 var stat = document.getElementById('status_div');
373 function handleEvent(e) {
374 var embed_element = document.getElementById('my_embed');
376 '
<br
>' + e.type + ': readyState = ' + embed_element.readyState;
378 var listener_element = document.getElementById('listener_div');
379 listener_element.addEventListener('loadstart', handleEvent, true);
380 listener_element.addEventListener('progress', handleEvent, true);
381 listener_element.addEventListener('load', handleEvent, true);
382 listener_element.addEventListener('loadend', handleEvent, true);
385 name=
"naclModule
"
386 id=
"my_embed
"
388 src=
"my_example.nmf
"
389 type=
"application/x-pnacl
" /
>
394 <h2 id=
"the-exitstatus-attribute">The
<code>exitStatus
</code> attribute
</h2>
395 <p>This read-only attribute is set if the application calls
<code>exit(n)
</code>,
396 <code>abort()
</code>, or crashes. Since NaCl modules are event handlers, there is no
397 need to call
<code>exit(n)
</code> in normal execution. If the module does exit or
398 crash, the
<code>crash
</code> progress event is issued and the
<code>exitStatus
</code> attribute
399 will contain the numeric value of the exit status:
</p>
400 <ul class=
"small-gap">
401 <li>In the case of explicit calls to
<code>exit(n)
</code>, the numeric value will be
402 <code>n
</code> (between
0 and
255).
</li>
403 <li>In the case of crashes and calls to
<code>abort()
</code>, the numeric value will
404 be non-zero, but the exact value will depend on the chosen libc and the
405 target architecture, and may change in the future. Applications should not
406 rely on the
<code>exitStatus
</code> value being stable in these cases, but the value
407 may nevertheless be useful for temporary debugging.
</li>
411 {{/partials.standard_nacl_article}}