OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 * Object representing an image item (a photo). | 8 * Object representing an image item (a photo). |
9 * | 9 * |
10 * @param {FileEntry} entry Image entry. | 10 * @param {FileEntry} entry Image entry. |
11 * @param {function():Promise} fethcedMediaProvider Function to provide the | 11 * @param {function():Promise} fethcedMediaProvider Function to provide the |
12 * fetchedMedia metadata. | 12 * fetchedMedia metadata. |
| 13 * @param {boolean} original Whether the entry is original or edited. |
| 14 * @param {boolean} readonly Whether the entry is located at readonly directory |
| 15 * or not. |
13 * @constructor | 16 * @constructor |
14 */ | 17 */ |
15 Gallery.Item = function(entry, metadata, metadataCache, original) { | 18 Gallery.Item = function(entry, metadata, metadataCache, original, readonly) { |
16 /** | 19 /** |
17 * @type {FileEntry} | 20 * @type {FileEntry} |
18 * @private | 21 * @private |
19 */ | 22 */ |
20 this.entry_ = entry; | 23 this.entry_ = entry; |
21 | 24 |
22 /** | 25 /** |
23 * @type {Object} | 26 * @type {Object} |
24 * @private | 27 * @private |
25 */ | 28 */ |
(...skipping 25 matching lines...) Expand all Loading... |
51 /** | 54 /** |
52 * Last accessed date to be used for selecting items whose cache are evicted. | 55 * Last accessed date to be used for selecting items whose cache are evicted. |
53 * @type {number} | 56 * @type {number} |
54 */ | 57 */ |
55 this.lastAccessed_ = Date.now(); | 58 this.lastAccessed_ = Date.now(); |
56 | 59 |
57 /** | 60 /** |
58 * @type {boolean} | 61 * @type {boolean} |
59 * @private | 62 * @private |
60 */ | 63 */ |
| 64 this.isReadOnly_ = readonly; |
| 65 |
| 66 /** |
| 67 * @type {boolean} |
| 68 * @private |
| 69 */ |
61 this.original_ = original; | 70 this.original_ = original; |
62 | 71 |
63 Object.seal(this); | 72 Object.seal(this); |
64 }; | 73 }; |
65 | 74 |
66 /** | 75 /** |
67 * @return {FileEntry} Image entry. | 76 * @return {FileEntry} Image entry. |
68 */ | 77 */ |
69 Gallery.Item.prototype.getEntry = function() { return this.entry_; }; | 78 Gallery.Item.prototype.getEntry = function() { return this.entry_; }; |
70 | 79 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 Gallery.Item.prototype.getFileName = function() { | 117 Gallery.Item.prototype.getFileName = function() { |
109 return this.entry_.name; | 118 return this.entry_.name; |
110 }; | 119 }; |
111 | 120 |
112 /** | 121 /** |
113 * @return {boolean} True if this image has not been created in this session. | 122 * @return {boolean} True if this image has not been created in this session. |
114 */ | 123 */ |
115 Gallery.Item.prototype.isOriginal = function() { return this.original_; }; | 124 Gallery.Item.prototype.isOriginal = function() { return this.original_; }; |
116 | 125 |
117 /** | 126 /** |
| 127 * @return {boolean} Whther the item is located at a readonly directory. |
| 128 */ |
| 129 Gallery.Item.prototype.isReadOnly = function() { |
| 130 return this.isReadOnly_; |
| 131 }; |
| 132 |
| 133 /** |
118 * Obtains the item is on the drive volume or not. | 134 * Obtains the item is on the drive volume or not. |
119 * @return {boolean} True if the item is on the drive volume. | 135 * @return {boolean} True if the item is on the drive volume. |
120 */ | 136 */ |
121 Gallery.Item.prototype.isOnDrive = function() { | 137 Gallery.Item.prototype.isOnDrive = function() { |
122 return !!this.metadata_.drive; | 138 return !!this.metadata_.drive; |
123 }; | 139 }; |
124 | 140 |
125 /** | 141 /** |
126 * Obtains the last accessed date. | 142 * Obtains the last accessed date. |
127 * @return {number} Last accessed date. | 143 * @return {number} Last accessed date. |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 tryNext.bind(null, tries - 1), | 228 tryNext.bind(null, tries - 1), |
213 callback.bind(null, name + ext)); | 229 callback.bind(null, name + ext)); |
214 } | 230 } |
215 | 231 |
216 tryNext(10); | 232 tryNext(10); |
217 }; | 233 }; |
218 | 234 |
219 /** | 235 /** |
220 * Writes the new item content to the file. | 236 * Writes the new item content to the file. |
221 * | 237 * |
222 * @param {Entry} overrideDir Directory to save to. If null, save to the same | 238 * @param {DirectoryEntry} fallbackDir If the entry is readonly, the edited |
223 * directory as the original. | 239 * image is saved to the directory. |
224 * @param {boolean} overwrite True if overwrite, false if copy. | 240 * @param {boolean} overwrite True if overwrite, false if copy. |
225 * @param {HTMLCanvasElement} canvas Source canvas. | 241 * @param {HTMLCanvasElement} canvas Source canvas. |
226 * @param {ImageEncoder.MetadataEncoder} metadataEncoder MetadataEncoder. | 242 * @param {ImageEncoder.MetadataEncoder} metadataEncoder MetadataEncoder. |
227 * @param {function(boolean)=} opt_callback Callback accepting true for success. | 243 * @param {function(boolean)=} opt_callback Callback accepting true for success. |
228 */ | 244 */ |
229 Gallery.Item.prototype.saveToFile = function( | 245 Gallery.Item.prototype.saveToFile = function( |
230 overrideDir, overwrite, canvas, metadataEncoder, opt_callback) { | 246 fallbackDir, overwrite, canvas, metadataEncoder, opt_callback) { |
231 ImageUtil.metrics.startInterval(ImageUtil.getMetricName('SaveTime')); | 247 ImageUtil.metrics.startInterval(ImageUtil.getMetricName('SaveTime')); |
232 | 248 |
233 var name = this.getFileName(); | 249 var name = this.getFileName(); |
234 | 250 |
235 var onSuccess = function(entry) { | 251 var onSuccess = function(entry) { |
236 ImageUtil.metrics.recordEnum(ImageUtil.getMetricName('SaveResult'), 1, 2); | 252 ImageUtil.metrics.recordEnum(ImageUtil.getMetricName('SaveResult'), 1, 2); |
237 ImageUtil.metrics.recordInterval(ImageUtil.getMetricName('SaveTime')); | 253 ImageUtil.metrics.recordInterval(ImageUtil.getMetricName('SaveTime')); |
238 this.entry_ = entry; | 254 this.entry_ = entry; |
| 255 this.isReadOnly_ = false; |
239 this.metadataCache_.clear([this.entry_], 'fetchedMedia'); | 256 this.metadataCache_.clear([this.entry_], 'fetchedMedia'); |
240 if (opt_callback) | 257 if (opt_callback) |
241 opt_callback(true); | 258 opt_callback(true); |
242 }.bind(this); | 259 }.bind(this); |
243 | 260 |
244 function onError(error) { | 261 function onError(error) { |
245 console.error('Error saving from gallery', name, error); | 262 console.error('Error saving from gallery', name, error); |
246 ImageUtil.metrics.recordEnum(ImageUtil.getMetricName('SaveResult'), 0, 2); | 263 ImageUtil.metrics.recordEnum(ImageUtil.getMetricName('SaveResult'), 0, 2); |
247 if (opt_callback) | 264 if (opt_callback) |
248 opt_callback(false); | 265 opt_callback(false); |
(...skipping 25 matching lines...) Expand all Loading... |
274 doSave.bind(null, newFile), onError); | 291 doSave.bind(null, newFile), onError); |
275 } | 292 } |
276 | 293 |
277 function checkExistence(dir) { | 294 function checkExistence(dir) { |
278 dir.getFile(name, {create: false, exclusive: false}, | 295 dir.getFile(name, {create: false, exclusive: false}, |
279 getFile.bind(null, dir, false /* existing file */), | 296 getFile.bind(null, dir, false /* existing file */), |
280 getFile.bind(null, dir, true /* create new file */)); | 297 getFile.bind(null, dir, true /* create new file */)); |
281 } | 298 } |
282 | 299 |
283 var saveToDir = function(dir) { | 300 var saveToDir = function(dir) { |
284 if (overwrite) { | 301 if (overwrite && !this.isReadOnly_) { |
285 checkExistence(dir); | 302 checkExistence(dir); |
286 } else { | 303 } else { |
287 this.createCopyName_(dir, function(copyName) { | 304 this.createCopyName_(dir, function(copyName) { |
288 this.original_ = false; | 305 this.original_ = false; |
289 name = copyName; | 306 name = copyName; |
290 checkExistence(dir); | 307 checkExistence(dir); |
291 }.bind(this)); | 308 }.bind(this)); |
292 } | 309 } |
293 }.bind(this); | 310 }.bind(this); |
294 | 311 |
295 if (overrideDir) { | 312 if (this.isReadOnly_) { |
296 saveToDir(overrideDir); | 313 saveToDir(fallbackDir); |
297 } else { | 314 } else { |
298 this.entry_.getParent(saveToDir, onError); | 315 this.entry_.getParent(saveToDir, onError); |
299 } | 316 } |
300 }; | 317 }; |
301 | 318 |
302 /** | 319 /** |
303 * Renames the item. | 320 * Renames the item. |
304 * | 321 * |
305 * @param {string} displayName New display name (without the extension). | 322 * @param {string} displayName New display name (without the extension). |
306 * @return {Promise} Promise fulfilled with when renaming completes, or rejected | 323 * @return {Promise} Promise fulfilled with when renaming completes, or rejected |
(...skipping 21 matching lines...) Expand all Loading... |
328 return Promise.reject(str('GALLERY_FILE_EXISTS')); | 345 return Promise.reject(str('GALLERY_FILE_EXISTS')); |
329 }, function() { | 346 }, function() { |
330 return new Promise( | 347 return new Promise( |
331 this.entry_.moveTo.bind(this.entry_, parentDirectory, newFileName)); | 348 this.entry_.moveTo.bind(this.entry_, parentDirectory, newFileName)); |
332 }.bind(this)); | 349 }.bind(this)); |
333 }.bind(this)); | 350 }.bind(this)); |
334 }.bind(this)).then(function(entry) { | 351 }.bind(this)).then(function(entry) { |
335 this.entry_ = entry; | 352 this.entry_ = entry; |
336 }.bind(this)); | 353 }.bind(this)); |
337 }; | 354 }; |
OLD | NEW |