+
+var gTileService = {
+ objStore: "tilecache",
+
+ ageLimit: 14 * 86400 * 1000, // 2 weeks (in ms)
+
+ get: function(aStyle, aCoords, aCallback) {
+ var norm = normalizeCoords(aCoords);
+ var dbkey = aStyle + "::" + norm.x + "," + norm.y + "," + norm.z;
+ this.getDBCache(dbkey, function(aResult, aEvent) {
+ if (aResult) {
+ // We did get a cached object.
+ aCallback(aResult.image, aStyle, aCoords);
+ // Look at the timestamp and return if it's not too old.
+ if (aResult.timestamp + gTileService.ageLimit > Date.now())
+ return;
+ // Reload cached tile otherwise.
+ var oldDate = new Date(aResult.timestamp);
+ console.log("reload cached tile: " + dbkey + " - " + oldDate.toUTCString());
+ }
+ // Retrieve image from the web and store it in the cache.
+ var XHR = new XMLHttpRequest();
+ XHR.open("GET",
+ gMapStyles[aStyle].url
+ .replace("{x}", norm.x)
+ .replace("{y}", norm.y)
+ .replace("{z}", norm.z)
+ .replace("[a-c]", String.fromCharCode(97 + Math.floor(Math.random() * 2)))
+ .replace("[1-4]", 1 + Math.floor(Math.random() * 3)),
+ true);
+ XHR.responseType = "blob";
+ XHR.addEventListener("load", function () {
+ if (XHR.status === 200) {
+ var blob = XHR.response;
+ aCallback(blob, aStyle, aCoords);
+ gTileService.setDBCache(dbkey, {image: blob, timestamp: Date.now()});
+ }
+ }, false);
+ XHR.send();
+ });
+ },
+
+ getDBCache: function(aKey, aCallback) {
+ if (!mainDB)
+ return;
+ var transaction = mainDB.transaction([this.objStore]);
+ var request = transaction.objectStore(this.objStore).get(aKey);
+ request.onsuccess = function(event) {
+ aCallback(request.result, event);
+ };
+ request.onerror = function(event) {
+ // Errors can be handled here.
+ aCallback(undefined, event);
+ };
+ },
+
+ setDBCache: function(aKey, aValue, aCallback) {
+ if (!mainDB)
+ return;
+ var success = false;
+ var transaction = mainDB.transaction([this.objStore], "readwrite");
+ var objStore = transaction.objectStore(this.objStore);
+ var request = objStore.put(aValue, aKey);
+ request.onsuccess = function(event) {
+ success = true;
+ if (aCallback)
+ aCallback(success, event);
+ };
+ request.onerror = function(event) {
+ // Errors can be handled here.
+ if (aCallback)
+ aCallback(success, event);
+ };
+ },
+
+ unsetDBCache: function(aKey, aCallback) {
+ if (!mainDB)
+ return;
+ var success = false;
+ var transaction = mainDB.transaction([this.objStore], "readwrite");
+ var request = transaction.objectStore(this.objStore).delete(aKey);
+ request.onsuccess = function(event) {
+ success = true;
+ if (aCallback)
+ aCallback(success, event);
+ };
+ request.onerror = function(event) {
+ // Errors can be handled here.
+ if (aCallback)
+ aCallback(success, event);
+ }
+ },
+
+ clearDB: function(aCallback) {
+ if (!mainDB)
+ return;
+ var success = false;
+ var transaction = mainDB.transaction([this.objStore], "readwrite");
+ var request = transaction.objectStore(this.objStore).clear();
+ request.onsuccess = function(event) {
+ success = true;
+ if (aCallback)
+ aCallback(success, event);
+ };
+ request.onerror = function(event) {
+ // Errors can be handled here.
+ if (aCallback)
+ aCallback(success, event);
+ }
+ }
+};