Merge pull request #793 from peitschie/remove-cache

Remove cache from browser runtime
This commit is contained in:
Philip Peitsch
2014-09-11 15:09:09 +10:00
7 changed files with 42 additions and 164 deletions
@@ -75,9 +75,6 @@ OdfView::slotLoadFinished(bool ok) {
" var data = nativeio.read(path, offset, length);"
" data = runtime.byteArrayFromString(data, 'binary');"
" callback(nativeio.error()||null, data);"
"};"
"runtime.getFileSize = function (path, callback) {"
" callback(nativeio.getFileSize(path));"
"};";
frame->evaluateJavaScript(js);
}
+1 -9
View File
@@ -783,8 +783,7 @@ function Docnosis(element) {
}
function enhanceRuntime() {
var read = runtime.read,
getFileSize = runtime.getFileSize;
var read = runtime.read;
runtime.read = function (path, offset, length, callback) {
if (openedFiles.hasOwnProperty(path)) {
return openedFiles[path].read(offset, length, callback);
@@ -792,13 +791,6 @@ function Docnosis(element) {
return read(path, offset, length, callback);
}
};
runtime.getFileSize = function (path, callback) {
if (openedFiles.hasOwnProperty(path)) {
return callback(openedFiles[path].file.size);
} else {
return getFileSize(path, callback);
}
};
}
form = createForm();
+1 -9
View File
@@ -77,8 +77,7 @@ function createEditor() {
function enhanceRuntime() {
var openedFiles = {},
read = runtime.read,
getFileSize = runtime.getFileSize;
read = runtime.read;
runtime.read = function (path, offset, length, callback) {
var array;
if (openedFiles.hasOwnProperty(path)) {
@@ -88,13 +87,6 @@ function createEditor() {
return read(path, offset, length, callback);
}
};
runtime.getFileSize = function (path, callback) {
if (openedFiles.hasOwnProperty(path)) {
return callback(openedFiles[path].byteLength);
} else {
return getFileSize(path, callback);
}
};
runtime.registerFile = function (path, data) {
openedFiles[path] = data;
};
+1 -9
View File
@@ -77,8 +77,7 @@ function createReviewEditor() {
function enhanceRuntime() {
var openedFiles = {},
read = runtime.read,
getFileSize = runtime.getFileSize;
read = runtime.read;
runtime.read = function (path, offset, length, callback) {
var array;
if (openedFiles.hasOwnProperty(path)) {
@@ -88,13 +87,6 @@ function createReviewEditor() {
return read(path, offset, length, callback);
}
};
runtime.getFileSize = function (path, callback) {
if (openedFiles.hasOwnProperty(path)) {
return callback(openedFiles[path].byteLength);
} else {
return getFileSize(path, callback);
}
};
runtime.registerFile = function (path, data) {
openedFiles[path] = data;
};
+36 -10
View File
@@ -37,6 +37,8 @@ core.Zip = function Zip(url, entriesReadCallback) {
"use strict";
var /**@type{Array.<!ZipEntry>}*/
entries,
/**@type{?Uint8Array}*/
fileData,
/**@type{number}*/
filesize,
/**@type{number}*/
@@ -47,6 +49,20 @@ core.Zip = function Zip(url, entriesReadCallback) {
zip = this,
base64 = new core.Base64();
/**
* @param {!number} offset
* @param {!number} length
* @param {!function(?string,?Uint8Array):undefined} callback
* @return {undefined}
*/
function read(offset, length, callback) {
if (fileData) {
callback(null, fileData.subarray(offset, offset + length));
} else {
callback("File data not loaded", null);
}
}
/**
* @param {!Uint8Array} data
* @return {!number}
@@ -226,7 +242,7 @@ core.Zip = function Zip(url, entriesReadCallback) {
if (size + offset > filesize) {
size = filesize - offset;
}
runtime.read(url, offset, size, function (err, data) {
read(offset, size, function (err, data) {
if (err || data === null) {
callback(err, data);
} else {
@@ -349,7 +365,7 @@ core.Zip = function Zip(url, entriesReadCallback) {
// for some reason cdsOffset is not always equal to offset calculated
// from the central directory size. The latter is reliable.
runtime.read(url, cdsOffset, filesize - cdsOffset,
read(cdsOffset, filesize - cdsOffset,
function (err, data) {
if (err || data === null) {
callback(err, zip);
@@ -628,7 +644,13 @@ core.Zip = function Zip(url, entriesReadCallback) {
// make sure all data is in memory, for each entry that has data
// undefined, try to load the entry
createByteArray(function (data) {
runtime.writeFile(newurl, data, callback);
runtime.writeFile(newurl, data, function (err) {
if (!err) {
fileData = data;
filesize = fileData.length;
}
callback(err);
});
}, callback);
}
/**
@@ -661,17 +683,21 @@ core.Zip = function Zip(url, entriesReadCallback) {
entries = [];
return;
}
runtime.getFileSize(url, function (size) {
filesize = size;
if (filesize < 0) {
entriesReadCallback("File '" + url + "' cannot be read.", zip);
runtime.readFile(url, "binary", function (err, result) {
if (typeof result === "string") {
err = "file was read as a string. Should be Uint8Array.";
}
if (err || !result || result.length === 0) {
entriesReadCallback("File '" + url + "' cannot be read. Err: " + (err || "[none]"), zip);
} else {
runtime.read(url, filesize - 22, 22, function (err, data) {
fileData = /**@type{!Uint8Array}*/(result);
filesize = fileData.length;
read(filesize - 22, 22, function (err, data) {
// todo: refactor entire zip class
if (err || entriesReadCallback === null || data === null) {
if (err || data === null) {
entriesReadCallback(err, zip);
} else {
handleCentralDirectoryEnd(data, entriesReadCallback);
handleCentralDirectoryEnd(data, /**@type{!function(?string, !core.Zip):undefined}*/(entriesReadCallback));
}
});
}
+1 -122
View File
@@ -108,18 +108,6 @@ Runtime.prototype.loadXML = function (path, callback) {"use strict"; };
* @return {undefined}
*/
Runtime.prototype.writeFile = function (path, data, callback) {"use strict"; };
/**
* @param {!string} path
* @param {!function(boolean):undefined} callback
* @return {undefined}
*/
Runtime.prototype.isFile = function (path, callback) {"use strict"; };
/**
* @param {!string} path
* @param {!function(number):undefined} callback
* @return {undefined}
*/
Runtime.prototype.getFileSize = function (path, callback) {"use strict"; };
/**
* @param {!string} path
* @param {!function(?string):undefined} callback
@@ -349,9 +337,7 @@ Runtime.assert = function (condition, message) {
*/
function BrowserRuntime(logoutput) {
"use strict";
var self = this,
/**@type{!Object.<!string|!Uint8Array>}*/
cache = {};
var self = this;
/**
* Return the number of bytes a string would take up when encoded as utf-8.
@@ -608,7 +594,6 @@ function BrowserRuntime(logoutput) {
// if we just want text, it's simple
data = xhr.responseText;
}
cache[path] = data;
r = {err: null, data: data};
} else {
// report error
@@ -644,10 +629,6 @@ function BrowserRuntime(logoutput) {
* @return {undefined}
*/
function readFile(path, encoding, callback) {
if (cache.hasOwnProperty(path)) {
callback(null, cache[path]);
return;
}
var xhr = createXHR(path, encoding, true);
function handleResult() {
var r;
@@ -712,7 +693,6 @@ function BrowserRuntime(logoutput) {
* @return {undefined}
*/
function writeFile(path, data, callback) {
cache[path] = data;
var xhr = new XMLHttpRequest(),
/**@type{!string|!ArrayBuffer}*/
d;
@@ -761,7 +741,6 @@ function BrowserRuntime(logoutput) {
* @return {undefined}
*/
function deleteFile(path, callback) {
delete cache[path];
var xhr = new XMLHttpRequest();
xhr.open('DELETE', path, true);
xhr.onreadystatechange = function () {
@@ -806,60 +785,12 @@ function BrowserRuntime(logoutput) {
callback(e.message, null);
}
}
/**
* @param {!string} path
* @param {!function(boolean):undefined} callback
* @return {undefined}
*/
function isFile(path, callback) {
self.getFileSize(path, function (size) {
callback(size !== -1);
});
}
/**
* @param {!string} path
* @param {!function(number):undefined} callback
* @return {undefined}
*/
function getFileSize(path, callback) {
if (cache.hasOwnProperty(path) && typeof cache[path] !== "string") {
callback(cache[path].length);
return;
}
var xhr = new XMLHttpRequest();
xhr.open("HEAD", path, true);
xhr.onreadystatechange = function () {
if (xhr.readyState !== 4) {
return;
}
var cl = xhr.getResponseHeader("Content-Length");
if (cl) {
callback(parseInt(cl, 10));
} else {
// Due to CORS implementation bugs, some browsers will not allow access to certain headers
// even if the server says it's ok. This specific bug was observed in Cocoa's WebView on OSX10.8.
// However, even though the browser won't pull the content-length header (coz it's a security risk!)
// the content can still be fetched.
// This data will be cached, so we'll still only ever have to fetch it once
readFile(path, "binary", function (err, data) {
if (!err) {
callback(data.length);
} else {
callback(-1);
}
});
}
};
xhr.send(null);
}
this.readFile = readFile;
this.read = read;
this.readFileSync = readFileSync;
this.writeFile = writeFile;
this.deleteFile = deleteFile;
this.loadXML = loadXML;
this.isFile = isFile;
this.getFileSize = getFileSize;
this.log = log;
this.enableAlerts = true;
this.assert = Runtime.assert;
@@ -1025,17 +956,6 @@ function NodeJSRuntime() {
*/
this.toJson = Runtime.toJson;
/**
* @param {!string} path
* @param {!function(boolean):undefined} callback
* @return {undefined}
*/
function isFile(path, callback) {
path = pathmod.resolve(currentDirectory, path);
fs.stat(path, function (err, stats) {
callback(!err && stats.isFile());
});
}
/**
* Read the contents of a file. Returns the result via a callback. If the
* encoding is 'binary', the result is returned as a Uint8Array,
@@ -1155,22 +1075,6 @@ function NodeJSRuntime() {
}
return s;
};
this.isFile = isFile;
/**
* @param {!string} path
* @param {!function(number):undefined} callback
* @return {undefined}
*/
this.getFileSize = function (path, callback) {
path = pathmod.resolve(currentDirectory, path);
fs.stat(path, function (err, stats) {
if (err) {
callback(-1);
} else {
callback(stats.size);
}
});
};
/**
* @param {!string} msgOrCategory
* @param {string=} msg
@@ -1405,18 +1309,6 @@ function RhinoRuntime() {
}
return readFile(path, encoding);
}
/**
* @param {!string} path
* @param {!function(boolean):undefined} callback
* @return {undefined}
*/
function isFile(path, callback) {
if (currentDirectory) {
path = currentDirectory + "/" + path;
}
var file = new Packages.java.io.File(path);
callback(file.isFile());
}
this.loadXML = loadXML;
this.readFile = runtimeReadFile;
/**
@@ -1495,19 +1387,6 @@ function RhinoRuntime() {
}
return s;
};
this.isFile = isFile;
/**
* @param {!string} path
* @param {!function(number):undefined} callback
* @return {undefined}
*/
this.getFileSize = function (path, callback) {
if (currentDirectory) {
path = currentDirectory + "/" + path;
}
var file = new Packages.java.io.File(path);
callback(file.length());
};
/**
* @param {!string} msgOrCategory
* @param {string=} msg
+2 -2
View File
@@ -47,8 +47,8 @@ core.ZipTests = function ZipTests(runner) {
var path = "core/ZipTests.js";
// check that file exists
path = r.resourcePrefix() + path;
runtime.isFile(path, function (exists) {
t.exists = exists;
runtime.readFile(path, "binary", function (error, data) {
t.exists = !error && Boolean(data);
r.shouldBe(t, "t.exists", "true");
// check that zip file opening returns an error
t.zip = new core.Zip("core/ZipTests.js", function (err) {