CommonFS.prototype.cacheRealToVirtual = {};
+CommonFS.prototype.cacheStorages = [];
+
CommonFS.prototype.getFileInfo = function(aPath, aStatObj, secondIter, aMode) {
var _result = {},
_pathTokens,
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);
}
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();
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}
});
}
if (!args.has.mode) {
args.mode = 'rw';
}
- commonFS_.initCache(this);
+ commonFS_.initCache();
if (args.location[0] === '/') {
setTimeout(function() {
{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) {
{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;
}
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);
}
}
{name: 'onerror', type: types_.FUNCTION, optional: true, nullable: true}
]);
+ commonFS_.initCache();
+
var register = false;
if (type_.isEmptyObject(callbacks)) {
register = true;
if (register) {
native_.addListener('StorageStateChangeListener', _StorageStateChangeListener);
-
var result = native_.callSync('FileSystemManager_addStorageStateChangeListener', {});
if (native_.isFailure(result)) {
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;