| 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 * Persistent cache storing images in an indexed database on the hard disk. | 8 * Persistent cache storing images in an indexed database on the hard disk. |
| 9 * @constructor | 9 * @constructor |
| 10 */ | 10 */ |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 */ | 48 */ |
| 49 Cache.EVICTION_CHUNK_SIZE = 50 * 1024 * 1024; // 50 MB. | 49 Cache.EVICTION_CHUNK_SIZE = 50 * 1024 * 1024; // 50 MB. |
| 50 | 50 |
| 51 /** | 51 /** |
| 52 * Creates a cache key. | 52 * Creates a cache key. |
| 53 * | 53 * |
| 54 * @param {Object} request Request options. | 54 * @param {Object} request Request options. |
| 55 * @return {string} Cache key. | 55 * @return {string} Cache key. |
| 56 */ | 56 */ |
| 57 Cache.createKey = function(request) { | 57 Cache.createKey = function(request) { |
| 58 return JSON.stringify({url: request.url, | 58 return JSON.stringify({ |
| 59 scale: request.scale, | 59 url: request.url, |
| 60 width: request.width, | 60 scale: request.scale, |
| 61 height: request.height, | 61 width: request.width, |
| 62 maxWidth: request.maxWidth, | 62 height: request.height, |
| 63 maxHeight: request.maxHeight}); | 63 maxWidth: request.maxWidth, |
| 64 maxHeight: request.maxHeight}); |
| 64 }; | 65 }; |
| 65 | 66 |
| 66 /** | 67 /** |
| 67 * Initializes the cache database. | 68 * Initializes the cache database. |
| 68 * @param {function()} callback Completion callback. | 69 * @param {function()} callback Completion callback. |
| 69 */ | 70 */ |
| 70 Cache.prototype.initialize = function(callback) { | 71 Cache.prototype.initialize = function(callback) { |
| 71 // Establish a connection to the database or (re)create it if not available | 72 // Establish a connection to the database or (re)create it if not available |
| 72 // or not up to date. After changing the database's schema, increment | 73 // or not up to date. After changing the database's schema, increment |
| 73 // Cache.DB_VERSION to force database recreating. | 74 // Cache.DB_VERSION to force database recreating. |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 * @param {number} timestamp Last modification timestamp. Used to detect | 217 * @param {number} timestamp Last modification timestamp. Used to detect |
| 217 * if the cache entry becomes out of date. | 218 * if the cache entry becomes out of date. |
| 218 */ | 219 */ |
| 219 Cache.prototype.saveImage = function(key, data, timestamp) { | 220 Cache.prototype.saveImage = function(key, data, timestamp) { |
| 220 if (!this.db_) { | 221 if (!this.db_) { |
| 221 console.warn('Cache database not available.'); | 222 console.warn('Cache database not available.'); |
| 222 return; | 223 return; |
| 223 } | 224 } |
| 224 | 225 |
| 225 var onNotFoundInCache = function() { | 226 var onNotFoundInCache = function() { |
| 226 var metadataEntry = {key: key, | 227 var metadataEntry = { |
| 227 timestamp: timestamp, | 228 key: key, |
| 228 size: data.length, | 229 timestamp: timestamp, |
| 229 lastLoadTimestamp: Date.now()}; | 230 size: data.length, |
| 230 var dataEntry = {key: key, | 231 lastLoadTimestamp: Date.now()}; |
| 231 data: data}; | 232 var dataEntry = {key: key, data: data}; |
| 232 | 233 |
| 233 var transaction = this.db_.transaction(['settings', 'metadata', 'data'], | 234 var transaction = this.db_.transaction(['settings', 'metadata', 'data'], |
| 234 'readwrite'); | 235 'readwrite'); |
| 235 var metadataStore = transaction.objectStore('metadata'); | 236 var metadataStore = transaction.objectStore('metadata'); |
| 236 var dataStore = transaction.objectStore('data'); | 237 var dataStore = transaction.objectStore('data'); |
| 237 | 238 |
| 238 var onCacheEvicted = function() { | 239 var onCacheEvicted = function() { |
| 239 metadataStore.put(metadataEntry); // Add asynchronously. | 240 metadataStore.put(metadataEntry); // Add asynchronously. |
| 240 dataStore.put(dataEntry); // Add asynchronously. | 241 dataStore.put(dataEntry); // Add asynchronously. |
| 241 }; | 242 }; |
| 242 | 243 |
| 243 // Make sure there is enough space in the cache. | 244 // Make sure there is enough space in the cache. |
| 244 this.evictCache_(data.length, onCacheEvicted, function() {}, transaction); | 245 this.evictCache_(data.length, onCacheEvicted, function() {}, transaction); |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 metadataReceived = true; | 401 metadataReceived = true; |
| 401 onPartialSuccess(); | 402 onPartialSuccess(); |
| 402 }; | 403 }; |
| 403 | 404 |
| 404 metadataRequest.onerror = function() { | 405 metadataRequest.onerror = function() { |
| 405 console.error('Failed to remove an image.'); | 406 console.error('Failed to remove an image.'); |
| 406 metadataReceived = true; | 407 metadataReceived = true; |
| 407 onPartialSuccess(); | 408 onPartialSuccess(); |
| 408 }; | 409 }; |
| 409 }; | 410 }; |
| OLD | NEW |