OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 /** | 5 /** |
6 * @typedef {{ | 6 * @typedef {{ |
7 * fulfill: function(PiexLoaderResponse):undefined, | 7 * fulfill: function(PiexLoaderResponse):undefined, |
8 * reject: function(string):undefined} | 8 * reject: function(string):undefined} |
9 * }} | 9 * }} |
10 */ | 10 */ |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 * @private {!Element} | 46 * @private {!Element} |
47 * @const | 47 * @const |
48 */ | 48 */ |
49 this.naclModule_ = document.createElement('embed'); | 49 this.naclModule_ = document.createElement('embed'); |
50 | 50 |
51 /** | 51 /** |
52 * @private {!Promise} | 52 * @private {!Promise} |
53 * @const | 53 * @const |
54 */ | 54 */ |
55 this.naclPromise_ = new Promise(function(fulfill) { | 55 this.naclPromise_ = new Promise(function(fulfill) { |
56 var embed = this.naclModule_; | 56 chrome.fileManagerPrivate.isPiexLoaderEnabled(fulfill); |
57 embed.setAttribute('type', 'application/x-pnacl'); | 57 }).then(function(enabled) { |
58 // The extension nmf is not allowed to load. We uses .nmf.js instead. | 58 if (!enabled) |
59 embed.setAttribute('src', '/pnacl/piex_loader.nmf.js'); | 59 return Promise.reject('PiexLoader is not enabled for chromium build.'); |
60 embed.width = 0; | 60 return new Promise(function(fulfill, reject) { |
61 embed.height = 0; | 61 var embed = this.naclModule_; |
| 62 embed.setAttribute('type', 'application/x-pnacl'); |
| 63 // The extension nmf is not allowed to load. We uses .nmf.js instead. |
| 64 embed.setAttribute('src', '/piex/piex.nmf.txt'); |
| 65 embed.width = 0; |
| 66 embed.height = 0; |
62 | 67 |
63 // The <EMBED> element is wrapped inside a <DIV>, which has both a 'load' | 68 // The <EMBED> element is wrapped inside a <DIV>, which has both a 'load' |
64 // and a 'message' event listener attached. This wrapping method is used | 69 // and a 'message' event listener attached. This wrapping method is used |
65 // instead of attaching the event listeners directly to the <EMBED> element | 70 // instead of attaching the event listeners directly to the <EMBED> |
66 // to ensure that the listeners are active before the NaCl module 'load' | 71 // element to ensure that the listeners are active before the NaCl module |
67 // event fires. | 72 // 'load' event fires. |
68 var listenerContainer = document.createElement('div'); | 73 var listenerContainer = document.createElement('div'); |
69 listenerContainer.appendChild(embed); | 74 listenerContainer.appendChild(embed); |
70 listenerContainer.addEventListener('load', fulfill, true); | 75 listenerContainer.addEventListener('load', fulfill, true); |
71 listenerContainer.addEventListener( | 76 listenerContainer.addEventListener( |
72 'message', this.onMessage_.bind(this), true); | 77 'message', this.onMessage_.bind(this), true); |
73 listenerContainer.addEventListener('error', function() { | 78 listenerContainer.addEventListener('error', function() { |
74 console.error(embed['lastError']); | 79 reject(embed['lastError']); |
75 }.bind(this), true); | 80 }.bind(this), true); |
76 listenerContainer.addEventListener('crash', function() { | 81 listenerContainer.addEventListener('crash', function() { |
77 console.error('PiexLoader crashed.'); | 82 reject('PiexLoader crashed.'); |
78 }.bind(this), true); | 83 }.bind(this), true); |
79 listenerContainer.style.height = '0px'; | 84 listenerContainer.style.height = '0px'; |
80 document.body.appendChild(listenerContainer); | 85 document.body.appendChild(listenerContainer); |
| 86 }.bind(this)); |
81 }.bind(this)); | 87 }.bind(this)); |
82 | 88 |
83 /** | 89 /** |
84 * @private {!Object<number, PiexRequestCallbacks>} | 90 * @private {!Object<number, PiexRequestCallbacks>} |
85 * @const | 91 * @const |
86 */ | 92 */ |
87 this.requests_ = {}; | 93 this.requests_ = {}; |
88 | 94 |
89 /** | 95 /** |
90 * @private {number} | 96 * @private {number} |
(...skipping 27 matching lines...) Expand all Loading... |
118 id: this.requestIdCount_++, | 124 id: this.requestIdCount_++, |
119 name: 'loadThumbnail', | 125 name: 'loadThumbnail', |
120 url: url | 126 url: url |
121 }; | 127 }; |
122 this.naclModule_.postMessage(message); | 128 this.naclModule_.postMessage(message); |
123 return new Promise(function(fulfill, reject) { | 129 return new Promise(function(fulfill, reject) { |
124 this.requests_[message.id] = {fulfill: fulfill, reject: reject}; | 130 this.requests_[message.id] = {fulfill: fulfill, reject: reject}; |
125 }.bind(this)); | 131 }.bind(this)); |
126 }.bind(this)); | 132 }.bind(this)); |
127 }; | 133 }; |
OLD | NEW |