1 /* 2 * Copyright (c) 2021-2022 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_IMPL_H 17 #define OHOS_ABILITY_RUNTIME_CONTEXT_IMPL_H 18 19 #include "context.h" 20 21 #include "configuration.h" 22 #include "bundle_mgr_interface.h" 23 24 namespace OHOS { 25 namespace AppExecFwk { 26 struct RunningProcessInfo; 27 class BundleMgrHelper; 28 } 29 namespace AbilityRuntime { 30 class ContextImpl : public Context { 31 public: 32 ContextImpl() = default; 33 virtual ~ContextImpl() = default; 34 35 /** 36 * @brief Obtains the bundle name of the current ability. 37 * 38 * @return Returns the bundle name of the current ability. 39 */ 40 std::string GetBundleName() const override; 41 42 /** 43 * @brief Obtains the path of the package containing the current ability. The returned path contains the resources, 44 * source code, and configuration files of a module. 45 * 46 * @return Returns the path of the package file. 47 */ 48 std::string GetBundleCodeDir() override; 49 50 /** 51 * @brief Obtains the application-specific cache directory on the device's internal storage. The system 52 * automatically deletes files from the cache directory if disk space is required elsewhere on the device. 53 * Older files are always deleted first. 54 * 55 * @return Returns the application-specific cache directory. 56 */ 57 std::string GetCacheDir() override; 58 59 /** 60 * @brief Checks whether the configuration of this ability is changing. 61 * 62 * @return Returns true if the configuration of this ability is changing and false otherwise. 63 */ 64 bool IsUpdatingConfigurations() override; 65 66 /** 67 * @brief Informs the system of the time required for drawing this Page ability. 68 * 69 * @return Returns the notification is successful or fail 70 */ 71 bool PrintDrawnCompleted() override; 72 73 /** 74 * @brief Obtains the temporary directory. 75 * 76 * @return Returns the application temporary directory. 77 */ 78 std::string GetTempDir() override; 79 80 std::string GetResourceDir() override; 81 82 /** 83 * @brief Get all temporary directories. 84 * 85 * @param tempPaths Return all temporary directories of the application. 86 */ 87 virtual void GetAllTempDir(std::vector<std::string> &tempPaths); 88 89 /** 90 * @brief Obtains the directory for storing files for the application on the device's internal storage. 91 * 92 * @return Returns the application file directory. 93 */ 94 std::string GetFilesDir() override; 95 96 /** 97 * @brief Obtains the local database path. 98 * If the local database path does not exist, the system creates one and returns the created path. 99 * 100 * @return Returns the local database file. 101 */ 102 std::string GetDatabaseDir() override; 103 104 /** 105 * @brief Obtains the local system database path. 106 * If the local group database path does not exist, the system creates one and returns the created path. 107 * 108 * @return Returns the local group database file. 109 */ 110 int32_t GetSystemDatabaseDir(const std::string &groupId, bool checkExist, std::string &databaseDir) override; 111 112 /** 113 * @brief Obtains the path storing the storage file of the application. 114 * 115 * @return Returns the local storage file. 116 */ 117 std::string GetPreferencesDir() override; 118 119 /** 120 * @brief Obtains the path storing the system storage file of the application. 121 * 122 * @return Returns the local system storage file. 123 */ 124 int32_t GetSystemPreferencesDir(const std::string &groupId, bool checkExist, std::string &preferencesDir) override; 125 126 /** 127 * @brief Obtains the path storing the group file of the application by the groupId. 128 * 129 * @return Returns the local group file. 130 */ 131 std::string GetGroupDir(std::string groupId) override; 132 133 /** 134 * @brief Obtains the path distributed file of the application 135 * 136 * @return Returns the distributed file. 137 */ 138 std::string GetDistributedFilesDir() override; 139 140 /** 141 * @brief Switch file area 142 * 143 * @param mode file area. 144 */ 145 void SwitchArea(int mode) override; 146 147 /** 148 * @brief Set color mode 149 * 150 * @param colorMode color mode. 151 */ 152 void SetColorMode(int colorMode); 153 154 /** 155 * @brief Set color mode 156 * 157 * @param colorMode color mode. 158 */ 159 void SetLanguage(std::string language); 160 161 /** 162 * @brief clear the application data by app self 163 */ 164 void ClearUpApplicationData(); 165 166 /** 167 * @brief Creates a Context object for a hap with the given module name. 168 * 169 * @param moduleName Indicates the module name of the hap. 170 * 171 * @return Returns a Context object created for the specified hap and app. 172 */ 173 std::shared_ptr<Context> CreateModuleContext(const std::string &moduleName) override; 174 175 /** 176 * @brief Creates a Context object for a hap with the given hap name and app name. 177 * 178 * @param bundleName Indicates the app name of the application. 179 * 180 * @param moduleName Indicates the module name of the hap. 181 * 182 * @return Returns a Context object created for the specified hap and app. 183 */ 184 std::shared_ptr<Context> CreateModuleContext(const std::string &bundleName, const std::string &moduleName) override; 185 186 /** 187 * @brief Get file area 188 * 189 * @return file area. 190 */ 191 int GetArea() override; 192 193 /** 194 * @brief set the ResourceManager. 195 * 196 * @param the ResourceManager has been initialized. 197 */ 198 void SetResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> &resourceManager); 199 200 /** 201 * @brief Obtains a resource manager. 202 * 203 * @return Returns a ResourceManager object. 204 */ 205 std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const override; 206 207 /** 208 * @brief Creates a Context object for an application with the given bundle name. 209 * 210 * @param bundleName Indicates the bundle name of the application. 211 * 212 * @return Returns a Context object created for the specified application. 213 */ 214 std::shared_ptr<Context> CreateBundleContext(const std::string &bundleName) override; 215 216 /** 217 * @brief Creates a ResourceManager object for a hap with the given hap name and app name. 218 * 219 * @param bundleName Indicates the app name of the application. 220 * 221 * @param moduleName Indicates the module name of the hap. 222 * 223 * @return Returns a ResourceManager object created for the specified hap and app. 224 */ 225 std::shared_ptr<Global::Resource::ResourceManager> CreateModuleResourceManager( 226 const std::string &bundleName, const std::string &moduleName) override; 227 228 /** 229 * @brief Obtains an IBundleMgr instance. 230 * You can use this instance to obtain information about the application bundle. 231 * 232 * @return Returns an IBundleMgr instance. 233 */ 234 ErrCode GetBundleManager(); 235 236 /** 237 * @brief Set ApplicationInfo 238 * 239 * @param info ApplicationInfo instance. 240 */ 241 void SetApplicationInfo(const std::shared_ptr<AppExecFwk::ApplicationInfo> &info); 242 243 /** 244 * @brief Obtains information about the current application. The returned application information includes basic 245 * information such as the application name and application permissions. 246 * 247 * @return Returns the ApplicationInfo for the current application. 248 */ 249 std::shared_ptr<AppExecFwk::ApplicationInfo> GetApplicationInfo() const override; 250 251 /** 252 * @brief Set ApplicationInfo 253 * 254 * @param info ApplicationInfo instance. 255 */ 256 void SetParentContext(const std::shared_ptr<Context> &context); 257 258 /** 259 * @brief Obtains the path of the package containing the current ability. The returned path contains the resources, 260 * source code, and configuration files of a module. 261 * 262 * @return Returns the path of the package file. 263 */ 264 std::string GetBundleCodePath() const override; 265 266 /** 267 * @brief Obtains the HapModuleInfo object of the application. 268 * 269 * @return Returns the HapModuleInfo object of the application. 270 */ 271 std::shared_ptr<AppExecFwk::HapModuleInfo> GetHapModuleInfo() const override; 272 273 /** 274 * @brief Set HapModuleInfo 275 * 276 * @param hapModuleInfo HapModuleInfo instance. 277 */ 278 void InitHapModuleInfo(const std::shared_ptr<AppExecFwk::AbilityInfo> &abilityInfo); 279 280 /** 281 * @brief Set HapModuleInfo 282 * 283 * @param hapModuleInfo HapModuleInfo instance. 284 */ 285 void InitHapModuleInfo(const AppExecFwk::HapModuleInfo &hapModuleInfo); 286 287 /** 288 * @brief Set the token witch the app launched. 289 * 290 * @param token The token which the is launched by app. 291 */ 292 void SetToken(const sptr<IRemoteObject> &token) override; 293 294 /** 295 * @brief Get the token witch the app launched. 296 * 297 * @return token The token which the is launched by app. 298 */ 299 sptr<IRemoteObject> GetToken() override; 300 301 /** 302 * @brief Get the token witch the app launched. 303 * 304 * @return token The token which the is launched by app. 305 */ 306 void SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &config); 307 308 /** 309 * @brief Kill process itself 310 * 311 * @return error code 312 */ 313 void KillProcessBySelf(); 314 315 /** 316 * @brief Get running informationfor cuirrent process 317 * 318 * @return error code 319 */ 320 int32_t GetProcessRunningInformation(AppExecFwk::RunningProcessInfo &info); 321 322 /** 323 * @brief Get the token witch the app launched. 324 * 325 * @return token The token which the is launched by app. 326 */ 327 std::shared_ptr<AppExecFwk::Configuration> GetConfiguration() const override; 328 329 /** 330 * @brief Obtains the application base directory on the device's internal storage. 331 * 332 * @return Returns the application base directory. 333 */ 334 std::string GetBaseDir() const override; 335 336 /** 337 * @brief Obtains the Device Type. 338 * 339 * @return Returns the Device Type. 340 */ 341 Global::Resource::DeviceType GetDeviceType() const override; 342 343 static const int EL_DEFAULT = 1; 344 345 protected: 346 sptr<IRemoteObject> token_; 347 348 private: 349 static const int64_t CONTEXT_CREATE_BY_SYSTEM_APP; 350 static const std::string CONTEXT_DATA_APP; 351 static const std::string CONTEXT_BUNDLE; 352 static const std::string CONTEXT_DISTRIBUTEDFILES_BASE_BEFORE; 353 static const std::string CONTEXT_DISTRIBUTEDFILES_BASE_MIDDLE; 354 static const std::string CONTEXT_DISTRIBUTEDFILES; 355 static const std::string CONTEXT_FILE_SEPARATOR; 356 static const std::string CONTEXT_DATA; 357 static const std::string CONTEXT_DATA_STORAGE; 358 static const std::string CONTEXT_BASE; 359 static const std::string CONTEXT_PRIVATE; 360 static const std::string CONTEXT_CACHE; 361 static const std::string CONTEXT_PREFERENCES; 362 static const std::string CONTEXT_GROUP; 363 static const std::string CONTEXT_DATABASE; 364 static const std::string CONTEXT_TEMP; 365 static const std::string CONTEXT_FILES; 366 static const std::string CONTEXT_HAPS; 367 static const std::string CONTEXT_ELS[]; 368 static const std::string CONTEXT_RESOURCE_END; 369 int flags_ = 0x00000000; 370 371 void InitResourceManager(const AppExecFwk::BundleInfo &bundleInfo, const std::shared_ptr<ContextImpl> &appContext, 372 bool currentBundle = false, const std::string &moduleName = ""); 373 bool IsCreateBySystemApp() const; 374 int GetCurrentAccountId() const; 375 void SetFlags(int64_t flags); 376 int GetCurrentActiveAccountId() const; 377 void CreateDirIfNotExist(const std::string& dirPath, const mode_t& mode) const; 378 379 int GetOverlayModuleInfos(const std::string &bundleName, const std::string &moduleName, 380 std::vector<AppExecFwk::OverlayModuleInfo> &overlayModuleInfos); 381 382 void OnOverlayChanged(const EventFwk::CommonEventData &data, 383 const std::shared_ptr<Global::Resource::ResourceManager> &resourceManager, const std::string &bundleName, 384 const std::string &moduleName, const std::string &loadPath); 385 386 std::vector<std::string> GetAddOverlayPaths( 387 const std::vector<AppExecFwk::OverlayModuleInfo> &overlayModuleInfos); 388 389 std::vector<std::string> GetRemoveOverlayPaths( 390 const std::vector<AppExecFwk::OverlayModuleInfo> &overlayModuleInfos); 391 392 void ChangeToLocalPath(const std::string &bundleName, 393 const std::string &sourcDir, std::string &localPath); 394 395 void CreateDirIfNotExistWithCheck(const std::string& dirPath, const mode_t& mode, bool checkExist = true); 396 int32_t GetDatabaseDirWithCheck(bool checkExist, std::string &databaseDir); 397 int32_t GetGroupDatabaseDirWithCheck(const std::string &groupId, bool checkExist, std::string &databaseDir); 398 int32_t GetPreferencesDirWithCheck(bool checkExist, std::string &preferencesDir); 399 int32_t GetGroupPreferencesDirWithCheck(const std::string &groupId, bool checkExist, std::string &preferencesDir); 400 int32_t GetGroupDirWithCheck(const std::string &groupId, bool checkExist, std::string &groupDir); 401 std::shared_ptr<Global::Resource::ResourceManager> InitOthersResourceManagerInner( 402 const AppExecFwk::BundleInfo &bundleInfo, bool currentBundle, const std::string& moduleName); 403 std::shared_ptr<Global::Resource::ResourceManager> InitResourceManagerInner( 404 const AppExecFwk::BundleInfo &bundleInfo, bool currentBundle, const std::string& moduleName); 405 void UpdateResConfig(std::shared_ptr<Global::Resource::ResourceManager> &resourceManager); 406 int32_t GetBundleInfo(const std::string &bundleName, AppExecFwk::BundleInfo &bundleInfo, bool ¤tBundle); 407 void GetBundleInfo(const std::string &bundleName, AppExecFwk::BundleInfo &bundleInfo, const int &accountId); 408 409 static Global::Resource::DeviceType deviceType_; 410 std::shared_ptr<AppExecFwk::ApplicationInfo> applicationInfo_ = nullptr; 411 std::shared_ptr<Context> parentContext_ = nullptr; 412 std::shared_ptr<Global::Resource::ResourceManager> resourceManager_ = nullptr; 413 std::shared_ptr<AppExecFwk::HapModuleInfo> hapModuleInfo_ = nullptr; 414 std::shared_ptr<AppExecFwk::Configuration> config_ = nullptr; 415 std::string currArea_ = "el2"; 416 std::vector<AppExecFwk::OverlayModuleInfo> overlayModuleInfos_; 417 std::set<std::string> checkedDirSet_; 418 std::mutex checkedDirSetLock_; 419 420 std::mutex bundleManagerMutex_; 421 std::shared_ptr<AppExecFwk::BundleMgrHelper> bundleMgr_; 422 423 // True: need to get a new fms remote object, 424 // False: no need to get a new fms remote object. 425 volatile bool resetFlag_ = false; 426 }; 427 } // namespace AbilityRuntime 428 } // namespace OHOS 429 #endif // OHOS_ABILITY_RUNTIME_CONTEXT_IMPL_H 430