• 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 "distributed_sched_stub.h"
25 #include "distributed_sched_continuation.h"
26 #include "iremote_object.h"
27 #include "iremote_proxy.h"
28 #include "mission/distributed_mission_info.h"
29 #include "nocopyable.h"
30 #include "single_instance.h"
31 #include "system_ability.h"
32 
33 namespace OHOS {
34 namespace DistributedSchedule {
35 class ConnectAbilitySession;
36 
37 enum class ServiceRunningState {
38     STATE_NO_START,
39     STATE_RUNNING
40 };
41 
42 enum class TargetComponent {
43     HARMONY_COMPONENT,
44 };
45 
46 struct ConnectInfo {
47     CallerInfo callerInfo;
48     sptr<IRemoteObject> callbackWrapper;
49     AppExecFwk::ElementName element;
50 };
51 
52 struct ProcessDiedNotifyInfo {
53     std::string remoteDeviceId;
54     CallerInfo callerInfo;
55     TargetComponent targetComponent;
56 };
57 
58 class DistributedSchedService : public SystemAbility, public DistributedSchedStub {
59 DECLARE_SYSTEM_ABILITY(DistributedSchedService);
60 
61 DECLARE_SINGLE_INSTANCE_BASE(DistributedSchedService);
62 
63 public:
64     ~DistributedSchedService() = default;
65     void OnStart() override;
66     void OnStop() override;
67     int32_t StartRemoteAbility(const OHOS::AAFwk::Want& want, int32_t callerUid, int32_t requestCode,
68         uint32_t accessToken) override;
69     int32_t StartAbilityFromRemote(const OHOS::AAFwk::Want& want,
70         const OHOS::AppExecFwk::AbilityInfo& abilityInfo, int32_t requestCode, const CallerInfo& callerInfo,
71         const AccountInfo& accountInfo) override;
72     int32_t ContinueMission(const std::string& srcDeviceId, const std::string& dstDeviceId,
73         int32_t missionId, const sptr<IRemoteObject>& callback, const OHOS::AAFwk::WantParams& wantParams) override;
74     int32_t StartContinuation(const OHOS::AAFwk::Want& want, int32_t missionId, int32_t callerUid,
75         int32_t status, uint32_t accessToken) override;
76     void NotifyCompleteContinuation(const std::u16string& devId, int32_t sessionId, bool isSuccess) override;
77     int32_t NotifyContinuationResultFromRemote(int32_t sessionId, bool isSuccess) override;
78     int32_t ConnectRemoteAbility(const OHOS::AAFwk::Want& want, const sptr<IRemoteObject>& connect,
79         int32_t callerUid, int32_t callerPid, uint32_t accessToken) override;
80     int32_t DisconnectRemoteAbility(const sptr<IRemoteObject>& connect, int32_t callerUid,
81         uint32_t accessToken) override;
82     int32_t ConnectAbilityFromRemote(const OHOS::AAFwk::Want& want, const AppExecFwk::AbilityInfo& abilityInfo,
83         const sptr<IRemoteObject>& connect, const CallerInfo& callerInfo, const AccountInfo& accountInfo) override;
84     int32_t DisconnectAbilityFromRemote(const sptr<IRemoteObject>& connect,
85         int32_t uid, const std::string& sourceDeviceId) override;
86     int32_t NotifyProcessDiedFromRemote(const CallerInfo& callerInfo) override;
87     int32_t GetMissionInfos(const std::string& deviceId, int32_t numMissions,
88         std::vector<AAFwk::MissionInfo>& missionInfos) override;
89     int32_t StoreSnapshotInfo(const std::string& deviceId, int32_t missionId,
90         const uint8_t* byteStream, size_t len) override;
91     int32_t RemoveSnapshotInfo(const std::string& deviceId, int32_t missionId) override;
92     int32_t NotifyMissionsChangedFromRemote(const std::vector<DstbMissionInfo>& missionInfos,
93         const CallerInfo& callerInfo) override;
94     void ProcessConnectDied(const sptr<IRemoteObject>& connect);
95     void ProcessDeviceOffline(const std::string& deviceId);
96     int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override;
97     void DumpConnectInfo(std::string& info);
98     void DumpSessionsLocked(const std::list<ConnectAbilitySession>& sessionsList, std::string& info);
99     void DumpElementLocked(const std::list<AppExecFwk::ElementName>& elementsList, std::string& info);
100     int32_t CheckSupportOsd(const std::string& deviceId) override;
101     void GetCachedOsdSwitch(std::vector<std::u16string>& deviceIds, std::vector<int32_t>& values) override;
102     int32_t GetOsdSwitchValueFromRemote() override;
103     int32_t UpdateOsdSwitchValueFromRemote(int32_t switchVal, const std::string& sourceDeviceId) override;
104     std::unique_ptr<Snapshot> GetRemoteSnapshotInfo(const std::u16string& deviceId, int32_t missionId) override;
105     int32_t GetRemoteMissionSnapshotInfo(const std::string& networkId, int32_t missionId,
106         std::unique_ptr<AAFwk::MissionSnapshot>& missionSnapshot) override;
107     int32_t StartSyncRemoteMissions(const std::string& devId, bool fixConflict, int64_t tag) override;
108     int32_t StartSyncMissionsFromRemote(const CallerInfo& callerInfo,
109         std::vector<DstbMissionInfo>& missionInfos) override;
110     int32_t StopSyncRemoteMissions(const std::string& devId) override;
111     int32_t StopSyncMissionsFromRemote(const CallerInfo& callerInfo) override;
112     int32_t RegisterMissionListener(const std::u16string& devId, const sptr<IRemoteObject>& obj) override;
113     int32_t UnRegisterMissionListener(const std::u16string& devId, const sptr<IRemoteObject>& obj) override;
114     int32_t StartRemoteAbilityByCall(const OHOS::AAFwk::Want& want, const sptr<IRemoteObject>& connect,
115         int32_t callerUid, int32_t callerPid, uint32_t accessToken) override;
116     int32_t ReleaseRemoteAbility(const sptr<IRemoteObject>& connect,
117         const AppExecFwk::ElementName &element) override;
118     int32_t StartAbilityByCallFromRemote(const OHOS::AAFwk::Want& want, const sptr<IRemoteObject>& connect,
119         const CallerInfo& callerInfo, const AccountInfo& accountInfo) override;
120     int32_t ReleaseAbilityFromRemote(const sptr<IRemoteObject>& connect, const AppExecFwk::ElementName &element,
121         const CallerInfo& callerInfo) override;
122     void ProcessCallerDied(const sptr<IRemoteObject>& connect);
123     void ProcessCalleeDied(const sptr<IRemoteObject>& connect);
124 private:
125     DistributedSchedService();
126     bool Init();
127     void NotifyContinuationCallbackResult(int32_t missionId, int32_t isSuccess);
128     void RemoteConnectAbilityMappingLocked(const sptr<IRemoteObject>& connect, const std::string& localDeviceId,
129         const std::string& remoteDeviceId, const AppExecFwk::ElementName& element, const CallerInfo& callerInfo,
130         TargetComponent targetComponent);
131     int32_t DisconnectEachRemoteAbilityLocked(const std::string& localDeviceId,
132         const std::string& remoteDeviceId, const sptr<IRemoteObject>& connect);
133     sptr<IDistributedSched> GetRemoteDms(const std::string& remoteDeviceId);
134     static bool GetLocalDeviceId(std::string& localDeviceId);
135     bool CheckDeviceId(const std::string& localDeviceId, const std::string& remoteDeviceId);
136     bool CheckDeviceIdFromRemote(const std::string& localDeviceId,
137         const std::string& destinationDeviceId, const std::string& sourceDeviceId);
138     void NotifyDeviceOfflineToAppLocked(const sptr<IRemoteObject>& connect, const ConnectAbilitySession& session);
139     int32_t NotifyApp(const sptr<IRemoteObject>& connect, const AppExecFwk::ElementName& element, int32_t errCode);
140     void NotifyProcessDiedAll(const std::list<ProcessDiedNotifyInfo>& notifyList);
141     void NotifyProcessDied(const std::string& remoteDeviceId, const CallerInfo& callerInfo,
142         TargetComponent targetComponent);
143     int32_t CheckDistributedConnectLocked(const CallerInfo& callerInfo) const;
144     void DecreaseConnectLocked(int32_t uid);
145     static int32_t GetUidLocked(const std::list<ConnectAbilitySession>& sessionList);
146     int32_t TryConnectRemoteAbility(const OHOS::AAFwk::Want& want,
147         const sptr<IRemoteObject>& connect, const CallerInfo& callerInfo);
148     int32_t ContinueToAbilityManager(const std::string& deviceId, int32_t missionId);
149     int32_t NotifyResultToAbilityManager(int32_t missionId, int32_t isSuccess);
150     int32_t CleanMission(int32_t missionId);
151     sptr<IRemoteObject> GetAbilityManagerProxy();
152     int32_t ContinueLocalMission(const std::string& dstDeviceId, int32_t missionId,
153         const sptr<IRemoteObject>& callback);
154     int32_t ContinueRemoteMission(const std::string& srcDeviceId, const std::string& dstDeviceId, int32_t missionId,
155         const sptr<IRemoteObject>& callback, const OHOS::AAFwk::WantParams& wantParams);
156     int32_t TryStartRemoteAbilityByCall(const OHOS::AAFwk::Want& want, const sptr<IRemoteObject>& connect,
157         const CallerInfo& callerInfo);
158 
159     std::shared_ptr<DSchedContinuation> dschedContinuation_;
160     std::map<sptr<IRemoteObject>, std::list<ConnectAbilitySession>> distributedConnectAbilityMap_;
161     std::map<sptr<IRemoteObject>, ConnectInfo> connectAbilityMap_;
162     std::unordered_map<int32_t, uint32_t> trackingUidMap_;
163     std::mutex distributedLock_;
164     std::mutex connectLock_;
165     sptr<IRemoteObject::DeathRecipient> connectDeathRecipient_;
166     sptr<IRemoteObject> abilityManagerProxy_;
167     std::mutex calleeLock_;
168     std::map<sptr<IRemoteObject>, ConnectInfo> calleeMap_;
169     sptr<IRemoteObject::DeathRecipient> callerDeathRecipient_;
170 };
171 
172 class ConnectAbilitySession {
173 public:
174     ConnectAbilitySession(const std::string& sourceDeviceId, const std::string& destinationDeviceId,
175         const CallerInfo& callerInfo, TargetComponent targetComponent = TargetComponent::HARMONY_COMPONENT);
176     ~ConnectAbilitySession() = default;
177 
GetSourceDeviceId()178     const std::string& GetSourceDeviceId() const
179     {
180         return sourceDeviceId_;
181     }
182 
GetDestinationDeviceId()183     const std::string& GetDestinationDeviceId() const
184     {
185         return destinationDeviceId_;
186     }
187 
GetElementsList()188     std::list<AppExecFwk::ElementName> GetElementsList() const
189     {
190         return elementsList_;
191     }
192 
GetCallerInfo()193     CallerInfo GetCallerInfo() const
194     {
195         return callerInfo_;
196     }
197 
GetTargetComponent()198     TargetComponent GetTargetComponent() const
199     {
200         return targetComponent_;
201     }
202 
203     bool IsSameCaller(const CallerInfo& callerInfo);
204     void AddElement(const AppExecFwk::ElementName& element);
205 
206 private:
207     std::string sourceDeviceId_;
208     std::string destinationDeviceId_;
209     std::list<AppExecFwk::ElementName> elementsList_;
210     CallerInfo callerInfo_;
211     TargetComponent targetComponent_;
212 };
213 
214 class CallerDeathRecipient : public IRemoteObject::DeathRecipient {
215 public:
216     CallerDeathRecipient() = default;
217     ~CallerDeathRecipient() override = default;
218     void OnRemoteDied(const wptr<IRemoteObject>& remote) override;
219 };
220 } // namespace DistributedSchedule
221 } // namespace OHOS
222 #endif // OHOS_DISTRIBUTED_SCHED_SERVICE_H
223