Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / installer / app_installer.h
1 /* 2014, Copyright © Intel Coporation, license APACHE-2.0, see LICENSE file */
2 // Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3 // Use of this source code is governed by a apache 2.0 license that can be
4 // found in the LICENSE file.
5
6 #ifndef COMMON_INSTALLER_APP_INSTALLER_H_
7 #define COMMON_INSTALLER_APP_INSTALLER_H_
8
9 #include <manifest_parser/utils/logging.h>
10
11 #include <algorithm>
12 #include <list>
13 #include <memory>
14 #include <string>
15 #include <utility>
16
17 #include "common/archive_info.h"
18 #include "common/history_logger.h"
19 #include "common/pkgmgr_interface.h"
20 #include "common/pkgmgr_signal.h"
21 #include "common/step/step.h"
22 #include "common/utils/file_logbackend.h"
23 #include "common/utils/macros.h"
24
25 namespace common_installer {
26
27 /**
28  * \brief Includes the main “steps” processing mechanisms. It holds
29  *        the lists of steps and runs each of the step in the configured
30  *        order.
31  */
32 class AppInstaller : public Step::IStepErrorSignal {
33  public:
34   /** Enumeration of possible returned Results */
35   enum class Result {
36     OK,
37     ERROR,
38     CLEANUP_ERROR,
39     UNDO_ERROR,
40     UNKNOWN
41   };
42
43   /**
44    * \brief explicit Constructor
45    *
46    * \param package_type package type
47    * \param pkgmgr pointer to PkgMgrInterface object
48    */
49   explicit AppInstaller(const char* package_type, PkgMgrPtr pkgmgr);
50
51   /** virtual desctructor */
52   virtual ~AppInstaller();
53
54   /** error handler for sending error signal */
55   void on_error(Step::Status result, const std::string& error) override;
56
57   /**
58    * \brief Adds new step to installer by specified type
59    *        Type of template parameter is used to create requested step
60    *        class instance.
61    *        Context of installer is passed to step in this method
62    *        and is not being exposed outside installer.
63    *        Step arguments are deduced and forwarded to constructor.
64    *
65    * \tparam StepT Step object to be added to the step list
66    * \tparam Args constructor arguments for StepT object
67    * \param args argument list
68    */
69   template<class StepT, class... Args>
70   void AddStep(Args&&... args) {
71     std::unique_ptr<Step> step(
72         new StepT(context_.get(), std::forward<Args>(args)...));
73     step->connect(this);
74     steps_.emplace_back(std::move(step));
75   }
76
77   /**
78    * \brief Removes specific step in steps list.
79    * \param step_name step name to be removed
80    */
81   void RemoveStep(std::string step_name) {
82     auto it = std::find_if(steps_.begin(), steps_.end(),
83         [&](const std::unique_ptr<Step>& s) {
84           return step_name == s->name();
85         });
86     if (steps_.end() == it) {
87       LOG(ERROR) << "Can't find the step : " << step_name;
88       return;
89     }
90     steps_.erase(it);
91   }
92
93   /**
94    * \brief Adds new step to installer by specified type on specyfic place
95    *        in steps list.
96    *        Type of template parameter is used to create requested step
97    *        class instance.
98    *        Context of installer is passed to step in this method
99    *        and is not being exposed outside installer.
100    *        Step arguments are deduced and forwarded to constructor.
101    *
102    * \tparam StepT Step object to be added to the step list
103    * \tparam index index at which the new step is added
104    * \tparam Args constructor arguments for StepT object
105    * \param args argument list
106    */
107   template<class StepT, class... Args>
108   void AddStepAtIndex(unsigned int index, Args&&... args) {
109     std::unique_ptr<Step> step(
110         new StepT(context_.get(), std::forward<Args>(args)...));
111     step->connect(this);
112     std::list<std::unique_ptr<Step>> tmpList;
113     tmpList.emplace_back(std::move(step));
114     auto it = steps_.begin();
115     std::advance(it, index);
116     steps_.splice(it, tmpList);
117   }
118
119   /**
120    * \brief Replaces new step to installer by specified type
121    *        Type of template parameter is used to create requested step
122    *        class instance.
123    *        Context of installer is passed to step in this method
124    *        and is not being exposed outside installer.
125    *        Step arguments are deduced and forwarded to constructor.
126    * \tparam StepT Step object to be added to the step list
127    * \tparam Args constructor arguments for StepT object
128    * \param step_name step name to replace
129    * \param args argument list
130    */
131   template<class StepT, class... Args>
132   void ReplaceStep(std::string step_name, Args&&... args) {
133     auto it = std::find_if(std::begin(steps_), std::end(steps_),
134         [&](const std::unique_ptr<Step>& s) {
135           return step_name == s->name();
136         });
137
138     if (steps_.end() == it) {
139       LOG(ERROR) << "Can't find the step : " << step_name;
140       return;
141     }
142
143     std::unique_ptr<Step> step(
144         new StepT(context_.get(), std::forward<Args>(args)...));
145     step->connect(this);
146     *it = std::move(step);
147   }
148
149   /**
150    * \brief Adds new step before specified step
151    *        Type of template parameter is used to create requested step
152    *        class instance.
153    *        Context of installer is passed to step in this method
154    *        and is not being exposed outside installer.
155    *        Step arguments are deduced and forwarded to constructor.
156    * \tparam StepT Step object to be added to the step list
157    * \tparam Args constructor arguments for StepT object
158    * \param step_name step name to find and add
159    * \param args argument list
160    */
161   template<class StepT, class... Args>
162   void AddStepBefore(std::string step_name, Args&&... args) {
163     auto it = std::find_if(std::begin(steps_), std::end(steps_),
164         [&](const std::unique_ptr<Step>& s) {
165           return step_name == s->name();
166         });
167
168     if (steps_.end() == it) {
169       LOG(ERROR) << "Can't find the step : " << step_name;
170       return;
171     }
172
173     std::unique_ptr<Step> step(
174         new StepT(context_.get(), std::forward<Args>(args)...));
175     step->connect(this);
176     steps_.insert(it, std::move(step));
177   }
178
179   /**
180    * \brief Adds new step after specified step
181    *        Type of template parameter is used to create requested step
182    *        class instance.
183    *        Context of installer is passed to step in this method
184    *        and is not being exposed outside installer.
185    *        Step arguments are deduced and forwarded to constructor.
186    * \tparam StepT Step object to be added to the step list
187    * \tparam Args constructor arguments for StepT object
188    * \param step_name step name to find and add
189    * \param args argument list
190    */
191   template<class StepT, class... Args>
192   void AddStepAfter(std::string step_name, Args&&... args) {
193     auto it = std::find_if(std::begin(steps_), std::end(steps_),
194         [&](const std::unique_ptr<Step>& s) {
195           return step_name == s->name();
196         });
197
198     if (steps_.end() == it) {
199       LOG(ERROR) << "Can't find the step : " << step_name;
200       return;
201     }
202
203     std::unique_ptr<Step> step(
204         new StepT(context_.get(), std::forward<Args>(args)...));
205     step->connect(this);
206     steps_.insert(++it, std::move(step));
207   }
208
209   /**
210    * \brief runs the steps in the specific sequence
211    *
212    * \return Result of the run (eg Result:OK)
213    */
214   Result Run();
215
216   /**
217    * \brief runs the process operations of steps
218    *
219    * \return Result of the run (eg Result:OK)
220    */
221   Result Process();
222
223   /**
224    * \brief runs the undo operations of steps
225    *
226    * \return Result of the run (eg Result:OK)
227    */
228   Result Undo();
229
230   /**
231    * \brief runs the clean operations of steps
232    *
233    * \return Result of the run (eg Result:OK)
234    */
235   Result Clean();
236
237   /**
238    * \brief This method can be used to check
239    *        the number of the registered steps.
240    *
241    * \return number of steps registered in app-installer
242    */
243   unsigned int StepCount() {
244     return steps_.size();
245   }
246
247   void SetIndex(int index) {
248     index_ = index;
249   }
250
251   int GetIndex() {
252     return index_;
253   }
254
255   virtual std::unique_ptr<ArchiveInfo> GetArchiveInfo() = 0;
256
257  protected:
258   PkgMgrPtr pkgmgr_;
259   std::unique_ptr<InstallerContext> context_;
260
261   virtual void InstallSteps();
262   virtual void UpdateSteps();
263   virtual void UninstallSteps();
264   virtual void ReinstallSteps();
265   virtual void DeltaSteps();
266   virtual void MoveSteps();
267   virtual void RecoverySteps();
268   virtual void MountInstallSteps();
269   virtual void MountUpdateSteps();
270   virtual void ManifestDirectInstallSteps();
271   virtual void ManifestDirectUpdateSteps();
272   virtual void ManifestPartialInstallSteps();
273   virtual void ManifestPartialUpdateSteps();
274   virtual void PartialUninstallSteps();
275   virtual void ReadonlyUpdateInstallSteps();
276   virtual void ReadonlyUpdateUninstallSteps();
277   virtual void DisablePkgSteps();
278   virtual void EnablePkgSteps();
279   virtual void MigrateExtImgSteps();
280   virtual void RecoverDBSteps();
281   virtual void UnknownSteps();
282
283  private:
284   void Init();
285
286   std::list<std::unique_ptr<Step>> steps_;
287   std::list<std::unique_ptr<Step>>::iterator it_;
288
289   // data used to send signal
290   std::unique_ptr<PkgmgrSignal> ps_;
291
292   bool SendStartIfNotSent(bool is_skippable);
293   void SendProgress(int progress);
294   void SendFinished(Step::Status status);
295   Step::Status SafeExecute(std::unique_ptr<Step> const& step_ptr,
296                            Step::Status (Step::*method)(),
297                            std::string name);
298   void HandleStepError(Step::Status result, const std::string& error);
299   std::string GetPackageVersion();
300
301   std::shared_ptr<utils::FileLogBackend> failure_logger_;
302   Step::Status status_;
303   Result result_;
304
305   int index_;
306   HistoryLogger history_logger_;
307
308   friend class InstallerRunner;
309
310   SCOPE_LOG_TAG(AppInstaller)
311
312   DISALLOW_COPY_AND_ASSIGN(AppInstaller);
313 };
314
315 }  // namespace common_installer
316
317 #endif  // COMMON_INSTALLER_APP_INSTALLER_H_