| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 'use strict'; | 5 'use strict'; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Loads and resizes an image. | 8 * Loads and resizes an image. |
| 9 * @constructor | 9 * @constructor |
| 10 */ | 10 */ |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 chrome.fileManagerPrivate.onMountCompleted.addListener( | 43 chrome.fileManagerPrivate.onMountCompleted.addListener( |
| 44 function(event) { | 44 function(event) { |
| 45 if (event.eventType == 'mount' && event.status == 'success') { | 45 if (event.eventType == 'mount' && event.status == 'success') { |
| 46 chrome.fileManagerPrivate.requestFileSystem( | 46 chrome.fileManagerPrivate.requestFileSystem( |
| 47 event.volumeMetadata.volumeId, function() {}); | 47 event.volumeMetadata.volumeId, function() {}); |
| 48 } | 48 } |
| 49 }); | 49 }); |
| 50 }.bind(this)); | 50 }.bind(this)); |
| 51 | 51 |
| 52 // Listen for incoming requests. | 52 // Listen for incoming requests. |
| 53 chrome.extension.onMessageExternal.addListener(function(request, | 53 chrome.extension.onMessageExternal.addListener( |
| 54 sender, | 54 function(request, sender, sendResponse) { |
| 55 sendResponse) { | 55 if (ImageLoader.ALLOWED_CLIENTS.indexOf(sender.id) !== -1) { |
| 56 if (ImageLoader.ALLOWED_CLIENTS.indexOf(sender.id) !== -1) { | 56 // Sending a response may fail if the receiver already went offline. |
| 57 // Sending a response may fail if the receiver already went offline. | 57 // This is not an error, but a normal and quite common situation. |
| 58 // This is not an error, but a normal and quite common situation. | 58 var failSafeSendResponse = function(response) { |
| 59 var failSafeSendResponse = function(response) { | 59 try { |
| 60 try { | 60 sendResponse(response); |
| 61 sendResponse(response); | 61 } |
| 62 catch (e) { |
| 63 // Ignore the error. |
| 64 } |
| 65 }; |
| 66 return this.onMessage_(sender.id, request, failSafeSendResponse); |
| 62 } | 67 } |
| 63 catch (e) { | 68 }.bind(this)); |
| 64 // Ignore the error. | |
| 65 } | |
| 66 }; | |
| 67 return this.onMessage_(sender.id, request, failSafeSendResponse); | |
| 68 } | |
| 69 }.bind(this)); | |
| 70 } | 69 } |
| 71 | 70 |
| 72 /** | 71 /** |
| 73 * List of extensions allowed to perform image requests. | 72 * List of extensions allowed to perform image requests. |
| 74 * | 73 * |
| 75 * @const | 74 * @const |
| 76 * @type {Array.<string>} | 75 * @type {Array.<string>} |
| 77 */ | 76 */ |
| 78 ImageLoader.ALLOWED_CLIENTS = [ | 77 ImageLoader.ALLOWED_CLIENTS = [ |
| 79 'hhaomjibdihmijegdhdafkllkbggdgoj', // File Manager's extension id. | 78 'hhaomjibdihmijegdhdafkllkbggdgoj', // File Manager's extension id. |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 } | 160 } |
| 162 | 161 |
| 163 var targetWidth = sourceWidth; | 162 var targetWidth = sourceWidth; |
| 164 var targetHeight = sourceHeight; | 163 var targetHeight = sourceHeight; |
| 165 | 164 |
| 166 if ('scale' in options) { | 165 if ('scale' in options) { |
| 167 targetWidth = sourceWidth * options.scale; | 166 targetWidth = sourceWidth * options.scale; |
| 168 targetHeight = sourceHeight * options.scale; | 167 targetHeight = sourceHeight * options.scale; |
| 169 } | 168 } |
| 170 | 169 |
| 171 if (options.maxWidth && | 170 if (options.maxWidth && targetWidth > options.maxWidth) { |
| 172 targetWidth > options.maxWidth) { | 171 var scale = options.maxWidth / targetWidth; |
| 173 var scale = options.maxWidth / targetWidth; | 172 targetWidth *= scale; |
| 174 targetWidth *= scale; | 173 targetHeight *= scale; |
| 175 targetHeight *= scale; | |
| 176 } | 174 } |
| 177 | 175 |
| 178 if (options.maxHeight && | 176 if (options.maxHeight && targetHeight > options.maxHeight) { |
| 179 targetHeight > options.maxHeight) { | 177 var scale = options.maxHeight / targetHeight; |
| 180 var scale = options.maxHeight / targetHeight; | 178 targetWidth *= scale; |
| 181 targetWidth *= scale; | 179 targetHeight *= scale; |
| 182 targetHeight *= scale; | |
| 183 } | 180 } |
| 184 | 181 |
| 185 if (options.width) | 182 if (options.width) |
| 186 targetWidth = options.width; | 183 targetWidth = options.width; |
| 187 | 184 |
| 188 if (options.height) | 185 if (options.height) |
| 189 targetHeight = options.height; | 186 targetHeight = options.height; |
| 190 | 187 |
| 191 targetWidth = Math.round(targetWidth); | 188 targetWidth = Math.round(targetWidth); |
| 192 targetHeight = Math.round(targetHeight); | 189 targetHeight = Math.round(targetHeight); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 targetContext.translate(target.width / 2, target.height / 2); | 224 targetContext.translate(target.width / 2, target.height / 2); |
| 228 targetContext.rotate(orientation * Math.PI / 2); | 225 targetContext.rotate(orientation * Math.PI / 2); |
| 229 targetContext.drawImage( | 226 targetContext.drawImage( |
| 230 source, | 227 source, |
| 231 0, 0, | 228 0, 0, |
| 232 source.width, source.height, | 229 source.width, source.height, |
| 233 -drawImageWidth / 2, -drawImageHeight / 2, | 230 -drawImageWidth / 2, -drawImageHeight / 2, |
| 234 drawImageWidth, drawImageHeight); | 231 drawImageWidth, drawImageHeight); |
| 235 targetContext.restore(); | 232 targetContext.restore(); |
| 236 }; | 233 }; |
| OLD | NEW |