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 // Set to true when the Document is loaded IFF "test=true" is in the query | 5 // Set to true when the Document is loaded IFF "test=true" is in the query |
6 // string. | 6 // string. |
7 var isTest = false; | 7 var isTest = false; |
8 | 8 |
9 // Javascript module pattern: | 9 // Javascript module pattern: |
10 // see http://en.wikipedia.org/wiki/Unobtrusive_JavaScript#Namespaces | 10 // see http://en.wikipedia.org/wiki/Unobtrusive_JavaScript#Namespaces |
11 // In essence, we define an anonymous function which is immediately called and | 11 // In essence, we define an anonymous function which is immediately called and |
12 // returns a new object. The new object contains only the exported definitions; | 12 // returns a new object. The new object contains only the exported definitions; |
13 // all other definitions in the anonymous function are inaccessible to external | 13 // all other definitions in the anonymous function are inaccessible to external |
14 // code. | 14 // code. |
15 var common = (function() { | 15 var common = (function() { |
16 | 16 |
17 function isHostToolchain(tool) { | |
18 return tool == 'win' || tool == 'linux' || tool == 'mac'; | |
19 } | |
20 | |
21 /** | |
22 * Return the mime type for NaCl plugin. | |
23 * | |
24 * @param {string} tool The name of the toolchain, e.g. "glibc", "newlib" etc. | |
25 * @param {bool} is_release True if this is a release build. | |
binji
2013/07/23 18:29:32
add @return doc
jvoung (off chromium)
2013/07/23 19:57:45
Done.
| |
26 */ | |
27 function mimeTypeForTool(tool, is_release) { | |
28 // For NaCl modules use application/x-nacl. | |
29 var mimetype = 'application/x-nacl'; | |
30 if (isHostToolchain(tool)) { | |
31 // For non-NaCl PPAPI plugins use the x-ppapi-debug/release | |
32 // mime type. | |
33 if (is_release) | |
34 mimetype = 'application/x-ppapi-release'; | |
35 else | |
36 mimetype = 'application/x-ppapi-debug'; | |
37 } else if (tool == 'pnacl') { | |
38 mimetype = 'application/x-pnacl'; | |
39 } | |
40 return mimetype; | |
41 } | |
42 | |
43 /** | |
44 * Return true if the browser supports NaCl plugins. | |
45 * | |
46 * @param {string} tool The name of the toolchain, e.g. "glibc", "newlib" etc. | |
binji
2013/07/23 18:29:32
add @return doc
jvoung (off chromium)
2013/07/23 19:57:45
Done.
| |
47 */ | |
48 function browserSupportsNaCl(tool) { | |
49 var mimetype = mimeTypeForTool(tool); | |
50 return navigator.mimeTypes[mimetype] !== undefined; | |
51 } | |
52 | |
17 /** | 53 /** |
18 * Create the Native Client <embed> element as a child of the DOM element | 54 * Create the Native Client <embed> element as a child of the DOM element |
19 * named "listener". | 55 * named "listener". |
20 * | 56 * |
21 * @param {string} name The name of the example. | 57 * @param {string} name The name of the example. |
22 * @param {string} tool The name of the toolchain, e.g. "glibc", "newlib" etc. | 58 * @param {string} tool The name of the toolchain, e.g. "glibc", "newlib" etc. |
23 * @param {string} path Directory name where .nmf file can be found. | 59 * @param {string} path Directory name where .nmf file can be found. |
24 * @param {number} width The width to create the plugin. | 60 * @param {number} width The width to create the plugin. |
25 * @param {number} height The height to create the plugin. | 61 * @param {number} height The height to create the plugin. |
26 * @param {Object} attrs Dictionary of attributes to set on the module. | 62 * @param {Object} attrs Dictionary of attributes to set on the module. |
27 */ | 63 */ |
28 function createNaClModule(name, tool, path, width, height, attrs) { | 64 function createNaClModule(name, tool, path, width, height, attrs) { |
29 var moduleEl = document.createElement('embed'); | 65 var moduleEl = document.createElement('embed'); |
30 moduleEl.setAttribute('name', 'nacl_module'); | 66 moduleEl.setAttribute('name', 'nacl_module'); |
31 moduleEl.setAttribute('id', 'nacl_module'); | 67 moduleEl.setAttribute('id', 'nacl_module'); |
32 moduleEl.setAttribute('width', width); | 68 moduleEl.setAttribute('width', width); |
33 moduleEl.setAttribute('height', height); | 69 moduleEl.setAttribute('height', height); |
34 moduleEl.setAttribute('path', path); | 70 moduleEl.setAttribute('path', path); |
35 moduleEl.setAttribute('src', path + '/' + name + '.nmf'); | 71 moduleEl.setAttribute('src', path + '/' + name + '.nmf'); |
36 | 72 |
37 // Add any optional arguments | 73 // Add any optional arguments |
38 if (attrs) { | 74 if (attrs) { |
39 for (var key in attrs) { | 75 for (var key in attrs) { |
40 moduleEl.setAttribute(key, attrs[key]) | 76 moduleEl.setAttribute(key, attrs[key]) |
41 } | 77 } |
42 } | 78 } |
43 | 79 |
44 // For NaCL modules use application/x-nacl. | 80 var mimetype = mimeTypeForTool(tool); |
45 var mimetype = 'application/x-nacl'; | |
46 var isHost = tool == 'win' || tool == 'linux' || tool == 'mac'; | |
47 if (isHost) { | |
48 // For non-nacl PPAPI plugins use the x-ppapi-debug/release | |
49 // mime type. | |
50 if (path.toLowerCase().indexOf('release') != -1) | |
51 mimetype = 'application/x-ppapi-release'; | |
52 else | |
53 mimetype = 'application/x-ppapi-debug'; | |
54 } else if (tool == 'pnacl') { | |
55 mimetype = 'application/x-pnacl'; | |
56 } | |
57 moduleEl.setAttribute('type', mimetype); | 81 moduleEl.setAttribute('type', mimetype); |
58 | 82 |
59 // The <EMBED> element is wrapped inside a <DIV>, which has both a 'load' | 83 // The <EMBED> element is wrapped inside a <DIV>, which has both a 'load' |
60 // and a 'message' event listener attached. This wrapping method is used | 84 // and a 'message' event listener attached. This wrapping method is used |
61 // instead of attaching the event listeners directly to the <EMBED> element | 85 // instead of attaching the event listeners directly to the <EMBED> element |
62 // to ensure that the listeners are active before the NaCl module 'load' | 86 // to ensure that the listeners are active before the NaCl module 'load' |
63 // event fires. | 87 // event fires. |
64 var listenerDiv = document.getElementById('listener'); | 88 var listenerDiv = document.getElementById('listener'); |
65 listenerDiv.appendChild(moduleEl); | 89 listenerDiv.appendChild(moduleEl); |
66 | 90 |
67 // Host plugins don't send a moduleDidLoad message. We'll fake it here. | 91 // Host plugins don't send a moduleDidLoad message. We'll fake it here. |
92 var isHost = isHostToolchain(tool); | |
68 if (isHost) { | 93 if (isHost) { |
69 window.setTimeout(function() { | 94 window.setTimeout(function() { |
70 var evt = document.createEvent('Event'); | 95 var evt = document.createEvent('Event'); |
71 evt.initEvent('load', true, true); // bubbles, cancelable | 96 evt.initEvent('load', true, true); // bubbles, cancelable |
72 moduleEl.dispatchEvent(evt); | 97 moduleEl.dispatchEvent(evt); |
73 }, 100); // 100 ms | 98 }, 100); // 100 ms |
74 } | 99 } |
75 | 100 |
76 // This is code that is only used to test the SDK. | 101 // This is code that is only used to test the SDK. |
77 if (isTest) { | 102 if (isTest) { |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
251 * @param {string} path Directory name where .nmf file can be found. | 276 * @param {string} path Directory name where .nmf file can be found. |
252 * @param {number} width The width to create the plugin. | 277 * @param {number} width The width to create the plugin. |
253 * @param {number} height The height to create the plugin. | 278 * @param {number} height The height to create the plugin. |
254 * @param {Object} attrs Optional dictionary of additional attributes. | 279 * @param {Object} attrs Optional dictionary of additional attributes. |
255 */ | 280 */ |
256 function domContentLoaded(name, tool, path, width, height, attrs) { | 281 function domContentLoaded(name, tool, path, width, height, attrs) { |
257 // If the page loads before the Native Client module loads, then set the | 282 // If the page loads before the Native Client module loads, then set the |
258 // status message indicating that the module is still loading. Otherwise, | 283 // status message indicating that the module is still loading. Otherwise, |
259 // do not change the status message. | 284 // do not change the status message. |
260 updateStatus('Page loaded.'); | 285 updateStatus('Page loaded.'); |
261 if (common.naclModule == null) { | 286 var is_release = path.toLowerCase().indexOf('release') != -1; |
binji
2013/07/23 18:29:32
nit: isRelease
jvoung (off chromium)
2013/07/23 19:57:45
Done.
Same code style for parameter names (above)
binji
2013/07/23 20:01:52
Yes, camelCase for all names in JavaScript.
| |
287 if (!browserSupportsNaCl(tool, is_release)) { | |
288 updateStatus( | |
289 'Browser does not support NaCl (' + tool + '), or NaCl is disabled'); | |
290 } else if (common.naclModule == null) { | |
262 updateStatus('Creating embed: ' + tool); | 291 updateStatus('Creating embed: ' + tool); |
263 | 292 |
264 // We use a non-zero sized embed to give Chrome space to place the bad | 293 // We use a non-zero sized embed to give Chrome space to place the bad |
265 // plug-in graphic, if there is a problem. | 294 // plug-in graphic, if there is a problem. |
266 width = typeof width !== 'undefined' ? width : 200; | 295 width = typeof width !== 'undefined' ? width : 200; |
267 height = typeof height !== 'undefined' ? height : 200; | 296 height = typeof height !== 'undefined' ? height : 200; |
268 attachDefaultListeners(); | 297 attachDefaultListeners(); |
269 createNaClModule(name, tool, path, width, height, attrs); | 298 createNaClModule(name, tool, path, width, height, attrs); |
270 } else { | 299 } else { |
271 // It's possible that the Native Client module onload event fired | 300 // It's possible that the Native Client module onload event fired |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
359 var pathFormat = body.dataset.path; | 388 var pathFormat = body.dataset.path; |
360 var path = pathFormat.replace('{tc}', tc).replace('{config}', config); | 389 var path = pathFormat.replace('{tc}', tc).replace('{config}', config); |
361 | 390 |
362 isTest = searchVars.test === 'true'; | 391 isTest = searchVars.test === 'true'; |
363 | 392 |
364 loadFunction(body.dataset.name, tc, path, body.dataset.width, | 393 loadFunction(body.dataset.name, tc, path, body.dataset.width, |
365 body.dataset.height, attrs); | 394 body.dataset.height, attrs); |
366 } | 395 } |
367 } | 396 } |
368 }); | 397 }); |
OLD | NEW |