diff options
Diffstat (limited to 'src/components/resumption/src/last_state_impl.cc')
-rw-r--r-- | src/components/resumption/src/last_state_impl.cc | 69 |
1 files changed, 58 insertions, 11 deletions
diff --git a/src/components/resumption/src/last_state_impl.cc b/src/components/resumption/src/last_state_impl.cc index 564266e19c..05c801d762 100644 --- a/src/components/resumption/src/last_state_impl.cc +++ b/src/components/resumption/src/last_state_impl.cc @@ -32,6 +32,7 @@ #include "resumption/last_state_impl.h" #include "utils/file_system.h" +#include "utils/jsoncpp_reader_wrapper.h" #include "utils/logger.h" namespace resumption { @@ -42,35 +43,57 @@ LastStateImpl::LastStateImpl(const std::string& app_storage_folder, const std::string& app_info_storage) : app_storage_folder_(app_storage_folder) , app_info_storage_(app_info_storage) { - LoadStateFromFileSystem(); + LoadFromFileSystem(); LOG4CXX_AUTO_TRACE(logger_); } LastStateImpl::~LastStateImpl() { LOG4CXX_AUTO_TRACE(logger_); - SaveStateToFileSystem(); + SaveToFileSystem(); } void LastStateImpl::SaveStateToFileSystem() { LOG4CXX_AUTO_TRACE(logger_); - const std::string& str = dictionary_.toStyledString(); - const std::vector<uint8_t> char_vector_pdata(str.begin(), str.end()); + std::string styled_string; + { + sync_primitives::AutoLock lock(dictionary_lock_); + styled_string = dictionary_.toStyledString(); + } + + const std::vector<uint8_t> char_vector_pdata(styled_string.begin(), + styled_string.end()); DCHECK(file_system::CreateDirectoryRecursively(app_storage_folder_)); LOG4CXX_INFO(logger_, - "LastState::SaveStateToFileSystem " << app_info_storage_ << str); + "LastState::SaveStateToFileSystem[DEPRECATED] " + << app_info_storage_ << styled_string); DCHECK(file_system::Write(app_info_storage_, char_vector_pdata)); } -Json::Value& LastStateImpl::get_dictionary() { - return dictionary_; +void LastStateImpl::SaveToFileSystem() { + LOG4CXX_AUTO_TRACE(logger_); + + std::string styled_string; + { + sync_primitives::AutoLock lock(dictionary_lock_); + styled_string = dictionary_.toStyledString(); + } + + const std::vector<uint8_t> char_vector_pdata(styled_string.begin(), + styled_string.end()); + DCHECK(file_system::CreateDirectoryRecursively(app_storage_folder_)); + LOG4CXX_INFO( + logger_, + "LastState::SaveToFileSystem " << app_info_storage_ << styled_string); + DCHECK(file_system::Write(app_info_storage_, char_vector_pdata)); } -void LastStateImpl::LoadStateFromFileSystem() { +void LastStateImpl::LoadFromFileSystem() { std::string buffer; - bool result = file_system::ReadFile(app_info_storage_, buffer); - Json::Reader m_reader; - if (result && m_reader.parse(buffer, dictionary_)) { + const bool result = file_system::ReadFile(app_info_storage_, buffer); + utils::JsonReader reader; + + if (result && reader.parse(buffer, &dictionary_)) { LOG4CXX_INFO(logger_, "Valid last state was found." << dictionary_.toStyledString()); return; @@ -78,4 +101,28 @@ void LastStateImpl::LoadStateFromFileSystem() { LOG4CXX_WARN(logger_, "No valid last state was found."); } +void LastStateImpl::RemoveFromFileSystem() { + LOG4CXX_AUTO_TRACE(logger_); + if (!file_system::DeleteFile(app_info_storage_)) { + LOG4CXX_WARN(logger_, "Failed attempt to delete " << app_info_storage_); + } +} + +Json::Value LastStateImpl::dictionary() const { + sync_primitives::AutoLock lock(dictionary_lock_); + return dictionary_; +} + +Json::Value& LastStateImpl::get_dictionary() { + sync_primitives::AutoLock lock(dictionary_lock_); + return dictionary_; +} + +void LastStateImpl::set_dictionary(const Json::Value& dictionary) { + DCHECK(dictionary.type() == Json::objectValue || + dictionary.type() == Json::nullValue); + sync_primitives::AutoLock lock(dictionary_lock_); + dictionary_ = dictionary; +} + } // namespace resumption |