[Filesystem] Implementation modified to use common virtual FS.
authorPawel Andruszkiewicz <p.andruszkie@samsung.com>
Fri, 27 Mar 2015 09:07:18 +0000 (10:07 +0100)
committerPawel Andruszkiewicz <p.andruszkie@samsung.com>
Tue, 21 Apr 2015 12:50:37 +0000 (21:50 +0900)
[Verification] TCT pass rate did not change.

Change-Id: If00593df3034dd5929128bce080ee72e38b8feaf

src/filesystem/filesystem.gyp
src/filesystem/filesystem_instance.cc
src/filesystem/filesystem_instance.h
src/filesystem/filesystem_manager.cc
src/filesystem/filesystem_manager.h
src/filesystem/filesystem_storage.cc [deleted file]
src/filesystem/filesystem_storage.h [deleted file]
src/filesystem/filesystem_utils.cc
src/filesystem/filesystem_utils.h
src/filesystem/js/common.js

index 9e89aa6043ca12f9fbb80943b66342b7cff8141b..0ab915203697c7dcde8d9e7184d22ed6c5e35444 100644 (file)
@@ -27,8 +27,6 @@
         'filesystem_manager.h',
         'filesystem_stat.cc',
         'filesystem_stat.h',
-        'filesystem_storage.cc',
-        'filesystem_storage.h',
         'filesystem_utils.cc',
         'filesystem_utils.h',
       ],
