• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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_DISTRIBUTED_SCHED_SERVICE_H
17 #define OHOS_DISTRIBUTED_SCHED_SERVICE_H
18 
19 #include <memory>
20 #include <mutex>
21 #include <set>
22 #include <unordered_map>
23 
24 #include "continuation_extra_params.h"
25 #include "distributed_sched_stub.h"
26 #include "distributed_sched_continuation.h"
27 #include "dms_callback_task.h"
28 #include "dms_notifier.h"
29 #ifdef SUPPORT_DISTRIBUTED_FORM_SHARE
30 #include "form_mgr_interface.h"
31 #endif
32 #include "iremote_object.h"
33 #include "iremote_proxy.h"
34 #ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
35 #include "mission/distributed_mission_info.h"
36 #include "nocopyable.h"
37 #endif
38 #include "single_instance.h"
39 
40 namespace OHOS {
41 namespace DistributedSchedule {
42 class ConnectAbilitySession;
43 
44 enum class ServiceRunningState {
45     STATE_NO_START,
46     STATE_RUNNING
47 };
48 
49 enum class TargetComponent {
50     HARMONY_COMPONENT,
51 };
52 
53 struct ConnectInfo {
54     CallerInfo callerInfo;
55     sptr<IRemoteObject> callbackWrapper;
56     AppExecFwk::ElementName element;
57 };
58 
59 struct ProcessDiedNotifyInfo {
60     std::string remoteDeviceId;
61     CallerInfo callerInfo;
62     TargetComponent targetComponent;
63 };
64 
65 class DistributedSchedService : public DistributedSchedStub {
66 
67 DECLARE_SINGLE_INSTANCE_BASE(DistributedSchedService);
68 
69 public:
70     ~DistributedSchedService() = default;
71     void OnStart();
72     int32_t StartRemoteAbility(const OHOS::AAFwk::Want& want, int32_t callerUid, int32_t requestCode,
73         uint32_t accessToken) override;
74     int32_t StartAbilityFromRemote(const OHOS::AAFwk::Want& want,
75         const OHOS::AppExecFwk::AbilityInfo& abilityInfo, int32_t requestCode, const CallerInfo& callerInfo,
76         const AccountInfo& accountInfo) override;
77     int32_t SendResultFromRemote(OHOS::AAFwk::Want& want, int32_t requestCode,
78         const CallerInfo& callerInfo, const AccountInfo& accountInfo, int32_t resultCode) override;
79     int32_t ContinueMission(const std::string& srcDeviceId, const std::string& dstDeviceId,
80         int32_t missionId, const sptr<IRemoteObject>& callback, const OHOS::AAFwk::WantParams& wantParams) override;
81     int32_t StartContinuation(const OHOS::AAFwk::Want& want, int32_t missionId, int32_t callerUid,
82         int32_t status, uint32_t accessToken) override;
83     void NotifyCompleteContinuation(const std::u16string& devId, int32_t sessionId, bool isSuccess) override;
84     int32_t NotifyContinuationResultFromRemote(int32_t sessionId, bool isSuccess) override;
85     void NotifyContinuationCallbackResult(int32_t missionId, int32_t resultCode);
86     int32_t NotifyFreeInstallResult(const CallbackTaskItem item, int32_t resultCode);
87     int32_t ConnectRemoteAbility(const OHOS::AAFwk::Want& want, const sptr<IRemoteObject>& connect,
88         int32_t callerUid, int32_t callerPid, uint32_t accessToken) override;
89     int32_t DisconnectRemoteAbility(const sptr<IRemoteObject>& connect, int32_t callerUid,
90         uint32_t accessToken) override;
91     int32_t ConnectAbilityFromRemote(const OHOS::AAFwk::Want& want, const AppExecFwk::AbilityInfo& abilityInfo,
92         const sptr<IRemoteObject>& connect, const CallerInfo& callerInfo, const AccountInfo& accountInfo) override;
93     int32_t DisconnectAbilityFromRemote(const sptr<IRemoteObject>& connect,
94         int32_t uid, const std::string& sourceDeviceId) override;
95     int32_t NotifyProcessDiedFromRemote(const CallerInfo& callerInfo) override;
96 #ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
97     int32_t GetMissionInfos(const std::string& deviceId, int32_t numMissions,
98         std::vector<AAFwk::MissionInfo>& missionInfos) override;
99     int32_t NotifyMissionsChangedFromRemote(const std::vector<DstbMissionInfo>& missionInfos,
100         const CallerInfo& callerInfo) override;
101 #endif
102     void ProcessConnectDied(const sptr<IRemoteObject>& connect);
103     void ProcessDeviceOffline(const std::string& deviceId);
104     void DumpConnectInfo(std::string& info);
105     void DumpSessionsLocked(const std::list<ConnectAbilitySession>& sessionsList, std::string& info);
106     void DumpElementLocked(const std::list<AppExecFwk::ElementName>& elementsList, std::string& info);
107 #ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
108     int32_t GetRemoteMissionSnapshotInfo(const std::string& networkId, int32_t missionId,
109         std::unique_ptr<AAFwk::MissionSnapshot>& missionSnapshot) override;
110     int32_t StartSyncRemoteMissions(const std::string& devId, bool fixConflict, int64_t tag) override;
111     int32_t StartSyncMissionsFromRemote(const CallerInfo& callerInfo,
112         std::vector<DstbMissionInfo>& missionInfos) override;
113     int32_t StopSyncRemoteMissions(const std::string& devId) override;
114     int32_t StopSyncMissionsFromRemote(const CallerInfo& callerInfo) override;
115     int32_t RegisterMissionListener(const std::u16string& devId, const sptr<IRemoteObject>& obj) override;
116     int32_t UnRegisterMissionListener(const std::u16string& devId, const sptr<IRemoteObject>& obj) override;
117 #endif
118     int32_t StartRemoteAbilityByCall(const OHOS::AAFwk::Want& want, const sptr<IRemoteObject>& connect,
119         int32_t callerUid, int32_t callerPid, uint32_t accessToken) override;
120     int32_t ReleaseRemoteAbility(const sptr<IRemoteObject>& connect,
121         const AppExecFwk::ElementName &element) override;
122     int32_t StartAbilityByCallFromRemote(const OHOS::AAFwk::Want& want, const sptr<IRemoteObject>& connect,
123         const CallerInfo& callerInfo, const AccountInfo& accountInfo) override;
124     int32_t ReleaseAbilityFromRemote(const sptr<IRemoteObject>& connect, const AppExecFwk::ElementName &element,
125         const CallerInfo& callerInfo) override;
126 #ifdef SUPPORT_DISTRIBUTED_FORM_SHARE
127     int32_t StartRemoteShareForm(const std::string& remoteDeviceId,
128         const AppExecFwk::FormShareInfo& formShareInfo) override;
129     int32_t StartShareFormFromRemote(
130         const std::string& remoteDeviceId, const AppExecFwk::FormShareInfo& formShareInfo) override;
131     void ProcessFormMgrDied(const wptr<IRemoteObject>& remote);
132 #endif
133     void ProcessCallerDied(const sptr<IRemoteObject>& connect, int32_t deviceType);
134     void ProcessCalleeDied(const sptr<IRemoteObject>& connect);
135     int32_t StartRemoteFreeInstall(const OHOS::AAFwk::Want& want, int32_t callerUid, int32_t requestCode,
136         uint32_t accessToken, const sptr<IRemoteObject>& callback) override;
137     int32_t StartFreeInstallFromRemote(const FreeInstallInfo& info, int64_t taskId) override;
138     int32_t NotifyCompleteFreeInstallFromRemote(int64_t taskId, int32_t resultCode) override;
139     int32_t NotifyCompleteFreeInstall(const FreeInstallInfo& info, int64_t taskId, int32_t resultCode);
140     int32_t GetDistributedComponentList(std::vector<std::string>& distributedComponents) override;
141     void SetContinuationTimeout(int32_t missionId, int32_t timeout);
142     void RemoveContinuationTimeout(int32_t missionId);
143     std::string GetContinuaitonDevice(int32_t missionId);
144 
145     int32_t ConnectAbility(const sptr<DmsNotifier>& dmsNotifier, int32_t token,
146         const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams);
147     int32_t DisconnectAbility();
148 private:
149     DistributedSchedService();
150     bool Init();
151     void RemoteConnectAbilityMappingLocked(const sptr<IRemoteObject>& connect, const std::string& localDeviceId,
152         const std::string& remoteDeviceId, const AppExecFwk::ElementName& element, const CallerInfo& callerInfo,
153         TargetComponent targetComponent);
154     int32_t DisconnectEachRemoteAbilityLocked(const std::string& localDeviceId,
155         const std::string& remoteDeviceId, const sptr<IRemoteObject>& connect);
156     sptr<IDistributedSched> GetRemoteDms(const std::string& remoteDeviceId);
157     static bool GetLocalDeviceId(std::string& localDeviceId);
158     bool CheckDeviceId(const std::string& localDeviceId, const std::string& remoteDeviceId);
159     bool CheckDeviceIdFromRemote(const std::string& localDeviceId,
160         const std::string& destinationDeviceId, const std::string& sourceDeviceId);
161     void NotifyDeviceOfflineToAppLocked(const sptr<IRemoteObject>& connect, const ConnectAbilitySession& session);
162     int32_t NotifyApp(const sptr<IRemoteObject>& connect, const AppExecFwk::ElementName& element, int32_t errCode);
163     void NotifyProcessDiedAll(const std::list<ProcessDiedNotifyInfo>& notifyList);
164     void NotifyProcessDied(const std::string& remoteDeviceId, const CallerInfo& callerInfo,
165         TargetComponent targetComponent);
166     int32_t CheckDistributedConnectLocked(const CallerInfo& callerInfo) const;
167     void DecreaseConnectLocked(int32_t uid);
168     static int32_t GetUidLocked(const std::list<ConnectAbilitySession>& sessionList);
169     int32_t TryConnectRemoteAbility(const OHOS::AAFwk::Want& want,
170         const sptr<IRemoteObject>& connect, const CallerInfo& callerInfo);
171 #ifdef SUPPORT_DISTRIBUTED_FORM_SHARE
172     sptr<AppExecFwk::IFormMgr> GetFormMgrProxy();
173 #endif
174     int32_t CleanMission(int32_t missionId);
175     int32_t SetCallerInfo(int32_t callerUid, std::string localDeviceId, uint32_t accessToken, CallerInfo& callerInfo);
176     int32_t SetWantForContinuation(AAFwk::Want& newWant, int32_t missionId);
177     int32_t ContinueLocalMission(const std::string& dstDeviceId, int32_t missionId,
178         const sptr<IRemoteObject>& callback, const OHOS::AAFwk::WantParams& wantParams);
179     int32_t ContinueAbilityWithTimeout(const std::string& dstDeviceId, int32_t missionId,
180         const sptr<IRemoteObject>& callback, uint32_t remoteBundleVersion = 0);
181     int32_t ContinueRemoteMission(const std::string& srcDeviceId, const std::string& dstDeviceId, int32_t missionId,
182         const sptr<IRemoteObject>& callback, const OHOS::AAFwk::WantParams& wantParams);
183     int32_t TryStartRemoteAbilityByCall(const OHOS::AAFwk::Want& want, const sptr<IRemoteObject>& connect,
184         const CallerInfo& callerInfo);
185     int32_t StartLocalAbility(const FreeInstallInfo& info, int64_t taskId, int32_t resultCode);
186     int32_t StartAbility(const OHOS::AAFwk::Want& want, int32_t requestCode);
187     int32_t HandleRemoteNotify(const FreeInstallInfo& info, int64_t taskId, int32_t resultCode);
188     void ReportDistributedComponentChange(const CallerInfo& callerInfo, int32_t changeType,
189         int32_t componentType, int32_t deviceType);
190     void ReportDistributedComponentChange(const ConnectInfo& connectInfo, int32_t changeType,
191         int32_t componentType, int32_t deviceType);
192     void HandleLocalCallerDied(const sptr<IRemoteObject>& connect);
193     void SaveCallerComponent(const OHOS::AAFwk::Want& want, const sptr<IRemoteObject>& connect,
194         const CallerInfo& callerInfo);
195     void RemoveCallerComponent(const sptr<IRemoteObject>& connect);
196     void ProcessCalleeOffline(const std::string& deviceId);
197     void GetConnectComponentList(std::vector<std::string>& distributedComponents);
198     void GetCallComponentList(std::vector<std::string>& distributedComponents);
199     void ProcessFreeInstallOffline(const std::string& deviceId);
200     int32_t CheckTargetPermission(const OHOS::AAFwk::Want& want, const CallerInfo& callerInfo,
201         const AccountInfo& accountInfo, int32_t flag, bool needQueryExtension);
202 
203     std::shared_ptr<DSchedContinuation> dschedContinuation_;
204     std::map<sptr<IRemoteObject>, std::list<ConnectAbilitySession>> distributedConnectAbilityMap_;
205     std::map<sptr<IRemoteObject>, ConnectInfo> connectAbilityMap_;
206     std::unordered_map<int32_t, uint32_t> trackingUidMap_;
207     std::mutex distributedLock_;
208     std::mutex connectLock_;
209     sptr<IRemoteObject::DeathRecipient> connectDeathRecipient_;
210 #ifdef SUPPORT_DISTRIBUTED_FORM_SHARE
211     sptr<IRemoteObject::DeathRecipient> formMgrDeathRecipient_;
212     sptr<AppExecFwk::IFormMgr> formMgrProxy_;
213     std::mutex formMgrLock_;
214 #endif
215     std::mutex calleeLock_;
216     std::map<sptr<IRemoteObject>, ConnectInfo> calleeMap_;
217     sptr<IRemoteObject::DeathRecipient> callerDeathRecipient_;
218     std::shared_ptr<DmsCallbackTask> dmsCallbackTask_;
219     std::shared_ptr<AppExecFwk::EventHandler> componentChangeHandler_;
220     std::mutex callerLock_;
221     std::map<sptr<IRemoteObject>, std::list<ConnectAbilitySession>> callerMap_;
222     sptr<IRemoteObject::DeathRecipient> callerDeathRecipientForLocalDevice_;
223     sptr<IRemoteObject> connect_;
224 };
225 
226 class ConnectAbilitySession {
227 public:
228     ConnectAbilitySession(const std::string& sourceDeviceId, const std::string& destinationDeviceId,
229         const CallerInfo& callerInfo, TargetComponent targetComponent = TargetComponent::HARMONY_COMPONENT);
230     ~ConnectAbilitySession() = default;
231 
GetSourceDeviceId()232     const std::string& GetSourceDeviceId() const
233     {
234         return sourceDeviceId_;
235     }
236 
GetDestinationDeviceId()237     const std::string& GetDestinationDeviceId() const
238     {
239         return destinationDeviceId_;
240     }
241 
GetElementsList()242     std::list<AppExecFwk::ElementName> GetElementsList() const
243     {
244         return elementsList_;
245     }
246 
GetCallerInfo()247     CallerInfo GetCallerInfo() const
248     {
249         return callerInfo_;
250     }
251 
GetTargetComponent()252     TargetComponent GetTargetComponent() const
253     {
254         return targetComponent_;
255     }
256 
257     bool IsSameCaller(const CallerInfo& callerInfo);
258     void AddElement(const AppExecFwk::ElementName& element);
259 
260 private:
261     std::string sourceDeviceId_;
262     std::string destinationDeviceId_;
263     std::list<AppExecFwk::ElementName> elementsList_;
264     CallerInfo callerInfo_;
265     TargetComponent targetComponent_;
266 };
267 
268 class CallerDeathRecipient : public IRemoteObject::DeathRecipient {
269 public:
270     CallerDeathRecipient() = default;
CallerDeathRecipient(int32_t deviceType)271     explicit CallerDeathRecipient(int32_t deviceType)
272     {
273         deviceType_ = deviceType;
274     }
275     ~CallerDeathRecipient() override = default;
276     void OnRemoteDied(const wptr<IRemoteObject>& remote) override;
277 private:
278     int32_t deviceType_ = IDistributedSched::CALLEE;
279 };
280 } // namespace DistributedSchedule
281 } // namespace OHOS
282 #endif // OHOS_DISTRIBUTED_SCHED_SERVICE_H
283