1 /* 2 * Copyright (c) 2021 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_ABILITY_RUNTIME_CONTINUATION_CONNECTOR_H 17 #define OHOS_ABILITY_RUNTIME_CONTINUATION_CONNECTOR_H 18 19 #include <string> 20 #include <atomic> 21 #include <vector> 22 #include <memory> 23 #include <mutex> 24 25 #include "ability_connect_callback_stub.h" 26 #include "continuation_request.h" 27 #include "continuation/kits/continuation_device_callback_interface.h" 28 #include "continuation/remote_register_service_interface.h" 29 #include "fa_context.h" 30 #include "element_name.h" 31 #include "extra_params.h" 32 #include "iremote_broker.h" 33 #include "refbase.h" 34 35 namespace OHOS { 36 namespace AppExecFwk { 37 class ContinuationConnector : public AAFwk::AbilityConnectionStub { 38 public: 39 explicit ContinuationConnector(const std::weak_ptr<Context> &context); 40 virtual ~ContinuationConnector() = default; 41 42 /** 43 * @brief get singleton of Class ContinuationConnector 44 * 45 * @param context: the running context for appcontext 46 * 47 * @return The singleton of ContinuationConnector 48 */ 49 static sptr<ContinuationConnector> GetInstance(const std::weak_ptr<Context> &context); 50 51 /** 52 * @brief This method is called back to receive the connection result after an ability calls the 53 * Ability#connectAbility(Want, IAbilityConnection) method to connect it to a Service ability. 54 * 55 * @param element: Indicates information about the connected Service ability. 56 * @param remote: Indicates the remote proxy object of the Service ability. 57 * @param resultCode: Indicates the connection result code. The value 0 indicates a successful connection, and any 58 * other value indicates a connection failure. 59 */ 60 void OnAbilityConnectDone( 61 const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode) override; 62 63 /** 64 * @brief This method is called back to receive the disconnection result after the connected Service ability crashes 65 * or is killed. If the Service ability exits unexpectedly, all its connections are disconnected, and each ability 66 * previously connected to it will call onAbilityDisconnectDone. 67 * 68 * @param element: Indicates information about the disconnected Service ability. 69 * @param resultCode: Indicates the disconnection result code. The value 0 indicates a successful disconnection, and 70 * any other value indicates a disconnection failure. 71 */ 72 void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override; 73 74 /** 75 * @brief bind remote ability of RemoteRegisterService. 76 * 77 * @param request: request for continuation. 78 */ 79 void BindRemoteRegisterAbility(const std::shared_ptr<AppExecFwk::ContinuationRequest> &request); 80 81 /** 82 * @brief unbind remote ability of RemoteRegisterService. 83 */ 84 void UnbindRemoteRegisterAbility(); 85 86 /** 87 * @brief check whether connected to remote register service. 88 * 89 * @return bool true if connected, otherwise false. 90 */ 91 bool IsAbilityConnected(); 92 93 /** 94 * @brief unregister to control center continuation register service. 95 * 96 * @param token token from register return value. 97 * 98 * @return bool result of unregister. 99 */ 100 bool Unregister(int token); 101 102 /** 103 * @brief notify continuation status to control center continuation register service. 104 * 105 * @param token token from register. 106 * @param deviceId device id. 107 * @param status device status. 108 * 109 * @return bool result of updateConnectStatus. 110 */ 111 bool UpdateConnectStatus(int token, const std::string &deviceId, int status); 112 113 /** 114 * @brief notify control center continuation register service to show device list. 115 * 116 * @param token token from register 117 * @param parameter filter with supported device list. 118 * @return bool result of showDeviceList. 119 */ 120 bool ShowDeviceList(int token, const AppExecFwk::ExtraParams ¶meter); 121 122 /** 123 * @brief register to control center continuation register service. 124 * 125 * @param context ability context. 126 * @param bundleName bundle name of ability. 127 * @param parameter filter with supported device list. 128 * @param callback callback for device connect and disconnect. 129 * 130 * @return int token. 131 */ 132 int Register(std::weak_ptr<Context> &context, const std::string bundleName, 133 const AppExecFwk::ExtraParams ¶meter, std::shared_ptr<IContinuationDeviceCallback> &callback); 134 135 private: 136 /** 137 * @brief bind remote ability of RemoteRegisterService. 138 */ 139 inline void BindRemoteRegisterAbility(); 140 141 private: 142 static const std::string CONNECTOR_DEVICE_ID; 143 static const std::string CONNECTOR_ABILITY_NAME; 144 static const std::string CONNECTOR_BUNDLE_NAME; 145 static sptr<ContinuationConnector> instance_; 146 static std::mutex mutex_; 147 std::weak_ptr<Context> context_; 148 std::vector<std::shared_ptr<ContinuationRequest>> continuationRequestList_; 149 std::atomic<bool> isConnected_ = {false}; 150 sptr<IRemoteRegisterService> remoteRegisterService_; 151 }; 152 } // namespace AppExecFwk 153 } // namespace OHOS 154 #endif // OHOS_ABILITY_RUNTIME_CONTINUATION_CONNECTOR_H 155