index 345cc5f45bb50d98330bb2b1c10ce7072e865f6f..3bc2de06becc663a5a43b80daf43c99608c424e0 100644 (file)
@@ -42,7 +42,7 @@ FilesystemInstance::FilesystemInstance() {
   REGISTER_SYNC("File_readSync", FileReadSync);
   REGISTER_ASYNC("File_write", FileWrite);
   REGISTER_SYNC("File_writeSync", FileWriteSync);
-  REGISTER_SYNC("Filesystem_getWidgetPaths", FilesystemGetWidgetPaths);
+  REGISTER_SYNC("Filesystem_fetchVirtualRoots", FilesystemFetchVirtualRoots);
   REGISTER_SYNC("FileSystemManager_addStorageStateChangeListener",
                 StartListening);
   REGISTER_SYNC("FileSystemManager_removeStorageStateChangeListener",
@@ -307,17 +307,17 @@ void FilesystemInstance::FileStatSync(const picojson::value& args,
   FilesystemManager::GetInstance().StatPath(location, onSuccess, onError);
 }
 
-void FilesystemInstance::FilesystemGetWidgetPaths(const picojson::value& args,
-                                                  picojson::object& out) {
+void FilesystemInstance::FilesystemFetchVirtualRoots(
+    const picojson::value& args, picojson::object& out) {
   LoggerD("enter");
 
-  auto onSuccess = [&](const std::map<std::string, std::string>& result) {
+  auto onSuccess = [&](const std::vector<common::VirtualRoot>& result) {
     LoggerD("enter");
-    picojson::object paths;
-    for (const auto& entry : result) {
-      paths[entry.first] = picojson::value(entry.second);
+    picojson::array roots;
+    for (const auto& root : result) {
+      roots.push_back(root.ToJson());
     }
-    ReportSuccess(picojson::value(paths), out);
+    ReportSuccess(picojson::value(roots), out);
   };
 
   auto onError = [&](FilesystemError e) {
@@ -325,7 +325,7 @@ void FilesystemInstance::FilesystemGetWidgetPaths(const picojson::value& args,
     PrepareError(e, out);
   };
 
-  FilesystemManager::GetInstance().GetWidgetPaths(onSuccess, onError);
+  FilesystemManager::GetInstance().GetVirtualRoots(onSuccess, onError);
 }
 
 void FilesystemInstance::FileSystemManagerFetchStorages(
@@ -333,12 +333,12 @@ void FilesystemInstance::FileSystemManagerFetchStorages(
     picojson::object& out) {
   LoggerD("enter");
 
-  auto onSuccess = [&](const std::vector<FilesystemStorage>& result) {
+  auto onSuccess = [&](const std::vector<common::VirtualStorage>& result) {
     LoggerD("enter");
     picojson::array storages;
     storages.reserve(result.size());
-    for (const FilesystemStorage& storage : result) {
-      storages.push_back(storage.toJSON());
+    for (const auto& storage : result) {
+      storages.push_back(storage.ToJson());
     }
     ReportSuccess(picojson::value(storages), out);
   };
@@ -364,14 +364,14 @@ void FilesystemInstance::StopListening(
   ReportSuccess(out);
 }
 
-void FilesystemInstance::onFilesystemStateChangeSuccessCallback(const std::string& label, const std::string& state, const std::string& type) {
+void FilesystemInstance::onFilesystemStateChangeSuccessCallback(const common::VirtualStorage& storage) {
   LoggerD("entered");
 
   picojson::value event = picojson::value(picojson::object());
   picojson::object& obj = event.get<picojson::object>();
-  obj["label"] = picojson::value(label);
-  obj["type"] = picojson::value(type);
-  obj["state"] = picojson::value(state);
+  obj["label"] = picojson::value(storage.name_);
+  obj["type"] = picojson::value(common::to_string(storage.type_));
+  obj["state"] = picojson::value(common::to_string(storage.state_));
   obj["listenerId"] = picojson::value("StorageStateChangeListener");
   PostMessage(event.serialize().c_str());
 }
index eac69ae826c79e39c81e42b1ac4cb8948eccc72f..80449a034150a316938b36f9b08c0c90c5a4f790 100644 (file)
@@ -27,8 +27,8 @@ class FilesystemInstance : public common::ParsedInstance,
   void FileReadSync(const picojson::value& args, picojson::object& out);
   void FileWrite(const picojson::value& args, picojson::object& out);
   void FileWriteSync(const picojson::value& args, picojson::object& out);
-  void FilesystemGetWidgetPaths(const picojson::value& args,
-                                picojson::object& out);
+  void FilesystemFetchVirtualRoots(const picojson::value& args,
+                                   picojson::object& out);
   void FileSystemManagerFetchStorages(const picojson::value& args,
                                       picojson::object& out);
   void FileSystemManagerMakeDirectory(const picojson::value& args,
@@ -42,9 +42,7 @@ class FilesystemInstance : public common::ParsedInstance,
   void StopListening(const picojson::value& args, picojson::object& out);
   void CopyTo(const picojson::value& args, picojson::object& out);
   void onFilesystemStateChangeErrorCallback();
-  void onFilesystemStateChangeSuccessCallback(const std::string& label,
-                                              const std::string& state,
-                                              const std::string& type);
+  void onFilesystemStateChangeSuccessCallback(const common::VirtualStorage& storage);
   void PrepareError(const FilesystemError& error, picojson::object& out);
 };
 
index 34f4f4070772552af747e4baa06a4049059dadf7..9e011f5a4d31b8e974e4da679d75ebf807ac988d 100644 (file)
@@ -36,10 +36,7 @@ void storage_cb(int storage_id, storage_state_e state, void* user_data) {
         static_cast<FilesystemStateChangeListener*>(user_data);
     storage_type_e type;
     storage_get_type(storage_id, &type);
-    listener->onFilesystemStateChangeSuccessCallback(
-        std::to_string(type) + std::to_string(storage_id),
-        std::to_string(state),
-        std::to_string(type));
+    listener->onFilesystemStateChangeSuccessCallback(common::VirtualStorage(storage_id, type, state));
   }
 }
 
@@ -165,23 +162,6 @@ FilesystemError perform_deep_copy(const std::string& originPath,
   return FilesystemError::None;
 }
 
-bool fetch_storages_cb(int storage_id,
-                       storage_type_e type,
-                       storage_state_e state,
-                       const char* path,
-                       void* user_data) {
-  LoggerD("enter");
-  if (!user_data) {
-    LoggerE("Invalid user_data pointer!");
-    return false;
-  }
-
-  std::vector<FilesystemStorage>* result =
-      static_cast<std::vector<FilesystemStorage>*>(user_data);
-  result->push_back(FilesystemStorage(storage_id, type, state, path));
-  return true;
-}
-
 FilesystemError make_directory_worker(const std::string& path) {
   LoggerD("enter: %s", path.c_str());
   auto fsstat = FilesystemStat::getStat(path);
@@ -213,16 +193,14 @@ FilesystemError make_directory_worker(const std::string& path) {
 }  // namespace
 
 void FilesystemManager::FetchStorages(
-    const std::function<void(const std::vector<FilesystemStorage>&)>&
-        success_cb,
+    const std::function<void(const std::vector<common::VirtualStorage>&)>& success_cb,
     const std::function<void(FilesystemError)>& error_cb) {
-  std::vector<FilesystemStorage> result;
-  if (STORAGE_ERROR_NONE !=
-      storage_foreach_device_supported(fetch_storages_cb, &result))
-    error_cb(FilesystemError::Other);
+  auto result = common::VirtualFs::GetInstance().GetStorages();
+
   for (auto storage : result) {
-    ids_.insert(storage.storage_id);
+    ids_.insert(storage.id_);
   }
+
   success_cb(result);
 }
 
@@ -255,67 +233,10 @@ void FilesystemManager::StatPath(
   success_cb(statData);
 }
 
-static std::string getAppRoot() {
-  std::string appId = common::GetCurrentExtension()->GetRuntimeVariable("app_id", 64);
-  app_info_h app_info;
-  int err = app_info_create(appId.c_str(), &app_info);
-  if (err != APP_MANAGER_ERROR_NONE) {
-    LoggerE("Can't create app info handle from appId (%s)", appId.c_str());
-    return "";
-  }
-  SCOPE_EXIT {
-    app_info_destroy(app_info);
-  };
-  char* package = NULL;
-  err = app_info_get_package(app_info, &package);
-  if (err != APP_MANAGER_ERROR_NONE) {
-    LoggerE("Can't get package name from app info (%s)",
-            get_error_message(err));
-    return "";
-  }
-  SCOPE_EXIT {
-    if (package != NULL)
-      free(package);
-  };
-
-  package_info_h pkg_info;
-  err = package_info_create(package, &pkg_info);
-  if (err != PACKAGE_MANAGER_ERROR_NONE) {
-    LoggerE("Can't create package info handle from pkg (%s)",
-            get_error_message(err));
-    return "";
-  }
-  SCOPE_EXIT {
-    package_info_destroy(pkg_info);
-  };
-  char* root = NULL;
-  package_info_get_root_path(pkg_info, &root);
-  if (err != PACKAGE_MANAGER_ERROR_NONE) {
-    LoggerE("Can't get root path from package info (%s)",
-            get_error_message(err));
-    return "";
-  }
-  std::string app_root_dir(root);
-  free(root);
-
-  return app_root_dir;
-}
-
-void FilesystemManager::GetWidgetPaths(
-    const std::function<void(const std::map<std::string, std::string>&)>&
-        success_cb,
+void FilesystemManager::GetVirtualRoots(
+    const std::function<void(const std::vector<common::VirtualRoot>&)>& success_cb,
     const std::function<void(FilesystemError)>& error_cb) {
-  std::map<std::string, std::string> result;
-  const std::string app_root = getAppRoot();
-  if (app_root.empty()) {
-    error_cb(FilesystemError::Other);
-    return;
-  }
-
-  result["wgt-package"] = app_root + "/res/wgt";
-  result["wgt-private"] = app_root + "/data";
-  result["wgt-private-tmp"] = app_root + "/tmp";
-  success_cb(result);
+  success_cb(common::VirtualFs::GetInstance().GetVirtualRoots());
 }
 
 void FilesystemManager::CreateFile(
index c94618631380afdbcd84d011a8a1fb7ed29740d2..35fea04043007ee0c2cb7aafb65ede930a025df3 100644 (file)
@@ -10,8 +10,9 @@
 #include <vector>
 #include <set>
 
+#include "common/virtual_fs.h"
+
 #include "filesystem_stat.h"
-#include "filesystem_storage.h"
 #include "filesystem_utils.h"
 
 namespace extension {
@@ -19,9 +20,9 @@ namespace filesystem {
 
 class FilesystemStateChangeListener {
  public:
+  virtual ~FilesystemStateChangeListener() {}
   virtual void onFilesystemStateChangeSuccessCallback(
-      const std::string& label, const std::string& state,
-      const std::string& type) = 0;
+      const common::VirtualStorage& storage) = 0;
   virtual void onFilesystemStateChangeErrorCallback() = 0;
 };
 
@@ -46,13 +47,12 @@ class FilesystemManager {
                 const std::function<void(const FilesystemStat&)>& success_cb,
                 const std::function<void(FilesystemError)>& error_cb);
 
-  void FetchStorages(const std::function<void(
-                         const std::vector<FilesystemStorage>&)>& success_cb,
-                     const std::function<void(FilesystemError)>& error_cb);
+  void FetchStorages(
+      const std::function<void(const std::vector<common::VirtualStorage>&)>& success_cb,
+      const std::function<void(FilesystemError)>& error_cb);
 
-  void GetWidgetPaths(
-      const std::function<void(const std::map<std::string, std::string>&)>&
-          success_cb,
+  void GetVirtualRoots(
+      const std::function<void(const std::vector<common::VirtualRoot>&)>& success_cb,
       const std::function<void(FilesystemError)>& error_cb);
 
   void CreateFile(const std::string& path,
diff --git a/src/filesystem/filesystem_storage.cc b/src/filesystem/filesystem_storage.cc
deleted file mode 100644 (file)
index f0e007d..0000000
+++ /dev/null
@@ -1,85 +0,0 @@
-// Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "filesystem_storage.h"
-#include "filesystem_utils.h"
-
-namespace extension {
-namespace filesystem {
-
-namespace {
-
-StoragePaths fetch_paths(int id) {
-  StoragePaths paths;
-  paths.images =
-      FilesystemUtils::get_storage_dir_path(id, STORAGE_DIRECTORY_IMAGES);
-  paths.sounds =
-      FilesystemUtils::get_storage_dir_path(id, STORAGE_DIRECTORY_SOUNDS);
-  paths.videos =
-      FilesystemUtils::get_storage_dir_path(id, STORAGE_DIRECTORY_VIDEOS);
-  paths.camera =
-      FilesystemUtils::get_storage_dir_path(id, STORAGE_DIRECTORY_CAMERA);
-  paths.downloads =
-      FilesystemUtils::get_storage_dir_path(id, STORAGE_DIRECTORY_DOWNLOADS);
-  paths.music =
-      FilesystemUtils::get_storage_dir_path(id, STORAGE_DIRECTORY_MUSIC);
-  paths.documents =
-      FilesystemUtils::get_storage_dir_path(id, STORAGE_DIRECTORY_DOCUMENTS);
-  paths.others =
-      FilesystemUtils::get_storage_dir_path(id, STORAGE_DIRECTORY_OTHERS);
-  paths.ringtones = FilesystemUtils::get_storage_dir_path(
-      id, STORAGE_DIRECTORY_SYSTEM_RINGTONES);
-  return paths;
-}
-
-picojson::value paths_to_json(const StoragePaths& storagePaths) {
-  picojson::object result;
-  if (!storagePaths.images.empty())
-    result["images"] = picojson::value(storagePaths.images);
-  if (!storagePaths.sounds.empty())
-    result["sounds"] = picojson::value(storagePaths.sounds);
-  if (!storagePaths.videos.empty())
-    result["videos"] = picojson::value(storagePaths.videos);
-  if (!storagePaths.camera.empty())
-    result["camera"] = picojson::value(storagePaths.camera);
-  if (!storagePaths.downloads.empty())
-    result["downloads"] = picojson::value(storagePaths.downloads);
-  if (!storagePaths.music.empty())
-    result["music"] = picojson::value(storagePaths.music);
-  if (!storagePaths.documents.empty())
-    result["documents"] = picojson::value(storagePaths.documents);
-  if (!storagePaths.others.empty())
-    result["others"] = picojson::value(storagePaths.others);
-  if (!storagePaths.ringtones.empty())
-    result["ringtones"] = picojson::value(storagePaths.ringtones);
-
-  return picojson::value(result);
-}
-}
-
-FilesystemStorage::FilesystemStorage(int storage_id_,
-                                     storage_type_e type_,
-                                     storage_state_e state_,
-                                     const char* path_)
-    : storage_id(storage_id_),
-      type(std::to_string(type_)),
-      state(std::to_string(state_)),
-      path(std::string(path_)),
-      name(std::to_string(type_) + std::to_string(storage_id_)),
-      paths(fetch_paths(storage_id)) {}
-
-picojson::value FilesystemStorage::toJSON() const {
-  picojson::value retval = picojson::value(picojson::object());
-  picojson::object& obj = retval.get<picojson::object>();
-
-  obj["storage_id"] = picojson::value(static_cast<double>(storage_id));
-  obj["type"] = picojson::value(type);
-  obj["state"] = picojson::value(state);
-  obj["path"] = picojson::value(path);
-  obj["name"] = picojson::value(name);
-  obj["paths"] = paths_to_json(paths);
-  return retval;
-}
-}
-}
diff --git a/src/filesystem/filesystem_storage.h b/src/filesystem/filesystem_storage.h
deleted file mode 100644 (file)
index 9ea5e87..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-// Copyright 2015 Samsung Electronics Co, Ltd. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef FILESYSTEM_FILESYSTEM_STORAGE_H
-#define FILESYSTEM_FILESYSTEM_STORAGE_H
-
-#include <string>
-#include <common/picojson.h>
-#include <storage-expand.h>
-
-namespace extension {
-namespace filesystem {
-
-struct StoragePaths {
-  std::string images;
-  std::string sounds;
-  std::string videos;
-  std::string camera;
-  std::string downloads;
-  std::string music;
-  std::string documents;
-  std::string others;
-  std::string ringtones;
-};
-
-class FilesystemStorage {
- public:
-  FilesystemStorage(int storage_id,
-                    storage_type_e type,
-                    storage_state_e,
-                    const char* path);
-
-  int storage_id;
-  std::string type;
-  std::string state;
-  std::string path;
-  std::string name;
-  StoragePaths paths;
-
-  picojson::value toJSON() const;
-};
-}  // namespace filesystem
-}  // namespace extension
-
-#endif  // FILESYSTEM_FILESYSTEM_STORAGE_H
index 32dfefeba1d553090b717bc7d4d84bc19d8fafd3..59bae29ff222c71fa9139f8c51dfc38ad1609f40 100644 (file)
@@ -7,37 +7,6 @@
 #include <libgen.h>
 #include "common/logger.h"
 
-namespace std {
-
-std::string to_string(storage_type_e type) {
-  LoggerD("enter");
-  switch (type) {
-    case STORAGE_TYPE_INTERNAL:
-      return "INTERNAL";
-    case STORAGE_TYPE_EXTERNAL:
-      return "EXTERNAL";
-    default:
-      return "";
-  }
-}
-
-std::string to_string(storage_state_e state) {
-  LoggerD("enter");
-  switch (state) {
-    case STORAGE_STATE_UNMOUNTABLE:
-      return "UNMOUNTABLE";
-    case STORAGE_STATE_REMOVED:
-      return "REMOVED";
-    case STORAGE_STATE_MOUNTED:
-      return "MOUNTED";
-    case STORAGE_STATE_MOUNTED_READ_ONLY:
-      return "MOUNTED";
-    default:
-      return "";
-  }
-}
-}
-
 namespace FilesystemUtils {
 std::string get_storage_dir_path(int id, storage_directory_e typeToCheck) {
   int result = STORAGE_ERROR_NONE;
index d75e115cd2b95d7aeb51ac80f676d5422947ba31..0375087126f9d8a5f12f83e0896be4cdb98d6f0a 100644 (file)
@@ -24,12 +24,6 @@ enum class FilesystemError {
 }  // namespace filesystem
 }  // namespace extension
 
-namespace std {
-
-std::string to_string(storage_type_e);
-std::string to_string(storage_state_e);
-}
-
 namespace FilesystemUtils {
 
 /**
index 13770332a5ea7d80fea7cd9306511a535983d052..1daead221411fce4a2ee185cc706c6e649ad70ba 100644 (file)
@@ -45,11 +45,10 @@ function CommonFS() {
       enumerable: true
     }
   });
-}
-
-CommonFS.prototype.cacheVirtualToReal = {};
 
-CommonFS.prototype.cacheStorages = [];
+  this.cacheVirtualToReal = {};
+  this.cacheStorages = [];
+}
 
 CommonFS.prototype.getFileInfo = function(aStatObj, secondIter, aMode) {
   var _result = {},
@@ -94,7 +93,7 @@ CommonFS.prototype.getFileInfo = function(aStatObj, secondIter, aMode) {
 
 CommonFS.prototype.isLocationAllowed = function(aPath) {
   if (!aPath) {
-      return false;
+    return false;
   }
   if (aPath.indexOf(this.cacheVirtualToReal.ringtones.path) === 0) {
     return false;
@@ -157,7 +156,7 @@ CommonFS.prototype.toVirtualPath = function(aPath) {
   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)
+      return aPath.replace(real_root_path, virtual_root);
     }
   }
 
@@ -169,63 +168,32 @@ CommonFS.prototype.initCache = function() {
     return;
   }
 
-  var result = native_.callSync('Filesystem_getWidgetPaths', {});
+  var result = native_.callSync('Filesystem_fetchVirtualRoots', {});
   if (native_.isFailure(result)) {
     throw native_.getErrorObject(result);
   }
-  var widgetsPaths = native_.getResultObject(result);
-
-  this.cacheVirtualToReal['wgt-package'] = {
-    path: widgetsPaths['wgt-package'],
-    type: FileSystemStorageType.INTERNAL,
-    state: FileSystemStorageState.MOUNTED
-  };
-  this.cacheVirtualToReal['wgt-private'] = {
-    path: widgetsPaths['wgt-private'],
-    type: FileSystemStorageType.INTERNAL,
-    state: FileSystemStorageState.MOUNTED
-  };
-  this.cacheVirtualToReal['wgt-private-tmp'] = {
-    path: widgetsPaths['wgt-private-tmp'],
-    type: FileSystemStorageType.INTERNAL,
-    state: FileSystemStorageState.MOUNTED
-  };
-
-  var d = this.cacheVirtualToReal;
-  for (var i in d) {
-    this.cacheStorages.push({
-      label: i,
-      type: d[i].type,
-      state: d[i].state
-    });
+  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
+    };
   }
+
   var result = native_.callSync('FileSystemManager_fetchStorages', {});
   if (native_.isFailure(result)) {
     throw native_.getErrorObject(result);
   }
 
-  var data = native_.getResultObject(result);
-  for (var i in data) {
-    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.cacheStorages.push({
-          label: j,
-          type: data[i].type,
-          state: data[i].state,
-          storage_id: data[i].storage_id
-        });
-      }
-    }
+  var storages = native_.getResultObject(result);
+  for (var i = 0; i < storages.length; ++i) {
     this.cacheStorages.push({
-      label: data[i].name,
-      type: data[i].type,
-      state: data[i].state,
-      storage_id: data[i].storage_id
+      label: storages[i].name,
+      type: storages[i].type,
+      state: storages[i].state,
+      storage_id: storages[i].storage_id
     });
   }
 };