[Filesystem] Listeners, getStorage, listStorages JS part
authorPawel Kaczmarek <p.kaczmarek3@samsung.com>
Fri, 20 Feb 2015 14:09:56 +0000 (15:09 +0100)
committerPawel Kaczmarek <p.kaczmarek3@samsung.com>
Fri, 20 Feb 2015 14:09:56 +0000 (15:09 +0100)
Change-Id: I814d39ff82f55438e20f345120d8cac60c1a87fc
Signed-off-by: Pawel Kaczmarek <p.kaczmarek3@samsung.com>
src/filesystem/js/common.js
src/filesystem/js/file_system_manager.js

index e1ca9c7e4ca5e2d22d0cc992fb05ce02fb3ceeac..294221be4b11a1600ec6df07588c2a81708f68cf 100644 (file)
@@ -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();
index 995c7904df18fed3e87e64f51196f03efa1b2f34..f2f9f6ef76fc429981237eda301d14d10ccaa55d 100644 (file)
@@ -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;