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