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
3 // found in the LICENSE file.
7 * fulfill: function(PiexLoaderResponse):undefined,
8 * reject: function(string):undefined}
11 var PiexRequestCallbacks;
23 * @param {{id:number, thumbnail:!ArrayBuffer, orientation:number,
24 * colorSpace: ColorSpace}}
25 * data Data directly returned from NaCl module.
29 function PiexLoaderResponse(data) {
37 * @public {!ArrayBuffer}
40 this.thumbnail = data.thumbnail;
43 * @public {!ImageOrientation}
47 ImageOrientation.fromExifOrientation(data.orientation);
50 * @public {ColorSpace}
53 this.colorSpace = data.colorSpace;
60 function PiexLoader() {
65 this.naclModule_ = document.createElement('embed');
71 this.naclPromise_ = new Promise(function(fulfill) {
72 chrome.fileManagerPrivate.isPiexLoaderEnabled(fulfill);
73 }).then(function(enabled) {
75 return Promise.reject('PiexLoader is not enabled for chromium build.');
76 return new Promise(function(fulfill, reject) {
77 var embed = this.naclModule_;
78 embed.setAttribute('type', 'application/x-pnacl');
79 // The extension nmf is not allowed to load. We uses .nmf.js instead.
80 embed.setAttribute('src', '/piex/piex.nmf.txt');
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
86 // instead of attaching the event listeners directly to the <EMBED>
87 // element to ensure that the listeners are active before the NaCl module
88 // 'load' event fires.
89 var listenerContainer = document.createElement('div');
90 listenerContainer.appendChild(embed);
91 listenerContainer.addEventListener('load', fulfill, true);
92 listenerContainer.addEventListener(
93 'message', this.onMessage_.bind(this), true);
94 listenerContainer.addEventListener('error', function() {
95 reject(embed['lastError']);
97 listenerContainer.addEventListener('crash', function() {
98 reject('PiexLoader crashed.');
100 listenerContainer.style.height = '0px';
101 document.body.appendChild(listenerContainer);
106 * @private {!Object<number, PiexRequestCallbacks>}
114 this.requestIdCount_ = 0;
118 * @param {Event} event
121 PiexLoader.prototype.onMessage_ = function(event) {
122 var id = event.data.id;
123 if (!event.data.error) {
124 var response = new PiexLoaderResponse(event.data);
125 this.requests_[id].fulfill(response);
127 this.requests_[id].reject(event.data.error);
129 delete this.requests_[id];
133 * Starts to load RAW image.
134 * @param {string} url
135 * @return {!Promise<!PiexLoaderResponse>}
137 PiexLoader.prototype.load = function(url) {
138 return this.naclPromise_.then(function() {
140 id: this.requestIdCount_++,
141 name: 'loadThumbnail',
144 this.naclModule_.postMessage(message);
145 return new Promise(function(fulfill, reject) {
146 this.requests_[message.id] = {fulfill: fulfill, reject: reject};