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 #ifndef FOUNDATION_APPEXECFWK_OHOS_CONTINUATION_REGISTER_MANAGER_PROXY_H 16 #define FOUNDATION_APPEXECFWK_OHOS_CONTINUATION_REGISTER_MANAGER_PROXY_H 17 18 #include <memory> 19 #include "continuation_register_manager_interface.h" 20 #include "continuation_request.h" 21 #include "continuation_connector.h" 22 23 namespace OHOS { 24 namespace AppExecFwk { 25 26 class ContinuationConnector; 27 class Context; 28 class IContinuationDeviceCallback; 29 class RequestCallback; 30 31 class ContinuationRequestBase : public ContinuationRequest { 32 public: 33 ContinuationRequestBase() = default; 34 virtual ~ContinuationRequestBase() = default; 35 SetContext(const std::weak_ptr<Context> & context)36 void SetContext(const std::weak_ptr<Context> &context) 37 { 38 context_ = context; 39 } 40 SetContinuationConnector(const sptr<ContinuationConnector> & continuatinConnector)41 void SetContinuationConnector(const sptr<ContinuationConnector> &continuatinConnector) 42 { 43 continuatinConnector_ = continuatinConnector; 44 } 45 SetRequestCallback(const std::shared_ptr<RequestCallback> & requestCallback)46 void SetRequestCallback(const std::shared_ptr<RequestCallback> &requestCallback) 47 { 48 requestCallback_ = requestCallback; 49 } 50 51 protected: 52 std::weak_ptr<Context> context_; 53 sptr<ContinuationConnector> continuatinConnector_; 54 std::shared_ptr<RequestCallback> requestCallback_ = nullptr; 55 }; 56 57 class ContinuationRequestRegister : public ContinuationRequestBase { 58 public: 59 ContinuationRequestRegister(const std::string &bundleName, const ExtraParams ¶meter, 60 const std::shared_ptr<IContinuationDeviceCallback> &deviceCallback); 61 virtual ~ContinuationRequestRegister() = default; 62 virtual void Execute() override; 63 64 private: 65 ExtraParams parameter_; 66 std::shared_ptr<IContinuationDeviceCallback> deviceCallback_ = nullptr; 67 std::string bundleName_ = ""; 68 }; 69 70 class ContinuationRequestUnRegister : public ContinuationRequestBase { 71 public: 72 ContinuationRequestUnRegister(int token); 73 virtual ~ContinuationRequestUnRegister() = default; 74 virtual void Execute() override; 75 76 private: 77 int token_ = 0; 78 }; 79 80 class ContinuationRequestUpdateConnectStatus : public ContinuationRequestBase { 81 public: 82 ContinuationRequestUpdateConnectStatus(int token, const std::string &deviceId, int status); 83 virtual ~ContinuationRequestUpdateConnectStatus() = default; 84 virtual void Execute() override; 85 86 private: 87 int token_ = 0; 88 std::string deviceId_ = ""; 89 int status_; 90 }; 91 92 class ContinuationRequestShowDeviceList : public ContinuationRequestBase { 93 public: 94 ContinuationRequestShowDeviceList(int token, const ExtraParams ¶meter); 95 virtual ~ContinuationRequestShowDeviceList() = default; 96 virtual void Execute() override; 97 98 private: 99 int token_ = 0; 100 ExtraParams parameter_; 101 }; 102 103 class ContinuationRegisterManagerProxy : public IContinuationRegisterManager { 104 public: 105 ContinuationRegisterManagerProxy(const std::weak_ptr<Context> &context); 106 virtual ~ContinuationRegisterManagerProxy(); 107 108 /** 109 * Registers an ability to be migrated with the Device+ control center and obtains the registration token assigned 110 * to the ability. 111 * 112 * @param bundleName Indicates the bundle name of the application whose ability is to be migrated. 113 * @param parameter Indicates the {@link ExtraParams} object containing the extra parameters used to filter 114 * the list of available devices. This parameter can be null. 115 * @param deviceCallback Indicates the callback to be invoked when the connection state of the selected device 116 * changes. 117 * @param requestCallback Indicates the callback to be invoked when the Device+ service is connected. 118 */ 119 virtual void Register(const std::string &bundleName, const ExtraParams ¶meter, 120 const std::shared_ptr<IContinuationDeviceCallback> &deviceCallback, 121 const std::shared_ptr<RequestCallback> &requestCallback) override; 122 123 /** 124 * Unregisters a specified ability from the Device+ control center based on the token obtained during ability 125 * registration. 126 * 127 * @param token Indicates the registration token of the ability. 128 * @param requestCallback Indicates the callback to be invoked when the Device+ service is connected. 129 * This parameter can be null. 130 */ 131 virtual void Unregister(int token, const std::shared_ptr<RequestCallback> &requestCallback) override; 132 133 /** 134 * Updates the connection state of the device where the specified ability is successfully migrated. 135 * 136 * @param token Indicates the registration token of the ability. 137 * @param deviceId Indicates the ID of the device whose connection state is to be updated. 138 * @param status Indicates the connection state to update, which can be {@link DeviceConnectState#FAILURE}, 139 * {@link DeviceConnectState#IDLE}, {@link DeviceConnectState#CONNECTING}, {@link DeviceConnectState#CONNECTED}, 140 * or {@link DeviceConnectState#DIS_CONNECTING}. 141 * @param requestCallback Indicates the callback to be invoked when the Device+ service is connected. 142 * This parameter can be null. 143 */ 144 virtual void UpdateConnectStatus(int token, const std::string &deviceId, int status, 145 const std::shared_ptr<RequestCallback> &requestCallback) override; 146 147 /** 148 * Shows the list of devices that can be selected for ability migration on the distributed network. 149 * 150 * @param token Indicates the registration token of the ability. 151 * @param parameter Indicates the {@link ExtraParams} object containing the extra parameters used to filter 152 * the list of available devices. This parameter can be null. 153 * @param requestCallback Indicates the callback to be invoked when the Device+ service is connected. 154 * This parameter can be null. 155 */ 156 virtual void ShowDeviceList( 157 int token, const ExtraParams ¶meter, const std::shared_ptr<RequestCallback> &requestCallback) override; 158 159 /** 160 * Disconnects from the Device+ control center. 161 */ 162 virtual void Disconnect(void) override; 163 164 private: 165 std::weak_ptr<Context> context_; 166 std::weak_ptr<Context> applicationContext_; 167 sptr<ContinuationConnector> continuatinConnector_; 168 169 void SendRequest(const std::weak_ptr<Context> &context, const std::shared_ptr<ContinuationRequest> &request); 170 }; 171 } // namespace AppExecFwk 172 } // namespace OHOS 173 #endif // FOUNDATION_APPEXECFWK_OHOS_CONTINUATION_REGISTER_MANAGER_PROXY_H