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