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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
58 * @struct | 58 * @struct |
59 */ | 59 */ |
60 function PiexLoader() { | 60 function PiexLoader() { |
61 /** | 61 /** |
62 * @private {!Element} | 62 * @private {!Element} |
63 * @const | 63 * @const |
64 */ | 64 */ |
65 this.naclModule_ = document.createElement('embed'); | 65 this.naclModule_ = document.createElement('embed'); |
66 | 66 |
67 /** | 67 /** |
68 * @private {!Promise} | 68 * @private {!Promise<boolean>} |
69 * @const | 69 * @const |
70 */ | 70 */ |
71 this.naclPromise_ = new Promise(function(fulfill) { | 71 this.naclPromise_ = new Promise(function(fulfill) { |
72 chrome.fileManagerPrivate.isPiexLoaderEnabled(fulfill); | 72 chrome.fileManagerPrivate.isPiexLoaderEnabled(fulfill); |
73 }).then(function(enabled) { | 73 }).then(function(enabled) { |
74 if (!enabled) | 74 if (!enabled) |
75 return Promise.reject('PiexLoader is not enabled for chromium build.'); | 75 return false; |
76 return new Promise(function(fulfill, reject) { | 76 return new Promise(function(fulfill, reject) { |
77 var embed = this.naclModule_; | 77 var embed = this.naclModule_; |
78 embed.setAttribute('type', 'application/x-pnacl'); | 78 embed.setAttribute('type', 'application/x-pnacl'); |
79 // The extension nmf is not allowed to load. We uses .nmf.js instead. | 79 // The extension nmf is not allowed to load. We uses .nmf.js instead. |
80 embed.setAttribute('src', '/piex/piex.nmf.txt'); | 80 embed.setAttribute('src', '/piex/piex.nmf.txt'); |
81 embed.width = 0; | 81 embed.width = 0; |
82 embed.height = 0; | 82 embed.height = 0; |
83 | 83 |
84 // The <EMBED> element is wrapped inside a <DIV>, which has both a 'load' | 84 // The <EMBED> element is wrapped inside a <DIV>, which has both a 'load' |
85 // and a 'message' event listener attached. This wrapping method is used | 85 // and a 'message' event listener attached. This wrapping method is used |
86 // instead of attaching the event listeners directly to the <EMBED> | 86 // instead of attaching the event listeners directly to the <EMBED> |
87 // element to ensure that the listeners are active before the NaCl module | 87 // element to ensure that the listeners are active before the NaCl module |
88 // 'load' event fires. | 88 // 'load' event fires. |
89 var listenerContainer = document.createElement('div'); | 89 var listenerContainer = document.createElement('div'); |
90 listenerContainer.appendChild(embed); | 90 listenerContainer.appendChild(embed); |
91 listenerContainer.addEventListener('load', fulfill, true); | 91 listenerContainer.addEventListener('load', fulfill, true); |
92 listenerContainer.addEventListener( | 92 listenerContainer.addEventListener( |
93 'message', this.onMessage_.bind(this), true); | 93 'message', this.onMessage_.bind(this), true); |
94 listenerContainer.addEventListener('error', function() { | 94 listenerContainer.addEventListener('error', function() { |
95 reject(embed['lastError']); | 95 reject(embed['lastError']); |
96 }.bind(this), true); | 96 }.bind(this), true); |
97 listenerContainer.addEventListener('crash', function() { | 97 listenerContainer.addEventListener('crash', function() { |
98 reject('PiexLoader crashed.'); | 98 reject('PiexLoader crashed.'); |
99 }.bind(this), true); | 99 }.bind(this), true); |
100 listenerContainer.style.height = '0px'; | 100 listenerContainer.style.height = '0px'; |
101 document.body.appendChild(listenerContainer); | 101 document.body.appendChild(listenerContainer); |
102 return true; | |
oka
2017/02/07 17:01:33
wondering why it's not fulfill(true).
hirono
2017/02/09 03:52:55
Yes, the line is wrong. Removed this and updated #
| |
102 }.bind(this)); | 103 }.bind(this)); |
103 }.bind(this)); | 104 }.bind(this)).catch(function (error) { |
105 console.error(error); | |
106 return false; | |
107 }); | |
104 | 108 |
105 /** | 109 /** |
106 * @private {!Object<number, PiexRequestCallbacks>} | 110 * @private {!Object<number, PiexRequestCallbacks>} |
107 * @const | 111 * @const |
108 */ | 112 */ |
109 this.requests_ = {}; | 113 this.requests_ = {}; |
110 | 114 |
111 /** | 115 /** |
112 * @private {number} | 116 * @private {number} |
113 */ | 117 */ |
(...skipping 14 matching lines...) Expand all Loading... | |
128 } | 132 } |
129 delete this.requests_[id]; | 133 delete this.requests_[id]; |
130 }; | 134 }; |
131 | 135 |
132 /** | 136 /** |
133 * Starts to load RAW image. | 137 * Starts to load RAW image. |
134 * @param {string} url | 138 * @param {string} url |
135 * @return {!Promise<!PiexLoaderResponse>} | 139 * @return {!Promise<!PiexLoaderResponse>} |
136 */ | 140 */ |
137 PiexLoader.prototype.load = function(url) { | 141 PiexLoader.prototype.load = function(url) { |
138 return this.naclPromise_.then(function() { | 142 return this.naclPromise_.then(function(loaded) { |
143 if (!loaded) | |
144 return Promise.reject('Piex is not loaded'); | |
139 var message = { | 145 var message = { |
140 id: this.requestIdCount_++, | 146 id: this.requestIdCount_++, |
141 name: 'loadThumbnail', | 147 name: 'loadThumbnail', |
142 url: url | 148 url: url |
143 }; | 149 }; |
144 this.naclModule_.postMessage(message); | 150 this.naclModule_.postMessage(message); |
145 return new Promise(function(fulfill, reject) { | 151 return new Promise(function(fulfill, reject) { |
146 this.requests_[message.id] = {fulfill: fulfill, reject: reject}; | 152 this.requests_[message.id] = {fulfill: fulfill, reject: reject}; |
147 }.bind(this)); | 153 }.bind(this)).catch(function(error) { |
154 console.error('PiexLoaderError: ', error); | |
155 return Promise.reject(error); | |
156 }); | |
148 }.bind(this)); | 157 }.bind(this)); |
149 }; | 158 }; |
OLD | NEW |