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