Addressing @elemoine's review comments.

Now the application no longer needs to care about the tile origin, because the CacheWrite control modifies the url if the CORS image loading is disabled and it is from a different origin. This only requires OpenLayers.ProxyHost to be properly configured. Also local storage keys use the original url instead of the proxied url, to make the CacheRead control work without proxy settings.
No deferred exceptions are thrown any more. Instead, OpenLayers.Console is used to show an error message for security exceptions.
We now check for OpenLayers.Tile.Image, because other tile types (e.g. UTFGrid) are not supported (yet).
To make the same origin handling in the CacheWrite control easier, OpenLayers.Request now exposes the same origin logic from request.issue as a separate function, so it can also be used by other components.
This commit is contained in:
ahocevar
2012-03-11 22:15:34 +01:00
parent 3d8a9168f0
commit bd262fce41
8 changed files with 138 additions and 72 deletions

View File

@@ -33,8 +33,8 @@ OpenLayers.Control.CacheRead = OpenLayers.Class(OpenLayers.Control, {
/**
* APIProperty: layers
* {Array(<OpenLayers.Layer.HTTPRequest>)}. Optional. If provided, only
* these layers will receive tiles from the cache.
* {Array(<OpenLayers.Layer.Grid>)}. Optional. If provided, only these
* layers will receive tiles from the cache.
*/
layers: null,
@@ -61,12 +61,12 @@ OpenLayers.Control.CacheRead = OpenLayers.Class(OpenLayers.Control, {
*/
setMap: function(map) {
OpenLayers.Control.prototype.setMap.apply(this, arguments);
var i, layers = this.layers || this.map.layers;
var i, layers = this.layers || map.layers;
for (i=layers.length-1; i>=0; --i) {
this.addLayer({layer: layers[i]});
}
if (!this.layers) {
this.map.events.on({
map.events.on({
addlayer: this.addLayer,
removeLayer: this.removeLayer,
scope: this
@@ -109,9 +109,17 @@ OpenLayers.Control.CacheRead = OpenLayers.Class(OpenLayers.Control, {
* evt - {Object} Event object with a tile property.
*/
fetch: function(evt) {
if (this.active && window.localStorage) {
if (this.active && window.localStorage &&
evt.tile instanceof OpenLayers.Tile.Image) {
var tile = evt.tile,
dataURI = window.localStorage.getItem("olCache_" + tile.url);
url = tile.url;
// deal with modified tile urls when both CacheWrite and CacheRead
// are active
if (!tile.layer.crossOriginKeyword && OpenLayers.ProxyHost &&
url.indexOf(OpenLayers.ProxyHost) === 0) {
url = OpenLayers.Control.CacheWrite.urlMap[url];
}
var dataURI = window.localStorage.getItem("olCache_" + tile.url);
if (dataURI) {
tile.url = dataURI;
if (evt.type === "tileerror") {