1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/**
* Loads and resizes an image.
* @constructor
*/
function ImageLoader() {
/**
* Persistent cache object.
* @type {Cache}
* @private
*/
this.cache_ = new Cache();
/**
* Manages pending requests and runs them in order of priorities.
* @type {Worker}
* @private
*/
this.worker_ = new Worker();
// Grant permissions to the local file system and initialize the cache.
chrome.fileBrowserPrivate.requestFileSystem(
'compatible',
function(filesystem) {
this.cache_.initialize(function() {
this.worker_.start();
}.bind(this));
}.bind(this));
chrome.extension.onMessageExternal.addListener(function(request,
sender,
sendResponse) {
if (ImageLoader.ALLOWED_CLIENTS.indexOf(sender.id) !== -1) {
// Sending a response may fail if the receiver already went offline.
// This is not an error, but a normal and quite common situation.
var failSafeSendResponse = function(response) {
try {
sendResponse(response);
}
catch (e) {
// Ignore the error.
}
};
return this.onMessage_(sender.id, request, failSafeSendResponse);
}
}.bind(this));
}
/**
* List of extensions allowed to perform image requests.
*
* @const
* @type {Array.<string>}
*/
ImageLoader.ALLOWED_CLIENTS =
['hhaomjibdihmijegdhdafkllkbggdgoj']; // File Manager's extension id.
/**
* Handles a request. Depending on type of the request, starts or stops
* an image task.
*
* @param {string} senderId Sender's extension id.
* @param {Object} request Request message as a hash array.
* @param {function} callback Callback to be called to return response.
* @return {boolean} True if the message channel should stay alive until the
* callback is called.
* @private
*/
ImageLoader.prototype.onMessage_ = function(senderId, request, callback) {
var requestId = senderId + ':' + request.taskId;
if (request.cancel) {
// Cancel a task.
this.worker_.remove(requestId);
return false; // No callback calls.
} else {
// Create a request task and add it to the worker (queue).
var requestTask = new Request(requestId, this.cache_, request, callback);
this.worker_.add(requestTask);
return true; // Request will call the callback.
}
};
/**
* Returns the singleton instance.
* @return {ImageLoader} ImageLoader object.
*/
ImageLoader.getInstance = function() {
if (!ImageLoader.instance_)
ImageLoader.instance_ = new ImageLoader();
return ImageLoader.instance_;
};
/**
* Checks if the options contain any image processing.
*
* @param {number} width Source width.
* @param {number} height Source height.
* @param {Object} options Resizing options as a hash array.
* @return {boolean} True if yes, false if not.
*/
ImageLoader.shouldProcess = function(width, height, options) {
var targetDimensions = ImageLoader.resizeDimensions(width, height, options);
// Dimensions has to be adjusted.
if (targetDimensions.width != width || targetDimensions.height != height)
return true;
// Orientation has to be adjusted.
if (options.orientation)
return true;
// No changes required.
return false;
};
/**
* Calculates dimensions taking into account resize options, such as:
* - scale: for scaling,
* - maxWidth, maxHeight: for maximum dimensions,
* - width, height: for exact requested size.
* Returns the target size as hash array with width, height properties.
*
* @param {number} width Source width.
* @param {number} height Source height.
* @param {Object} options Resizing options as a hash array.
* @return {Object} Dimensions, eg. {width: 100, height: 50}.
*/
ImageLoader.resizeDimensions = function(width, height, options) {
var sourceWidth = width;
var sourceHeight = height;
// Flip dimensions for odd orientation values: 1 (90deg) and 3 (270deg).
if (options.orientation && options.orientation % 2) {
sourceWidth = height;
sourceHeight = width;
}
var targetWidth = sourceWidth;
var targetHeight = sourceHeight;
if ('scale' in options) {
targetWidth = sourceWidth * options.scale;
targetHeight = sourceHeight * options.scale;
}
if (options.maxWidth &&
targetWidth > options.maxWidth) {
var scale = options.maxWidth / targetWidth;
targetWidth *= scale;
targetHeight *= scale;
}
if (options.maxHeight &&
targetHeight > options.maxHeight) {
var scale = options.maxHeight / targetHeight;
targetWidth *= scale;
targetHeight *= scale;
}
if (options.width)
targetWidth = options.width;
if (options.height)
targetHeight = options.height;
targetWidth = Math.round(targetWidth);
targetHeight = Math.round(targetHeight);
return {width: targetWidth, height: targetHeight};
};
/**
* Performs resizing of the source image into the target canvas.
*
* @param {HTMLCanvasElement|Image} source Source image or canvas.
* @param {HTMLCanvasElement} target Target canvas.
* @param {Object} options Resizing options as a hash array.
*/
ImageLoader.resize = function(source, target, options) {
var targetDimensions = ImageLoader.resizeDimensions(
source.width, source.height, options);
target.width = targetDimensions.width;
target.height = targetDimensions.height;
// Default orientation is 0deg.
var orientation = options.orientation || 0;
// For odd orientation values: 1 (90deg) and 3 (270deg) flip dimensions.
var drawImageWidth;
var drawImageHeight;
if (orientation % 2) {
drawImageWidth = target.height;
drawImageHeight = target.width;
} else {
drawImageWidth = target.width;
drawImageHeight = target.height;
}
var targetContext = target.getContext('2d');
targetContext.save();
targetContext.translate(target.width / 2, target.height / 2);
targetContext.rotate(orientation * Math.PI / 2);
targetContext.drawImage(
source,
0, 0,
source.width, source.height,
-drawImageWidth / 2, -drawImageHeight / 2,
drawImageWidth, drawImageHeight);
targetContext.restore();
};
|