Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / pkgmgr_interface.h
1 // Copyright (c) 2015 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 #ifndef COMMON_PKGMGR_INTERFACE_H_
6 #define COMMON_PKGMGR_INTERFACE_H_
7
8 #include <manifest_parser/utils/logging.h>
9 #include <pkgmgr_installer.h>
10
11 #include <filesystem>
12 #include <map>
13 #include <memory>
14 #include <optional>
15 #include <string>
16
17 #include "common/app_query_interface.h"
18 #include "common/utils/macros.h"
19 #include "common/utils/request.h"
20
21 namespace common_installer {
22
23 enum class InstallationMode {
24   ONLINE,
25   OFFLINE
26 };
27
28 class PkgmgrSignal;
29 class PkgMgrInterface;
30 typedef std::shared_ptr<PkgMgrInterface> PkgMgrPtr;
31
32 /**
33  * \brief The PkgmgrInstallerInterface class
34  *        Interface defining strategy for creation of pkgmgr_installer object
35  *        and PkgmgrSignal object.
36  *
37  * This interface is injected to PkgMgrInterface class to decide:
38  *  - how to create pkgmgr_installer,
39  *  - if to create PkgmgrSignal object,
40  *  - what installation mode should be set in installer context.
41  */
42 class PkgmgrInstallerInterface {
43  public:
44   virtual ~PkgmgrInstallerInterface() = default;
45
46   virtual bool CreatePkgMgrInstaller(pkgmgr_installer** installer,
47                              InstallationMode* mode) = 0;
48   virtual bool ShouldCreateSignal() const = 0;
49 };
50
51 /**
52  * \brief The PkgmgrInstaller class
53  *        Implementation of PkgmgrInstallerInterface that handles creation of
54  *        pkgmgr_installer class in online and offline mode.
55  */
56 class PkgmgrInstaller : public PkgmgrInstallerInterface {
57  public:
58   bool CreatePkgMgrInstaller(pkgmgr_installer** installer,
59                              InstallationMode* mode) override;
60   bool ShouldCreateSignal() const override;
61 };
62
63 /**
64  * \brief Encapsulates pkgmgr API which handles parsing backend options
65  *        and returns values/modes for installation process.
66  */
67 class PkgMgrInterface {
68  public:
69   /**
70    * Set current AppQueryInterface for request
71    *
72    * \param idx index of request
73    *
74    * \return True if setting was success. Otherwise, return false
75    */
76   bool SetAppQueryInterface(int idx);
77
78   /**
79    * Add AppQueryInterface for request
80    *
81    * \param idx index of request
82    * \param interface AppQueryInterface for request
83    */
84   void AddAppQueryInterface(int idx,
85       std::shared_ptr<AppQueryInterface> interface);
86
87   /**
88    * Get AppQueryInterface for current request
89    *
90    * \return AppQueryInterface ptr if current AppQueryInterface exist.
91    *         Otherwise, return null shared_ptr
92    */
93   std::shared_ptr<AppQueryInterface> GetAppQueryInterface();
94
95   /**
96    * Returns Request type passed from pkgmgr_installer
97    *
98    * \param idx index of request
99    *
100    * \return request type retrieved from pkgmgr_installer
101    */
102   RequestType GetRequestType(int idx = 0) const;
103
104   /**
105    * Returns uid passed from pkgmgr_installer
106    *
107    * \return uid retrieved from pkgmgr_installer
108    */
109   uid_t GetUid() const;
110
111   /**
112    * Returns Request info passed from pkgmgr_installer
113    *
114    * \param idx index of request
115    *
116    * \return request info retrieved from pkgmgr_installer
117    */
118   std::string GetRequestInfo(int idx = 0) const;
119
120   /**
121    * Creates PkgMgrInterface
122    *
123    * \param argc main() argc argument passed to the backend
124    * \param argv main() argv argument passed to the backend
125    * \param pkgmgr_installer_interface interface defining strategy of creation
126    *                                   of pkgmgr_installer
127    * \param interface pointer to AppQueryInterface
128    *
129    * \return Smart pointer to the PkgMgrInterface
130    */
131   static PkgMgrPtr Create(int argc, char** argv,
132         PkgmgrInstallerInterface* pkgmgr_installer_interface,
133         std::shared_ptr<AppQueryInterface> interface = nullptr);
134
135   /**
136   * Returns TEP path passed from pkgmgr_installer
137   *
138   * \return TEP path retrieved from pkgmgr_installer
139   */
140   std::filesystem::path GetTepPath() const;
141
142   /**
143   * Returns True if TEP file should be moved. Otherwise, return false
144   *
145   * \return True if TEP file should be moved. Otherwise, return false
146   */
147   bool GetIsTepMove() const;
148
149   /**
150   * Returns True if move request is to external. Otherwise, return false
151   *
152   * \return True if move request is to external. Otherwise, return false
153   */
154   bool GetIsMoveToExternal() const;
155
156   /**
157    * Get MoveType for Move request
158    *
159    * \return value of MoveType
160    */
161   int GetMoveType() const;
162
163   /**
164   * Returns True if the request is for preload. Otherwise, return false
165   *
166   * \return True if the request is for preload. Otherwise, return false
167   */
168   bool GetIsPreloadRequest() const;
169
170   /**
171   * Returns True if the request is for preload-rw. Otherwise, return false
172   *
173   * \return True if the request is for preload-rw. Otherwise, return false
174   */
175   bool GetIsPreloadRWRequest() const;
176
177   /**
178   * Returns True if the request has force-remove flag. Otherwise, return false
179   *
180   * \return True if the request has force-remove flag. Otherwise, return false
181   */
182   bool GetIsForceRemoval() const;
183
184   /**
185   * Returns True if the request has no-remove flag. Otherwise, return false
186   *
187   * \return True if the request has no-remove flag. Otherwise, return false
188   */
189   bool GetIsNoRemoval() const;
190
191   /**
192   * Returns True if the request has keep-rwdata flag. Otherwise, return false
193   *
194   * \return True if the request has keep-rwdata flag. Otherwise, return false
195   */
196   bool GetIsKeepRWData() const;
197
198   /**
199   * Returns True if the request has partial-rw flag. Otherwise, return false
200   *
201   * \return True if the request has partial-rw flag. Otherwise, return false
202   */
203   bool GetIsPartialRW() const;
204
205   /**
206    * Returns True if the request is debug mode. Otherwise, return false;
207    *
208    * \return True if the request is debug mode. Otherwise, return false;
209    */
210   bool GetDebugMode() const;
211
212   /**
213    * Returns True if the request has skip-check-reference flag.
214    *         Otherwise, return false;
215    *
216    * \return True if the request has skip-check-reference flag.
217    *         Otherwise, return false;
218    */
219   bool GetIsSkipCheckReference() const;
220
221   /**
222    * Returns True if the request is skip optimization. Otherwise, return false;
223    *
224    * \return True if the request is skip optimization. Otherwise, return false;
225    */
226   bool GetSkipOptimization() const;
227
228   /**
229    * Returns the number of request info count.
230    *
231    * \return The number of request info count.
232    */
233   int GetRequestInfoCount() const;
234
235   /**
236    * Returns whether recovery mode is set to cleanup or not.
237    *
238    * \return True if recovery mode is set to cleanup. Otherwise, return false.
239    */
240   bool GetRecoveryCleanup() const;
241
242   /**
243    * Get Raw pointer to pkgmgr_installer object
244    * NOTE: It should not be used (PkgMgrInterface can destroy it
245    *
246    * \return raw pkgmgr_installer pointer
247    */
248   DEPRECATED pkgmgr_installer *GetRawPi() const { return pi_; }
249
250   /**
251   * Returns installation mode
252   *
253   * \return 'ONLINE' for online installation, 'OFFLINE' otherwise
254   */
255   InstallationMode GetInstallationMode() const { return install_mode_; }
256
257   /**
258    * @brief CreatePkgmgrSignal
259    *
260    * @return creates pkgmgr signal
261    */
262   std::unique_ptr<PkgmgrSignal> CreatePkgmgrSignal() const;
263
264   /** PkgMgrInstance destructor. */
265   ~PkgMgrInterface();
266
267  private:
268   explicit PkgMgrInterface(PkgmgrInstallerInterface* pkgmgr_installer_interface,
269                            std::shared_ptr<AppQueryInterface> interface)
270       : pi_(nullptr),
271         install_mode_(InstallationMode::ONLINE),
272         is_app_installed_(std::nullopt),
273         pkgmgr_installer_interface_(pkgmgr_installer_interface),
274         query_interface_(interface) {}
275   int InitInternal(int argc, char** argv);
276
277   pkgmgr_installer* pi_;
278   InstallationMode install_mode_;
279   mutable std::optional<bool> is_app_installed_;
280   PkgmgrInstallerInterface* pkgmgr_installer_interface_;
281
282   std::shared_ptr<AppQueryInterface> query_interface_;
283   std::map<int, std::shared_ptr<AppQueryInterface>> query_interface_map_;
284
285   SCOPE_LOG_TAG(PkgMgrInterface)
286   DISALLOW_COPY_AND_ASSIGN(PkgMgrInterface);
287 };
288
289 }  // namespace common_installer
290
291 #endif  // COMMON_PKGMGR_INTERFACE_H_