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_CONTEXT_CONTAINER_H 17 #define OHOS_ABILITY_RUNTIME_CONTEXT_CONTAINER_H 18 19 #include "context_deal.h" 20 21 namespace OHOS { 22 namespace AppExecFwk { 23 class ContextContainer : public Context { 24 public: 25 ContextContainer() = default; 26 virtual ~ContextContainer() = default; 27 28 /** 29 * Attaches a Context object to the current ability. 30 * Generally, this method is called after Ability is loaded to provide the application context for the current 31 * ability. 32 * 33 * @param base Indicates a Context object. 34 */ 35 void AttachBaseContext(const std::shared_ptr<ContextDeal> &base); 36 37 /** 38 * @brief Detach a attatched context. 39 * 40 */ 41 void DetachBaseContext(); 42 43 /** 44 * Called when getting the ProcessInfo 45 * 46 * @return ProcessInfo 47 */ 48 std::shared_ptr<ProcessInfo> GetProcessInfo() const override; 49 50 /** 51 * Called when setting the ProcessInfo 52 * 53 * @param info ProcessInfo instance 54 */ 55 void SetProcessInfo(const std::shared_ptr<ProcessInfo> &info); 56 57 /** 58 * @brief Obtains information about the current application. The returned application information includes basic 59 * information such as the application name and application permissions. 60 * 61 * @return Returns the ApplicationInfo for the current application. 62 */ 63 std::shared_ptr<ApplicationInfo> GetApplicationInfo() const override; 64 65 /** 66 * @brief Obtains the Context object of the application. 67 * 68 * @return Returns the Context object of the application. 69 */ 70 std::shared_ptr<Context> GetApplicationContext() const override; 71 72 /** 73 * @brief Obtains the path of the package containing the current ability. The returned path contains the resources, 74 * source code, and configuration files of a module. 75 * 76 * @return Returns the path of the package file. 77 */ 78 virtual std::string GetBundleCodePath() override; 79 80 /** 81 * @brief Obtains information about the current ability. 82 * The returned information includes the class name, bundle name, and other information about the current ability. 83 * 84 * @return Returns the AbilityInfo object for the current ability. 85 */ 86 virtual const std::shared_ptr<AbilityInfo> GetAbilityInfo() override; 87 88 /** 89 * @brief Obtains the Context object of the application. 90 * 91 * @return Returns the Context object of the application. 92 */ 93 std::shared_ptr<Context> GetContext() override; 94 95 /** 96 * @brief Obtains an IBundleMgr instance. 97 * You can use this instance to obtain information about the application bundle. 98 * 99 * @return Returns an IBundleMgr instance. 100 */ 101 sptr<IBundleMgr> GetBundleManager() const override; 102 103 /** 104 * @brief Obtains a resource manager. 105 * 106 * @return Returns a ResourceManager object. 107 */ 108 std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const override; 109 110 /** 111 * @brief Obtains the local database path. 112 * If the local database path does not exist, the system creates one and returns the created path. 113 * 114 * @return Returns the local database file. 115 */ 116 std::string GetDatabaseDir() override; 117 118 /** 119 * @brief Obtains the absolute path where all private data files of this application are stored. 120 * 121 * @return Returns the absolute path storing all private data files of this application. 122 */ 123 std::string GetDataDir() override; 124 125 /** 126 * @brief Obtains the directory for storing custom data files of the application. 127 * You can use the returned File object to create and access files in this directory. The files 128 * can be accessible only by the current application. 129 * 130 * @param name Indicates the name of the directory to retrieve. This directory is created as part 131 * of your application data. 132 * @param mode Indicates the file operating mode. The value can be 0 or a combination of MODE_PRIVATE. 133 * 134 * @return Returns a File object for the requested directory. 135 */ 136 std::string GetDir(const std::string &name, int mode) override; 137 138 /** 139 * @brief Obtains the directory for storing files for the application on the device's internal storage. 140 * 141 * @return Returns the application file directory. 142 */ 143 std::string GetFilesDir() override; 144 145 /** 146 * @brief Obtains the bundle name of the current ability. 147 * 148 * @return Returns the bundle name of the current ability. 149 */ 150 std::string GetBundleName() const override; 151 152 /** 153 * @brief Obtains the path of the OHOS Ability Package (HAP} containing this ability. 154 * 155 * @return Returns the path of the HAP containing this ability. 156 */ 157 std::string GetBundleResourcePath() override; 158 159 /** 160 * @brief Obtains an ability manager. 161 * The ability manager provides information about running processes and memory usage of an application. 162 * 163 * @return Returns an IAbilityManager instance. 164 */ 165 sptr<AAFwk::IAbilityManager> GetAbilityManager() override; 166 167 /** 168 * @brief Obtains the type of this application. 169 * 170 * @return Returns system if this application is a system application; 171 * returns normal if it is released in OHOS AppGallery; 172 * returns other if it is released by a third-party vendor; 173 * returns an empty string if the query fails. 174 */ 175 std::string GetAppType() override; 176 177 /** 178 * @brief Sets the pattern of this Context based on the specified pattern ID. 179 * 180 * @param patternId Indicates the resource ID of the pattern to set. 181 */ 182 void SetPattern(int patternId) override; 183 184 /** 185 * @brief Obtains the HapModuleInfo object of the application. 186 * 187 * @return Returns the HapModuleInfo object of the application. 188 */ 189 std::shared_ptr<HapModuleInfo> GetHapModuleInfo() override; 190 191 /** 192 * @brief Obtains the name of the current process. 193 * 194 * @return Returns the current process name. 195 */ 196 std::string GetProcessName() override; 197 198 /** 199 * @brief Creates a Context object for an application with the given bundle name. 200 * 201 * @param bundleName Indicates the bundle name of the application. 202 * @param flag Indicates the flag for creating a Context object. It can be 0, any of 203 * the following values, or any combination of the following values: CONTEXT_IGNORE_SECURITY, 204 * CONTEXT_INCLUDE_CODE, and CONTEXT_RESTRICTED. The value 0 indicates that there is no restriction 205 * on creating contexts for applications. 206 * @param accountId Indicates the account id. 207 * 208 * @return Returns a Context object created for the specified application. 209 */ 210 std::shared_ptr<Context> CreateBundleContext(std::string bundleName, int flag, int accountId = DEFAULT_ACCOUNT_ID); 211 212 /** 213 * @brief Obtains information about the caller of this ability. 214 * 215 * @return Returns the caller information. 216 */ 217 Uri GetCaller() override; 218 219 /** 220 * @brief SetUriString 221 * 222 * @param uri the uri to set. 223 */ 224 void SetUriString(const std::string &uri); 225 226 /** 227 * @brief InitResourceManager 228 * 229 * @param bundleInfo BundleInfo 230 */ 231 void InitResourceManager(BundleInfo &bundleInfo, std::shared_ptr<ContextDeal> &deal); 232 233 /** 234 * @brief Get the string of this Context based on the specified resource ID. 235 * 236 * @param resId Indicates the resource ID of the string to get. 237 * 238 * @return Returns the string of this Context. 239 */ 240 std::string GetString(int resId) override; 241 242 /** 243 * @brief Get the string array of this Context based on the specified resource ID. 244 * 245 * @param resId Indicates the resource ID of the string array to get. 246 * 247 * @return Returns the string array of this Context. 248 */ 249 std::vector<std::string> GetStringArray(int resId) override; 250 251 /** 252 * @brief Get the integer array of this Context based on the specified resource ID. 253 * 254 * @param resId Indicates the resource ID of the integer array to get. 255 * 256 * @return Returns the integer array of this Context. 257 */ 258 std::vector<int> GetIntArray(int resId) override; 259 260 /** 261 * @brief Obtains the theme of this Context. 262 * 263 * @return theme Returns the theme of this Context. 264 */ 265 std::map<std::string, std::string> GetTheme() override; 266 267 /** 268 * @brief Sets the theme of this Context based on the specified theme ID. 269 * 270 * @param themeId Indicates the resource ID of the theme to set. 271 */ 272 void SetTheme(int themeId) override; 273 274 /** 275 * @brief Obtains the pattern of this Context. 276 * 277 * @return getPattern in interface Context 278 */ 279 std::map<std::string, std::string> GetPattern() override; 280 281 /** 282 * @brief Get the color of this Context based on the specified resource ID. 283 * 284 * @param resId Indicates the resource ID of the color to get. 285 * 286 * @return Returns the color value of this Context. 287 */ 288 int GetColor(int resId) override; 289 290 /** 291 * @brief Obtains the theme id of this Context. 292 * 293 * @return int Returns the theme id of this Context. 294 */ 295 int GetThemeId() override; 296 297 /** 298 * @brief Obtains the current display orientation of this ability. 299 * 300 * @return Returns the current display orientation. 301 */ 302 int GetDisplayOrientation() override; 303 304 /** 305 * @brief Obtains the path storing the preference file of the application. 306 * If the preference file path does not exist, the system creates one and returns the created path. 307 * 308 * @return Returns the preference file path . 309 */ 310 std::string GetPreferencesDir() override; 311 312 /** 313 * @brief Set color mode 314 * 315 * @param the value of color mode. 316 */ 317 void SetColorMode(int mode) override; 318 319 /** 320 * @brief Obtains color mode. 321 * 322 * @return Returns the color mode value. 323 */ 324 int GetColorMode() override; 325 326 /** 327 * @brief Obtains the unique ID of the mission containing this ability. 328 * 329 * @return Returns the unique mission ID. 330 */ 331 int GetMissionId() override; 332 333 /** 334 * @brief Obtains the lifecycle state info. 335 * 336 * @return Returns the lifecycle state info. 337 */ 338 AAFwk::LifeCycleStateInfo GetLifeCycleStateInfo() const; 339 340 /** 341 * @brief Set the LifeCycleStateInfo to the deal. 342 * 343 * @param info the info to set. 344 */ 345 void SetLifeCycleStateInfo(const AAFwk::LifeCycleStateInfo &info); 346 private: 347 std::shared_ptr<ContextDeal> baseContext_ = nullptr; 348 std::shared_ptr<ProcessInfo> processInfo_ = nullptr; 349 AAFwk::LifeCycleStateInfo lifeCycleStateInfo_; 350 std::string uriString_ = ""; 351 }; 352 } // namespace AppExecFwk 353 } // namespace OHOS 354 #endif // OHOS_ABILITY_RUNTIME_CONTEXT_CONTAINER_H 355