Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
|
noelallen1
2013/05/24 18:42:50
Why does this have it's own common?
nfullagar1
2013/05/24 19:06:00
removed
| |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Javascript module pattern: | |
| 6 // see http://en.wikipedia.org/wiki/Unobtrusive_JavaScript#Namespaces | |
| 7 // In essence, we define an anonymous function which is immediately called and | |
| 8 // returns a new object. The new object contains only the exported definitions; | |
| 9 // all other definitions in the anonymous function are inaccessible to external | |
| 10 // code. | |
| 11 var common = (function () { | |
| 12 | |
| 13 /** | |
| 14 * Create the Native Client <embed> element as a child of the DOM element | |
| 15 * named "listener". | |
| 16 * | |
| 17 * @param {string} name The name of the example. | |
| 18 * @param {string} tool The name of the toolchain, e.g. "glibc", "newlib" etc. | |
| 19 * @param {string} path Directory name where .nmf file can be found. | |
| 20 * @param {number} width The width to create the plugin. | |
| 21 * @param {number} height The height to create the plugin. | |
| 22 * @param {Object} optional dictionary of args to send to DidCreateInstance | |
| 23 */ | |
| 24 function createNaClModule(name, tool, path, width, height, args) { | |
| 25 var moduleEl = document.createElement('embed'); | |
| 26 moduleEl.setAttribute('name', 'nacl_module'); | |
| 27 moduleEl.setAttribute('id', 'nacl_module'); | |
| 28 moduleEl.setAttribute('width', width); | |
| 29 moduleEl.setAttribute('height',height); | |
| 30 moduleEl.setAttribute('src', path + '/' + name + '.nmf'); | |
| 31 | |
| 32 // Add any optional arguments | |
| 33 if (args) { | |
| 34 for (var key in args) { | |
| 35 moduleEl.setAttribute(key, args[key]) | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 // For NaCL modules use application/x-nacl. | |
| 40 var mimetype = 'application/x-nacl'; | |
| 41 var isHost = tool == 'win' || tool == 'linux' || tool == 'mac'; | |
| 42 if (isHost) { | |
| 43 // For non-nacl PPAPI plugins use the x-ppapi-debug/release | |
| 44 // mime type. | |
| 45 if (path.toLowerCase().indexOf('release') != -1) | |
| 46 mimetype = 'application/x-ppapi-release'; | |
| 47 else | |
| 48 mimetype = 'application/x-ppapi-debug'; | |
| 49 } | |
| 50 moduleEl.setAttribute('type', mimetype); | |
| 51 | |
| 52 // The <EMBED> element is wrapped inside a <DIV>, which has both a 'load' | |
| 53 // and a 'message' event listener attached. This wrapping method is used | |
| 54 // instead of attaching the event listeners directly to the <EMBED> element | |
| 55 // to ensure that the listeners are active before the NaCl module 'load' | |
| 56 // event fires. | |
| 57 var listenerDiv = document.getElementById('listener'); | |
| 58 listenerDiv.appendChild(moduleEl); | |
| 59 | |
| 60 // Host plugins don't send a moduleDidLoad message. We'll fake it here. | |
| 61 if (isHost) { | |
| 62 window.setTimeout(function () { | |
| 63 var evt = document.createEvent('Event'); | |
| 64 evt.initEvent('load', true, true); // bubbles, cancelable | |
| 65 moduleEl.dispatchEvent(evt); | |
| 66 }, 100); // 100 ms | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 /** | |
| 71 * Add the default "load" and "message" event listeners to the element with | |
| 72 * id "listener". | |
| 73 * | |
| 74 * The "load" event is sent when the module is successfully loaded. The | |
| 75 * "message" event is sent when the naclModule posts a message using | |
| 76 * PPB_Messaging.PostMessage() (in C) or pp::Instance().PostMessage() (in | |
| 77 * C++). | |
| 78 */ | |
| 79 function attachDefaultListeners() { | |
| 80 var listenerDiv = document.getElementById('listener'); | |
| 81 listenerDiv.addEventListener('load', moduleDidLoad, true); | |
| 82 listenerDiv.addEventListener('message', handleMessage, true); | |
| 83 | |
| 84 if (typeof window.attachListeners !== 'undefined') { | |
| 85 window.attachListeners(); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 /** | |
| 90 * Called when the NaCl module is loaded. | |
| 91 * | |
| 92 * This event listener is registered in createNaClModule above. | |
| 93 */ | |
| 94 function moduleDidLoad() { | |
| 95 common.naclModule = document.getElementById('nacl_module'); | |
| 96 updateStatus('SUCCESS'); | |
| 97 | |
| 98 if (typeof window.moduleDidLoad !== 'undefined') { | |
| 99 window.moduleDidLoad(); | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 /** | |
| 104 * Hide the NaCl module's embed element. | |
| 105 * | |
| 106 * We don't want to hide by default; if we do, it is harder to determine that | |
| 107 * a plugin failed to load. Instead, call this function inside the example's | |
| 108 * "moduleDidLoad" function. | |
| 109 * | |
| 110 */ | |
| 111 function hideModule() { | |
| 112 // Setting common.naclModule.style.display = "None" doesn't work; the | |
| 113 // module will no longer be able to receive postMessages. | |
| 114 common.naclModule.style.height = "0"; | |
| 115 } | |
| 116 | |
| 117 /** | |
| 118 * Return true when |s| starts with the string |prefix|. | |
| 119 * | |
| 120 * @param {string} s The string to search. | |
| 121 * @param {string} prefix The prefix to search for in |s|. | |
| 122 */ | |
| 123 function startsWith(s, prefix) { | |
| 124 // indexOf would search the entire string, lastIndexOf(p, 0) only checks at | |
| 125 // the first index. See: http://stackoverflow.com/a/4579228 | |
| 126 return s.lastIndexOf(prefix, 0) === 0; | |
| 127 } | |
| 128 | |
| 129 /** Maximum length of logMessageArray. */ | |
| 130 var kMaxLogMessageLength = 20; | |
| 131 | |
| 132 /** An array of messages to display in the element with id "log". */ | |
| 133 var logMessageArray = []; | |
| 134 | |
| 135 /** | |
| 136 * Add a message to an element with id "log". | |
| 137 * | |
| 138 * This function is used by the default "log:" message handler. | |
| 139 * | |
| 140 * @param {string} message The message to log. | |
| 141 */ | |
| 142 function logMessage(message) { | |
| 143 logMessageArray.push(message); | |
| 144 if (logMessageArray.length > kMaxLogMessageLength) | |
| 145 logMessageArray.shift(); | |
| 146 | |
| 147 document.getElementById('log').textContent = logMessageArray.join(''); | |
| 148 console.log(message) | |
| 149 } | |
| 150 | |
| 151 /** | |
| 152 */ | |
| 153 var defaultMessageTypes = { | |
| 154 'alert': alert, | |
| 155 'log': logMessage | |
| 156 }; | |
| 157 | |
| 158 /** | |
| 159 * Called when the NaCl module sends a message to JavaScript (via | |
| 160 * PPB_Messaging.PostMessage()) | |
| 161 * | |
| 162 * This event listener is registered in createNaClModule above. | |
| 163 * | |
| 164 * @param {Event} message_event A message event. message_event.data contains | |
| 165 * the data sent from the NaCl module. | |
| 166 */ | |
| 167 function handleMessage(message_event) { | |
| 168 if (typeof message_event.data === 'string') { | |
| 169 for (var type in defaultMessageTypes) { | |
| 170 if (defaultMessageTypes.hasOwnProperty(type)) { | |
| 171 if (startsWith(message_event.data, type + ':')) { | |
| 172 func = defaultMessageTypes[type]; | |
| 173 func(message_event.data.slice(type.length + 1)); | |
| 174 } | |
| 175 } | |
| 176 } | |
| 177 } | |
| 178 | |
| 179 if (typeof window.handleMessage !== 'undefined') { | |
| 180 window.handleMessage(message_event); | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 /** | |
| 185 * Called when the DOM content has loaded; i.e. the page's document is fully | |
| 186 * parsed. At this point, we can safely query any elements in the document via | |
| 187 * document.querySelector, document.getElementById, etc. | |
| 188 * | |
| 189 * @param {string} name The name of the example. | |
| 190 * @param {string} tool The name of the toolchain, e.g. "glibc", "newlib" etc. | |
| 191 * @param {string} path Directory name where .nmf file can be found. | |
| 192 * @param {number} width The width to create the plugin. | |
| 193 * @param {number} height The height to create the plugin. | |
| 194 */ | |
| 195 function domContentLoaded(name, tool, path, width, height) { | |
| 196 // If the page loads before the Native Client module loads, then set the | |
| 197 // status message indicating that the module is still loading. Otherwise, | |
| 198 // do not change the status message. | |
| 199 updateStatus('Page loaded.'); | |
| 200 if (common.naclModule == null) { | |
| 201 updateStatus('Creating embed: ' + tool) | |
| 202 | |
| 203 // We use a non-zero sized embed to give Chrome space to place the bad | |
| 204 // plug-in graphic, if there is a problem. | |
| 205 width = typeof width !== 'undefined' ? width : 200; | |
| 206 height = typeof height !== 'undefined' ? height : 200; | |
| 207 attachDefaultListeners(); | |
| 208 createNaClModule(name, tool, path, width, height); | |
| 209 } else { | |
| 210 // It's possible that the Native Client module onload event fired | |
| 211 // before the page's onload event. In this case, the status message | |
| 212 // will reflect 'SUCCESS', but won't be displayed. This call will | |
| 213 // display the current message. | |
| 214 updateStatus('Waiting.'); | |
| 215 } | |
| 216 } | |
| 217 | |
| 218 /** Saved text to display in the element with id 'statusField'. */ | |
| 219 var statusText = 'NO-STATUSES'; | |
| 220 | |
| 221 /** | |
| 222 * Set the global status message. If the element with id 'statusField' | |
| 223 * exists, then set its HTML to the status message as well. | |
| 224 * | |
| 225 * @param {string} opt_message The message to set. If null or undefined, then | |
| 226 * set element 'statusField' to the message from the last call to | |
| 227 * updateStatus. | |
| 228 */ | |
| 229 function updateStatus(opt_message) { | |
| 230 if (opt_message) { | |
| 231 statusText = opt_message; | |
| 232 } | |
| 233 var statusField = document.getElementById('statusField'); | |
| 234 if (statusField) { | |
| 235 statusField.innerHTML = statusText; | |
| 236 } | |
| 237 } | |
| 238 | |
| 239 // The symbols to export. | |
| 240 return { | |
| 241 /** A reference to the NaCl module, once it is loaded. */ | |
| 242 naclModule: null, | |
| 243 | |
| 244 attachDefaultListeners: attachDefaultListeners, | |
| 245 domContentLoaded: domContentLoaded, | |
| 246 createNaClModule: createNaClModule, | |
| 247 hideModule: hideModule, | |
| 248 logMessage: logMessage, | |
| 249 updateStatus: updateStatus | |
| 250 }; | |
| 251 | |
| 252 }()); | |
| 253 | |
| 254 // Listen for the DOM content to be loaded. This event is fired when parsing of | |
| 255 // the page's document has finished. | |
| 256 document.addEventListener('DOMContentLoaded', function() { | |
| 257 var body = document.querySelector('body'); | |
| 258 | |
| 259 // The data-* attributes on the body can be referenced via body.dataset. | |
| 260 if (body.dataset) { | |
| 261 var loadFunction; | |
| 262 if (!body.dataset.customLoad) { | |
| 263 loadFunction = common.domContentLoaded; | |
| 264 } else if (typeof window.domContentLoaded !== 'undefined') { | |
| 265 loadFunction = window.domContentLoaded; | |
| 266 } | |
| 267 | |
| 268 // From https://developer.mozilla.org/en-US/docs/DOM/window.location | |
| 269 var searchVars = {}; | |
| 270 if (window.location.search.length > 1) { | |
| 271 var pairs = window.location.search.substr(1).split("&"); | |
| 272 for (var key_ix = 0; key_ix < pairs.length; key_ix++) { | |
| 273 var keyValue = pairs[key_ix].split("="); | |
| 274 searchVars[unescape(keyValue[0])] = | |
| 275 keyValue.length > 1 ? unescape(keyValue[1]) : ""; | |
| 276 } | |
| 277 } | |
| 278 | |
| 279 if (loadFunction) { | |
| 280 var toolchains = body.dataset.tools.split(' '); | |
| 281 var configs = body.dataset.configs.split(' '); | |
| 282 | |
| 283 var tc = toolchains.indexOf(searchVars.tc) !== -1 ? | |
| 284 searchVars.tc : toolchains[0]; | |
| 285 var config = configs.indexOf(searchVars.config) !== -1 ? | |
| 286 searchVars.config : configs[0]; | |
| 287 var pathFormat = body.dataset.path; | |
| 288 var path = pathFormat.replace('{tc}', tc).replace('{config}', config); | |
| 289 | |
| 290 loadFunction(body.dataset.name, tc, path, body.dataset.width, | |
| 291 body.dataset.height); | |
| 292 } | |
| 293 } | |
| 294 }); | |
| OLD | NEW |