Fix Metadata Plugin Parser
[platform/core/appfw/aul-1.git] / parser / metadata / alias-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 "alias-appid/appsvc_db.hh"
18 #include "alias-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("Exception(%d) occurs", e.GetErrorCode());
49     return false;
50   }
51
52   return true;
53 }
54
55 bool PluginManager::StepInstall(const std::unique_ptr<AppEventArgs>& args) {
56   auto* db = dynamic_cast<AppSvcDB*>(GetDB());
57   if (db == nullptr) {
58     _E("MetadataPlugin is not prepared");
59     return false;
60   }
61
62   for (auto& metadata : args->GetMetadataList()) {
63     try {
64       db->Insert(metadata->GetValue(), args->GetAppId());
65     } catch (Exception& e) {
66       return false;
67     }
68   }
69   return true;
70 }
71
72 bool PluginManager::StepUninstall(const std::unique_ptr<AppEventArgs>& args) {
73   auto* db = dynamic_cast<AppSvcDB*>(GetDB());
74   if (db == nullptr) {
75     _E("MetadataPlugin is not prepared");
76     return false;
77   }
78
79   if (args->GetMetadataList().empty()) {
80     try {
81       db->Delete(args->GetAppId());
82     } catch (Exception& e) {
83       _W("Failed to delete alias info. appid(%s)", args->GetAppId().c_str());
84     }
85     return true;
86   }
87
88   for (auto& metadata : args->GetMetadataList()) {
89     try {
90       db->Delete(metadata->GetValue(), args->GetAppId());
91     } catch (Exception& e) {
92       return false;
93     }
94   }
95   return true;
96 }
97
98 bool PluginManager::StepUpgrade(const std::unique_ptr<AppEventArgs>& args) {
99   auto* db = dynamic_cast<AppSvcDB*>(GetDB());
100   if (db == nullptr) {
101     _E("MetadataPlugin is not prepared");
102     return false;
103   }
104
105   try {
106     db->Delete(args->GetAppId());
107   } catch (Exception& e) {
108     _W("Failed to delete alias info. appid(%s)", args->GetAppId().c_str());
109   }
110
111   return StepInstall(args);
112 }
113
114 bool PluginManager::StepRestore() {
115   _E("Restore");
116   auto* db = dynamic_cast<AppSvcDB*>(GetDB());
117   if (db == nullptr) {
118     _E("MetadataPlugin is not prepared");
119     return false;
120   }
121
122   for (auto& info : infos_) {
123     try {
124       db->Insert(info->GetAliasAppId(), info->GetAppId());
125     } catch (Exception& e) {
126       _E("Exception(%d) occurs", e.GetErrorCode());
127       return false;
128     }
129   }
130
131   return true;
132 }
133
134 }  // namespace plugin