[Archive] Updating cached storages 75/170375/2
authorSzymon Jastrzebski <s.jastrzebsk@partner.samsung.com>
Mon, 19 Feb 2018 11:19:44 +0000 (12:19 +0100)
committerSzymon Jastrzebski <s.jastrzebsk@partner.samsung.com>
Wed, 21 Feb 2018 09:21:34 +0000 (10:21 +0100)
Currently the implementation obtains all available storages once, during
launching Archive plugin. The fix is based on solution from Filesystem.

[Verification] Archive 100% pass rate.

Change-Id: If0368d89fef3ac25c95bd7d5cf031d6d635643a4
Signed-off-by: Szymon Jastrzebski <s.jastrzebsk@partner.samsung.com>
src/archive/archive_api.js

index 8b59c63c6a145fd11b6f9e869587ecb098d407aa..41a8caac44023975c6c5cbcbdedd4b41c1ea67d6 100755 (executable)
@@ -22,23 +22,40 @@ var privUtils_ = xwalk.utils;
 function CommonFS() {};
 
 CommonFS.cacheVirtualToReal = {};
+CommonFS.isCacheReady = false;
+CommonFS.listenerRegistered = false;
+
+function clearCache() {
+    CommonFS.cacheVirtualToReal = {};
+    CommonFS.isCacheReady = false;
+}
 
 function _initializeCache() {
-    try {
-        var result = native_.callSync('Archive_fetchStorages', {});
+    if (CommonFS.isCacheReady) {
+        return;
+    }
+    var result = native_.callSync('Archive_fetchStorages', {});
 
-        if (native_.isFailure(result)) {
-            throw native_.getErrorObject(result);
-        }
+    if (native_.isFailure(result)) {
+        privUtils_.log("Exception while getting widget paths was thrown: " + native_.getErrorObject(result).message);
+        return;
+    }
 
-        result = native_.getResultObject(result);
-        for (var i = 0; i < result.length; ++i) {
-            CommonFS.cacheVirtualToReal[result[i].name] = {
-                path: result[i].path
-            };
+    result = native_.getResultObject(result);
+    for (var i = 0; i < result.length; ++i) {
+        CommonFS.cacheVirtualToReal[result[i].name] = {
+            path: result[i].path
+        };
+    }
+    CommonFS.isCacheReady = true;
+    if (!CommonFS.listenerRegistered) {
+        try {
+          tizen.filesystem.addStorageStateChangeListener(clearCache);
+          CommonFS.listenerRegistered = true;
+        } catch (e) {
+          privUtils_.log('Failed to register storage change listener, '
+              + 'storage information may be corrupted: ' + e.message);
         }
-    } catch(e) {
-        privUtils_.log("Exception while getting widget paths was thrown: " + e);
     }
 }
 
@@ -50,6 +67,7 @@ CommonFS.toRealPath = function(aPath) {
         _fileRealPath = aPath.substr(_uriPrefix.length);
     } else if (aPath[0] != '/') {
         // virtual path$
+        _initializeCache();
         var _pathTokens = aPath.split('/');
         if (this.cacheVirtualToReal[_pathTokens[0]]
                 && (this.cacheVirtualToReal[_pathTokens[0]].state === undefined || this.cacheVirtualToReal[_pathTokens[0]].state === 'MOUNTED')) {
@@ -72,7 +90,7 @@ CommonFS.toRealPath = function(aPath) {
 
 CommonFS.isVirtualPath = function(aPath) {
     var root = aPath.split("/")[0];
-
+    _initializeCache();
     return this.cacheVirtualToReal[root] != undefined;
 };