Fix Metadata Plugin Parser
[platform/core/appfw/aul-1.git] / parser / metadata / allowed-appid / plugin_manager.cc
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
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"
21
22 namespace plugin {
23
24 PluginManager::PluginManager()
25   : MetadataPlugin(std::unique_ptr<Database>(
26         new AppSvcDB(MetadataPlugin::GetUid()))) {
27 }
28
29 PluginManager::~PluginManager() = default;
30
31 PluginManager& PluginManager::GetInst() {
32   static PluginManager inst;
33   return inst;
34 }
35
36 bool PluginManager::StepBackup(const std::unique_ptr<AppEventArgs>& args) {
37   auto* db = dynamic_cast<AppSvcDB*>(GetDB());
38   if (db == nullptr) {
39     _E("MetadataPlugin is not prepared");
40     return false;
41   }
42
43   try {
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());
49     return false;
50   }
51
52   return true;
53 }
54
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;
59   std::size_t pos;
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());
64   }
65   result.push_back(string);
66   return result;
67 }
68
69 bool PluginManager::StepInstall(const std::unique_ptr<AppEventArgs>& args) {
70   auto* db = dynamic_cast<AppSvcDB*>(GetDB());
71   if (db == nullptr) {
72     _E("MetadataPlugin is not prepared");
73     return false;
74   }
75
76   for (auto& metadata : args->GetMetadataList()) {
77     for (auto& app_id : Split(metadata->GetValue(), "|")) {
78       try {
79         db->Insert(args->GetAppId(), app_id);
80       } catch (Exception& e) {
81         return false;
82       }
83     }
84   }
85   return true;
86 }
87
88 bool PluginManager::StepUninstall(const std::unique_ptr<AppEventArgs>& args) {
89   auto* db = dynamic_cast<AppSvcDB*>(GetDB());
90   if (db == nullptr) {
91     _E("MetadataPlugin is not prepared");
92     return false;
93   }
94
95   try {
96     db->Delete(args->GetAppId());
97   } catch (Exception& e) {
98     _W("Failed to delete alias info. appid(%s)", args->GetAppId().c_str());
99   }
100   return true;
101 }
102
103 bool PluginManager::StepUpgrade(const std::unique_ptr<AppEventArgs>& args) {
104   if (!StepUninstall(args))
105     return false;
106
107   return StepInstall(args);
108 }
109
110 bool PluginManager::StepRestore() {
111   _E("Restore");
112   auto* db = dynamic_cast<AppSvcDB*>(GetDB());
113   if (db == nullptr) {
114     _E("MetadataPlugin is not prepared");
115     return false;
116   }
117
118   for (auto& info : infos_) {
119     try {
120       db->Insert(info->GetAppId(), info->GetAllowedAppId());
121     } catch (Exception& e) {
122       _E("Exception(%d) occurs", e.GetErrorCode());
123       return false;
124     }
125   }
126
127   return true;
128 }
129
130 }  // namespace plugin