1 /* 2 * Copyright (c) 2021-2023 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_ABILITY_CONTEXT_IMPL_H 17 #define OHOS_ABILITY_RUNTIME_ABILITY_CONTEXT_IMPL_H 18 19 #include "ability_context.h" 20 21 #include <uv.h> 22 23 #include "context_impl.h" 24 #include "configuration.h" 25 #include "local_call_container.h" 26 27 namespace OHOS { 28 namespace AbilityRuntime { 29 class AbilityContextImpl : public AbilityContext { 30 public: 31 AbilityContextImpl() = default; 32 virtual ~AbilityContextImpl() = default; 33 34 Global::Resource::DeviceType GetDeviceType() const override; 35 std::string GetBaseDir() const override; 36 std::string GetBundleCodeDir() override; 37 std::string GetCacheDir() override; 38 std::string GetTempDir() override; 39 std::string GetResourceDir() override; 40 std::string GetFilesDir() override; 41 bool IsUpdatingConfigurations() override; 42 bool PrintDrawnCompleted() override; 43 std::string GetDatabaseDir() override; 44 std::string GetGroupDir(std::string groupId) override; 45 std::string GetPreferencesDir() override; 46 std::string GetDistributedFilesDir() override; 47 std::string GetCloudFileDir() override; 48 int32_t GetSystemDatabaseDir(const std::string &groupId, bool checkExist, std::string &databaseDir) override; 49 int32_t GetSystemPreferencesDir(const std::string &groupId, bool checkExist, std::string &preferencesDir) override; 50 void SwitchArea(int mode) override; 51 int GetArea() override; 52 std::string GetBundleName() const override; 53 std::shared_ptr<AppExecFwk::ApplicationInfo> GetApplicationInfo() const override; 54 std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const override; 55 std::shared_ptr<Context> CreateBundleContext(const std::string &bundleName) override; 56 std::shared_ptr<Context> CreateModuleContext(const std::string &moduleName) override; 57 std::shared_ptr<Context> CreateModuleContext(const std::string &bundleName, const std::string &moduleName) override; 58 std::shared_ptr<Global::Resource::ResourceManager> CreateModuleResourceManager( 59 const std::string &bundleName, const std::string &moduleName) override; 60 int32_t CreateSystemHspModuleResourceManager(const std::string &bundleName, 61 const std::string &moduleName, std::shared_ptr<Global::Resource::ResourceManager> &resourceManager) override; 62 63 std::string GetBundleCodePath() const override; 64 ErrCode StartAbility(const AAFwk::Want &want, int requestCode) override; 65 ErrCode StartAbilityWithAccount(const AAFwk::Want &want, int accountId, int requestCode) override; 66 ErrCode StartAbility(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions, int requestCode) override; 67 ErrCode StartAbilityAsCaller(const AAFwk::Want &want, int requestCode) override; 68 ErrCode StartAbilityAsCaller(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions, 69 int requestCode) override; 70 ErrCode StartAbilityWithAccount( 71 const AAFwk::Want &want, int accountId, const AAFwk::StartOptions &startOptions, int requestCode) override; 72 ErrCode StartAbilityForResult(const AAFwk::Want &want, int requestCode, RuntimeTask &&task) override; 73 ErrCode StartAbilityForResultWithAccount( 74 const AAFwk::Want &want, int accountId, int requestCode, RuntimeTask &&task) override; 75 ErrCode StartAbilityForResultWithAccount(const AAFwk::Want &want, int accountId, 76 const AAFwk::StartOptions &startOptions, int requestCode, RuntimeTask &&task) override; 77 ErrCode StartAbilityForResult(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions, 78 int requestCode, RuntimeTask &&task) override; 79 ErrCode StartServiceExtensionAbility(const Want &want, int32_t accountId = -1) override; 80 ErrCode StopServiceExtensionAbility(const Want& want, int32_t accountId = -1) override; 81 ErrCode TerminateAbilityWithResult(const AAFwk::Want &want, int resultCode) override; 82 ErrCode BackToCallerAbilityWithResult(const AAFwk::Want &want, int resultCode, int64_t requestCode) override; 83 void OnAbilityResult(int requestCode, int resultCode, const AAFwk::Want &resultData) override; 84 ErrCode ConnectAbility(const AAFwk::Want &want, 85 const sptr<AbilityConnectCallback> &connectCallback) override; 86 ErrCode ConnectAbilityWithAccount(const AAFwk::Want &want, int accountId, 87 const sptr<AbilityConnectCallback> &connectCallback) override; 88 void DisconnectAbility(const AAFwk::Want &want, const sptr<AbilityConnectCallback> &connectCallback, 89 int32_t accountId = -1) override; 90 std::shared_ptr<AppExecFwk::HapModuleInfo> GetHapModuleInfo() const override; 91 std::shared_ptr<AppExecFwk::AbilityInfo> GetAbilityInfo() const override; 92 void MinimizeAbility(bool fromUser = false) override; 93 94 ErrCode OnBackPressedCallBack(bool &needMoveToBackground) override; 95 96 ErrCode MoveAbilityToBackground() override; 97 98 ErrCode MoveUIAbilityToBackground() override; 99 100 ErrCode TerminateSelf() override; 101 102 ErrCode CloseAbility() override; 103 104 sptr<IRemoteObject> GetToken() override; 105 106 ErrCode RestoreWindowStage(napi_env env, napi_value contentStorage) override; 107 108 void SetStageContext(const std::shared_ptr<AbilityRuntime::Context> &stageContext); 109 110 /** 111 * @brief Set the Ability Info object 112 * 113 * set ability info to ability context 114 */ 115 void SetAbilityInfo(const std::shared_ptr<AppExecFwk::AbilityInfo> &abilityInfo); 116 117 /** 118 * @brief Attachs ability's token. 119 * 120 * @param token The token represents ability. 121 */ SetToken(const sptr<IRemoteObject> & token)122 void SetToken(const sptr<IRemoteObject> &token) override 123 { 124 token_ = token; 125 } 126 127 /** 128 * @brief Get ContentStorage. 129 * 130 * @return Returns the ContentStorage. 131 */ GetContentStorage()132 std::unique_ptr<NativeReference>& GetContentStorage() override 133 { 134 return contentStorage_; 135 } 136 137 /** 138 * @brief Get LocalCallContainer. 139 * 140 * @return Returns the LocalCallContainer. 141 */ GetLocalCallContainer()142 std::shared_ptr<LocalCallContainer> GetLocalCallContainer() override 143 { 144 return localCallContainer_; 145 } 146 147 void SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &config) override; 148 149 std::shared_ptr<AppExecFwk::Configuration> GetConfiguration() const override; 150 151 /** 152 * call function by callback objectS 153 * 154 * @param want Request info for ability. 155 * @param callback Indicates the callback object. 156 * @param accountId Indicates the account to start. 157 * 158 * @return Returns zero on success, others on failure. 159 */ 160 ErrCode StartAbilityByCall(const AAFwk::Want& want, const std::shared_ptr<CallerCallBack> &callback, 161 int32_t accountId = DEFAULT_INVAL_VALUE) override; 162 163 /** 164 * caller release by callback object 165 * 166 * @param callback Indicates the callback object. 167 * 168 * @return Returns zero on success, others on failure. 169 */ 170 ErrCode ReleaseCall(const std::shared_ptr<CallerCallBack> &callback) override; 171 172 /** 173 * clear failed call connection by callback object 174 * 175 * @param callback Indicates the callback object. 176 * 177 * @return void. 178 */ 179 void ClearFailedCallConnection(const std::shared_ptr<CallerCallBack> &callback) override; 180 181 /** 182 * register ability callback 183 * 184 * @param abilityCallback Indicates the abilityCallback object. 185 */ 186 void RegisterAbilityCallback(std::weak_ptr<AppExecFwk::IAbilityCallback> abilityCallback) override; 187 IsTerminating()188 bool IsTerminating() override 189 { 190 return isTerminating_.load(); 191 } 192 193 void SetWeakSessionToken(const wptr<IRemoteObject>& sessionToken) override; 194 void SetAbilityRecordId(int32_t abilityRecordId) override; 195 int32_t GetAbilityRecordId() override; 196 SetTerminating(bool state)197 void SetTerminating(bool state) override 198 { 199 isTerminating_.store(state); 200 } 201 202 ErrCode RequestDialogService(napi_env env, AAFwk::Want &want, RequestDialogResultTask &&task) override; 203 204 ErrCode RequestDialogService(AAFwk::Want &want, RequestDialogResultTask &&task) override; 205 206 ErrCode ReportDrawnCompleted() override; 207 208 ErrCode GetMissionId(int32_t &missionId) override; 209 210 /** 211 * @brief Set mission continue state of this ability. 212 * 213 * @param state the mission continuation state of this ability. 214 * @return Returns ERR_OK if success. 215 */ 216 ErrCode SetMissionContinueState(const AAFwk::ContinueState &state) override; 217 218 ErrCode StartAbilityByType(const std::string &type, 219 AAFwk::WantParams &wantParam, const std::shared_ptr<JsUIExtensionCallback> &uiExtensionCallbacks) override; 220 221 ErrCode RequestModalUIExtension(const Want &want) override; 222 223 ErrCode ChangeAbilityVisibility(bool isShow) override; 224 225 ErrCode AddFreeInstallObserver(const sptr<AbilityRuntime::IFreeInstallObserver> &observer) override; 226 227 ErrCode OpenLink(const AAFwk::Want& want, int requestCode) override; 228 229 ErrCode OpenAtomicService(AAFwk::Want& want, const AAFwk::StartOptions &options, int requestCode, 230 RuntimeTask &&task) override; 231 232 void RegisterAbilityLifecycleObserver(const std::shared_ptr<AppExecFwk::ILifecycleObserver> &observer) override; 233 234 void UnregisterAbilityLifecycleObserver(const std::shared_ptr<AppExecFwk::ILifecycleObserver> &observer) override; 235 236 void InsertResultCallbackTask(int requestCode, RuntimeTask&& task) override; 237 238 void RemoveResultCallbackTask(int requestCode) override; 239 240 std::shared_ptr<AAFwk::Want> GetWant() override; 241 242 #ifdef SUPPORT_GRAPHICS 243 /** 244 * @brief Set mission label of this ability. 245 * 246 * @param label the label of this ability. 247 * @return Returns ERR_OK if success. 248 */ 249 ErrCode SetMissionLabel(const std::string &label) override; 250 251 /** 252 * @brief Set mission icon of this ability. 253 * 254 * @param icon the icon of this ability. 255 * @return Returns ERR_OK if success. 256 */ 257 ErrCode SetMissionIcon(const std::shared_ptr<OHOS::Media::PixelMap> &icon) override; 258 259 /** 260 * @brief get current window mode. 261 * 262 * @return Returns the current window mode. 263 */ 264 int GetCurrentWindowMode() override; 265 266 /** 267 * @brief Get window rectangle of this ability. 268 * 269 * @param the left position of window rectangle. 270 * @param the top position of window rectangle. 271 * @param the width position of window rectangle. 272 * @param the height position of window rectangle. 273 */ 274 void GetWindowRect(int32_t &left, int32_t &top, int32_t &width, int32_t &height) override; 275 276 /** 277 * @brief Get ui content object. 278 * 279 * @return UIContent object of ACE. 280 */ 281 Ace::UIContent* GetUIContent() override; 282 283 /** 284 * @brief create modal UIExtension. 285 * @param want Create modal UIExtension with want object. 286 */ 287 ErrCode CreateModalUIExtensionWithApp(const Want &want) override; 288 void EraseUIExtension(int32_t sessionId) override; 289 bool IsUIExtensionExist(const AAFwk::Want &want); 290 #endif 291 292 private: 293 sptr<IRemoteObject> token_ = nullptr; 294 std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo_ = nullptr; 295 std::shared_ptr<AbilityRuntime::Context> stageContext_ = nullptr; 296 std::map<int, RuntimeTask> resultCallbacks_; 297 std::unique_ptr<NativeReference> contentStorage_ = nullptr; 298 std::shared_ptr<AppExecFwk::Configuration> config_ = nullptr; 299 std::shared_ptr<LocalCallContainer> localCallContainer_ = nullptr; 300 std::weak_ptr<AppExecFwk::IAbilityCallback> abilityCallback_; 301 std::atomic<bool> isTerminating_ = false; 302 int32_t missionId_ = -1; 303 int32_t abilityRecordId_ = 0; 304 std::mutex sessionTokenMutex_; 305 wptr<IRemoteObject> sessionToken_; 306 std::mutex uiExtensionMutex_; 307 std::map<int32_t, Want> uiExtensionMap_; 308 309 static void RequestDialogResultJSThreadWorker(uv_work_t* work, int status); 310 void OnAbilityResultInner(int requestCode, int resultCode, const AAFwk::Want &resultData); 311 sptr<IRemoteObject> GetSessionToken(); 312 }; 313 } // namespace AbilityRuntime 314 } // namespace OHOS 315 #endif // OHOS_ABILITY_RUNTIME_ABILITY_CONTEXT_IMPL_H 316