Fix InstallerRunner::Run()
[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       AppInstaller::Result ret = (*it)->Undo();
34       if (ret != AppInstaller::Result::OK && ret != AppInstaller::Result::ERROR)
35         result = AppInstaller::Result::UNDO_ERROR;
36     } while (it-- != installers_.begin());
37   } else {
38     --it;
39     do {
40       // Clean operation always succeeds
41       (*it)->Clean();
42     } while (it-- != installers_.begin());
43   }
44
45   return result;
46 }
47
48 void InstallerRunner::Init() {
49   for (int i = 0; i < pkgmgr_->GetRequestInfoCount(); i++) {
50     std::unique_ptr<AppInstaller> installer =
51         factory_->CreateInstaller(pkgmgr_, i);
52     installers_.emplace_back(std::move(installer));
53   }
54 }
55
56 }  // namespace common_installer