2 * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "allowed-appid/appsvc_db.hh"
18 #include "allowed-appid/plugin_manager.hh"
19 #include "common/exception.hh"
20 #include "common/log_private.hh"
24 PluginManager::PluginManager()
25 : MetadataPlugin(std::unique_ptr<Database>(
26 new AppSvcDB(MetadataPlugin::GetUid()))) {
29 PluginManager::~PluginManager() = default;
31 PluginManager& PluginManager::GetInst() {
32 static PluginManager inst;
36 bool PluginManager::StepBackup(const std::unique_ptr<AppEventArgs>& args) {
37 auto* db = dynamic_cast<AppSvcDB*>(GetDB());
39 _E("MetadataPlugin is not prepared");
44 auto info_arr = db->Select(args->GetAppId());
45 if (info_arr.size() != 0)
46 infos_.insert(infos_.end(), info_arr.begin(), info_arr.end());
47 } catch (Exception& e) {
48 _E("Execption(%d) occurs", e.GetErrorCode());
55 std::vector<std::string> PluginManager::Split(const std::string& str,
56 const std::string& delim) {
57 std::string string(str);
58 std::vector<std::string> result;
60 while ((pos = string.find(delim)) != std::string::npos) {
61 std::string token = string.substr(0, pos);
62 result.push_back(token);
63 string.erase(0, pos + delim.length());
65 result.push_back(string);
69 bool PluginManager::StepInstall(const std::unique_ptr<AppEventArgs>& args) {
70 auto* db = dynamic_cast<AppSvcDB*>(GetDB());
72 _E("MetadataPlugin is not prepared");
76 for (auto& metadata : args->GetMetadataList()) {
77 for (auto& app_id : Split(metadata->GetValue(), "|")) {
79 db->Insert(args->GetAppId(), app_id);
80 } catch (Exception& e) {
88 bool PluginManager::StepUninstall(const std::unique_ptr<AppEventArgs>& args) {
89 auto* db = dynamic_cast<AppSvcDB*>(GetDB());
91 _E("MetadataPlugin is not prepared");
96 db->Delete(args->GetAppId());
97 } catch (Exception& e) {
98 _W("Failed to delete alias info. appid(%s)", args->GetAppId().c_str());
103 bool PluginManager::StepUpgrade(const std::unique_ptr<AppEventArgs>& args) {
104 if (!StepUninstall(args))
107 return StepInstall(args);
110 bool PluginManager::StepRestore() {
112 auto* db = dynamic_cast<AppSvcDB*>(GetDB());
114 _E("MetadataPlugin is not prepared");
118 for (auto& info : infos_) {
120 db->Insert(info->GetAppId(), info->GetAllowedAppId());
121 } catch (Exception& e) {
122 _E("Exception(%d) occurs", e.GetErrorCode());
130 } // namespace plugin