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 /** | 5 /** |
6 * Overrided metadata worker's path. | 6 * Overrided metadata worker's path. |
7 * @type {string} | 7 * @type {string} |
8 */ | 8 */ |
9 ContentProvider.WORKER_SCRIPT = '/js/metadata_worker.js'; | 9 ContentProvider.WORKER_SCRIPT = '/js/metadata_worker.js'; |
10 | 10 |
11 /** | 11 /** |
12 * Data model for gallery. | |
13 * | |
14 * @param {!MetadataCache} metadataCache Metadata cache. | |
15 * @constructor | |
16 * @extends {cr.ui.ArrayDataModel} | |
17 */ | |
18 function GalleryDataModel(metadataCache) { | |
19 cr.ui.ArrayDataModel.call(this, []); | |
20 | |
21 /** | |
22 * Metadata cache. | |
23 * @type {!MetadataCache} | |
24 * @private | |
25 */ | |
26 this.metadataCache_ = metadataCache; | |
27 | |
28 /** | |
29 * Directory where the image is saved if the image is located in a read-only | |
30 * volume. | |
31 * @type {DirectoryEntry} | |
32 */ | |
33 this.fallbackSaveDirectory = null; | |
34 | |
35 // Start to watch file system entries. | |
36 var watcher = new EntryListWatcher(this); | |
37 watcher.getEntry = function(item) { return item.getEntry(); }; | |
38 } | |
39 | |
40 /** | |
41 * Maximum number of full size image cache. | |
42 * @type {number} | |
43 * @const | |
44 * @private | |
45 */ | |
46 GalleryDataModel.MAX_FULL_IMAGE_CACHE_ = 3; | |
47 | |
48 /** | |
49 * Maximum number of screen size image cache. | |
50 * @type {number} | |
51 * @const | |
52 * @private | |
53 */ | |
54 GalleryDataModel.MAX_SCREEN_IMAGE_CACHE_ = 5; | |
55 | |
56 GalleryDataModel.prototype = { | |
57 __proto__: cr.ui.ArrayDataModel.prototype | |
58 }; | |
59 | |
60 /** | |
61 * Saves new image. | |
62 * | |
63 * @param {!VolumeManager} volumeManager Volume manager instance. | |
64 * @param {!Gallery.Item} item Original gallery item. | |
65 * @param {!HTMLCanvasElement} canvas Canvas containing new image. | |
66 * @param {boolean} overwrite Whether to overwrite the image to the item or not. | |
67 * @return {!Promise} Promise to be fulfilled with when the operation completes. | |
68 */ | |
69 GalleryDataModel.prototype.saveItem = function( | |
70 volumeManager, item, canvas, overwrite) { | |
71 var oldEntry = item.getEntry(); | |
72 var oldMetadata = item.getMetadata(); | |
73 var oldLocationInfo = item.getLocationInfo(); | |
74 return new Promise(function(fulfill, reject) { | |
75 item.saveToFile( | |
76 volumeManager, | |
77 this.fallbackSaveDirectory, | |
78 overwrite, | |
79 canvas, | |
80 function(success) { | |
81 if (!success) { | |
82 reject('Failed to save the image.'); | |
83 return; | |
84 } | |
85 | |
86 // Current entry is updated. | |
87 // Dispatch an event. | |
88 var event = new Event('content'); | |
89 event.item = item; | |
90 event.oldEntry = oldEntry; | |
91 event.metadata = item.getMetadata(); | |
92 this.dispatchEvent(event); | |
93 | |
94 if (!util.isSameEntry(oldEntry, item.getEntry())) { | |
95 // New entry is added and the item now tracks it. | |
96 // Add another item for the old entry. | |
97 var anotherItem = new Gallery.Item( | |
98 oldEntry, | |
99 oldLocationInfo, | |
100 oldMetadata, | |
101 this.metadataCache_, | |
102 item.isOriginal()); | |
103 // The item must be added behind the existing item so that it does | |
104 // not change the index of the existing item. | |
105 // TODO(hirono): Update the item index of the selection model | |
106 // correctly. | |
107 this.splice(this.indexOf(item) + 1, 0, anotherItem); | |
108 } | |
109 | |
110 fulfill(); | |
111 }.bind(this)); | |
112 }.bind(this)); | |
113 }; | |
114 | |
115 /** | |
116 * Evicts image caches in the items. | |
117 */ | |
118 GalleryDataModel.prototype.evictCache = function() { | |
119 // Sort the item by the last accessed date. | |
120 var sorted = this.slice().sort(function(a, b) { | |
121 return b.getLastAccessedDate() - a.getLastAccessedDate(); | |
122 }); | |
123 | |
124 // Evict caches. | |
125 var contentCacheCount = 0; | |
126 var screenCacheCount = 0; | |
127 for (var i = 0; i < sorted.length; i++) { | |
128 if (sorted[i].contentImage) { | |
129 if (++contentCacheCount > GalleryDataModel.MAX_FULL_IMAGE_CACHE_) { | |
130 if (sorted[i].contentImage.parentNode) { | |
131 console.error('The content image has a parent node.'); | |
132 } else { | |
133 // Force to free the buffer of the canvas by assigning zero size. | |
134 sorted[i].contentImage.width = 0; | |
135 sorted[i].contentImage.height = 0; | |
136 sorted[i].contentImage = null; | |
137 } | |
138 } | |
139 } | |
140 if (sorted[i].screenImage) { | |
141 if (++screenCacheCount > GalleryDataModel.MAX_SCREEN_IMAGE_CACHE_) { | |
142 if (sorted[i].screenImage.parentNode) { | |
143 console.error('The screen image has a parent node.'); | |
144 } else { | |
145 // Force to free the buffer of the canvas by assigning zero size. | |
146 sorted[i].screenImage.width = 0; | |
147 sorted[i].screenImage.height = 0; | |
148 sorted[i].screenImage = null; | |
149 } | |
150 } | |
151 } | |
152 } | |
153 }; | |
154 | |
155 /** | |
156 * Gallery for viewing and editing image files. | 12 * Gallery for viewing and editing image files. |
157 * | 13 * |
158 * @param {!VolumeManager} volumeManager The VolumeManager instance of the | 14 * @param {!VolumeManager} volumeManager The VolumeManager instance of the |
159 * system. | 15 * system. |
160 * @constructor | 16 * @constructor |
161 * @struct | 17 * @struct |
162 */ | 18 */ |
163 function Gallery(volumeManager) { | 19 function Gallery(volumeManager) { |
164 /** | 20 /** |
165 * @type {{appWindow: chrome.app.window.AppWindow, onClose: function(), | 21 * @type {{appWindow: chrome.app.window.AppWindow, onClose: function(), |
(...skipping 872 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1038 }; | 894 }; |
1039 | 895 |
1040 /** | 896 /** |
1041 * Loads entries. | 897 * Loads entries. |
1042 * @param {!Array.<Entry>} entries Array of entries. | 898 * @param {!Array.<Entry>} entries Array of entries. |
1043 * @param {!Array.<Entry>} selectedEntries Array of selected entries. | 899 * @param {!Array.<Entry>} selectedEntries Array of selected entries. |
1044 */ | 900 */ |
1045 window.loadEntries = function(entries, selectedEntries) { | 901 window.loadEntries = function(entries, selectedEntries) { |
1046 gallery.load(entries, selectedEntries); | 902 gallery.load(entries, selectedEntries); |
1047 }; | 903 }; |
OLD | NEW |