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} isRelease True if this is a release build. |
| 26 * @return {string} The mime-type for the kind of NaCl plugin matching |
| 27 * the given toolchain. |
| 28 */ |
| 29 function mimeTypeForTool(tool, isRelease) { |
| 30 // For NaCl modules use application/x-nacl. |
| 31 var mimetype = 'application/x-nacl'; |
| 32 if (isHostToolchain(tool)) { |
| 33 // For non-NaCl PPAPI plugins use the x-ppapi-debug/release |
| 34 // mime type. |
| 35 if (isRelease) |
| 36 mimetype = 'application/x-ppapi-release'; |
| 37 else |
| 38 mimetype = 'application/x-ppapi-debug'; |
| 39 } else if (tool == 'pnacl') { |
| 40 mimetype = 'application/x-pnacl'; |
| 41 } |
| 42 return mimetype; |
| 43 } |
| 44 |
| 45 /** |
| 46 * Check if the browser supports NaCl plugins. |
| 47 * |
| 48 * @param {string} tool The name of the toolchain, e.g. "glibc", "newlib" etc. |
| 49 * @return {bool} True if the browser supports the type of NaCl plugin |
| 50 * produced by the given toolchain. |
| 51 */ |
| 52 function browserSupportsNaCl(tool) { |
| 53 // Assume host toolchains always work with the given browser. |
| 54 // The below mime-type checking might not work with |
| 55 // --register-pepper-plugins. |
| 56 if (isHostToolchain(tool)) { |
| 57 return true; |
| 58 } |
| 59 var mimetype = mimeTypeForTool(tool); |
| 60 return navigator.mimeTypes[mimetype] !== undefined; |
| 61 } |
| 62 |
17 /** | 63 /** |
18 * Create the Native Client <embed> element as a child of the DOM element | 64 * Create the Native Client <embed> element as a child of the DOM element |
19 * named "listener". | 65 * named "listener". |
20 * | 66 * |
21 * @param {string} name The name of the example. | 67 * @param {string} name The name of the example. |
22 * @param {string} tool The name of the toolchain, e.g. "glibc", "newlib" etc. | 68 * @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. | 69 * @param {string} path Directory name where .nmf file can be found. |
24 * @param {number} width The width to create the plugin. | 70 * @param {number} width The width to create the plugin. |
25 * @param {number} height The height to create the plugin. | 71 * @param {number} height The height to create the plugin. |
26 * @param {Object} attrs Dictionary of attributes to set on the module. | 72 * @param {Object} attrs Dictionary of attributes to set on the module. |
27 */ | 73 */ |
28 function createNaClModule(name, tool, path, width, height, attrs) { | 74 function createNaClModule(name, tool, path, width, height, attrs) { |
29 var moduleEl = document.createElement('embed'); | 75 var moduleEl = document.createElement('embed'); |
30 moduleEl.setAttribute('name', 'nacl_module'); | 76 moduleEl.setAttribute('name', 'nacl_module'); |
31 moduleEl.setAttribute('id', 'nacl_module'); | 77 moduleEl.setAttribute('id', 'nacl_module'); |
32 moduleEl.setAttribute('width', width); | 78 moduleEl.setAttribute('width', width); |
33 moduleEl.setAttribute('height', height); | 79 moduleEl.setAttribute('height', height); |
34 moduleEl.setAttribute('path', path); | 80 moduleEl.setAttribute('path', path); |
35 moduleEl.setAttribute('src', path + '/' + name + '.nmf'); | 81 moduleEl.setAttribute('src', path + '/' + name + '.nmf'); |
36 | 82 |
37 // Add any optional arguments | 83 // Add any optional arguments |
38 if (attrs) { | 84 if (attrs) { |
39 for (var key in attrs) { | 85 for (var key in attrs) { |
40 moduleEl.setAttribute(key, attrs[key]) | 86 moduleEl.setAttribute(key, attrs[key]) |
41 } | 87 } |
42 } | 88 } |
43 | 89 |
44 // For NaCL modules use application/x-nacl. | 90 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); | 91 moduleEl.setAttribute('type', mimetype); |
58 | 92 |
59 // The <EMBED> element is wrapped inside a <DIV>, which has both a 'load' | 93 // 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 | 94 // and a 'message' event listener attached. This wrapping method is used |
61 // instead of attaching the event listeners directly to the <EMBED> element | 95 // instead of attaching the event listeners directly to the <EMBED> element |
62 // to ensure that the listeners are active before the NaCl module 'load' | 96 // to ensure that the listeners are active before the NaCl module 'load' |
63 // event fires. | 97 // event fires. |
64 var listenerDiv = document.getElementById('listener'); | 98 var listenerDiv = document.getElementById('listener'); |
65 listenerDiv.appendChild(moduleEl); | 99 listenerDiv.appendChild(moduleEl); |
66 | 100 |
67 // Host plugins don't send a moduleDidLoad message. We'll fake it here. | 101 // Host plugins don't send a moduleDidLoad message. We'll fake it here. |
| 102 var isHost = isHostToolchain(tool); |
68 if (isHost) { | 103 if (isHost) { |
69 window.setTimeout(function() { | 104 window.setTimeout(function() { |
70 var evt = document.createEvent('Event'); | 105 var evt = document.createEvent('Event'); |
71 evt.initEvent('load', true, true); // bubbles, cancelable | 106 evt.initEvent('load', true, true); // bubbles, cancelable |
72 moduleEl.dispatchEvent(evt); | 107 moduleEl.dispatchEvent(evt); |
73 }, 100); // 100 ms | 108 }, 100); // 100 ms |
74 } | 109 } |
75 | 110 |
76 // This is code that is only used to test the SDK. | 111 // This is code that is only used to test the SDK. |
77 if (isTest) { | 112 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. | 286 * @param {string} path Directory name where .nmf file can be found. |
252 * @param {number} width The width to create the plugin. | 287 * @param {number} width The width to create the plugin. |
253 * @param {number} height The height to create the plugin. | 288 * @param {number} height The height to create the plugin. |
254 * @param {Object} attrs Optional dictionary of additional attributes. | 289 * @param {Object} attrs Optional dictionary of additional attributes. |
255 */ | 290 */ |
256 function domContentLoaded(name, tool, path, width, height, attrs) { | 291 function domContentLoaded(name, tool, path, width, height, attrs) { |
257 // If the page loads before the Native Client module loads, then set the | 292 // If the page loads before the Native Client module loads, then set the |
258 // status message indicating that the module is still loading. Otherwise, | 293 // status message indicating that the module is still loading. Otherwise, |
259 // do not change the status message. | 294 // do not change the status message. |
260 updateStatus('Page loaded.'); | 295 updateStatus('Page loaded.'); |
261 if (common.naclModule == null) { | 296 var isRelease = path.toLowerCase().indexOf('release') != -1; |
| 297 if (!browserSupportsNaCl(tool, isRelease)) { |
| 298 updateStatus( |
| 299 'Browser does not support NaCl (' + tool + '), or NaCl is disabled'); |
| 300 } else if (common.naclModule == null) { |
262 updateStatus('Creating embed: ' + tool); | 301 updateStatus('Creating embed: ' + tool); |
263 | 302 |
264 // We use a non-zero sized embed to give Chrome space to place the bad | 303 // We use a non-zero sized embed to give Chrome space to place the bad |
265 // plug-in graphic, if there is a problem. | 304 // plug-in graphic, if there is a problem. |
266 width = typeof width !== 'undefined' ? width : 200; | 305 width = typeof width !== 'undefined' ? width : 200; |
267 height = typeof height !== 'undefined' ? height : 200; | 306 height = typeof height !== 'undefined' ? height : 200; |
268 attachDefaultListeners(); | 307 attachDefaultListeners(); |
269 createNaClModule(name, tool, path, width, height, attrs); | 308 createNaClModule(name, tool, path, width, height, attrs); |
270 } else { | 309 } else { |
271 // It's possible that the Native Client module onload event fired | 310 // 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; | 398 var pathFormat = body.dataset.path; |
360 var path = pathFormat.replace('{tc}', tc).replace('{config}', config); | 399 var path = pathFormat.replace('{tc}', tc).replace('{config}', config); |
361 | 400 |
362 isTest = searchVars.test === 'true'; | 401 isTest = searchVars.test === 'true'; |
363 | 402 |
364 loadFunction(body.dataset.name, tc, path, body.dataset.width, | 403 loadFunction(body.dataset.name, tc, path, body.dataset.width, |
365 body.dataset.height, attrs); | 404 body.dataset.height, attrs); |
366 } | 405 } |
367 } | 406 } |
368 }); | 407 }); |
OLD | NEW |