896d615edcc9da58cac5ce51920a7a7336bf42d5
[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 std::vector<std::string> PluginManager::Split(const std::string& str,
37     const std::string& delim) {
38   std::string string(str);
39   std::vector<std::string> result;
40   std::size_t pos;
41   while ((pos = string.find(delim)) != std::string::npos) {
42     std::string token = string.substr(0, pos);
43     result.push_back(token);
44     string.erase(0, pos + delim.length());
45   }
46   result.push_back(string);
47   return result;
48 }
49
50 bool PluginManager::StepInstall(const std::unique_ptr<AppEventArgs>& args) {
51   auto* db = dynamic_cast<AppSvcDB*>(GetDB());
52   if (db == nullptr) {
53     _E("MetadataPlugin is not prepared");
54     return false;
55   }
56
57   for (auto& metadata : args->GetMetadataList()) {
58     for (auto& app_id : Split(metadata->GetValue(), "|")) {
59       try {
60         db->Insert(args->GetAppId(), app_id);
61       } catch (Exception& e) {
62         return false;
63       }
64     }
65   }
66   return true;
67 }
68
69 bool PluginManager::StepUninstall(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   try {
77     db->Delete(args->GetAppId());
78   } catch (Exception& e) {
79     _W("Failed to delete alias info. appid(%s)", args->GetAppId().c_str());
80   }
81   return true;
82 }
83
84 bool PluginManager::StepUpgrade(const std::unique_ptr<AppEventArgs>& args) {
85   if (!StepUninstall(args))
86     return false;
87
88   return StepInstall(args);
89 }
90
91 }  // namespace plugin