From: Pawel Kaczmarek Date: Fri, 20 Feb 2015 14:09:56 +0000 (+0100) Subject: [Filesystem] Listeners, getStorage, listStorages JS part X-Git-Tag: submit/tizen_tv/20150603.064601~1^2~381^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=a354157037907473dccd6cd0ba9126f2671ef7a4;p=platform%2Fcore%2Fapi%2Fwebapi-plugins.git [Filesystem] Listeners, getStorage, listStorages JS part Change-Id: I814d39ff82f55438e20f345120d8cac60c1a87fc Signed-off-by: Pawel Kaczmarek --- diff --git a/src/filesystem/js/common.js b/src/filesystem/js/common.js index e1ca9c7e..294221be 100644 --- a/src/filesystem/js/common.js +++ b/src/filesystem/js/common.js @@ -51,6 +51,8 @@ CommonFS.prototype.cacheVirtualToReal = {}; CommonFS.prototype.cacheRealToVirtual = {}; +CommonFS.prototype.cacheStorages = []; + CommonFS.prototype.getFileInfo = function(aPath, aStatObj, secondIter, aMode) { var _result = {}, _pathTokens, @@ -153,10 +155,11 @@ CommonFS.prototype.toVirtualPath = function(aPath) { return aPath; }; -CommonFS.prototype.initCache = function(manager) { - if (manager._isWidgetPathFound) { +CommonFS.prototype.initCache = function() { + if (this.cacheStorages.length > 0) { return; } + var result = native_.callSync('Filesystem_getWidgetPaths', {}); if (native_.isFailure(result)) { throw native_.getErrorObject(result); @@ -189,21 +192,30 @@ CommonFS.prototype.initCache = function(manager) { } var data = native_.getResultObject(result); - for (var i in data) { - if (data[i].state === FileSystemStorageState.MOUNTED) { - for (var j in data[i].paths) { + for (var j in data[i].paths) { + if (data[i].type === FileSystemStorageType.INTERNAL) { this.cacheVirtualToReal[j] = { path: data[i].paths[j], type: data[i].type, state: data[i].state }; - this.cacheRealToVirtual[data[i].paths[j]] = j; } + this.cacheRealToVirtual[data[i].paths[j]] = j; } + this.cacheStorages.push({ + label: data[i].name, + type: data[i].type, + state: data[i].state, + storage_id: data[i].storage_id + }); } - manager._isWidgetPathFound = true; +}; +CommonFS.prototype.clearCache = function() { + this.cacheRealToVirtual = {}; + this.cacheVirtualToReal = {}; + this.cacheStorages = []; }; var commonFS_ = new CommonFS(); diff --git a/src/filesystem/js/file_system_manager.js b/src/filesystem/js/file_system_manager.js index 995c7904..f2f9f6ef 100644 --- a/src/filesystem/js/file_system_manager.js +++ b/src/filesystem/js/file_system_manager.js @@ -14,8 +14,7 @@ var PATH_MAX = 4096; function FileSystemManager() { Object.defineProperties(this, { - maxPathLength: {value: PATH_MAX, writable: false, enumerable: true}, - _isWidgetPathFound: {value: false, writable: true} + maxPathLength: {value: PATH_MAX, writable: false, enumerable: true} }); } @@ -30,7 +29,7 @@ FileSystemManager.prototype.resolve = function(location, onsuccess, onerror, mod if (!args.has.mode) { args.mode = 'rw'; } - commonFS_.initCache(this); + commonFS_.initCache(); if (args.location[0] === '/') { setTimeout(function() { @@ -88,25 +87,23 @@ FileSystemManager.prototype.getStorage = function(label, onsuccess, onerror) { {name: 'onerror', type: types_.FUNCTION, optional: true, nullable: true} ]); - commonFS_.initCache(this); + commonFS_.initCache(); - var cachedObj = commonFS_.cacheVirtualToReal[args.label]; - if (undefined === cachedObj) { - setTimeout(function() { + var storage, i; + setTimeout(function() { + for (i = 0; i < commonFS_.cacheStorages.length; i++) { + if (commonFS_.cacheStorages[i].label === args.label) { + storage = new FileSystemStorage(commonFS_.cacheStorages[i]); + } + } + + if (storage === undefined) { native_.callIfPossible(args.onerror, - new tizen.WebAPIException(tizen.WebAPIException.NOT_FOUND_ERR, - 'Storage not found.')); - }, 0); - } else { - setTimeout(function() { - var storage = new FileSystemStorage({ - label: args.label, - type: cachedObj.type, - state: cachedObj.state - }); + new tizen.WebAPIException(tizen.WebAPIException.NOT_FOUND_ERR, 'Storage not found.')); + } else { native_.callIfPossible(args.onsuccess, storage); - }, 0); - } + } + }, 0); }; FileSystemManager.prototype.listStorages = function(onsuccess, onerror) { @@ -115,21 +112,16 @@ FileSystemManager.prototype.listStorages = function(onsuccess, onerror) { {name: 'onerror', type: types_.FUNCTION, optional: true, nullable: true} ]); - commonFS_.initCache(this); - - var _storages = []; + commonFS_.initCache(); - for (var _storage in commonFS_.cacheVirtualToReal) { - var storageObj = commonFS_.cacheVirtualToReal[_storage]; - _storages.push(new FileSystemStorage({ - label: _storage, - type: storageObj.type ? storageObj.type : FileSystemStorageType.INTERNAL, - state: storageObj.state ? storageObj.state : FileSystemStorageState.MOUNTED - })); - } - - setTimeout(function() {args.onsuccess(_storages);}, 0); + var storages = [], i; + setTimeout(function() { + for (i = 0; i < commonFS_.cacheStorages.length; i++) { + storages.push(new FileSystemStorage(commonFS_.cacheStorages[i])); + } + native_.callIfPossible(args.onsuccess, storages); + }, 0); }; var callbackId = 0; @@ -140,11 +132,9 @@ function nextCallbackId() { } function _StorageStateChangeListener(result) { - var storage = new FileSystemStorage(native_.getResultObject(result)); + var storage = new FileSystemStorage(result); for (var id in callbacks) { - if (callbacks.hasOwnProperty(id)) { - native_.callIfPossible(callbacks[id].onsuccess, storage); - } + native_.callIfPossible(callbacks[id], storage); } } @@ -154,6 +144,8 @@ FileSystemManager.prototype.addStorageStateChangeListener = function(onsuccess, {name: 'onerror', type: types_.FUNCTION, optional: true, nullable: true} ]); + commonFS_.initCache(); + var register = false; if (type_.isEmptyObject(callbacks)) { register = true; @@ -164,7 +156,6 @@ FileSystemManager.prototype.addStorageStateChangeListener = function(onsuccess, if (register) { native_.addListener('StorageStateChangeListener', _StorageStateChangeListener); - var result = native_.callSync('FileSystemManager_addStorageStateChangeListener', {}); if (native_.isFailure(result)) { @@ -190,8 +181,17 @@ FileSystemManager.prototype.removeStorageStateChangeListener = function(watchId) delete callbacks[id]; if (type_.isEmptyObject(callbacks)) { - native_.callSync('FileSystemManager_removeStorageStateChangeListener', id); + native_.callSync('FileSystemManager_removeStorageStateChangeListener', {}); } }; -exports = new FileSystemManager(); +var filesystem = new FileSystemManager(); + +function onStorageStateChanged() { + commonFS_.clearCache(); + commonFS_.initCache(); +} + +filesystem.addStorageStateChangeListener(onStorageStateChanged); + +exports = filesystem;