1/*
2 * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef OHOS_ABILITY_RUNTIME_FREE_INSTALL_MANAGER_H
17#define OHOS_ABILITY_RUNTIME_FREE_INSTALL_MANAGER_H
18
19#include <future>
20#include "cpp/mutex.h"
21
22#include <iremote_object.h>
23#include <iremote_stub.h>
24#include <memory>
25
26#include "ability_info.h"
27#include "free_install_observer_manager.h"
28#include "want.h"
29
30namespace OHOS {
31namespace AAFwk {
32class AbilityManagerService;
33
34struct FreeInstallInfo {
35    Want want;
36    int32_t userId = -1;
37    int32_t requestCode = -1;
38    std::shared_ptr<std::promise<int32_t>> promise;
39    bool isInstalled = false;
40    std::string identity;
41    sptr<IRemoteObject> callerToken = nullptr;
42    sptr<IRemoteObject> dmsCallback = nullptr;
43    bool isPreStartMissionCalled = false;
44    bool isStartUIAbilityBySCBCalled = false;
45    uint32_t specifyTokenId = 0;
46    bool isFreeInstallFinished = false;
47    int resultCode = 0;
48    bool isOpenAtomicServiceShortUrl = false;
49    std::shared_ptr<Want> originalWant = nullptr;
50};
51
52/**
53 * @class FreeInstallManager
54 * FreeInstallManager.
55 */
56class FreeInstallManager : public std::enable_shared_from_this<FreeInstallManager> {
57public:
58    explicit FreeInstallManager(const std::weak_ptr<AbilityManagerService> &server);
59    virtual ~FreeInstallManager() = default;
60
61    /**
62     * OnInstallFinished, StartFreeInstall is complete.
63     *
64     * @param resultCode, ERR_OK on success, others on failure.
65     * @param want, installed ability.
66     * @param userId, user`s id.
67     */
68    void OnInstallFinished(int32_t recordId, int resultCode, const Want &want, int32_t userId, bool isAsync = false);
69
70    /**
71     * OnRemoteInstallFinished, DMS has finished.
72     *
73     * @param resultCode, ERR_OK on success, others on failure.
74     * @param want, installed ability.
75     * @param userId, user`s id.
76     */
77    void OnRemoteInstallFinished(int32_t recordId, int resultCode, const Want &want, int32_t userId);
78
79    /**
80     * Start to free install.
81     *
82     * @param want, the want of the ability to free install.
83     * @param userId, designation User ID.
84     * @param requestCode, ability request code.
85     * @param callerToken, caller ability token.
86     * @param isAsync, the request is async.
87     * @param isOpenAtomicServiceShortUrl, the flag of open atomic service short url.
88     * @return Returns ERR_OK on success, others on failure.
89     */
90    int StartFreeInstall(const Want &want, int32_t userId, int requestCode, const sptr<IRemoteObject> &callerToken,
91        bool isAsync = false, uint32_t specifyTokenId = 0, bool isOpenAtomicServiceShortUrl = false,
92        std::shared_ptr<Want> originalWant = nullptr);
93
94    /**
95     * Start to remote free install.
96     *
97     * @param want, the want of the ability to free install.
98     * @param requestCode, ability request code.
99     * @param validUserId, designation User ID.
100     * @param callerToken, caller ability token.
101     * @return Returns ERR_OK on success, others on failure.
102     */
103    int StartRemoteFreeInstall(const Want &want, int requestCode, int32_t validUserId,
104        const sptr<IRemoteObject> &callerToken);
105
106    /**
107     * Start to free install from another devices.
108     * The request is send from DMS.
109     *
110     * @param want, the want of the ability to free install.
111     * @param callback, used to notify caller the result of free install.
112     * @param userId, designation User ID.
113     * @param requestCode, ability request code.
114     * @return Returns ERR_OK on success, others on failure.
115     */
116    int FreeInstallAbilityFromRemote(const Want &want, const sptr<IRemoteObject> &callback,
117        int32_t userId, int requestCode);
118
119    /**
120     * Connect if the request is free install.
121     * @param want, the want of the ability to free install.
122     * @param userId, designation User ID.
123     * @param callerToken, caller ability token.
124     * @param localDeviceId, the device id of local.
125     * @return Returns ERR_OK on success, others on failure.
126     */
127    int ConnectFreeInstall(const Want &want, int32_t userId, const sptr<IRemoteObject> &callerToken,
128        const std::string& localDeviceId);
129
130    /**
131     * Add an observer from application into freeInstallObserverManager.
132     * @param observer, the observer of the ability to free install.
133     * @return Returns ERR_OK on success, others on failure.
134     */
135    int AddFreeInstallObserver(const sptr<IRemoteObject> &callerToken,
136        const sptr<AbilityRuntime::IFreeInstallObserver> &observer);
137
138    /**
139     * Get free install task info.
140     *
141     * @param bundleName, the bundle name of the task.
142     * @param abilityName, the ability name of the task.
143     * @param startTime, the start time of the task.
144     * @param taskInfo, the found task info
145     * @return Returns true on success, false on failure.
146    */
147    bool GetFreeInstallTaskInfo(const std::string& bundleName, const std::string& abilityName,
148        const std::string& startTime, FreeInstallInfo& taskInfo);
149
150    /**
151     * Get free install task info.
152     *
153     * @param sessionId, the sessionId of the task.
154     * @param taskInfo, the found task info
155     * @return Returns true on success, false on failure.
156    */
157    bool GetFreeInstallTaskInfo(const std::string& sessionId, FreeInstallInfo& taskInfo);
158
159    /**
160     * Set the isStartUIAbilityBySCBCalled flag of the given free install task.
161     *
162     * @param bundleName, the bundle name of the task.
163     * @param abilityName, the abilitu name of the task.
164     * @param startTime, the start time of the task.
165     * @param scbCallStatus, the status of whether StartUIAbilityBySCB is called.
166    */
167    void SetSCBCallStatus(const std::string& bundleName, const std::string& abilityName,
168        const std::string& startTime, bool scbCallStatus);
169
170    /**
171     * Set the isPreStartMissionCalled flag of the given free install task.
172     *
173     * @param bundleName, the bundle name of the task.
174     * @param abilityName, the abilitu name of the task.
175     * @param startTime, the start time of the task.
176     * @param preStartMissionCallStatus, the status of whether PreStartMission is called.
177    */
178    void SetPreStartMissionCallStatus(const std::string& bundleName, const std::string& abilityName,
179        const std::string& startTime, bool preStartMissionCallStatus);
180
181    /**
182     * Set the sessionId of the given free install task.
183     *
184     * @param bundleName, the bundle name of the task.
185     * @param abilityName, the abilitu name of the task.
186     * @param startTime, the start time of the task.
187     * @param sessionId, the sessionId of the free install task.
188    */
189    void SetFreeInstallTaskSessionId(const std::string& bundleName, const std::string& abilityName,
190        const std::string& startTime, const std::string& sessionId);
191
192private:
193    std::weak_ptr<AbilityManagerService> server_;
194    std::vector<FreeInstallInfo> freeInstallList_;
195    std::vector<FreeInstallInfo> dmsFreeInstallCbs_;
196    std::map<std::string, std::time_t> timeStampMap_;
197    ffrt::mutex distributedFreeInstallLock_;
198    ffrt::mutex freeInstallListLock_;
199    ffrt::mutex freeInstallObserverLock_;
200
201    int SetAppRunningState(Want &want);
202
203    /**
204     * Start remote free install.
205     *
206     * @param want, the want of the ability to remote free install.
207     * @param userId, designation User ID.
208     * @param requestCode, ability request code.
209     * @param callerToken, caller ability token.
210     * @return Returns ERR_OK on success, others on failure.
211     */
212    int RemoteFreeInstall(const Want &want, int32_t userId, int requestCode, const sptr<IRemoteObject> &callerToken);
213
214    int NotifyDmsCallback(const Want &want, int resultCode);
215    bool IsTopAbility(const sptr<IRemoteObject> &callerToken);
216    void NotifyFreeInstallResult(int32_t recordId, const Want &want, int resultCode, bool isAsync = false);
217    FreeInstallInfo BuildFreeInstallInfo(const Want &want, int32_t userId, int requestCode,
218        const sptr<IRemoteObject> &callerToken, bool isAsync, uint32_t specifyTokenId = 0,
219        bool isOpenAtomicServiceShortUrl = false, std::shared_ptr<Want> originalWant = nullptr);
220    std::time_t GetTimeStamp();
221
222    void RemoveFreeInstallInfo(const std::string &bundleName, const std::string &abilityName,
223        const std::string &startTime);
224
225    void PostUpgradeAtomicServiceTask(int resultCode, const Want &want, int32_t userId);
226
227    void RemoveTimeoutTask(const std::string &bundleName, const std::string &abilityName, const std::string &startTime);
228
229    void StartAbilityByFreeInstall(FreeInstallInfo &info, std::string &bundleName, std::string &abilityName,
230        std::string &startTime);
231    void StartAbilityByPreInstall(int32_t recordId, FreeInstallInfo &info, std::string &bundleName,
232        std::string &abilityName, std::string &startTime);
233    int32_t UpdateElementName(Want &want, int32_t userId) const;
234    void HandleFreeInstallResult(int32_t recordId, FreeInstallInfo &freeInstallInfo, int resultCode, bool isAsync);
235    void HandleOnFreeInstallSuccess(int32_t recordId, FreeInstallInfo &freeInstallInfo, bool isAsync);
236    void HandleOnFreeInstallFail(int32_t recordId, FreeInstallInfo &freeInstallInfo, int resultCode, bool isAsync);
237    void StartAbilityByConvertedWant(FreeInstallInfo &info, const std::string &startTime);
238    void StartAbilityByOriginalWant(FreeInstallInfo &info, const std::string &startTime);
239    bool VerifyStartFreeInstallPermission(const sptr<IRemoteObject> &callerToken);
240    int32_t GetRecordIdByToken(const sptr<IRemoteObject> &callerToken);
241    void NotifyInsightIntentFreeInstallResult(const Want &want, int resultCode);
242    void NotifyInsightIntentExecuteDone(const Want &want, int resultCode);
243};
244}  // namespace AAFwk
245}  // namespace OHOS
246#endif  // OHOS_ABILITY_RUNTIME_FREE_INSTALL_MANAGER_H
247