1 /*
2  * Copyright (c) 2022-2023 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_ABILITY_MANAGER_SERVICE_H
17 #define OHOS_DISTRIBUTED_ABILITY_MANAGER_SERVICE_H
18 
19 #include "bundlemgr/bundle_mgr_interface.h"
20 #include "bundlemgr/bundle_mgr_proxy.h"
21 #include "continuation_manager/notifier_info.h"
22 #include "distributed_ability_manager_stub.h"
23 #include "event_handler.h"
24 #include "ffrt.h"
25 #include "single_instance.h"
26 #include "system_ability.h"
27 
28 namespace OHOS {
29 namespace DistributedSchedule {
30 class DistributedAbilityManagerService : public SystemAbility, public DistributedAbilityManagerStub {
31     DECLARE_SYSTEM_ABILITY(DistributedAbilityManagerService);
32     DECLARE_SINGLE_INSTANCE_BASE(DistributedAbilityManagerService);
33 
34 public:
35     ~DistributedAbilityManagerService() = default;
36     void OnStart() override;
37     void OnStop() override;
38     int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override;
39 
40     int32_t Register(
41         const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams, int32_t& token) override;
42     int32_t Unregister(int32_t token) override;
43     int32_t RegisterDeviceSelectionCallback(
44         int32_t token, const std::string& cbType, const sptr<IRemoteObject>& notifier) override;
45     int32_t UnregisterDeviceSelectionCallback(int32_t token, const std::string& cbType) override;
46     int32_t UpdateConnectStatus(int32_t token, const std::string& deviceId,
47         const DeviceConnectStatus& deviceConnectStatus) override;
48     int32_t StartDeviceManager(
49         int32_t token, const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams = nullptr) override;
50 
51     void ProcessNotifierDied(const sptr<IRemoteObject>& notifier);
52     void ScheduleStartDeviceManager(const sptr<IRemoteObject>& appProxy, int32_t token,
53         const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams = nullptr);
54     int32_t OnDeviceConnect(int32_t token, const std::vector<ContinuationResult>& continuationResults);
55     int32_t OnDeviceDisconnect(int32_t token, const std::vector<ContinuationResult>& continuationResults);
56     int32_t OnDeviceCancel();
57     void DumpAppRegisterInfo(std::string& info);
58 private:
59     DistributedAbilityManagerService();
60     bool IsExceededRegisterMaxNum(uint32_t accessToken);
61     bool IsContinuationModeValid(ContinuationMode continuationMode);
62     bool IsConnectStatusValid(DeviceConnectStatus deviceConnectStatus);
63     bool IsTokenRegistered(uint32_t accessToken, int32_t token);
64     bool IsNotifierRegistered(int32_t token);
65     bool IsNotifierRegisteredLocked(int32_t token, const std::string& cbType);
66     bool QueryTokenByNotifier(const sptr<IRemoteObject>& notifier, int32_t& token);
67     bool HandleDeviceConnect(const sptr<IRemoteObject>& notifier,
68         const std::vector<ContinuationResult>& continuationResults);
69     bool HandleDeviceDisconnect(const sptr<IRemoteObject>& notifier,
70         const std::vector<ContinuationResult>& continuationResults);
71     int32_t ConnectAbility(int32_t token, const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams);
72     int32_t DisconnectAbility();
73     bool QueryExtensionAbilityInfo(const int32_t& activeAccountId, const AAFwk::Want& want,
74         AppExecFwk::ExtensionAbilityInfo& extensionInfo);
75     bool HandleDisconnectAbility();
76     void HandleNotifierDied(const sptr<IRemoteObject>& notifier);
77     void HandleStartDeviceManager(int32_t token,
78         const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams = nullptr);
79     void HandleUpdateConnectStatus(int32_t token, std::string deviceId,
80         const DeviceConnectStatus& deviceConnectStatus);
81     void DumpNotifierLocked(const std::vector<int32_t>& tokenVec, std::string& info);
82 
83     std::mutex tokenMutex_;
84     std::atomic<int32_t> token_ {0};
85     std::mutex tokenMapMutex_;
86     std::map<uint32_t, std::vector<int32_t>> tokenMap_;
87     std::mutex callbackMapMutex_;
88     std::map<int32_t, std::unique_ptr<NotifierInfo>> callbackMap_;
89     sptr<IRemoteObject::DeathRecipient> notifierDeathRecipient_;
90     sptr<IRemoteObject> connect_;
91     std::mutex appProxyMutex_;
92     sptr<IRemoteObject> appProxy_;
93     std::shared_ptr<ffrt::queue> continuationHandler_;
94 };
95 } // namespace DistributedSchedule
96 } // namespace OHOS
97 #endif // OHOS_DISTRIBUTED_ABILITY_MANAGER_SERVICE_H
98