From cf92d72f2376b423a84cb1a77eefb92539e8343e Mon Sep 17 00:00:00 2001 From: Pawel Andruszkiewicz Date: Fri, 27 Mar 2015 10:07:18 +0100 Subject: [PATCH] [Filesystem] Implementation modified to use common virtual FS. [Verification] TCT pass rate did not change. Change-Id: If00593df3034dd5929128bce080ee72e38b8feaf --- src/filesystem/filesystem.gyp | 2 - src/filesystem/filesystem_instance.cc | 32 ++++----- src/filesystem/filesystem_instance.h | 8 +-- src/filesystem/filesystem_manager.cc | 97 +++------------------------ src/filesystem/filesystem_manager.h | 18 ++--- src/filesystem/filesystem_storage.cc | 85 ----------------------- src/filesystem/filesystem_storage.h | 46 ------------- src/filesystem/filesystem_utils.cc | 31 --------- src/filesystem/filesystem_utils.h | 6 -- src/filesystem/js/common.js | 74 ++++++-------------- 10 files changed, 58 insertions(+), 341 deletions(-) delete mode 100644 src/filesystem/filesystem_storage.cc delete mode 100644 src/filesystem/filesystem_storage.h diff --git a/src/filesystem/filesystem.gyp b/src/filesystem/filesystem.gyp index 9e89aa60..0ab91520 100644 --- a/src/filesystem/filesystem.gyp +++ b/src/filesystem/filesystem.gyp @@ -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', ], diff --git a/src/filesystem/filesystem_instance.cc b/src/filesystem/filesystem_instance.cc index 345cc5f4..3bc2de06 100644 --- a/src/filesystem/filesystem_instance.cc +++ b/src/filesystem/filesystem_instance.cc @@ -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& result) { + auto onSuccess = [&](const std::vector& 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& result) { + auto onSuccess = [&](const std::vector& 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(); - 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()); } diff --git a/src/filesystem/filesystem_instance.h b/src/filesystem/filesystem_instance.h index eac69ae8..80449a03 100644 --- a/src/filesystem/filesystem_instance.h +++ b/src/filesystem/filesystem_instance.h @@ -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); }; diff --git a/src/filesystem/filesystem_manager.cc b/src/filesystem/filesystem_manager.cc index 34f4f407..9e011f5a 100644 --- a/src/filesystem/filesystem_manager.cc +++ b/src/filesystem/filesystem_manager.cc @@ -36,10 +36,7 @@ void storage_cb(int storage_id, storage_state_e state, void* user_data) { static_cast(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* result = - static_cast*>(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&)>& - success_cb, + const std::function&)>& success_cb, const std::function& error_cb) { - std::vector 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&)>& - success_cb, +void FilesystemManager::GetVirtualRoots( + const std::function&)>& success_cb, const std::function& error_cb) { - std::map 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( diff --git a/src/filesystem/filesystem_manager.h b/src/filesystem/filesystem_manager.h index c9461863..35fea040 100644 --- a/src/filesystem/filesystem_manager.h +++ b/src/filesystem/filesystem_manager.h @@ -10,8 +10,9 @@ #include #include +#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& success_cb, const std::function& error_cb); - void FetchStorages(const std::function&)>& success_cb, - const std::function& error_cb); + void FetchStorages( + const std::function&)>& success_cb, + const std::function& error_cb); - void GetWidgetPaths( - const std::function&)>& - success_cb, + void GetVirtualRoots( + const std::function&)>& success_cb, const std::function& 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 index f0e007da..00000000 --- a/src/filesystem/filesystem_storage.cc +++ /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(); - - obj["storage_id"] = picojson::value(static_cast(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 index 9ea5e879..00000000 --- a/src/filesystem/filesystem_storage.h +++ /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 -#include -#include - -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 diff --git a/src/filesystem/filesystem_utils.cc b/src/filesystem/filesystem_utils.cc index 32dfefeb..59bae29f 100644 --- a/src/filesystem/filesystem_utils.cc +++ b/src/filesystem/filesystem_utils.cc @@ -7,37 +7,6 @@ #include #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; diff --git a/src/filesystem/filesystem_utils.h b/src/filesystem/filesystem_utils.h index d75e115c..03750871 100644 --- a/src/filesystem/filesystem_utils.h +++ b/src/filesystem/filesystem_utils.h @@ -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 { /** diff --git a/src/filesystem/js/common.js b/src/filesystem/js/common.js index 13770332..1daead22 100644 --- a/src/filesystem/js/common.js +++ b/src/filesystem/js/common.js @@ -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 }); } }; -- 2.34.1