1 /* 2 * Copyright (c) 2021-2024 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_EXTENSION_IMPL_H 17 #define OHOS_ABILITY_RUNTIME_EXTENSION_IMPL_H 18 19 #include "extension.h" 20 #include "extension_ability_info.h" 21 #include "lifecycle_state_info.h" 22 23 namespace OHOS { 24 class IRemoteObject; 25 namespace AAFwk { 26 class Want; 27 } 28 namespace AppExecFwk { 29 struct AbilityInfo; 30 class OHOSApplication; 31 class AbilityHandler; 32 class AbilityLocalRecord; 33 } 34 namespace AbilityRuntime { 35 /** 36 * @brief Responsible for managing and scheduling the life cycle of extension. 37 */ 38 class ExtensionImpl : public std::enable_shared_from_this<ExtensionImpl> { 39 public: 40 ExtensionImpl() = default; 41 virtual ~ExtensionImpl(); 42 43 /** 44 * @brief Init the object. 45 * 46 * @param application the application info. 47 * @param record the extension record. 48 * @param extension the extension object. 49 * @param handler the extension handler. 50 * @param token the remote token. 51 */ 52 void Init(const std::shared_ptr<AppExecFwk::OHOSApplication> &application, 53 const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record, 54 std::shared_ptr<Extension> &extension, 55 std::shared_ptr<AppExecFwk::AbilityHandler> &handler, const sptr<IRemoteObject> &token); 56 57 /** 58 * @brief Connect the Extension. and Calling information back to Extension. 59 * 60 * @param want The Want object to connect to. 61 * @param targetState The terget state. 62 * @param sessionInfo Indicates the sessionInfo. 63 * 64 */ 65 virtual void HandleExtensionTransaction(const Want &want, const AAFwk::LifeCycleStateInfo &targetState, 66 sptr<AAFwk::SessionInfo> sessionInfo = nullptr); 67 68 /** 69 * @brief scheduling update configuration of extension. 70 * 71 * @param config Configuration 72 */ 73 void ScheduleUpdateConfiguration(const AppExecFwk::Configuration &config); 74 75 /** 76 * @brief Notify current memory level. 77 * 78 * @param level Current memory level. 79 */ 80 void NotifyMemoryLevel(int level); 81 82 /** 83 * @brief Connect the Extension. and Calling information back to Extension. 84 * 85 * @param want The Want object to connect to. 86 * 87 */ 88 sptr<IRemoteObject> ConnectExtension(const Want &want); 89 90 /** 91 * @brief Connect the Extension. and Calling information back to Extension. 92 * 93 * @param want The Want object to connect to. 94 * @param isAsyncCallback Indicates whether it is an asynchronous lifecycle callback 95 */ 96 sptr<IRemoteObject> ConnectExtension(const Want &want, bool &isAsyncCallback); 97 98 /** 99 * @brief The callback of connect. 100 */ 101 void ConnectExtensionCallback(sptr<IRemoteObject> &service); 102 103 /** 104 * @brief Disconnects the connected object. 105 * 106 * @param want The Want object to disconnect to. 107 */ 108 void DisconnectExtension(const Want &want); 109 110 /** 111 * @brief Disconnects the connected object. 112 * 113 * @param want The Want object to disconnect to. 114 * @param isAsyncCallback Indicates whether it is an asynchronous lifecycle callback 115 */ 116 void DisconnectExtension(const Want &want, bool &isAsyncCallback); 117 118 /** 119 * @brief The callback of disconnect. 120 */ 121 void DisconnectExtensionCallback(); 122 123 /** 124 * @brief Command the Extension. and Calling information back to Extension. 125 * 126 * @param want The Want object to command to. 127 * 128 * * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being 129 * destroyed, and the value false indicates a normal startup. 130 * 131 * @param startId Indicates the number of times the Service Extension has been started. The startId is incremented 132 * by 1 every time the Extension is started. For example, if the Extension has been started for six times, the value 133 * of startId is 6. 134 */ 135 void CommandExtension(const Want &want, bool restart, int startId); 136 137 /** 138 * @brief Handle insight intent. 139 * 140 * @param want The Want object with insight intent to handle. 141 */ 142 bool HandleInsightIntent(const Want &want); 143 144 void CommandExtensionWindow(const Want &want, const sptr<AAFwk::SessionInfo> &sessionInfo, 145 AAFwk::WindowCommand winCmd); 146 147 /* 148 * SendResult, Send result to app when extension ability is terminated with result want. 149 * 150 * @param requestCode, the requestCode of the extension ability to start. 151 * @param resultCode, the resultCode of the extension ability to terminate. 152 * @param resultData, the want of the extension ability to terminate. 153 */ 154 void SendResult(int requestCode, int resultCode, const Want &resultData); 155 156 /** 157 * @brief Save information about ability launch. 158 * 159 * @param launchParam Used to save information about ability launch param. 160 */ 161 void SetLaunchParam(const AAFwk::LaunchParam &launchParam); 162 163 protected: 164 /** 165 * @brief Toggles the lifecycle status of Extension to AAFwk::ABILITY_STATE_INACTIVE. And notifies the application 166 * that it belongs to of the lifecycle status. 167 * 168 * @param want The Want object to switch the life cycle. 169 * @param sessionInfo Indicates the sessionInfo. 170 */ 171 void Start(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo = nullptr); 172 173 /** 174 * @brief Toggles the lifecycle status of Extension to AAFwk::ABILITY_STATE_INITIAL. And notifies the application 175 * that it belongs to of the lifecycle status. 176 * 177 */ 178 void Stop(); 179 /** 180 * @brief Toggles the lifecycle status of Extension to AAFwk::ABILITY_STATE_INITIAL. And notifies the application 181 * that it belongs to of the lifecycle status. 182 * @param isAsyncCallback Indicates whether it is an asynchronous lifecycle callback 183 * @param want Indicates want. 184 * @param sessionInfo Indicates the sessionInfo, nullptr when not uiextension. 185 */ 186 void Stop(bool &isAsyncCallback, const Want &want, sptr<AAFwk::SessionInfo> sessionInfo); 187 void AbilityTransactionCallback(const AAFwk::AbilityLifeCycleState &state); 188 189 /** 190 * @brief Toggles the lifecycle status of Extension to AAFwk::ABILITY_STATE_INACTIVE. And notifies the application 191 * that it belongs to of the lifecycle status. 192 * 193 * @param want The Want object to switch the life cycle. 194 */ 195 void Foreground(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo); 196 197 /** 198 * @brief Toggles the lifecycle status of Extension to AAFwk::ABILITY_STATE_BACKGROUND. And notifies the 199 * application that it belongs to of the lifecycle status. 200 * @param want Indicates want. 201 * @param sessionInfo Indicates the sessionInfo, nullptr when not uiextension. 202 */ 203 void Background(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo); 204 205 private: 206 inline bool UIExtensionAbilityExecuteInsightIntent(const Want &want); 207 208 void PrintTokenInfo() const; 209 210 int lifecycleState_ = AAFwk::ABILITY_STATE_INITIAL; 211 sptr<IRemoteObject> token_; 212 std::shared_ptr<Extension> extension_; 213 bool skipCommandExtensionWithIntent_ = false; 214 AppExecFwk::ExtensionAbilityType extensionType_ = AppExecFwk::ExtensionAbilityType::UNSPECIFIED; 215 216 class ExtensionWindowLifeCycleImpl : public Rosen::IWindowLifeCycle { 217 public: ExtensionWindowLifeCycleImpl(const sptr<IRemoteObject> & token,const std::shared_ptr<ExtensionImpl> & owner)218 ExtensionWindowLifeCycleImpl(const sptr<IRemoteObject>& token, const std::shared_ptr<ExtensionImpl>& owner) 219 : token_(token), owner_(owner) {} ~ExtensionWindowLifeCycleImpl()220 virtual ~ExtensionWindowLifeCycleImpl() {} 221 void AfterForeground() override; 222 void AfterBackground() override; 223 void AfterActive() override; 224 void AfterInactive() override; 225 private: 226 sptr<IRemoteObject> token_ = nullptr; 227 std::weak_ptr<ExtensionImpl> owner_; 228 }; 229 }; 230 } 231 } 232 #endif // OHOS_ABILITY_RUNTIME_EXTENSION_IMPL_H 233