Progress Events
There is five types of events that developers can respond into Native client:progress, message, view change, focus, and Input events (each described in the glossary below). This sections describes how to monitor progress events (events that occur during the loading and execution of a Native Clie NT module). This section assumes you is familiar with the material presented in the Technical overview. The load_progress example illustrates progress event handling. You can find this code in The/pepper_<version>/examples/tutorial/load_progress/directory in the Native Client SDK Download. Module Loading and progress events
The Native Client Runtime reports a set of state changes during the module loading process by means of DOM progress events . This set of events are a direct port of the proposed Progress events standard (except for the crash event which are an E XTension of the. The following table lists the events types reported by the Native Client Runtime:
Event | Times
triggered | When
triggered | How you
might respond |
Loadstart Native Client has started to load a Native client module. |
Once |
The first progress event after the Native Client module is instantiated and initialized. |
Display A status message, such as "Loading ..." |
Progress part of the module has been loaded. |
Zero or more |
Afterloadstarthas been dispatched. |
Display a progress bar. |
Error The Native Client module failed to start execution (includes any error before or during initialization of the module ). The LastError attribute (mentioned later) provides details on the error (initialization failed, Sel_ldr do not start, and So on). |
Zero or once |
After the lastprogressevent have been dispatched, or Afterloadstart if no progressevent was dispatched. |
Inform user, the application failed to load. |
Abort Loading of the Nativeclient module was aborted by the user. |
Zero or once |
After the lastprogressevent have been dispatched, or Afterloadstart if no progressevent was dispatched. |
It's not likely you'll want to respond to the this event. |
Load the Native Client module was successfully loaded, and execution was started. (the module was initialized successfully.) |
Zero or once |
After the lastprogressevent have been dispatched, or Afterloadstart if no progressevent was dispatched. |
Remove the progress bar. |
Loadend Loading of the Native Client module has stopped. Load succeeded (load), failed (error), or was aborted (abort). |
Once |
After Anerror,abort, Orload event is dispatched. |
Indicate loading is over (regardless of failure or not). |
Crash the Native Client module is not responding (died in an ASSERT () or exit ()) after a successful load. This event is a unique to Native Client and are not part of the Progress Events standard. The Exitstatus attribute provides the numeric exit status. |
Zero or once |
After Aloadend. |
Notify user, the module did something illegal. |
The sequence of events for a successful module, load is as follows:
Event is dispatched |
... | then this task is attempted
Loadstart |
Load the manifest file |
Progress (first time) |
Load the module |
Progress (subsequent times) |
|
Load |
Start executing the module |
Loadend |
|
Errors that occur during loading is logged to the JavaScript console in Google Chrome (Select the menu icon > Tools & Gt JavaScript console). Handling Progress Events
You should add event listeners in A <script> element to listen for these events before The <em Bed> element is parsed. For example, the following code adds a listener for the load event to a parent <div> element tha T also contains the Native client <embed> element. First, the listener is attached. Then, when the Listener <div>receives the load event, the Javascript moduledidload () function is called. The following code is excerpted from the example in getting_started/part1/:
<!--Load the published pexe.
Note:since This module does is a real-estate in the browser with its width and height is set to 0. Note:the <embed> element is wrapped inside a <div>, which have both a ' load ' and a ' message ' event listener at Tached. This wrapping method was used instead of attaching the event listeners directly to the <embed> element to ensure that The listeners is active before the NaCl module ' Load ' event fires. This also allows the use of Ppb_messaging.postmessage () (in C) or pp::instance.
PostMessage () (in C + +) from within the initialization code in your module. --<div id= "Listener" > <script type= "Text/javascript" > var listener = document.getElementById (' Liste
Ner ');
Listener.addeventlistener (' Load ', moduledidload, true);
Listener.addeventlistener (' message ', handlemessage, true); </script> <embed id= "hello_tutorial" width=0 height=0 src= "HELLO_TUTORIAL.NMF" type = "ApplicaTion/x-pnacl "/> </div>
Event listeners can added to any DOM object. Since listeners set at the outermost scopes capture events for their contained elements, can set listeners on outer ele ments (including the <body> element) to handle events from inner elements. For more information, see the W3 specifications for event flow capture Andevent listener registration. Displaying Load Status
One common response to progress events are to display the percentage of the module, which has been loaded. In the load_progress example, when the progress event was triggered the moduleloadprogress function is called. This function uses the lengthcomputable, loaded, and total attributes (described in the proposed Progress Events stand ARD) of the event to calculate the percentage of the module, which has loaded.
function moduleloadprogress (event) {var loadpercent = 0.0; var loadpercentstring