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 GetFilesDir() override; 40 bool IsUpdatingConfigurations() override; 41 bool PrintDrawnCompleted() override; 42 std::string GetDatabaseDir() override; 43 std::string GetGroupDir(std::string groupId) override; 44 std::string GetPreferencesDir() override; 45 std::string GetDistributedFilesDir() override; 46 int32_t GetSystemDatabaseDir(const std::string &groupId, bool checkExist, std::string &databaseDir) override; 47 int32_t GetSystemPreferencesDir(const std::string &groupId, bool checkExist, std::string &preferencesDir) override; 48 void SwitchArea(int mode) override; 49 int GetArea() override; 50 std::string GetBundleName() const override; 51 std::shared_ptr<AppExecFwk::ApplicationInfo> GetApplicationInfo() const override; 52 std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const override; 53 std::shared_ptr<Context> CreateBundleContext(const std::string &bundleName) override; 54 std::shared_ptr<Context> CreateModuleContext(const std::string &moduleName) override; 55 std::shared_ptr<Context> CreateModuleContext(const std::string &bundleName, const std::string &moduleName) override; 56 57 std::string GetBundleCodePath() const override; 58 ErrCode StartAbility(const AAFwk::Want &want, int requestCode) override; 59 ErrCode StartAbilityWithAccount(const AAFwk::Want &want, int accountId, int requestCode) override; 60 ErrCode StartAbility(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions, int requestCode) override; 61 ErrCode StartAbilityAsCaller(const AAFwk::Want &want, int requestCode) override; 62 ErrCode StartAbilityAsCaller(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions, 63 int requestCode) override; 64 ErrCode StartAbilityWithAccount( 65 const AAFwk::Want &want, int accountId, const AAFwk::StartOptions &startOptions, int requestCode) override; 66 ErrCode StartAbilityForResult(const AAFwk::Want &want, int requestCode, RuntimeTask &&task) override; 67 ErrCode StartAbilityForResultWithAccount( 68 const AAFwk::Want &want, int accountId, int requestCode, RuntimeTask &&task) override; 69 ErrCode StartAbilityForResultWithAccount(const AAFwk::Want &want, int accountId, 70 const AAFwk::StartOptions &startOptions, int requestCode, RuntimeTask &&task) override; 71 ErrCode StartAbilityForResult(const AAFwk::Want &want, const AAFwk::StartOptions &startOptions, 72 int requestCode, RuntimeTask &&task) override; 73 ErrCode StartServiceExtensionAbility(const Want &want, int32_t accountId = -1) override; 74 ErrCode StopServiceExtensionAbility(const Want& want, int32_t accountId = -1) override; 75 ErrCode TerminateAbilityWithResult(const AAFwk::Want &want, int resultCode) override; 76 void OnAbilityResult(int requestCode, int resultCode, const AAFwk::Want &resultData) override; 77 ErrCode ConnectAbility(const AAFwk::Want &want, 78 const sptr<AbilityConnectCallback> &connectCallback) override; 79 ErrCode ConnectAbilityWithAccount(const AAFwk::Want &want, int accountId, 80 const sptr<AbilityConnectCallback> &connectCallback) override; 81 void DisconnectAbility(const AAFwk::Want &want, 82 const sptr<AbilityConnectCallback> &connectCallback) override; 83 std::shared_ptr<AppExecFwk::HapModuleInfo> GetHapModuleInfo() const override; 84 std::shared_ptr<AppExecFwk::AbilityInfo> GetAbilityInfo() const override; 85 void MinimizeAbility(bool fromUser = false) override; 86 87 ErrCode OnBackPressedCallBack(bool &needMoveToBackground) override; 88 89 ErrCode MoveAbilityToBackground() override; 90 91 ErrCode TerminateSelf() override; 92 93 ErrCode CloseAbility() override; 94 95 sptr<IRemoteObject> GetToken() override; 96 97 ErrCode RestoreWindowStage(NativeEngine& engine, NativeValue* contentStorage) override; 98 99 void SetStageContext(const std::shared_ptr<AbilityRuntime::Context> &stageContext); 100 101 /** 102 * @brief Set the Ability Info object 103 * 104 * set ability info to ability context 105 */ 106 void SetAbilityInfo(const std::shared_ptr<AppExecFwk::AbilityInfo> &abilityInfo); 107 108 /** 109 * @brief Attachs ability's token. 110 * 111 * @param token The token represents ability. 112 */ SetToken(const sptr<IRemoteObject> & token)113 void SetToken(const sptr<IRemoteObject> &token) override 114 { 115 token_ = token; 116 } 117 118 /** 119 * @brief Get ContentStorage. 120 * 121 * @return Returns the ContentStorage. 122 */ GetContentStorage()123 std::unique_ptr<NativeReference>& GetContentStorage() override 124 { 125 return contentStorage_; 126 } 127 128 /** 129 * @brief Get LocalCallContainer. 130 * 131 * @return Returns the LocalCallContainer. 132 */ GetLocalCallContainer()133 std::shared_ptr<LocalCallContainer> GetLocalCallContainer() override 134 { 135 return localCallContainer_; 136 } 137 138 void SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &config) override; 139 140 std::shared_ptr<AppExecFwk::Configuration> GetConfiguration() const override; 141 142 /** 143 * call function by callback objectS 144 * 145 * @param want Request info for ability. 146 * @param callback Indicates the callback object. 147 * @param accountId Indicates the account to start. 148 * 149 * @return Returns zero on success, others on failure. 150 */ 151 ErrCode StartAbilityByCall(const AAFwk::Want& want, const std::shared_ptr<CallerCallBack> &callback, 152 int32_t accountId = DEFAULT_INVAL_VALUE) override; 153 154 /** 155 * caller release by callback object 156 * 157 * @param callback Indicates the callback object. 158 * 159 * @return Returns zero on success, others on failure. 160 */ 161 ErrCode ReleaseCall(const std::shared_ptr<CallerCallBack> &callback) override; 162 163 /** 164 * clear failed call connection by callback object 165 * 166 * @param callback Indicates the callback object. 167 * 168 * @return void. 169 */ 170 void ClearFailedCallConnection(const std::shared_ptr<CallerCallBack> &callback) override; 171 172 /** 173 * register ability callback 174 * 175 * @param abilityCallback Indicates the abilityCallback object. 176 */ 177 void RegisterAbilityCallback(std::weak_ptr<AppExecFwk::IAbilityCallback> abilityCallback) override; 178 IsTerminating()179 bool IsTerminating() override 180 { 181 return isTerminating_; 182 } 183 184 void SetWeakSessionToken(const wptr<IRemoteObject>& sessionToken) override; 185 SetTerminating(bool state)186 void SetTerminating(bool state) override 187 { 188 isTerminating_ = state; 189 } 190 191 ErrCode RequestDialogService(NativeEngine &engine, AAFwk::Want &want, RequestDialogResultTask &&task) override; 192 193 ErrCode ReportDrawnCompleted() override; 194 195 ErrCode GetMissionId(int32_t &missionId) override; 196 197 /** 198 * @brief Set mission continue state of this ability. 199 * 200 * @param state the mission continuation state of this ability. 201 * @return Returns ERR_OK if success. 202 */ 203 ErrCode SetMissionContinueState(const AAFwk::ContinueState &state) override; 204 205 #ifdef SUPPORT_GRAPHICS 206 /** 207 * @brief Set mission label of this ability. 208 * 209 * @param label the label of this ability. 210 * @return Returns ERR_OK if success. 211 */ 212 ErrCode SetMissionLabel(const std::string &label) override; 213 214 /** 215 * @brief Set mission icon of this ability. 216 * 217 * @param icon the icon of this ability. 218 * @return Returns ERR_OK if success. 219 */ 220 ErrCode SetMissionIcon(const std::shared_ptr<OHOS::Media::PixelMap> &icon) override; 221 222 /** 223 * @brief get current window mode. 224 * 225 * @return Returns the current window mode. 226 */ 227 int GetCurrentWindowMode() override; 228 229 /** 230 * @brief Get window rectangle of this ability. 231 * 232 * @param the left position of window rectangle. 233 * @param the top position of window rectangle. 234 * @param the width position of window rectangle. 235 * @param the height position of window rectangle. 236 */ 237 void GetWindowRect(int32_t &left, int32_t &top, int32_t &width, int32_t &height) override; 238 239 /** 240 * @brief Get ui content object. 241 * 242 * @return UIContent object of ACE. 243 */ 244 Ace::UIContent* GetUIContent() override; 245 #endif 246 247 private: 248 sptr<IRemoteObject> token_ = nullptr; 249 std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo_ = nullptr; 250 std::shared_ptr<AbilityRuntime::Context> stageContext_ = nullptr; 251 std::map<int, RuntimeTask> resultCallbacks_; 252 std::unique_ptr<NativeReference> contentStorage_ = nullptr; 253 std::shared_ptr<AppExecFwk::Configuration> config_ = nullptr; 254 std::shared_ptr<LocalCallContainer> localCallContainer_ = nullptr; 255 std::weak_ptr<AppExecFwk::IAbilityCallback> abilityCallback_; 256 bool isTerminating_ = false; 257 int32_t missionId_ = -1; 258 wptr<IRemoteObject> sessionToken_; 259 260 static void RequestDialogResultJSThreadWorker(uv_work_t* work, int status); 261 void OnAbilityResultInner(int requestCode, int resultCode, const AAFwk::Want &resultData); 262 }; 263 } // namespace AbilityRuntime 264 } // namespace OHOS 265 #endif // OHOS_ABILITY_RUNTIME_ABILITY_CONTEXT_IMPL_H 266