1 /* 2 * Copyright (c) 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_SVC_DISTRIBUTED_CONNECTION_H 17 #define OHOS_SVC_DISTRIBUTED_CONNECTION_H 18 19 #include "ability_connect_callback_stub.h" 20 #include "bundle/bundle_manager_internal.h" 21 #include "common_event_manager.h" 22 #include "common_event_subscribe_info.h" 23 #include "common_event_subscriber.h" 24 #include "i_distributed_extension.h" 25 26 namespace OHOS { 27 namespace DistributedSchedule { 28 class SvcDistributedConnection : public AAFwk::AbilityConnectionStub { 29 public: 30 /** 31 * @brief This method is called back to receive the connection result after an ability calls the 32 * ConnectAbility method to connect it to an extension ability. 33 * 34 * @param element: Indicates information about the connected extension ability. 35 * @param remote: Indicates the remote proxy object of the extension ability. 36 * @param resultCode: Indicates the connection result code. The value 0 indicates a successful connection, and any 37 * other value indicates a connection failure. 38 */ 39 void OnAbilityConnectDone(const AppExecFwk::ElementName &element, 40 const sptr<IRemoteObject> &remoteObject, int resultCode) override; 41 42 /** 43 * @brief This method is called back to receive the disconnection result after the connected extension ability 44 * crashes or is killed. If the extension ability exits unexpectedly, all its connections are disconnected, and 45 * each ability previously connected to it will call onAbilityDisconnectDone. 46 * 47 * @param element: Indicates information about the disconnected extension ability. 48 * @param resultCode: Indicates the disconnection result code. The value 0 indicates a successful disconnection, 49 * and any other value indicates a disconnection failure. 50 */ 51 void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override; 52 53 /** 54 * @brief connect remote ability of ExtBackup. 55 */ 56 ErrCode ConnectDExtAbility(AAFwk::Want &want, int32_t userId, bool isCleanCalled, const std::string& delegatee, 57 bool &isDelay); 58 59 /** 60 * @brief disconnect remote ability of ExtBackup. 61 */ 62 ErrCode DisconnectDistributedExtAbility(); 63 64 /** 65 * @brief check whether connected to remote extension ability. 66 * 67 * @return bool true if connected, otherwise false. 68 */ 69 bool IsExtAbilityConnected(); 70 71 /** 72 * @brief get the proxy of backup extension ability. 73 * 74 * @return the proxy of backup extension ability. 75 */ 76 sptr<IDExtension> GetDistributedExtProxy(); 77 78 /** 79 * @brief Set the Callback object 80 * 81 * @param callConnected 82 */ 83 void SetCallback(std::function<void(const std::string &&)> callConnected); 84 85 /** 86 * @brief publish a dextension notification. 87 */ 88 void PublishDExtensionNotification(const std::string &deviceId, const std::string &bundleName, 89 const int32_t userId, const std::string &networkId, AppExecFwk::BundleResourceInfo &bundleResourceInfo); 90 91 /** 92 * @brief Terminate the current DExtension. 93 */ 94 void EndTaskFunction(); 95 96 /** 97 * @brief Register an event listener for receiving common events. 98 */ 99 void RegisterEventListener(); 100 101 public: SvcDistributedConnection(std::string bundleNameIndexInfo)102 SvcDistributedConnection(std::string bundleNameIndexInfo) : bundleNameIndexInfo_(bundleNameIndexInfo) 103 {} ~SvcDistributedConnection()104 ~SvcDistributedConnection() override {}; 105 106 private: 107 std::mutex mutex_; 108 std::condition_variable condition_; 109 std::atomic<bool> isConnected_ = {false}; 110 std::atomic<bool> isCleanCalled_ = {false}; 111 std::atomic<bool> isConnectCalled_ = {false}; 112 std::atomic<bool> isDelay_ = {false}; 113 sptr<IDExtension> distributedProxy_; 114 115 std::function<void(const std::string &&)> callConnected_; 116 std::string bundleNameIndexInfo_; 117 }; 118 119 class EndTaskEventSubscriber : public EventFwk::CommonEventSubscriber { 120 public: EndTaskEventSubscriber(const EventFwk::CommonEventSubscribeInfo & subscribeInfo,SvcDistributedConnection * connection)121 explicit EndTaskEventSubscriber(const EventFwk::CommonEventSubscribeInfo &subscribeInfo, 122 SvcDistributedConnection *connection) 123 : EventFwk::CommonEventSubscriber(subscribeInfo), distributedConnection_(connection) {} 124 OnReceiveEvent(const EventFwk::CommonEventData & data)125 void OnReceiveEvent(const EventFwk::CommonEventData &data) override 126 { 127 std::string action = data.GetWant().GetAction(); 128 if (action == "DMS_ACTION_END_TASK") { 129 if (distributedConnection_ != nullptr) { 130 distributedConnection_->EndTaskFunction(); 131 } 132 } 133 } 134 135 private: 136 SvcDistributedConnection *distributedConnection_; 137 }; 138 } 139 } 140 141 #endif // OHOS_SVC_DISTRIBUTED_CONNECTION_H 142