Fix Metadata Plugin Parser
[platform/core/appfw/aul-1.git] / parser / metadata / common / metadata_plugin.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 <unistd.h>
18 #include <pkgmgr_installer_info.h>
19
20 #include "common/database.hh"
21 #include "common/exception.hh"
22 #include "common/log_private.hh"
23 #include "common/metadata_plugin.hh"
24 #include "common/metadata_type.hh"
25
26 namespace plugin {
27
28 void MetadataPlugin::AddAppEventArgs(const std::string& pkgid,
29     const std::string& appid, EventType event_type, GList* list) {
30   auto args = new (std::nothrow) AppEventArgs(appid, pkgid, event_type);
31   if (args == nullptr) {
32     _E("Out of memory");
33     return;
34   }
35
36   GList* iter = list;
37   while (iter) {
38     metadata_t* md = static_cast<metadata_t*>(iter->data);
39     args->AddMetadata(std::unique_ptr<Metadata>(
40           new (std::nothrow) Metadata(md->key, md->value)));
41     iter = g_list_next(iter);
42   }
43
44   list_.emplace_back(args);
45 }
46
47 void MetadataPlugin::Do() {
48   _W("Do");
49   if (!Prepare())
50     return;
51
52   for (auto& args : list_) {
53     bool ret = StepBackup(args);
54     if (!ret) {
55       Post();
56       return;
57     }
58   }
59
60   for (auto& args : list_) {
61     bool ret = true;
62     if (args->GetEventType() == EventType::Install) {
63       ret = StepInstall(args);
64     } else if (args->GetEventType() == EventType::Uninstall) {
65       ret = StepUninstall(args);
66     } else if (args->GetEventType() == EventType::Upgrade) {
67       ret = StepUpgrade(args);
68     }
69
70     if (!ret) {
71       Rollback();
72       return;
73     }
74   }
75   Post();
76 }
77
78 void MetadataPlugin::Clean() {
79   _W("Clean");
80 }
81
82 Database* MetadataPlugin::GetDB() {
83   return db_.get();
84 }
85
86 uid_t MetadataPlugin::GetUid() {
87   uid_t target_uid;
88   pkgmgr_installer_info_get_target_uid(&target_uid);
89   return target_uid;
90 }
91
92 bool MetadataPlugin::Prepare() {
93   try {
94     db_->Open();
95     if (!db_->IntegrityCheck())
96       return false;
97     db_->BeginTransaction();
98   } catch (Exception& e) {
99     return false;
100   }
101   return true;
102 }
103
104 bool MetadataPlugin::StepBackup(const std::unique_ptr<AppEventArgs>& args) {
105   return true;
106 }
107
108 bool MetadataPlugin::StepInstall(const std::unique_ptr<AppEventArgs>& args) {
109   return true;
110 }
111
112 bool MetadataPlugin::StepUninstall(const std::unique_ptr<AppEventArgs>& args) {
113   return true;
114 }
115
116 bool MetadataPlugin::StepUpgrade(const std::unique_ptr<AppEventArgs>& args) {
117   return true;
118 }
119
120 bool MetadataPlugin::StepRestore() {
121   return true;
122 }
123
124 void MetadataPlugin::Post() {
125   try {
126     db_->EndTransaction();
127     db_->Close();
128   } catch (Exception& e) {
129     _E("Exception(%d) occurs", e.GetErrorCode());
130   }
131 }
132
133 void MetadataPlugin::Rollback() {
134   try {
135     db_->Rollback();
136   } catch (Exception& e) {
137     _E("Exception(%d) occurs", e.GetErrorCode());
138   }
139 }
140
141 void MetadataPlugin::Undo() {
142   _E("Undo");
143   if (!Prepare())
144     return;
145
146   for (auto& args : list_) {
147     if (!StepUninstall(args))
148       _E("StepUninstall() is failed");
149   }
150
151   if (!StepRestore())
152     _E("StepRestore() is failed");
153
154   Post();
155 }
156
157 }  // namespace plugin