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