Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / native_client_sdk / src / doc / devguide / coding / progress-events.rst
1 .. _devcycle-progress-events:
2
3 ###############
4 Progress Events
5 ###############
6
7 .. contents::
8   :local:
9   :backlinks: none
10   :depth: 2
11
12 There are five types of events that developers can respond to in Native Client:
13 progress, message, view change, focus, and input events (each described in the
14 glossary below). This section describes how to monitor progress events (events
15 that occur during the loading and execution of a Native Client module). This
16 section assumes you are familiar with the material presented in the
17 :doc:`Technical Overview <../../overview>`.
18
19 .. Note::
20   :class: note
21
22   The load_progress example illustrates progress event handling. You can find
23   this code in the ``/pepper_<version>/examples/tutorial/load_progress/`` 
24   directory in the Native Client SDK download.
25
26 Module loading and progress events
27 ==================================
28
29 The Native Client runtime reports a set of state changes during the module
30 loading process by means of DOM progress events. This set of events is a direct
31 port of the proposed W3C `Progress Events
32 <http://www.w3.org/TR/progress-events/>`_ standard (except for the ``crash``
33 event which is an extension of the W3C standard). The following table lists the
34 events types reported by the Native Client runtime:
35
36 +----------------------------------+-----------+---------------+---------------+
37 | Event                            | Times     | When          | How you might |
38 |                                  | triggered | triggered     | respond       |
39 +==================================+===========+===============+===============+
40 |``loadstart``                     | once      | The           | Display a     |
41 |  Native Client has started to    |           | first         | status        |
42 |  load a Native Client module.    |           | progress      | message, such |
43 |                                  |           | event         | as            |
44 |                                  |           | after the     | "Loading..."  |
45 |                                  |           | Native Client |               |
46 |                                  |           | module is     |               |
47 |                                  |           | instantiated  |               |
48 |                                  |           | and           |               |
49 |                                  |           | initialized.  |               |
50 +----------------------------------+-----------+---------------+---------------+
51 |``progress``                      | zero or   | After         | Display a     |
52 |  Part of the module has been     | more      | ``loadstart`` | progress bar. |
53 |  loaded.                         |           | has been      |               |
54 |                                  |           | dispatched.   |               |
55 +----------------------------------+-----------+---------------+---------------+
56 |``error``                         | zero or   | After the last| Inform user   |
57 |  The Native Client module failed | once      | ``progress``  | that the      |
58 |  to start execution (includes any|           | event has been| application   |
59 |  error before or during          |           | dispatched,   | failed to     |
60 |  initialization of the module).  |           | or after      | load.         |
61 |  The ``lastError`` attribute     |           | ``loadstart`` |               |
62 |  (mentioned later) provides      |           | if no         |               |
63 |  details on the error            |           | ``progress``  |               |
64 |  (initialization failed, sel_ldr |           | event was     |               |
65 |  did not start, and so on).      |           | dispatched.   |               |
66 +----------------------------------+-----------+---------------+---------------+
67 |``abort``                         | zero or   | After the last| It's not      |
68 |  Loading of the NativeClient     | once      | ``progress``  | likely you    |
69 |  module was aborted by the user. |           | event has been| will want to  |
70 |                                  |           | dispatched, or| respond to    |
71 |                                  |           | after         | this event.   |
72 |                                  |           | ``loadstart`` |               |
73 |                                  |           | if no         |               |
74 |                                  |           | ``progress``  |               |
75 |                                  |           | event was     |               |
76 |                                  |           | dispatched.   |               |
77 +----------------------------------+-----------+---------------+---------------+
78 |``load``                          | zero or   | After the     | Remove the    |
79 |  The Native Client module was    | once      | last          | progress bar. |
80 |  successfully loaded, and        |           | ``progress``  |               |
81 |  execution was started.          |           | event has been|               |
82 |  (The module was initialized     |           | dispatched, or|               |
83 |  successfully.)                  |           | after         |               |
84 |                                  |           | ``loadstart`` |               |
85 |                                  |           | if no         |               |
86 |                                  |           | ``progress``  |               |
87 |                                  |           | event was     |               |
88 |                                  |           | dispatched.   |               |
89 +----------------------------------+-----------+---------------+---------------+
90 |``loadend``                       | once      | After an      | Indicate      |
91 |  Loading of the Native Client    |           | ``error``,    | loading is    |
92 |  module has stopped. Load        |           | ``abort``, or | over          |
93 |  succeeded (``load``), failed    |           | ``load``      | (regardless of|
94 |  (``error``), or was aborted     |           | event was     | failure or    |
95 |  (``abort``).                    |           | dispatched.   | not).         | 
96 +----------------------------------+-----------+---------------+---------------+
97 |``crash``                         | zero or   | After a       | Notify user   |
98 |  The Native Client module is not | once      | ``loadend``.  | that the      |
99 |  responding (died on an          |           |               | module did    |
100 |  ``assert()`` or ``exit()``)     |           |               | something     |
101 |  after a successful load. This   |           |               | illegal.      |
102 |  event is unique to Native Client|           |               |               |
103 |  and is not part of the W3C      |           |               |               |
104 |  Progress Events standard. The   |           |               |               |
105 |  ``exitStatus`` attribute        |           |               |               |
106 |  provides the numeric exit       |           |               |               |
107 |  status.                         |           |               |               |
108 +----------------------------------+-----------+---------------+---------------+
109
110 The sequence of events for a successful module load is as follows:
111
112 =============================== ===============================
113 Event is dispatched             ... then this task is attempted
114 =============================== ===============================
115 ``loadstart``                   load the manifest file
116 ``progress`` (first time)       load the module
117 ``progress`` (subsequent times)
118 ``load``                        start executing the module
119 ``loadend``
120 =============================== ===============================
121
122 Errors that occur during loading are logged to the JavaScript console in Google
123 Chrome (select the menu icon |menu-icon| > Tools > JavaScript console).
124
125 .. |menu-icon| image:: /images/menu-icon.png
126
127 Handling progress events
128 ========================
129
130 You should add event listeners in a ``<script>`` element to listen for these
131 events before the ``<embed>`` element is parsed. For example, the following code
132 adds a listener for the ``load`` event to a parent ``<div>`` element that also
133 contains the Native Client ``<embed>`` element. First, the listener is
134 attached. Then, when the listener ``<div>`` receives the ``load`` event, the
135 JavaScript ``moduleDidLoad()`` function is called. The following code is
136 excerpted from the example in ``getting_started/part1/``:
137
138 .. naclcode::
139
140   <!--
141   Load the published pexe.
142   Note: Since this module does not use any real-estate in the browser, its
143   width and height are set to 0.
144
145   Note: The <embed> element is wrapped inside a <div>, which has both a 'load'
146   and a 'message' event listener attached.  This wrapping method is used
147   instead of attaching the event listeners directly to the <embed> element to
148   ensure that the listeners are active before the NaCl module 'load' event
149   fires.  This also allows you to use PPB_Messaging.PostMessage() (in C) or
150   pp::Instance.PostMessage() (in C++) from within the initialization code in
151   your module.
152   -->
153   <div id="listener">
154     <script type="text/javascript">
155       var listener = document.getElementById('listener');
156       listener.addEventListener('load', moduleDidLoad, true);
157       listener.addEventListener('message', handleMessage, true);
158     </script>
159
160     <embed id="hello_tutorial"
161            width=0 height=0
162            src="hello_tutorial.nmf"
163            type="application/x-pnacl" />
164   </div>
165
166 Event listeners can be added to any DOM object. Since listeners set at the
167 outermost scope capture events for their contained elements, you can set
168 listeners on outer elements (including the ``<body>`` element) to handle events
169 from inner elements. For more information, see the W3 specifications for `event
170 flow capture
171 <http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-flow-capture>`_ and
172 `event listener registration
173 <http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-registration>`_.
174
175 Displaying load status
176 ======================
177
178 One common response to progress events is to display the percentage of the
179 module that has been loaded. In the load_progress example, when the ``progress``
180 event is triggered the ``moduleLoadProgress`` function is called. This function
181 uses the ``lengthComputable``, ``loaded``, and ``total`` attributes (described
182 in the proposed W3C `Progress Events <http://www.w3.org/TR/progress-events/>`_
183 standard) of the event to calculate the percentage of the module that has
184 loaded.
185
186 .. naclcode::
187
188   function moduleLoadProgress(event) {
189     var loadPercent = 0.0;
190     var loadPercentString;
191     if (event.lengthComputable && event.total > 0) {
192       loadPercent = event.loaded / event.total * 100.0;
193       loadPercentString = loadPercent + '%';
194       common.logMessage('progress: ' + event.url + ' ' + loadPercentString +
195                        ' (' + event.loaded + ' of ' + event.total + ' bytes)');
196     } else {
197       // The total length is not yet known.
198       common.logMessage('progress: Computing...');
199     }
200   }
201
202 The ``lastError`` attribute
203 ===========================
204
205 The ``<embed>`` element has a ``lastError`` attribute that is set to an
206 informative string whenever a load failure (an ``error`` or ``abort`` event)
207 occurs.
208
209 The following code adds an event listener before the ``<embed>`` element to
210 capture and handle an error in loading the Native Client module. The
211 ``handleError()`` function listens for an ``error`` event. When an error occurs,
212 this function prints the contents of the ``lastError`` attribute
213 (``embed_element.lastError``) as an alert.
214
215 .. naclcode::
216
217   function domContentLoaded(name, tc, config, width, height) {
218     var listener = document.getElementById('listener');
219     ...
220     listener.addEventListener('error', moduleLoadError, true);
221     ...
222     common.createNaClModule(name, tc, config, width, height);
223   }
224
225   function moduleLoadError() {
226     common.logMessage('error: ' + common.naclModule.lastError);
227   }
228
229 The ``readyState`` attribute
230 ============================
231
232 You can use the ``readyState`` attribute to monitor the loading process. This
233 attribute is particularly useful if you don't care about the details of
234 individual progress events or when you want to poll for current load state
235 without registering listeners. The value of ``readyState`` progresses as follows
236 for a successful load:
237
238 ===================     ====================
239 Event                   ``readyState`` value
240 ===================     ====================
241 (before any events)     ``undefined``
242 ``loadstart``           1
243 ``progress``            3
244 ``load``                4
245 ``loadend``             4
246 ===================     ====================
247
248 The following code demonstrates how to monitor the loading process using the
249 ``readyState`` attribute. As before, the script that adds the event listeners
250 precedes the ``<embed>`` element so that the event listeners are in place before
251 the progress events are generated.
252
253 .. naclcode::
254
255   <html>
256   ...
257     <body id="body">
258       <div id="status_div">
259       </div>
260       <div id="listener_div">
261         <script type="text/javascript">
262            var stat = document.getElementById('status_div');
263            function handleEvent(e) {
264              var embed_element = document.getElementById('my_embed');
265              stat.innerHTML +=
266              '<br>' + e.type + ': readyState = ' + embed_element.readyState;
267            }
268            var listener_element = document.getElementById('listener_div');
269            listener_element.addEventListener('loadstart', handleEvent, true);
270            listener_element.addEventListener('progress', handleEvent, true);
271            listener_element.addEventListener('load', handleEvent, true);
272            listener_element.addEventListener('loadend', handleEvent, true);
273         </script>
274         <embed
275           name="naclModule"
276           id="my_embed"
277           width=0 height=0
278           src="my_example.nmf"
279           type="application/x-pnacl" />
280       </div>
281     </body>
282   </html>
283
284 The ``exitStatus`` attribute
285 ============================
286
287 This read-only attribute is set if the application calls ``exit(n)``,
288 ``abort()``, or crashes. Since NaCl modules are event handlers, there is no
289 need to call ``exit(n)`` in normal execution. If the module does exit or
290 crash, the ``crash`` progress event is issued and the ``exitStatus`` attribute
291 will contain the numeric value of the exit status:
292
293 * In the case of explicit calls to ``exit(n)``, the numeric value will be
294   ``n`` (between 0 and 255).
295 * In the case of crashes and calls to ``abort()``, the numeric value will
296   be non-zero, but the exact value will depend on the chosen libc and the
297   target architecture, and may change in the future. Applications should not
298   rely on the ``exitStatus`` value being stable in these cases, but the value
299   may nevertheless be useful for temporary debugging.