1 /* 2 * Copyright (c) 2023-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_UI_EXTENSION_CONTEXT_H 17 #define OHOS_ABILITY_RUNTIME_UI_EXTENSION_CONTEXT_H 18 19 #include <map> 20 21 #include "ability_connect_callback.h" 22 #include "extension_context.h" 23 #include "free_install_observer_interface.h" 24 #include "start_options.h" 25 #include "want.h" 26 #include "window.h" 27 28 namespace OHOS { 29 namespace Ace { 30 class UIContent; 31 } 32 namespace AbilityRuntime { 33 using RuntimeTask = std::function<void(int, const AAFwk::Want &, bool)>; 34 /** 35 * @brief context supply for UIExtension 36 * 37 */ 38 class UIExtensionContext : public ExtensionContext { 39 public: 40 UIExtensionContext() = default; 41 virtual ~UIExtensionContext() = default; 42 43 /** 44 * @brief Starts a new ability. 45 * An ability using the AbilityInfo.AbilityType.EXTENSION or AbilityInfo.AbilityType.PAGE template uses this method 46 * to start a specific ability. The system locates the target ability from installed abilities based on the value 47 * of the want parameter and then starts it. You can specify the ability to start using the want parameter. 48 * 49 * @param want Indicates the Want containing information about the target ability to start. 50 * 51 * @return errCode ERR_OK on success, others on failure. 52 */ 53 virtual ErrCode StartAbility(const AAFwk::Want &want) const; 54 virtual ErrCode StartAbility(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions) const; 55 virtual ErrCode StartAbility(const AAFwk::Want &want, int requestCode) const; 56 /** 57 * @brief Destroys the current ui extension ability. 58 * 59 * @return errCode ERR_OK on success, others on failure. 60 */ 61 virtual ErrCode TerminateSelf(); 62 /** 63 * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template. 64 * 65 * @param want Indicates the want containing information about the ability to connect 66 * 67 * @param conn Indicates the callback object when the target ability is connected. 68 * 69 * @return Returns zero on success, others on failure. 70 */ 71 virtual ErrCode ConnectAbility( 72 const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback) const; 73 74 /** 75 * @brief Disconnects the current ability from an ability. 76 * 77 * @param conn Indicates the IAbilityConnection callback object passed by connectAbility after the connection 78 * is set up. The IAbilityConnection object uniquely identifies a connection between two abilities. 79 * 80 * @return errCode ERR_OK on success, others on failure. 81 */ 82 virtual ErrCode DisconnectAbility( 83 const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback) const; 84 /** 85 * Start other ability for result. 86 * 87 * @param want Information of other ability. 88 * @param startOptions Indicates the StartOptions containing service side information about the target ability to 89 * start. 90 * @param requestCode Request code for abilityMS to return result. 91 * @param task Represent std::function<void(int, const AAFwk::Want &, bool)>. 92 * 93 * @return errCode ERR_OK on success, others on failure. 94 */ 95 virtual ErrCode StartAbilityForResult(const AAFwk::Want &want, int requestCode, RuntimeTask &&task); 96 virtual ErrCode StartAbilityForResult( 97 const AAFwk::Want &want, const AAFwk::StartOptions &startOptions, int requestCode, RuntimeTask &&task); 98 99 /** 100 * Starts a new ability for result using the original caller information. 101 * 102 * @param want Information of other ability. 103 * @param requestCode Request code for abilityMS to return result. 104 * @param task Represent std::function<void(int, const AAFwk::Want &, bool)>. 105 * 106 * @return errCode ERR_OK on success, others on failure. 107 */ 108 virtual ErrCode StartAbilityForResultAsCaller(const AAFwk::Want &want, int requestCode, RuntimeTask &&task); 109 110 /** 111 * Starts a new ability for result using the original caller information. 112 * 113 * @param want Information of other ability. 114 * @param startOptions Indicates the StartOptions containing service side information about the target ability to 115 * start. 116 * @param requestCode Request code for abilityMS to return result. 117 * @param task Represent std::function<void(int, const AAFwk::Want &, bool)>. 118 * 119 * @return errCode ERR_OK on success, others on failure. 120 */ 121 virtual ErrCode StartAbilityForResultAsCaller( 122 const AAFwk::Want &want, const AAFwk::StartOptions &startOptions, int requestCode, RuntimeTask &&task); 123 124 /** 125 * @brief Called when startAbilityForResult(ohos.aafwk.content.Want,int) is called to start an extension ability 126 * and the result is returned. 127 * @param requestCode Indicates the request code returned after the ability is started. You can define the request 128 * code to identify the results returned by abilities. The value ranges from 0 to 65535. 129 * @param resultCode Indicates the result code returned after the ability is started. You can define the result 130 * code to identify an error. 131 * @param resultData Indicates the data returned after the ability is started. You can define the data returned. The 132 * value can be null. 133 */ 134 virtual void OnAbilityResult(int requestCode, int resultCode, const AAFwk::Want &resultData); 135 136 virtual int GenerateCurRequestCode(); 137 138 virtual ErrCode ReportDrawnCompleted(); 139 140 void SetWindow(sptr<Rosen::Window> window); 141 142 sptr<Rosen::Window> GetWindow(); 143 144 Ace::UIContent* GetUIContent(); 145 146 ErrCode OpenLink(const AAFwk::Want& want, int reuqestCode); 147 148 ErrCode OpenAtomicService(AAFwk::Want& want, const AAFwk::StartOptions &options, int requestCode, 149 RuntimeTask &&task); 150 151 ErrCode AddFreeInstallObserver(const sptr<AbilityRuntime::IFreeInstallObserver> &observer); 152 153 void InsertResultCallbackTask(int requestCode, RuntimeTask&& task); 154 155 void RemoveResultCallbackTask(int requestCode); 156 157 using SelfType = UIExtensionContext; 158 static const size_t CONTEXT_TYPE_ID; 159 160 protected: IsContext(size_t contextTypeId)161 bool IsContext(size_t contextTypeId) override 162 { 163 return contextTypeId == CONTEXT_TYPE_ID || ExtensionContext::IsContext(contextTypeId); 164 } 165 166 private: 167 static int ILLEGAL_REQUEST_CODE; 168 std::map<int, RuntimeTask> resultCallbacks_; 169 170 static int32_t curRequestCode_; 171 static std::mutex requestCodeMutex_; 172 173 sptr<Rosen::Window> window_ = nullptr; 174 175 std::mutex mutexlock_; 176 /** 177 * @brief Get Current Ability Type 178 * 179 * @return Current Ability Type 180 */ 181 OHOS::AppExecFwk::AbilityType GetAbilityInfoType() const; 182 183 void OnAbilityResultInner(int requestCode, int resultCode, const AAFwk::Want &resultData); 184 }; 185 } // namespace AbilityRuntime 186 } // namespace OHOS 187 #endif // OHOS_ABILITY_RUNTIME_UI_EXTENSION_CONTEXT_H 188