[Filesystem] Refactoring of commonFS.
authorPawel Andruszkiewicz <p.andruszkie@samsung.com>
Tue, 21 Apr 2015 15:06:32 +0000 (17:06 +0200)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Fri, 24 Apr 2015 12:24:43 +0000 (21:24 +0900)
Internal implementation hidden, just needed methods are exposed.

Change-Id: I6a92b01c210f98c9496a98d5709697cdf3793bdb

src/filesystem/js/common.js
src/filesystem/js/file_system_manager.js

index 1daead2..f92ec25 100644 (file)
@@ -32,175 +32,221 @@ var FileMode = {
   a: 'a'
 };
 
-function CommonFS() {
-  Object.defineProperties(this, {
-    PRIVILEGE_FILESYSTEM_READ: {
-      value: 'http://tizen.org/privilege/filesystem.read',
-      writable: false,
-      enumerable: true
-    },
-    PRIVILEGE_FILESYSTEM_WRITE: {
-      value: 'http://tizen.org/privilege/filesystem.write',
-      writable: false,
-      enumerable: true
-    }
-  });
-
-  this.cacheVirtualToReal = {};
-  this.cacheStorages = [];
-}
-
-CommonFS.prototype.getFileInfo = function(aStatObj, secondIter, aMode) {
-  var _result = {},
-      _pathTokens,
-      _fileParentPath = '',
-      i;
-  var aPath = this.toVirtualPath(aStatObj.path);
-
-  _result.readOnly = aStatObj.readOnly;
-  _result.isFile = aStatObj.isFile;
-  _result.isDirectory = aStatObj.isDirectory;
-  _result.created = new Date(aStatObj.ctime * 1000);
-  _result.modified = new Date(aStatObj.mtime * 1000);
-  _result.fullPath = aPath;
-  _result.fileSize = aStatObj.size;
-  _result.mode = aMode;
-  if (_result.isDirectory) {
-    try {
-      _result.length = aStatObj.nlink;
-    } catch (err) {
-      _result.length = 0;
-    }
-  } else {
-    _result.length = undefined;
+var commonFS_ = (function() {
+  var cacheReady = false;
+  var listenerRegistered = false;
+  var cacheVirtualToReal = {};
+  var cacheStorages = [];
+  var uriPrefix = 'file://';
+
+  function clearCache() {
+    cacheVirtualToReal = {};
+    cacheStorages = [];
+    cacheReady = false;
   }
 
-  _pathTokens = aPath.split('/');
-  if (_pathTokens.length > 1) {
-    for (i = 0; i < _pathTokens.length - 1; ++i) {
-      _fileParentPath += _pathTokens[i] + '/';
-    }
-    _result.path = _fileParentPath;
-    _result.name = _pathTokens[_pathTokens.length - 1];
-    _result.parent = (secondIter) ? null : _fileParentPath;
-  } else {
-    _result.parent = null;
-    _result.path = aPath;
-    _result.name = '';
-  }
-  return _result;
-};
+  function initCache() {
+    if (cacheReady) {
+      return;
+    }
 
-CommonFS.prototype.isLocationAllowed = function(aPath) {
-  if (!aPath) {
-    return false;
-  }
-  if (aPath.indexOf(this.cacheVirtualToReal.ringtones.path) === 0) {
-    return false;
-  }
-  if (aPath.indexOf(this.cacheVirtualToReal['wgt-package'].path) === 0) {
-    return false;
-  }
+    var result = native_.callSync('Filesystem_fetchVirtualRoots', {});
+    if (native_.isFailure(result)) {
+      throw native_.getErrorObject(result);
+    }
+    var virtualRoots = native_.getResultObject(result);
+
+    for (var i = 0; i < virtualRoots.length; ++i) {
+      cacheVirtualToReal[virtualRoots[i].name] = {
+        path: virtualRoots[i].path,
+        type: FileSystemStorageType.INTERNAL,
+        state: FileSystemStorageState.MOUNTED
+      };
+    }
 
-  return true;
-};
+    var result = native_.callSync('FileSystemManager_fetchStorages', {});
+    if (native_.isFailure(result)) {
+      throw native_.getErrorObject(result);
+    }
 
-CommonFS.prototype.f_isSubDir = function(fullPathToCheck, fullPath) {
-  return (-1 !== fullPathToCheck.indexOf(this.toRealPath(fullPath)));
-};
+    var storages = native_.getResultObject(result);
+    for (var i = 0; i < storages.length; ++i) {
+      cacheStorages.push({
+        label: storages[i].name,
+        type: storages[i].type,
+        state: storages[i].state,
+        storage_id: storages[i].storage_id
+      });
+    }
 
-CommonFS.prototype.f_isCorrectRelativePath = function(relativePath) {
-  return ((-1 === relativePath.indexOf('/')) &&
-      (-1 === relativePath.indexOf('\\')) &&
-      (-1 === relativePath.indexOf('?')) &&
-      (-1 === relativePath.indexOf('*')) &&
-      (-1 === relativePath.indexOf(':')) &&
-      (-1 === relativePath.indexOf('"')) &&
-      (-1 === relativePath.indexOf('<')) &&
-      (-1 === relativePath.indexOf('>')));
-};
+    if (!listenerRegistered) {
+      try {
+        tizen.filesystem.addStorageStateChangeListener(function() {
+          clearCache();
+        });
+        listenerRegistered = true;
+      } catch (e) {
+        console.log('Failed to register storage change listener, ' +
+                    'storage information may be corrupted: ' + e.message);
+      }
+    }
+
+    cacheReady = true;
+  }
 
-CommonFS.prototype.toRealPath = function(aPath) {
-  var _fileRealPath = '',
-      _uriPrefix = 'file://',
-      i;
-  if (aPath.indexOf(_uriPrefix) === 0) {
-    _fileRealPath = aPath.substr(_uriPrefix.length);
-  } else if (aPath[0] !== '/') {
-    //virtual path
-    var _pathTokens = aPath.split('/');
-    if (this.cacheVirtualToReal[_pathTokens[0]] && (
-        this.cacheVirtualToReal[_pathTokens[0]].state === undefined ||
-        this.cacheVirtualToReal[_pathTokens[0]].state === FileSystemStorageState.MOUNTED)) {
-      _fileRealPath = this.cacheVirtualToReal[_pathTokens[0]].path;
-      for (i = 1; i < _pathTokens.length; ++i) {
-        _fileRealPath += '/' + _pathTokens[i];
+  function toRealPath(aPath) {
+    var _fileRealPath = '';
+
+    if (aPath.indexOf(uriPrefix) === 0) {
+      _fileRealPath = aPath.substr(uriPrefix.length);
+    } else if (aPath[0] !== '/') {
+      //virtual path
+      initCache();
+
+      var _pathTokens = aPath.split('/');
+
+      if (cacheVirtualToReal[_pathTokens[0]] && (
+          cacheVirtualToReal[_pathTokens[0]].state === undefined ||
+          cacheVirtualToReal[_pathTokens[0]].state === FileSystemStorageState.MOUNTED)) {
+        _fileRealPath = cacheVirtualToReal[_pathTokens[0]].path;
+        for (var i = 1; i < _pathTokens.length; ++i) {
+          _fileRealPath += '/' + _pathTokens[i];
+        }
+      } else {
+        //If path token is not present in cache then it is invalid
+        _fileRealPath = undefined;
       }
     } else {
-      //If path token is not present in cache then it is invalid
-      _fileRealPath = undefined;
+      _fileRealPath = aPath;
     }
-  } else {
-    _fileRealPath = aPath;
+
+    return _fileRealPath;
   }
 
-  return _fileRealPath;
-};
+  function toVirtualPath(aPath) {
+    var _virtualPath = aPath;
+    if (_virtualPath.indexOf(uriPrefix) === 0) {
+      _virtualPath = _virtualPath.substr(uriPrefix.length);
+    }
 
-CommonFS.prototype.toVirtualPath = function(aPath) {
-  var _virtualPath = aPath;
-  if (_virtualPath.indexOf('file://') === 0) {
-    _virtualPath = _virtualPath.substr('file://'.length);
-  }
+    initCache();
 
-  for (var virtual_root in this.cacheVirtualToReal) {
-    var real_root_path = this.cacheVirtualToReal[virtual_root].path;
-    if (aPath.indexOf(real_root_path, 0) === 0) {
-      return aPath.replace(real_root_path, virtual_root);
+    for (var virtual_root in cacheVirtualToReal) {
+      var real_root_path = cacheVirtualToReal[virtual_root].path;
+      if (aPath.indexOf(real_root_path, 0) === 0) {
+        return aPath.replace(real_root_path, virtual_root);
+      }
     }
+
+    return aPath;
   }
 
-  return aPath;
-};
+  function getFileInfo(aStatObj, secondIter, aMode) {
+    var _result = {},
+        _pathTokens,
+        _fileParentPath = '',
+        i;
+    var aPath = toVirtualPath(aStatObj.path);
+
+    _result.readOnly = aStatObj.readOnly;
+    _result.isFile = aStatObj.isFile;
+    _result.isDirectory = aStatObj.isDirectory;
+    _result.created = new Date(aStatObj.ctime * 1000);
+    _result.modified = new Date(aStatObj.mtime * 1000);
+    _result.fullPath = aPath;
+    _result.fileSize = aStatObj.size;
+    _result.mode = aMode;
+    if (_result.isDirectory) {
+      try {
+        _result.length = aStatObj.nlink;
+      } catch (err) {
+        _result.length = 0;
+      }
+    } else {
+      _result.length = undefined;
+    }
 
-CommonFS.prototype.initCache = function() {
-  if (this.cacheStorages.length > 0) {
-    return;
+    _pathTokens = aPath.split('/');
+    if (_pathTokens.length > 1) {
+      for (i = 0; i < _pathTokens.length - 1; ++i) {
+        _fileParentPath += _pathTokens[i] + '/';
+      }
+      _result.path = _fileParentPath;
+      _result.name = _pathTokens[_pathTokens.length - 1];
+      _result.parent = (secondIter) ? null : _fileParentPath;
+    } else {
+      _result.parent = null;
+      _result.path = aPath;
+      _result.name = '';
+    }
+    return _result;
   }
 
-  var result = native_.callSync('Filesystem_fetchVirtualRoots', {});
-  if (native_.isFailure(result)) {
-    throw native_.getErrorObject(result);
+  function isLocationAllowed(aPath) {
+    if (!aPath) {
+      return false;
+    }
+    initCache();
+    if (aPath.indexOf(cacheVirtualToReal.ringtones.path) === 0) {
+      return false;
+    }
+    if (aPath.indexOf(cacheVirtualToReal['wgt-package'].path) === 0) {
+      return false;
+    }
+
+    return true;
   }
-  var virtualRoots = native_.getResultObject(result);
 
-  for (var i = 0; i < virtualRoots.length; ++i) {
-    this.cacheVirtualToReal[virtualRoots[i].name] = {
-      path: virtualRoots[i].path,
-      type: FileSystemStorageType.INTERNAL,
-      state: FileSystemStorageState.MOUNTED
+  function f_isSubDir(fullPathToCheck, fullPath) {
+    return (-1 !== fullPathToCheck.indexOf(toRealPath(fullPath)));
+  };
+
+  function f_isCorrectRelativePath(relativePath) {
+    return ((-1 === relativePath.indexOf('/')) &&
+        (-1 === relativePath.indexOf('\\')) &&
+        (-1 === relativePath.indexOf('?')) &&
+        (-1 === relativePath.indexOf('*')) &&
+        (-1 === relativePath.indexOf(':')) &&
+        (-1 === relativePath.indexOf('"')) &&
+        (-1 === relativePath.indexOf('<')) &&
+        (-1 === relativePath.indexOf('>')));
+  };
+
+  function cloneStorage(storage) {
+    return {
+      label: storage.label,
+      type: storage.type,
+      state: storage.state
     };
   }
 
-  var result = native_.callSync('FileSystemManager_fetchStorages', {});
-  if (native_.isFailure(result)) {
-    throw native_.getErrorObject(result);
+  function getStorage(label) {
+    initCache();
+    for (var i = 0; i < cacheStorages.length; ++i) {
+      if (cacheStorages[i].label === label) {
+        return cloneStorage(cacheStorages[i]);
+      }
+    }
+    return null;
   }
 
-  var storages = native_.getResultObject(result);
-  for (var i = 0; i < storages.length; ++i) {
-    this.cacheStorages.push({
-      label: storages[i].name,
-      type: storages[i].type,
-      state: storages[i].state,
-      storage_id: storages[i].storage_id
-    });
+  function getAllStorages() {
+    var ret = [];
+    initCache();
+    for (var i = 0; i < cacheStorages.length; ++i) {
+      ret.push(cloneStorage(cacheStorages[i]));
+    }
+    return ret;
   }
-};
-
-CommonFS.prototype.clearCache = function() {
-  this.cacheVirtualToReal = {};
-  this.cacheStorages = [];
-};
 
-var commonFS_ = new CommonFS();
+  return {
+    toRealPath: toRealPath,
+    toVirtualPath: toVirtualPath,
+    getFileInfo: getFileInfo,
+    isLocationAllowed: isLocationAllowed,
+    f_isSubDir: f_isSubDir,
+    f_isCorrectRelativePath: f_isCorrectRelativePath,
+    getStorage: getStorage,
+    getAllStorages: getAllStorages
+  };
+})();
index 95654b0..765e87c 100644 (file)
@@ -34,7 +34,6 @@ FileSystemManager.prototype.resolve = function(location, onsuccess, onerror, mod
   if (!args.has.mode) {
     args.mode = 'rw';
   }
-  commonFS_.initCache();
 
   if (args.location[0] === '/') {
     setTimeout(function() {
@@ -87,21 +86,14 @@ FileSystemManager.prototype.getStorage = function(label, onsuccess, onerror) {
     {name: 'onerror', type: types_.FUNCTION, optional: true, nullable: true}
   ]);
 
-  commonFS_.initCache();
-
-  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]);
-      }
-    }
+    var storage = commonFS_.getStorage(args.label);
 
-    if (storage === undefined) {
+    if (!storage) {
       native_.callIfPossible(args.onerror,
           new WebAPIException(WebAPIException.NOT_FOUND_ERR, 'Storage not found.'));
     } else {
-      native_.callIfPossible(args.onsuccess, storage);
+      native_.callIfPossible(args.onsuccess, new FileSystemStorage(storage));
     }
   }, 0);
 };
@@ -114,12 +106,11 @@ FileSystemManager.prototype.listStorages = function(onsuccess, onerror) {
     {name: 'onerror', type: types_.FUNCTION, optional: true, nullable: true}
   ]);
 
-  commonFS_.initCache();
-
-  var storages = [], i;
   setTimeout(function() {
-    for (i = 0; i < commonFS_.cacheStorages.length; i++) {
-      storages.push(new FileSystemStorage(commonFS_.cacheStorages[i]));
+    var storages = [];
+    var cache = commonFS_.getAllStorages();
+    for (var i = 0; i < cache.length; ++i) {
+      storages.push(new FileSystemStorage(cache[i]));
     }
 
     native_.callIfPossible(args.onsuccess, storages);
@@ -148,8 +139,6 @@ 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;
@@ -195,13 +184,4 @@ FileSystemManager.prototype.removeStorageStateChangeListener = function(watchId)
   }
 };
 
-var filesystem = new FileSystemManager();
-
-function onStorageStateChanged() {
-  commonFS_.clearCache();
-  commonFS_.initCache();
-}
-
-filesystem.addStorageStateChangeListener(onStorageStateChanged);
-
-exports = filesystem;
+exports = new FileSystemManager();