* @author smus@google.com (Boris Smus)
*/
-(function(gal) {
+(function (gal) {
var ROOT_DIR;
var DEFAULT_QUOTA = 1024 * 1024 * 100;
function onError(error) {
- var message;
+ var message = 'unknown';
switch (error.code) {
case FileError.NOT_FOUND_ERR:
message = 'not found';
message = 'path exists';
break;
}
- console.error('Filesystem error:', error.code + ' :: ' + message);
+ fb('Filesystem error:', error.code + ' :: ' + message);
}
- ;
-
/**
* @return DirectoryEntry to the directory that was just created.
*/
function createDir_(root, folders, callback) {
+ fb('createDir_ :: ' + root.toURL() + " ;; " + folders.join('/'));
// Throw out './' or '/' and move on to prevent something like '/foo/.//bar'.
if (folders.length && (folders[0] === '.' || folders[0] === '')) {
folders = folders.slice(1);
}
if (!folders.length) {
- root.setMetadata(function() {
- }, function() {
+ root.setMetadata(function () {
+ }, function () {
}, {'com.apple.MobileBackup': 1});
callback(root);
}
var f = folders.shift();
- root.getDirectory(f, {create: true}, function(dirEntry) {
+ var timeout = setTimeout(function () {
+ window.location.reload(true);
+ }, 1000);
+
+ root.getDirectory(f, {create: true}, function (dirEntry) {
+ fb('dirCreated !! ' + folders.join('/') + ' :: ' + folders.length);
+ clearTimeout(timeout);
if (folders.length > 0) {
createDir_(dirEntry, folders, callback);
} else {
* initialized.
* @param {string} opt_quota The quota (in bytes) to request (optional).
*/
- GALFS.prototype.init = function(fsprefix, callback, opt_quota) {
+ GALFS.prototype.init = function (fsprefix, callback, opt_quota) {
+ fb('gal fs init');
if (fsprefix == undefined) {
fsprefix = '';
}
var that = this;
// Callback when the filesystem has been initialized
- var onInitFs = function(fs) {
+ var onInitFs = function (fs) {
+ fb('fs inited');
that.fs = fs;
that.regenerate(callback);
};
- // Callback when the filesystem API has granted quota
- var quotaCallback = function(grantedBytes) {
- var persistent = LocalFileSystem.PERSISTENT;
- // Save grantedBytes in the adapter
- that.grantedBytes = grantedBytes;
- // Once quota is grantedBytes, initialize a filesystem
- requestFileSystem(persistent, 0, onInitFs, onError);
- };
-
- if (storageInfo != undefined) {
- // Get quota
- storageInfo.requestQuota(window.PERSISTENT, quota,
- quotaCallback, onError);
- } else {
- quotaCallback(0);
- }
-
+ fb('request file system');
+ requestFileSystem(LocalFileSystem.PERSISTENT, 0, onInitFs, onError);
+ var interval = setInterval(function () {
+ if (that.fs) {
+ clearInterval(interval);
+ } else {
+ fb('request file system again');
+ requestFileSystem(LocalFileSystem.PERSISTENT, 0, onInitFs, onError);
+ }
+ }, 500);
};
/**
* @param {function} callback The function to call when the asset was saved.
* @param {function} failCallback The function to call when an error occurred.
*/
- GALFS.prototype.saveAsset = function(key, url, callback, failCallback) {
+ GALFS.prototype.saveAsset = function (key, url, callback, failCallback) {
// BlobBuilder shim
var BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder;
var root = this.root;
var lookupTable = this.lookupTable;
- createDir_(root, dirname_(key).split('/'), function(dir) {
+ createDir_(root, dirname_(key).split('/'), function (dir) {
var file = basename_(key);
- dir.getFile(file, {create: true}, function(fileEntry) {
+ dir.getFile(file, {create: true}, function (fileEntry) {
var fileTransfer = new FileTransfer();
var u = fileEntry.toURL();
if (PLATFORM == 'ios') {
- u = 'cdvfile://localhost/persistent/' + fileEntry.fullPath;
+ u = cordova.file.dataDirectory + fileEntry.fullPath;
}
- fileTransfer.download(url, u, function(entry) {
+ fb('download to ' + u)
+
+ fileTransfer.download(url, u, function (entry) {
var e = entry.toURL();
if (PLATFORM == 'ios') {
- e = 'cdvfile://localhost/persistent/' + entry.fullPath;
+ e = cordova.file.dataDirectory + entry.fullPath;
}
lookupTable[key] = e;
- entry.setMetadata(function(entry) {
+ entry.setMetadata(function (entry) {
}, failCallback, {'com.apple.MobileBackup': 1});
callback();
}, failCallback);
* @param {string} key The key path to the asset.
* @return {string} URL to the filesystem.
*/
- GALFS.prototype.getAssetUrl = function(key) {
+ GALFS.prototype.getAssetUrl = function (key) {
return this.lookupTable[key];
};
* @param {function} failCallback The callback to call if the file
* doesn't exist.
*/
- GALFS.prototype.checkAsset = function(key, callback, failCallback) {
+ GALFS.prototype.checkAsset = function (key, callback, failCallback) {
var lookupTable = this.lookupTable;
- this.root.getFile(key, {}, function(fileEntry) {
+ this.root.getFile(key, {}, function (fileEntry) {
// Save the file in the lookup table.
lookupTable[key] = fileEntry.toURL();
callback();
/**
* Clears everything out of the root directory. Mostly for unit testing.
*/
- GALFS.prototype.clear = function(cb) {
+ GALFS.prototype.clear = function (cb) {
var $this = this;
// Remove the root directory
- this.root.removeRecursively(function() {
+ this.root.removeRecursively(function () {
$this.regenerate(cb);
}, onError);
};
- GALFS.prototype.regenerate = function(callback) {
+ GALFS.prototype.regenerate = function (callback) {
+ fb('gal fs regenerate');
var that = this;
// And then recreate it
var fs = this.fs;
- createDir_(fs.root, ROOT_DIR.split('/'), function(res) {
+ createDir_(fs.root, ROOT_DIR.split('/'), function (res) {
+ fb('root dir created :: ' + res.toURL());
// Create a directory for the root of the GAL
- fs.root.getDirectory(ROOT_DIR, {create: true}, function(dirEntry) {
- that.root = dirEntry;
- callback();
- }, onError);
+ that.root = res;
+ callback();
});
}
* @fileoverview Asset loader library.
* @author smus@google.com (Boris Smus)
*/
-(function(exports) {
+(function (exports) {
/**
* @constructor
* @param {Object} manifest URL to manifest file.
*/
- var GAL = function(manifest, fsprefix) {
+ var GAL = function (manifest, fsprefix) {
if (fsprefix == undefined) {
fsprefix = '';
}
* @param {Function} callback called when the library finishes loading the
* manifest.
*/
- GAL.prototype.init = function(callback) {
+ GAL.prototype.init = function (callback) {
+ fb('gal init');
var that = this;
+ fb(this._manifest);
finishInit_.call(that, this._manifest, callback)
};
* Downloads assets contained in the named bundle.
* @param {string} bundleName name of single bundle to download.
*/
- GAL.prototype.download = function(bundleName, callback) {
+ GAL.prototype.download = function (bundleName, callback) {
var bundle = this.bundles[bundleName];
if (callback == undefined) {
- callback = function() {
+ callback = function () {
};
}
// If offline, check for downloaded resources, and if they exist, callback.
- this.check(bundleName, function(result) {
+ this.check(bundleName, function (result) {
if (result.success) {
// Already downloaded. Success!
fireCallback_(that.loaded, bundleName, {
// Get the full url to the asset.
var url = encodeURI(that.manifest.assetRoot + key);
- var cb = function() {
+ var cb = function () {
fireCallback_(that.progress, bundleName, {
current: index + 1,
total: bundle.length
// Fetch full url and store it locally.
- that.adapter.saveAsset(key, url, cb, function() {
+ that.adapter.saveAsset(key, url, cb, function () {
});
* monitor all bundles).
* @param {function} callback Called after bundle was downloaded.
*/
- GAL.prototype.onLoaded = function(opt_bundleName, callback) {
+ GAL.prototype.onLoaded = function (opt_bundleName, callback) {
addCallback_(this.loaded, opt_bundleName, callback);
};
* monitor all bundles).
* @param {function} callback Called after bundle download progressed.
*/
- GAL.prototype.onProgress = function(opt_bundleName, callback) {
+ GAL.prototype.onProgress = function (opt_bundleName, callback) {
addCallback_(this.progress, opt_bundleName, callback);
};
* monitor all bundles).
* @param {function} callback Called after bundle download failed.
*/
- GAL.prototype.onError = function(opt_bundleName, callback) {
+ GAL.prototype.onError = function (opt_bundleName, callback) {
addCallback_(this.error, opt_bundleName, callback);
};
- GAL.prototype.downloadAndCall = function(bundleName, callback) {
+ GAL.prototype.downloadAndCall = function (bundleName, callback) {
+ fb('download and call ' + bundleName);
var $this = this;
- this.check(bundleName, function(info) {
+ var timeout = setTimeout(function () {
+ $this.downloadAndCall(bundleName, callback)
+ }, 1000);
+ this.check(bundleName, function (info) {
+ clearTimeout(timeout);
if (info.success) {
+ fb('already downloaded');
callback();
} else {
+ fb('i download');
$this.download(bundleName, callback);
}
});
* @param {string} bundleName Name of bundle to check.
* @param {function} callback Called with the result.
*/
- GAL.prototype.check = function(bundleName, callback) {
+ GAL.prototype.check = function (bundleName, callback) {
var bundle = this.bundles[bundleName];
if (!bundle) {
callback({success: false});
return;
}
var key = bundle[index];
- adapter.checkAsset(key, function() {
+ adapter.checkAsset(key, function () {
// Iterate to the next file.
loop(index + 1);
- }, function() {
+ }, function () {
// Failure.
callback({success: false});
});
* @return {string} url to the asset in the local filesystem.
* @throws {exception} if the asset doesn't actually exist.
*/
- GAL.prototype.get = function(assetPath) {
+ GAL.prototype.get = function (assetPath) {
return this.adapter.getAssetUrl(assetPath) || null;
};
* @param {string} assetPath relative path of asset.
* @return {number} UNIX time of the last time the asset was cached.
*/
- GAL.prototype.cacheTime = function(assetPath) {
+ GAL.prototype.cacheTime = function (assetPath) {
// TODO(smus): implement me!
return Math.random();
};
/**
* @return {Boolean} true iff the browser is online.
*/
- GAL.prototype.online = function() {
+ GAL.prototype.online = function () {
return navigator.onLine;
};
- GAL.prototype.getRootURL = function() {
+ GAL.prototype.getRootURL = function () {
return this.adapter.root.toURL();
};
- GAL.prototype.getRootNativeURL = function() {
+ GAL.prototype.getRootNativeURL = function () {
try {
- return this.adapter.root.toNativeURL();
+ if (PLATFORM === 'ios') {
+ return cordova.file.dataDirectory + "/gal/";
+ } else {
+ return this.adapter.root.toNativeURL();
+ }
} catch (err) {
return this.getRootURL();
}
};
- GAL.prototype.initAdapter = function(callback) {
+ GAL.prototype.initAdapter = function (callback) {
if (null == this.adapter) {
this.initAdapter_(callback);
} else {
callback(this.adapter);
}
};
- GAL.prototype.downloadAll = function() {
+ GAL.prototype.downloadAll = function () {
downloadAll_.call(this);
};
- GAL.prototype.clear = function(callback) {
+ GAL.prototype.clear = function (callback) {
var $this = this;
this.initAdapter(
- function(adapter) {
+ function (adapter) {
adapter.clear(callback);
}
);
};
- GAL.prototype.initAdapter_ = function(callback) {
+ GAL.prototype.initAdapter_ = function (callback) {
+ fb('gal init adapter');
var $this = this;
if (this.adapter != null) {
}
this.adapter = new GAL.adapterClass();
- this.adapter.init(this._fsprefix, function() {
+ this.adapter.init(this._fsprefix, function () {
+ fb('gal adapter inited');
callback($this.adapter);
});
};
* @param {object} manifest The manifest object.
*/
function setManifest_(manifest) {
+ fb('gal set manifest');
this.manifest = manifest;
// Set this.bundles object and this.bundleOrder array
for (var i = 0, bundle; bundle = manifest.bundles[i]; ++i) {
* @param {function} callback Called when the initialization is finished.
*/
function finishInit_(manifest, callback) {
+ fb('gal finish init');
var context = this;
- this.initAdapter_.call(context, function() {
+ this.initAdapter_.call(context, function () {
+ fb('gal set manifest');
setManifest_.call(context, manifest);
// Optionally, start auto-download.
if (manifest.autoDownload) {
var that = this;
// Start by downloading the first bundle, then download subsequent ones.
(function loop(bundleIndex) {
- console.log(bundleIndex);
+
if (bundleIndex == that.bundleOrder.length) {
// We're done downloading stuff!
return;
var bundleName = that.bundleOrder[bundleIndex];
- that.onLoaded(bundleName, function() {
+ that.onLoaded(bundleName, function () {
// Once bundle is loaded, load the next bundle.
loop(bundleIndex + 1);
});