ab64fa2ca734d1012f862bc083d1e48c46999688
[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::Clean() {
48   if (!Prepare())
49     return;
50
51   for (auto& args : list_) {
52     bool ret = true;
53     if (args->GetEventType() == EventType::Install) {
54       ret = StepInstall(args);
55     } else if (args->GetEventType() == EventType::Uninstall) {
56       ret = StepUninstall(args);
57     } else if (args->GetEventType() == EventType::Upgrade) {
58       ret = StepUpgrade(args);
59     }
60
61     if (!ret) {
62       Rollback();
63       return;
64     }
65   }
66   Post();
67 }
68
69 Database* MetadataPlugin::GetDB() {
70   return db_.get();
71 }
72
73 uid_t MetadataPlugin::GetUid() {
74   uid_t target_uid;
75   pkgmgr_installer_info_get_target_uid(&target_uid);
76   return target_uid;
77 }
78
79 bool MetadataPlugin::Prepare() {
80   try {
81     db_->Open();
82     if (!db_->IntegrityCheck())
83       return false;
84     db_->BeginTransaction();
85   } catch (Exception& e) {
86     return false;
87   }
88   return true;
89 }
90
91 bool MetadataPlugin::StepInstall(const std::unique_ptr<AppEventArgs>& args) {
92   return true;
93 }
94
95 bool MetadataPlugin::StepUninstall(const std::unique_ptr<AppEventArgs>& args) {
96   return true;
97 }
98
99 bool MetadataPlugin::StepUpgrade(const std::unique_ptr<AppEventArgs>& args) {
100   return true;
101 }
102
103 void MetadataPlugin::Post() {
104   try {
105     db_->EndTransaction();
106     db_->Close();
107   } catch (Exception& e) {
108     _E("Exception(%d) occurs", e.GetErrorCode());
109   }
110 }
111
112 void MetadataPlugin::Rollback() {
113   try {
114     db_->Rollback();
115   } catch (Exception& e) {
116     _E("Exception(%d) occurs", e.GetErrorCode());
117   }
118 }
119
120 void MetadataPlugin::Undo() {
121   _E("Undo");
122 }
123
124 }  // namespace plugin