Add InstallerFactory, InstallerRunner classes for multi pkg install
[platform/core/appfw/app-installers.git] / src / common / installer_runner.cc
1 // Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
2 // Use of this source code is governed by an apache 2.0 license that can be
3 // found in the LICENSE file.
4
5 #include "common/installer_runner.h"
6
7 #include <manifest_parser/utils/logging.h>
8
9 #include <list>
10
11 #include "common/app_installer.h"
12 #include "common/installer_factory.h"
13 #include "common/pkgmgr_interface.h"
14
15 namespace common_installer {
16
17 InstallerRunner::InstallerRunner(
18     std::unique_ptr<InstallerFactory> factory, PkgMgrPtr pkgmgr)
19     : factory_(std::move(factory)), pkgmgr_(pkgmgr) {
20   Init();
21 }
22
23 AppInstaller::Result InstallerRunner::Run() {
24   AppInstaller::Result result = AppInstaller::Result::OK;
25   std::list<std::unique_ptr<AppInstaller>>::iterator it(installers_.begin());
26   for (; it != installers_.end(); ++it) {
27     result = (*it)->Process();
28     if (result != AppInstaller::Result::OK)
29       break;
30   }
31   if (it != installers_.end() && result == AppInstaller::Result::ERROR) {
32     do {
33       if ((*it)->Undo() != AppInstaller::Result::OK)
34         result = AppInstaller::Result::UNDO_ERROR;
35     } while (it-- != installers_.begin());
36   } else {
37     --it;
38     do {
39       if ((*it)->Clean() != AppInstaller::Result::OK)
40         result = AppInstaller::Result::CLEANUP_ERROR;
41     } while (it-- != installers_.begin());
42   }
43
44   return result;
45 }
46
47 void InstallerRunner::Init() {
48   for (int i = 0; i < pkgmgr_->GetRequestInfoCount(); i++) {
49     std::unique_ptr<AppInstaller> installer =
50         factory_->CreateInstaller(pkgmgr_, i);
51     installers_.emplace_back(std::move(installer));
52   }
53 }
54
55 }  // namespace common_installer