• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 
25 #include "ability_info.h"
26 #include "free_install_observer_manager.h"
27 #include "want.h"
28 
29 namespace OHOS {
30 namespace AAFwk {
31 class AbilityManagerService;
32 
33 struct FreeInstallInfo {
34     Want want;
35     int32_t userId = -1;
36     int32_t requestCode = -1;
37     std::shared_ptr<std::promise<int32_t>> promise;
38     bool isInstalled = false;
39     std::string identity;
40     sptr<IRemoteObject> callerToken = nullptr;
41     sptr<IRemoteObject> dmsCallback = nullptr;
42 };
43 
44 /**
45  * @class FreeInstallManager
46  * FreeInstallManager.
47  */
48 class FreeInstallManager : public std::enable_shared_from_this<FreeInstallManager> {
49 public:
50     explicit FreeInstallManager(const std::weak_ptr<AbilityManagerService> &server);
51     virtual ~FreeInstallManager() = default;
52 
53     /**
54      * OnInstallFinished, StartFreeInstall is complete.
55      *
56      * @param resultCode, ERR_OK on success, others on failure.
57      * @param want, installed ability.
58      * @param userId, user`s id.
59      */
60     void OnInstallFinished(int resultCode, const Want &want, int32_t userId, bool isAsync = false);
61 
62     /**
63      * OnRemoteInstallFinished, DMS has finished.
64      *
65      * @param resultCode, ERR_OK on success, others on failure.
66      * @param want, installed ability.
67      * @param userId, user`s id.
68      */
69     void OnRemoteInstallFinished(int resultCode, const Want &want, int32_t userId);
70 
71     /**
72      * Start to free install.
73      *
74      * @param want, the want of the ability to free install.
75      * @param userId, designation User ID.
76      * @param requestCode, ability request code.
77      * @param callerToken, caller ability token.
78      * @param isAsync, the request is async.
79      * @return Returns ERR_OK on success, others on failure.
80      */
81     int StartFreeInstall(const Want &want, int32_t userId, int requestCode, const sptr<IRemoteObject> &callerToken,
82         bool isAsync = false);
83 
84     /**
85      * Start to remote free install.
86      *
87      * @param want, the want of the ability to free install.
88      * @param requestCode, ability request code.
89      * @param validUserId, designation User ID.
90      * @param callerToken, caller ability token.
91      * @return Returns ERR_OK on success, others on failure.
92      */
93     int StartRemoteFreeInstall(const Want &want, int requestCode, int32_t validUserId,
94         const sptr<IRemoteObject> &callerToken);
95 
96     /**
97      * Start to free install from another devices.
98      * The request is send from DMS.
99      *
100      * @param want, the want of the ability to free install.
101      * @param callback, used to notify caller the result of free install.
102      * @param userId, designation User ID.
103      * @param requestCode, ability request code.
104      * @return Returns ERR_OK on success, others on failure.
105      */
106     int FreeInstallAbilityFromRemote(const Want &want, const sptr<IRemoteObject> &callback,
107         int32_t userId, int requestCode);
108 
109     /**
110      * Connect if the request is free install.
111      * @param want, the want of the ability to free install.
112      * @param userId, designation User ID.
113      * @param callerToken, caller ability token.
114      * @param localDeviceId, the device id of local.
115      * @return Returns ERR_OK on success, others on failure.
116      */
117     int ConnectFreeInstall(const Want &want, int32_t userId, const sptr<IRemoteObject> &callerToken,
118         const std::string& localDeviceId);
119 
120     /**
121      * Add an observer from application into freeInstallObserverManager.
122      * @param observer, the observer of the ability to free install.
123      * @return Returns ERR_OK on success, others on failure.
124      */
125     int AddFreeInstallObserver(const sptr<AbilityRuntime::IFreeInstallObserver> &observer);
126 
127     /**
128      * Remove the timeout task when bms connect FA center.
129      * @param want, the want of the ability to free install.
130      */
131     void OnRemoveTimeoutTask(const Want &want);
132 
133 private:
134     std::weak_ptr<AbilityManagerService> server_;
135     std::vector<FreeInstallInfo> freeInstallList_;
136     std::vector<FreeInstallInfo> dmsFreeInstallCbs_;
137     std::map<std::string, std::time_t> timeStampMap_;
138     ffrt::mutex distributedFreeInstallLock_;
139     ffrt::mutex freeInstallListLock_;
140     ffrt::mutex freeInstallObserverLock_;
141     /**
142      * Start remote free install.
143      *
144      * @param want, the want of the ability to remote free install.
145      * @param userId, designation User ID.
146      * @param requestCode, ability request code.
147      * @param callerToken, caller ability token.
148      * @return Returns ERR_OK on success, others on failure.
149      */
150     int RemoteFreeInstall(const Want &want, int32_t userId, int requestCode, const sptr<IRemoteObject> &callerToken);
151 
152     int NotifyDmsCallback(const Want &want, int resultCode);
153     bool IsTopAbility(const sptr<IRemoteObject> &callerToken);
154     void NotifyFreeInstallResult(const Want &want, int resultCode, bool isAsync = false);
155     FreeInstallInfo BuildFreeInstallInfo(const Want &want, int32_t userId, int requestCode,
156         const sptr<IRemoteObject> &callerToken, bool isAsync);
157     std::time_t GetTimeStamp();
158 
159     void RemoveFreeInstallInfo(const std::string &bundleName, const std::string &abilityName,
160         const std::string &startTime);
161 
162     void PostUpgradeAtomicServiceTask(int resultCode, const Want &want, int32_t userId);
163 
164     void PostTimeoutTask(const Want &want);
165     void HandleTimeoutTask(const std::string &bundleName, const std::string &abilityName, const std::string &startTime);
166     void RemoveTimeoutTask(const std::string &bundleName, const std::string &abilityName, const std::string &startTime);
167 };
168 }  // namespace AAFwk
169 }  // namespace OHOS
170 #endif  // OHOS_ABILITY_RUNTIME_FREE_INSTALL_MANAGER_H
171