1 /* 2 * Copyright (c) 2022-2025 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 DeviceConnectStatus deviceConnectStatus) override; 48 int32_t StartDeviceManager( 49 int32_t token, const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams = nullptr) override; 50 int32_t RegisterWithoutExtraParam(int32_t& token) override; 51 int32_t StartDeviceManagerWithoutExtraParam(int32_t token) override; 52 53 void ProcessNotifierDied(const sptr<IRemoteObject>& notifier); 54 void ScheduleStartDeviceManager(const sptr<IRemoteObject>& appProxy, int32_t token, 55 const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams = nullptr); 56 int32_t OnDeviceConnect(int32_t token, const std::vector<ContinuationResult>& continuationResults); 57 int32_t OnDeviceDisconnect(int32_t token, const std::vector<ContinuationResult>& continuationResults); 58 int32_t OnDeviceCancel(); 59 void DumpAppRegisterInfo(std::string& info); 60 private: 61 DistributedAbilityManagerService(); 62 bool IsExceededRegisterMaxNum(uint32_t accessToken); 63 bool IsContinuationModeValid(ContinuationMode continuationMode); 64 bool IsConnectStatusValid(DeviceConnectStatus deviceConnectStatus); 65 bool IsTokenRegistered(uint32_t accessToken, int32_t token); 66 bool IsNotifierRegistered(int32_t token); 67 bool IsNotifierRegisteredLocked(int32_t token, const std::string& cbType); 68 bool QueryTokenByNotifier(const sptr<IRemoteObject>& notifier, int32_t& token); 69 bool HandleDeviceConnect(const sptr<IRemoteObject>& notifier, 70 const std::vector<ContinuationResult>& continuationResults); 71 bool HandleDeviceDisconnect(const sptr<IRemoteObject>& notifier, 72 const std::vector<ContinuationResult>& continuationResults); 73 int32_t ConnectAbility(int32_t token, const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams); 74 int32_t DisconnectAbility(); 75 bool QueryExtensionAbilityInfo(const int32_t& activeAccountId, const AAFwk::Want& want, 76 AppExecFwk::ExtensionAbilityInfo& extensionInfo); 77 bool HandleDisconnectAbility(); 78 bool VerifyPermission(uint32_t accessToken, const std::string& permissionName) const; 79 void HandleNotifierDied(const sptr<IRemoteObject>& notifier); 80 void HandleStartDeviceManager(int32_t token, 81 const std::shared_ptr<ContinuationExtraParams>& continuationExtraParams = nullptr); 82 void HandleUpdateConnectStatus(int32_t token, std::string deviceId, 83 const DeviceConnectStatus& deviceConnectStatus); 84 void DumpNotifierLocked(const std::vector<int32_t>& tokenVec, std::string& info); 85 86 std::mutex tokenMutex_; 87 std::atomic<int32_t> token_ {0}; 88 std::mutex tokenMapMutex_; 89 std::map<uint32_t, std::vector<int32_t>> tokenMap_; 90 std::mutex callbackMapMutex_; 91 std::map<int32_t, std::unique_ptr<NotifierInfo>> callbackMap_; 92 sptr<IRemoteObject::DeathRecipient> notifierDeathRecipient_; 93 sptr<IRemoteObject> connect_; 94 std::mutex appProxyMutex_; 95 sptr<IRemoteObject> appProxy_; 96 std::shared_ptr<ffrt::queue> continuationHandler_; 97 }; 98 } // namespace DistributedSchedule 99 } // namespace OHOS 100 #endif // OHOS_DISTRIBUTED_ABILITY_MANAGER_SERVICE_H 101