From: Vincent Vanwaelscappel Date: Mon, 13 Oct 2014 09:42:31 +0000 (+0000) Subject: (no commit message) X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=e34cb659cf38443d8a1a3d0a1d081c7180217a17;p=fluidbook-html5.git --- diff --git a/js/libs/gal/gal.filesystem.js b/js/libs/gal/gal.filesystem.js index 572aec2b..4dc0e366 100644 --- a/js/libs/gal/gal.filesystem.js +++ b/js/libs/gal/gal.filesystem.js @@ -20,12 +20,12 @@ * @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'; @@ -64,10 +64,8 @@ message = 'path exists'; break; } - console.error('Filesystem error:', error.code + ' :: ' + message); + fb('Filesystem error:', error.code + ' :: ' + message); } - ; - /** @@ -80,14 +78,15 @@ * @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); } @@ -95,7 +94,13 @@ 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 { @@ -145,7 +150,8 @@ * 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 = ''; } @@ -162,28 +168,22 @@ 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); }; /** @@ -194,30 +194,32 @@ * @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); @@ -231,7 +233,7 @@ * @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]; }; @@ -242,9 +244,9 @@ * @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(); @@ -254,25 +256,25 @@ /** * 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(); }); } diff --git a/js/libs/gal/gal.js b/js/libs/gal/gal.js index 311ef4ab..44634e0e 100644 --- a/js/libs/gal/gal.js +++ b/js/libs/gal/gal.js @@ -19,13 +19,13 @@ * @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 = ''; } @@ -48,8 +48,10 @@ * @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) }; @@ -57,10 +59,10 @@ * 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 () { }; } @@ -73,7 +75,7 @@ // 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, { @@ -107,7 +109,7 @@ // 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 @@ -118,7 +120,7 @@ // Fetch full url and store it locally. - that.adapter.saveAsset(key, url, cb, function() { + that.adapter.saveAsset(key, url, cb, function () { }); @@ -137,7 +139,7 @@ * 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); }; @@ -147,7 +149,7 @@ * 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); }; @@ -157,16 +159,23 @@ * 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); } }); @@ -177,7 +186,7 @@ * @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}); @@ -192,11 +201,11 @@ 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}); }); @@ -211,7 +220,7 @@ * @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; }; @@ -220,7 +229,7 @@ * @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(); }; @@ -228,44 +237,49 @@ /** * @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) { @@ -274,7 +288,8 @@ } this.adapter = new GAL.adapterClass(); - this.adapter.init(this._fsprefix, function() { + this.adapter.init(this._fsprefix, function () { + fb('gal adapter inited'); callback($this.adapter); }); }; @@ -285,6 +300,7 @@ * @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) { @@ -301,8 +317,10 @@ * @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) { @@ -365,7 +383,7 @@ 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; @@ -373,7 +391,7 @@ var bundleName = that.bundleOrder[bundleIndex]; - that.onLoaded(bundleName, function() { + that.onLoaded(bundleName, function () { // Once bundle is loaded, load the next bundle. loop(bundleIndex + 1); });