Home | History | Annotate | Download | only in load_progress
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Called by the common.js module.
      6 function domContentLoaded(name, tc, config, width, height) {
      7   var listener = document.getElementById('listener');
      8   listener.addEventListener('loadstart', moduleDidStartLoad, true);
      9   listener.addEventListener('progress', moduleLoadProgress, true);
     10   listener.addEventListener('error', moduleLoadError, true);
     11   listener.addEventListener('abort', moduleLoadAbort, true);
     12   listener.addEventListener('load', moduleDidLoad, true);
     13   listener.addEventListener('loadend', moduleDidEndLoad, true);
     14   listener.addEventListener('message', handleMessage, true);
     15 
     16   common.createNaClModule(name, tc, config, width, height);
     17 }
     18 
     19 // Handler that gets called when the NaCl module starts loading.  This
     20 // event is always triggered when an <EMBED> tag has a MIME type of
     21 // application/x-nacl.
     22 function moduleDidStartLoad() {
     23   common.logMessage('loadstart');
     24 }
     25 
     26 // Progress event handler.  |event| contains a couple of interesting
     27 // properties that are used in this example:
     28 //     total The size of the NaCl module in bytes.  Note that this value
     29 //         is 0 until |lengthComputable| is true.  In particular, this
     30 //         value is 0 for the first 'progress' event.
     31 //     loaded The number of bytes loaded so far.
     32 //     lengthComputable A boolean indicating that the |total| field
     33 //         represents a valid length.
     34 //
     35 // event The ProgressEvent that triggered this handler.
     36 function moduleLoadProgress(event) {
     37   var loadPercent = 0.0;
     38   var loadPercentString;
     39   if (event.lengthComputable && event.total > 0) {
     40     loadPercent = event.loaded / event.total * 100.0;
     41     loadPercentString = loadPercent + '%';
     42     common.logMessage('progress: ' + event.url + ' ' + loadPercentString +
     43                      ' (' + event.loaded + ' of ' + event.total + ' bytes)');
     44   } else {
     45     // The total length is not yet known.
     46     common.logMessage('progress: Computing...');
     47   }
     48 }
     49 
     50 // Handler that gets called if an error occurred while loading the NaCl
     51 // module.  Note that the event does not carry any meaningful data about
     52 // the error, you have to check lastError on the <EMBED> element to find
     53 // out what happened.
     54 function moduleLoadError() {
     55   common.logMessage('error: ' + common.naclModule.lastError);
     56 }
     57 
     58 // Handler that gets called if the NaCl module load is aborted.
     59 function moduleLoadAbort() {
     60   common.logMessage('abort');
     61 }
     62 
     63 // When the NaCl module has loaded indicate success.
     64 function moduleDidLoad() {
     65   common.logMessage('load');
     66   common.updateStatus('LOADED');
     67 }
     68 
     69 // Handler that gets called when the NaCl module loading has completed.
     70 // You will always get one of these events, regardless of whether the NaCl
     71 // module loaded successfully or not.  For example, if there is an error
     72 // during load, you will get an 'error' event and a 'loadend' event.  Note
     73 // that if the NaCl module loads successfully, you will get both a 'load'
     74 // event and a 'loadend' event.
     75 function moduleDidEndLoad() {
     76   common.logMessage('loadend');
     77   var lastError = event.target.lastError;
     78   if (lastError == undefined || lastError.length == 0) {
     79     lastError = '<none>';
     80   }
     81   common.logMessage('lastError: ' + lastError);
     82 }
     83 
     84 // Handle a message coming from the NaCl module.
     85 function handleMessage(message_event) {
     86   common.logMessage('Received PostMessage: ' + message_event.data);
     87 }
     88