9e5b6af2c58fbd764c9320ebf06342452f123ef5
[platform/core/appfw/app-installers.git] / src / common / step / step.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 /*
7   A step is made of 4 functions (that must be defined)
8   and one data pointer.
9
10   The functions are:
11     - precheck checks the input data used during process method
12     - process  handles the job to be done
13     - undo     undo the step's work after failure
14     - clean    remove temporary data of the step after success
15
16   All these functions have the same signature: they do not accept any arguments
17   and they return a value which states the execution result.
18
19   The returned code of Step::Status::OK indicates a successful execution.
20   Otherwise, the returned code should be set to value different than
21   Step::Status::OK.
22
23   Errornous result of processing is casted to integer value and sent to
24   client which initialized request.
25 */
26 #ifndef COMMON_STEP_STEP_H_
27 #define COMMON_STEP_STEP_H_
28
29 #include <pkgmgr_installer_error.h>
30
31 #include <string>
32
33 #include "common/installer_context.h"
34
35 // This macro should be defined at the end of class definition
36 #define STEP_NAME(NAME)                                                        \
37   SCOPE_LOG_TAG(NAME)                                                          \
38   const char* name() const override { return #NAME; }                          \
39
40 namespace common_installer {
41
42 /**
43  * \brief Abstract base class for all Steps* used for requests handling.
44  *
45  * It is an abstract base class that demands the definition of
46  * 4 methods: process, precheck, undo and clean.
47  */
48 class Step {
49  public:
50   /** Possible code returned by process, undo, clean, precheck methods. */
51   enum class Status {
52     GLOBALSYMLINK_ERROR = PKGMGR_INSTALLER_ERRCODE_GLOBALSYMLINK_ERROR,
53     GRANT_PERMISSION_ERROR = PKGMGR_INSTALLER_ERRCODE_GRANT_PERMISSION_ERROR,
54     IMAGE_ERROR = PKGMGR_INSTALLER_ERRCODE_IMAGE_ERROR,
55     UNZIP_ERROR = PKGMGR_INSTALLER_ERRCODE_UNZIP_ERROR,
56     SECURITY_ERROR = PKGMGR_INSTALLER_ERRCODE_SECURITY_ERROR,
57     REGISTER_ERROR = PKGMGR_INSTALLER_ERRCODE_REGISTER_ERROR,
58     PRIVILEGE_ERROR = PKGMGR_INSTALLER_ERRCODE_PRIVILEGE_ERROR,
59     PARSE_ERROR = PKGMGR_INSTALLER_ERRCODE_PARSE_ERROR,
60     RECOVERY_ERROR = PKGMGR_INSTALLER_ERRCODE_RECOVERY_ERROR,
61     DELTA_ERROR = PKGMGR_INSTALLER_ERRCODE_DELTA_ERROR,
62     APP_DIR_ERROR = PKGMGR_INSTALLER_ERRCODE_APP_DIR_ERROR,
63     CONFIG_ERROR = PKGMGR_INSTALLER_ERRCODE_CONFIG_ERROR,
64     SIGNATURE_ERROR = PKGMGR_INSTALLER_ERRCODE_SIGNATURE_ERROR,
65     SIGNATURE_INVALID = PKGMGR_INSTALLER_ERRCODE_SIGNATURE_INVALID,
66     CERT_ERROR = PKGMGR_INSTALLER_ERRCODE_CERT_ERROR,
67     AUTHOR_CERT_NOT_MATCH = PKGMGR_INSTALLER_ERRCODE_AUTHOR_CERT_NOT_MATCH,
68     AUTHOR_CERT_NOT_FOUND = PKGMGR_INSTALLER_ERRCODE_AUTHOR_CERT_NOT_FOUND,
69     ICON_ERROR = PKGMGR_INSTALLER_ERRCODE_ICON_ERROR,
70     ICON_NOT_FOUND = PKGMGR_INSTALLER_ERRCODE_ICON_NOT_FOUND,
71     MANIFEST_ERROR = PKGMGR_INSTALLER_ERRCODE_MANIFEST_ERROR,
72     MANIFEST_NOT_FOUND = PKGMGR_INSTALLER_ERRCODE_MANIFEST_NOT_FOUND,
73     PACKAGE_NOT_FOUND = PKGMGR_INSTALLER_ERRCODE_PACKAGE_NOT_FOUND,
74     OPERATION_NOT_ALLOWED = PKGMGR_INSTALLER_ERRCODE_OPERATION_NOT_ALLOWED,
75     OUT_OF_SPACE = PKGMGR_INSTALLER_ERRCODE_OUT_OF_SPACE,
76     INVALID_VALUE = PKGMGR_INSTALLER_ERRCODE_INVALID_VALUE,
77     ERROR = PKGMGR_INSTALLER_ERRCODE_ERROR,
78     OK = PKGMGR_INSTALLER_ERRCODE_OK,
79     RECOVERY_DONE = PKGMGR_INSTALLER_ERRCODE_OK + 1
80   };
81
82   class IStepErrorSignal {
83    public:
84     virtual ~IStepErrorSignal() {}
85     virtual void on_error(Step::Status result, const std::string& error) = 0;
86   };
87
88   /** Standard constructor */
89   explicit Step(InstallerContext* context) : context_(context) { }
90
91   /** Virtual "empty" destructor */
92   virtual ~Step() { }
93
94   /** Handles the job to be done */
95   virtual Status process() = 0;
96
97   /** Undos the step's work after failure */
98   virtual Status undo() = 0;
99
100   /** Removes temporary data of the step after success */
101   virtual Status clean() = 0;
102
103   /** Checks the input data used during process method */
104   virtual Status precheck() = 0;
105
106   /** Returns step name */
107   virtual const char* name() const = 0;
108
109   void connect(IStepErrorSignal* signal) {
110     on_error_ = signal;
111   }
112
113  protected:
114   InstallerContext* context_;
115   IStepErrorSignal* on_error_;
116 };
117
118 }  // namespace common_installer
119
120 #endif  // COMMON_STEP_STEP_H_