Skip to content
This repository has been archived by the owner on Nov 15, 2017. It is now read-only.

Commit

Permalink
this fixes #193
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Feb 23, 2014
1 parent b9b862c commit b7c4b3a
Showing 1 changed file with 41 additions and 31 deletions.
72 changes: 41 additions & 31 deletions js/assets.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ File system structure:
user
blacklisted-hosts.txt
...
Useful ref.: // Ref.: http://www.html5rocks.com/en/tutorials/file/filesystem/
*/

/******************************************************************************/
Expand Down Expand Up @@ -78,6 +81,25 @@ var cachePathFromPath = function(path) {

/******************************************************************************/

var requestFileSystem = function(onSuccess, onError) {
if ( fileSystem ) {
return onSuccess(fileSystem);
}

var onRequestFileSystem = function(fs) {
fileSystem = fs;
onSuccess(fs);
};

var onRequestQuota = function(grantedBytes) {
window.webkitRequestFileSystem(window.PERSISTENT, grantedBytes, onRequestFileSystem, onError);
};

navigator.webkitPersistentStorage.requestQuota(fileSystemQuota, onRequestQuota, onError);
};

/******************************************************************************/

var readLocalFile = function(path, msg) {
var sendMessage = function(content, err) {
var details = {
Expand Down Expand Up @@ -125,14 +147,16 @@ var readLocalFile = function(path, msg) {
getTextFileFromURL(chrome.runtime.getURL(path), onLocalFileLoaded, onLocalFileError);
};

// From cache?
if ( fileSystem ) {
fileSystem.root.getFile(cachePathFromPath(path), null, onCacheEntryFound, onCacheEntryError);
return;
}
var onRequestFileSystemError = function(err) {
console.error('HTTP Switchboard> readLocalFile() / onRequestFileSystemError(): Could not get virtual file system:', err.name);
getTextFileFromURL(chrome.runtime.getURL(path), onLocalFileLoaded, onLocalFileError);
};

var onRequestFileSystem = function(fs) {
fs.root.getFile(cachePathFromPath(path), null, onCacheEntryFound, onCacheEntryError);
};

// From built-in local directory
getTextFileFromURL(chrome.runtime.getURL(path), onLocalFileLoaded, onLocalFileError);
requestFileSystem(onRequestFileSystem, onRequestFileSystemError);
};

/******************************************************************************/
Expand Down Expand Up @@ -192,9 +216,15 @@ var writeLocalFile = function(path, content, msg) {
sendMessage(err);
};

if ( fileSystem ) {
fileSystem.root.getFile(cachePathFromPath(path), { create: true }, onCacheEntryFound, onCacheEntryError);
}
var onRequestFileSystemError = function(err) {
console.error('HTTP Switchboard> writeLocalFile() / onRequestFileSystemError(): Could not get virtual file system:', err.name);
};

var onRequestFileSystem = function(fs) {
fs.root.getFile(cachePathFromPath(path), { create: true }, onCacheEntryFound, onCacheEntryError);
};

requestFileSystem(onRequestFileSystem, onRequestFileSystemError);
};

/******************************************************************************/
Expand All @@ -220,29 +250,9 @@ var updateFromRemote = function(path, msg) {
this.onload = this.onerror = null;
};

if ( fileSystem ) {
getTextFileFromURL(remoteURL, onRemoteFileLoaded, onRemoteFileError);
}
};

/******************************************************************************/

// Ref.: http://www.html5rocks.com/en/tutorials/file/filesystem/

var onError = function(err) {
console.error('HTTP Switchboard> Could not get virtual file system:', err.name);
getTextFileFromURL(remoteURL, onRemoteFileLoaded, onRemoteFileError);
};

var onRequestFileSystem = function(fs) {
fileSystem = fs;
};

var onRequestQuota = function(grantedBytes) {
window.webkitRequestFileSystem(window.PERSISTENT, grantedBytes, onRequestFileSystem, onError);
};

navigator.webkitPersistentStorage.requestQuota(fileSystemQuota, onRequestQuota, onError);

/******************************************************************************/

// Export API
Expand Down

0 comments on commit b7c4b3a

Please sign in to comment.