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