From 78fdd9cd40fd66e833ae0905d8aeb07be2d99d22 Mon Sep 17 00:00:00 2001 From: Jakub Izydorczyk Date: Wed, 4 Feb 2015 16:21:07 +0100 Subject: [PATCH] [Widget-manifest-parser] Remove LocalManifestData Clean up in widget_manifest_parser.cc file, which includes removal of LocalStorage and LocalManifestData. Change-Id: Ie09349504fe014ac807253f4de87c19dc98e2522 --- src/wgt/step/step_parse.cc | 2 +- .../widget_manifest_parser.cc | 291 ++++++--------------- .../widget_manifest_parser.h | 21 +- 3 files changed, 87 insertions(+), 227 deletions(-) diff --git a/src/wgt/step/step_parse.cc b/src/wgt/step/step_parse.cc index da0a942..dc6018b 100644 --- a/src/wgt/step/step_parse.cc +++ b/src/wgt/step/step_parse.cc @@ -26,7 +26,7 @@ common_installer::Step::Status StepParse::process() { const ManifestData* data = nullptr; const char* error = nullptr; if (!ParseManifest(config_.c_str(), &data, &error)) { - LOG(ERROR) << "Parse failed. " << error; + LOG(ERROR) << "Parse failed. " << error; if (!ReleaseData(data, error)) LOG(ERROR) << " Release data failed."; return common_installer::Step::Status::ERROR; diff --git a/src/widget-manifest-parser/widget_manifest_parser.cc b/src/widget-manifest-parser/widget_manifest_parser.cc index a24903b..dc99aca 100644 --- a/src/widget-manifest-parser/widget_manifest_parser.cc +++ b/src/widget-manifest-parser/widget_manifest_parser.cc @@ -28,6 +28,7 @@ namespace bf = boost::filesystem; namespace keys = common_installer::application_widget_keys; +namespace parser = common_installer::widget_manifest_parser; namespace { @@ -47,208 +48,63 @@ const char kErrMsgNoMandatoryKey[] = const char kName[] = "name"; const char kShortName[] = "shortName"; const char kVersion[] = "version"; -const char kWidgetPrivilegeFullKey[] = "widget.privilege"; -const char kWidgetPrivilegeNameKey[] = "@name"; const char kWidgetIconFullKey[] = "widget.icon"; const char kWidgetIconSrcKey[] = "@src"; -class LocalManifestData { - public: - LocalManifestData() { - data_.package = package_.c_str(); - data_.id = id_.c_str(); - data_.name = name_.c_str(); - data_.short_name = short_name_.c_str(); - data_.version = version_.c_str(); - data_.icon = icon_.c_str(); - data_.api_version = api_version_.c_str(); - data_.privilege_count = privilege_list_.size(); - data_.privilege_list = privilege_list_.data(); - } - - void SetPackage(const std::string& value) { - package_ = value; - data_.package = package_.c_str(); - } - - void SetId(const std::string& value) { - id_ = value; - data_.id = id_.c_str(); - } - - void SetName(const std::string& value) { - name_ = value; - data_.name = name_.c_str(); - } - - void SetShortName(const std::string& value) { - short_name_ = value; - data_.short_name = short_name_.c_str(); - } - - void SetVersion(const std::string& value) { - version_ = value; - data_.version = version_.c_str(); - } - - void SetIcon(const std::string& value) { - icon_ = value; - data_.icon = icon_.c_str(); - } - - void SetApiVersion(const std::string& value) { - api_version_ = value; - data_.api_version = api_version_.c_str(); - } - - void SetPrivileges(const std::set& value) { - std::copy(value.begin(), value.end(), std::back_inserter(privileges_)); - privilege_list_.clear(); - for (const std::string& p : privileges_) - privilege_list_.push_back(p.c_str()); - data_.privilege_count = privilege_list_.size(); - data_.privilege_list = privilege_list_.data(); - } - - const ManifestData* GetManifestData() const { - return &data_; - } - - private: - std::string package_; - std::string id_; - std::string name_; - std::string short_name_; - std::string version_; - std::string icon_; - std::string api_version_; - std::vector privileges_; - std::vector privilege_list_; - ManifestData data_; - - DISALLOW_COPY_AND_ASSIGN(LocalManifestData); -}; - typedef std::string LocalError; -class LocalStorage { - public: - static LocalStorage* GetInstance() { - static LocalStorage instance; - return &instance; - } - - const ManifestData* Add(std::shared_ptr data) { - const ManifestData* result = data->GetManifestData(); - data_vector_.push_back(data); - return result; - } - - bool Remove(const ManifestData* data) { - LocalManifestDataVector::iterator it; - for (it = data_vector_.begin(); it != data_vector_.end(); ++it) - if ((*it)->GetManifestData() == data) { - data_vector_.erase(it); - return true; - } - return false; - } - - const char* Add(LocalError* error) { - const char* result = error->c_str(); - error_vector_.push_back(std::shared_ptr(error)); - return result; - } - - bool Remove(const char* error) { - ErrorVector::iterator it; - for (it = error_vector_.begin(); it != error_vector_.end(); ++it) - if ((*it)->c_str() == error) { - error_vector_.erase(it); - return true; - } - return false; - } - - private: - typedef std::vector> - LocalManifestDataVector; - typedef std::vector> ErrorVector; - - LocalManifestDataVector data_vector_; - ErrorVector error_vector_; - - LocalStorage() { } - - DISALLOW_COPY_AND_ASSIGN(LocalStorage); -}; - void SetError(const std::string& message, const char** error) { if (error) - *error = LocalStorage::GetInstance()->Add(new std::string(message)); + *error = strdup(message.c_str()); } void SetError(const std::string& message, const std::string& arg, const char** error) { if (error) - *error = LocalStorage::GetInstance()->Add(new std::string(message + arg)); + *error = strdup((message + arg).c_str()); } bool ExtractPackage( - const common_installer::widget_manifest_parser::ApplicationData& app_data, - std::string* value) { - common_installer::widget_manifest_parser::TizenApplicationInfo* info = - static_cast< - common_installer::widget_manifest_parser::TizenApplicationInfo*>( + const parser::ApplicationData& app_data, std::string* value) { + parser::TizenApplicationInfo* info = + static_cast( app_data.GetManifestData(keys::kTizenApplicationKey)); - if (!info) - return false; - *value = info->package(); - return true; + if (info) + *value = info->package(); + return info; } bool ExtractId( - const common_installer::widget_manifest_parser::ApplicationData& app_data, - std::string* value) { - common_installer::widget_manifest_parser::TizenApplicationInfo* info = - static_cast< - common_installer::widget_manifest_parser::TizenApplicationInfo*>( - app_data.GetManifestData(keys::kTizenApplicationKey)); - if (!info) - return false; - *value = info->id(); - return true; + const parser::ApplicationData& app_data, std::string* value) { + parser::TizenApplicationInfo* info = + static_cast( + app_data.GetManifestData(keys::kTizenApplicationKey)); + if (info) + *value = info->id(); + return info; } -bool ExtractName( - const common_installer::widget_manifest_parser::ApplicationData& app_data, - std::string* value) { - common_installer::widget_manifest_parser::WidgetInfo* info = - static_cast( - app_data.GetManifestData(keys::kWidgetKey)); +bool ExtractName(const parser::ApplicationData& app_data, std::string* value) { + parser::WidgetInfo* info = static_cast( + app_data.GetManifestData(keys::kWidgetKey)); if (!info) return false; - return info->GetWidgetInfo()->GetString(kName, value); } -bool ExtractShortName( - const common_installer::widget_manifest_parser::ApplicationData& app_data, - std::string* value) { - common_installer::widget_manifest_parser::WidgetInfo* info = - static_cast( - app_data.GetManifestData(keys::kWidgetKey)); +bool ExtractShortName(const parser::ApplicationData& app_data, + std::string* value) { + parser::WidgetInfo* info = static_cast( + app_data.GetManifestData(keys::kWidgetKey)); if (!info) return false; return info->GetWidgetInfo()->GetString(kShortName, value); } -bool ExtractVersion( - const common_installer::widget_manifest_parser::ApplicationData& app_data, - std::string* value) { - common_installer::widget_manifest_parser::WidgetInfo* info = - static_cast( - app_data.GetManifestData(keys::kWidgetKey)); +bool ExtractVersion(const parser::ApplicationData& app_data, + std::string* value) { + parser::WidgetInfo* info = static_cast( + app_data.GetManifestData(keys::kWidgetKey)); if (!info) return false; return info->GetWidgetInfo()->GetString(kVersion, value); @@ -270,9 +126,8 @@ bool ExtractIconSrc(const common_installer::utils::Value& dict, return true; } -bool ExtractIcons( - const common_installer::widget_manifest_parser::Manifest& manifest, - std::vector* value, const char** error) { +bool ExtractIcons(const parser::Manifest& manifest, + std::vector* value, const char** error) { common_installer::utils::Value* key_value; if (!manifest.Get(kWidgetIconFullKey, &key_value)) { value->clear(); @@ -303,12 +158,10 @@ bool ExtractIcons( return true; } -bool ExtractApiVersion( - const common_installer::widget_manifest_parser::ApplicationData& app_data, - std::string* value) { - common_installer::widget_manifest_parser::TizenApplicationInfo* info = - static_cast< - common_installer::widget_manifest_parser::TizenApplicationInfo*>( +bool ExtractApiVersion(const parser::ApplicationData& app_data, + std::string* value) { + parser::TizenApplicationInfo* info = + static_cast( app_data.GetManifestData(keys::kTizenApplicationKey)); if (!info) return false; @@ -316,15 +169,13 @@ bool ExtractApiVersion( return true; } -bool ExtractPrivileges( - const common_installer::widget_manifest_parser::ApplicationData& app_data, - std::set* value, const char** error) { - const common_installer::widget_manifest_parser::PermissionsInfo* perm_info = - static_cast( +bool ExtractPrivileges(const parser::ApplicationData& app_data, + std::set* value, const char** error) { + const parser::PermissionsInfo* perm_info = + static_cast( app_data.GetManifestData(keys::kTizenPermissionsKey)); - common_installer::widget_manifest_parser::PermissionSet permissions = - perm_info->GetAPIPermissions(); - *value = permissions; + parser::PermissionSet permissions = perm_info->GetAPIPermissions(); + *value = permissions; return true; } @@ -346,21 +197,17 @@ API_EXPORT bool ParseManifest(const char* path, std::string local_error; - std::unique_ptr manifest = - common_installer::widget_manifest_parser::LoadManifest( - path, common_installer::widget_manifest_parser::Manifest::TYPE_WIDGET, - &local_error); + std::unique_ptr manifest = parser::LoadManifest( + path, parser::Manifest::TYPE_WIDGET, &local_error); if (!manifest) { SetError(local_error, error); return false; } - std::shared_ptr - app_data = - common_installer::widget_manifest_parser::ApplicationData::Create( - bf::path(), std::string(), - common_installer::widget_manifest_parser::ApplicationData::INTERNAL, - std::move(manifest), &local_error); + std::shared_ptr app_data = + parser::ApplicationData::Create(bf::path(), std::string(), + parser::ApplicationData::INTERNAL, + std::move(manifest), &local_error); if (!app_data.get()) { SetError(local_error, error); return false; @@ -411,29 +258,41 @@ API_EXPORT bool ParseManifest(const char* path, return false; if (data) { - std::shared_ptr local_manifest_data( - new LocalManifestData); - local_manifest_data->SetPackage(package); - local_manifest_data->SetId(id); - local_manifest_data->SetName(name); - local_manifest_data->SetShortName(short_name); - local_manifest_data->SetVersion(version); - local_manifest_data->SetIcon(icons.empty() ? "" : icons.front()); - local_manifest_data->SetApiVersion(api_version); - local_manifest_data->SetPrivileges(privileges); - *data = LocalStorage::GetInstance()->Add(local_manifest_data); + ManifestData* manifest_data = new ManifestData; + manifest_data->package = strdup(package.c_str()); + manifest_data->id = strdup(id.c_str()); + manifest_data->name = strdup(name.c_str()); + manifest_data->short_name = strdup(short_name.c_str()); + manifest_data->version = strdup(version.c_str()); + manifest_data->icon = + icons.empty() ? strdup("") : strdup(icons.front().c_str()); + manifest_data->api_version = strdup(api_version.c_str()); + manifest_data->privilege_count = privileges.size(); + manifest_data->privilege_list = new char*[privileges.size()]; + char** privileges_it = manifest_data->privilege_list; + for (const std::string& p : privileges) { + *privileges_it = strdup(p.c_str()); + ++privileges_it; + } + *data = manifest_data; } return true; } -API_EXPORT bool ReleaseData(const ManifestData* data, const char* error) { - bool result = true; - if (data) - result = result && LocalStorage::GetInstance()->Remove(data); - if (error) - result = result && LocalStorage::GetInstance()->Remove(error); - return result; +API_EXPORT void ReleaseData(const ManifestData* data, const char* error) { + free(data->package); + free(data->id); + free(data->name); + free(data->short_name); + free(data->version); + free(data->icon); + free(data->api_version); + for (int i = 0; i < data->privilege_count; ++i) + free(data->privilege_list[i]); + delete[] data->privilege_list; + delete data; + free((void*)error); } } // extern "C" diff --git a/src/widget-manifest-parser/widget_manifest_parser.h b/src/widget-manifest-parser/widget_manifest_parser.h index d4bd60a..5f12926 100644 --- a/src/widget-manifest-parser/widget_manifest_parser.h +++ b/src/widget-manifest-parser/widget_manifest_parser.h @@ -6,21 +6,23 @@ #ifndef WIDGET_MANIFEST_PARSER_WIDGET_MANIFEST_PARSER_H_ #define WIDGET_MANIFEST_PARSER_WIDGET_MANIFEST_PARSER_H_ +#include + #ifdef __cplusplus extern "C" { #endif // Represents a manifest data struct ManifestData { - const char* package; - const char* id; - const char* name; - const char* short_name; - const char* version; - const char* icon; - const char* api_version; + char* package; + char* id; + char* name; + char* short_name; + char* version; + char* icon; + char* api_version; unsigned int privilege_count; - const char** privilege_list; + char** privilege_list; }; // Reads manifest from specified file and filles specified data argument @@ -30,8 +32,7 @@ bool ParseManifest(const char* path, const ManifestData** data, const char** error); // Releses the data and the error returned by ParseManifest. -// Returns true on success or false otherwise. -bool ReleaseData(const ManifestData* data, const char* error); +void ReleaseData(const ManifestData* data, const char* error); #ifdef __cplusplus } // extern "C" -- 2.7.4