1 /* 2 * Copyright (c) 2021-2024 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 "bundle_mgr_interface.h" 22 #include "configuration.h" 23 24 namespace OHOS { 25 namespace AppExecFwk { 26 struct RunningProcessInfo; 27 class BundleMgrHelper; 28 class OverlayEventSubscriber; 29 } 30 namespace AAFwk { 31 class Want; 32 } 33 namespace AbilityRuntime { 34 #ifdef SUPPORT_GRAPHICS 35 using GetDisplayConfigCallback = std::function<bool(uint64_t displayId, float &density, std::string &directionStr)>; 36 #endif 37 class ContextImpl : public Context { 38 public: 39 ContextImpl() = default; 40 virtual ~ContextImpl(); 41 42 /** 43 * @brief Obtains the bundle name of the current ability. 44 * 45 * @return Returns the bundle name of the current ability. 46 */ 47 std::string GetBundleName() const override; 48 49 /** 50 * @brief Obtains the path of the package containing the current ability. The returned path contains the resources, 51 * source code, and configuration files of a module. 52 * 53 * @return Returns the path of the package file. 54 */ 55 std::string GetBundleCodeDir() override; 56 57 /** 58 * @brief Obtains the application-specific cache directory on the device's internal storage. The system 59 * automatically deletes files from the cache directory if disk space is required elsewhere on the device. 60 * Older files are always deleted first. 61 * 62 * @return Returns the application-specific cache directory. 63 */ 64 std::string GetCacheDir() override; 65 66 /** 67 * @brief Checks whether the configuration of this ability is changing. 68 * 69 * @return Returns true if the configuration of this ability is changing and false otherwise. 70 */ 71 bool IsUpdatingConfigurations() override; 72 73 /** 74 * @brief Informs the system of the time required for drawing this Page ability. 75 * 76 * @return Returns the notification is successful or fail 77 */ 78 bool PrintDrawnCompleted() override; 79 80 /** 81 * @brief Obtains the temporary directory. 82 * 83 * @return Returns the application temporary directory. 84 */ 85 std::string GetTempDir() override; 86 87 std::string GetResourceDir() override; 88 89 /** 90 * @brief Get all temporary directories. 91 * 92 * @param tempPaths Return all temporary directories of the application. 93 */ 94 virtual void GetAllTempDir(std::vector<std::string> &tempPaths); 95 96 /** 97 * @brief Obtains the directory for storing files for the application on the device's internal storage. 98 * 99 * @return Returns the application file directory. 100 */ 101 std::string GetFilesDir() override; 102 103 /** 104 * @brief Obtains the local database path. 105 * If the local database path does not exist, the system creates one and returns the created path. 106 * 107 * @return Returns the local database file. 108 */ 109 std::string GetDatabaseDir() override; 110 111 /** 112 * @brief Obtains the local system database path. 113 * If the local group database path does not exist, the system creates one and returns the created path. 114 * 115 * @return Returns the local group database file. 116 */ 117 int32_t GetSystemDatabaseDir(const std::string &groupId, bool checkExist, std::string &databaseDir) override; 118 119 /** 120 * @brief Obtains the path storing the storage file of the application. 121 * 122 * @return Returns the local storage file. 123 */ 124 std::string GetPreferencesDir() override; 125 126 /** 127 * @brief Obtains the path storing the system storage file of the application. 128 * 129 * @return Returns the local system storage file. 130 */ 131 int32_t GetSystemPreferencesDir(const std::string &groupId, bool checkExist, std::string &preferencesDir) override; 132 133 /** 134 * @brief Obtains the path storing the group file of the application by the groupId. 135 * 136 * @return Returns the local group file. 137 */ 138 std::string GetGroupDir(std::string groupId) override; 139 140 /** 141 * @brief Obtains the path distributed file of the application 142 * 143 * @return Returns the distributed file. 144 */ 145 std::string GetDistributedFilesDir() override; 146 147 std::string GetCloudFileDir() override; 148 149 /** 150 * @brief Switch file area 151 * 152 * @param mode file area. 153 */ 154 void SwitchArea(int mode) override; 155 156 /** 157 * @brief Set color mode 158 * 159 * @param colorMode color mode. 160 */ 161 void SetColorMode(int colorMode); 162 163 /** 164 * @brief Set language 165 * 166 * @param language language. 167 */ 168 void SetLanguage(std::string language); 169 170 /** 171 * @brief Set font 172 * 173 * @param Font font. 174 */ 175 void SetFont(std::string font); 176 177 void SetMcc(std::string mcc); 178 179 void SetMnc(std::string mnc); 180 181 /** 182 * @brief clear the application data by app self 183 */ 184 void ClearUpApplicationData(); 185 186 /** 187 * @brief Creates a Context object for a hap with the given module name. 188 * 189 * @param moduleName Indicates the module name of the hap. 190 * 191 * @return Returns a Context object created for the specified hap and app. 192 */ 193 std::shared_ptr<Context> CreateModuleContext(const std::string &moduleName) override; 194 195 /** 196 * @brief Creates a Context object for a hap with the given hap name and app name. 197 * 198 * @param bundleName Indicates the app name of the application. 199 * 200 * @param moduleName Indicates the module name of the hap. 201 * 202 * @return Returns a Context object created for the specified hap and app. 203 */ 204 std::shared_ptr<Context> CreateModuleContext(const std::string &bundleName, const std::string &moduleName) override; 205 206 std::shared_ptr<Context> CreateModuleContext(const std::string &moduleName, std::shared_ptr<Context> inputContext); 207 208 std::shared_ptr<Context> CreateModuleContext(const std::string &bundleName, 209 const std::string &moduleName, std::shared_ptr<Context> inputContext); 210 211 std::string GetBundleNameWithContext(std::shared_ptr<Context> inputContext = nullptr) const; 212 213 /** 214 * @brief Get file area 215 * 216 * @return file area. 217 */ 218 int GetArea() override; 219 220 /** 221 * @brief Get process name 222 * 223 * @return process name. 224 */ 225 std::string GetProcessName() override; 226 227 /** 228 * @brief set the ResourceManager. 229 * 230 * @param the ResourceManager has been initialized. 231 */ 232 void SetResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> &resourceManager); 233 234 /** 235 * @brief Obtains a resource manager. 236 * 237 * @return Returns a ResourceManager object. 238 */ 239 std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const override; 240 241 /** 242 * @brief Creates a Context object for an application with the given bundle name. 243 * 244 * @param bundleName Indicates the bundle name of the application. 245 * 246 * @return Returns a Context object created for the specified application. 247 */ 248 std::shared_ptr<Context> CreateBundleContext(const std::string &bundleName) override; 249 250 int32_t CreateBundleContext(std::shared_ptr<Context> &context, const std::string &bundleName, 251 std::shared_ptr<Context> inputContext); 252 /** 253 * @brief Creates a ResourceManager object for a hap with the given hap name and app name. 254 * 255 * @param bundleName Indicates the app name of the application. 256 * 257 * @param moduleName Indicates the module name of the hap. 258 * 259 * @return Returns a ResourceManager object created for the specified hap and app. 260 */ 261 std::shared_ptr<Global::Resource::ResourceManager> CreateModuleResourceManager( 262 const std::string &bundleName, const std::string &moduleName) override; 263 264 int32_t CreateSystemHspModuleResourceManager(const std::string &bundleName, const std::string &moduleName, 265 std::shared_ptr<Global::Resource::ResourceManager> &resourceManager) override; 266 267 int32_t CreateHspModuleResourceManager(const std::string &bundleName, 268 const std::string &moduleName, std::shared_ptr<Global::Resource::ResourceManager> &resourceManager) override; 269 /** 270 * @brief Obtains an IBundleMgr instance. 271 * You can use this instance to obtain information about the application bundle. 272 * 273 * @return Returns an IBundleMgr instance. 274 */ 275 ErrCode GetBundleManager(); 276 277 /** 278 * @brief Set ApplicationInfo 279 * 280 * @param info ApplicationInfo instance. 281 */ 282 void SetApplicationInfo(const std::shared_ptr<AppExecFwk::ApplicationInfo> &info); 283 284 /** 285 * @brief Obtains information about the current application. The returned application information includes basic 286 * information such as the application name and application permissions. 287 * 288 * @return Returns the ApplicationInfo for the current application. 289 */ 290 std::shared_ptr<AppExecFwk::ApplicationInfo> GetApplicationInfo() const override; 291 292 /** 293 * @brief Set ApplicationInfo 294 * 295 * @param info ApplicationInfo instance. 296 */ 297 void SetParentContext(const std::shared_ptr<Context> &context); 298 299 /** 300 * @brief Obtains the path of the package containing the current ability. The returned path contains the resources, 301 * source code, and configuration files of a module. 302 * 303 * @return Returns the path of the package file. 304 */ 305 std::string GetBundleCodePath() const override; 306 307 /** 308 * @brief Obtains the HapModuleInfo object of the application. 309 * 310 * @return Returns the HapModuleInfo object of the application. 311 */ 312 std::shared_ptr<AppExecFwk::HapModuleInfo> GetHapModuleInfo() const override; 313 314 std::shared_ptr<AppExecFwk::HapModuleInfo> GetHapModuleInfoWithContext( 315 std::shared_ptr<Context> inputContext = nullptr) const; 316 317 /** 318 * @brief Set HapModuleInfo 319 * 320 * @param hapModuleInfo HapModuleInfo instance. 321 */ 322 void InitHapModuleInfo(const std::shared_ptr<AppExecFwk::AbilityInfo> &abilityInfo); 323 324 /** 325 * @brief Set HapModuleInfo 326 * 327 * @param hapModuleInfo HapModuleInfo instance. 328 */ 329 void InitHapModuleInfo(const AppExecFwk::HapModuleInfo &hapModuleInfo); 330 331 /** 332 * @brief Set the token witch the app launched. 333 * 334 * @param token The token which the is launched by app. 335 */ 336 void SetToken(const sptr<IRemoteObject> &token) override; 337 338 /** 339 * @brief Get the token witch the app launched. 340 * 341 * @return token The token which the is launched by app. 342 */ 343 sptr<IRemoteObject> GetToken() override; 344 345 /** 346 * @brief Get the token witch the app launched. 347 * 348 * @return token The token which the is launched by app. 349 */ 350 void SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &config); 351 352 /** 353 * @brief Kill process itself 354 * 355 * @return error code 356 */ 357 void KillProcessBySelf(const bool clearPageStack = false); 358 359 /** 360 * @brief Get running informationfor cuirrent process 361 * 362 * @return error code 363 */ 364 int32_t GetProcessRunningInformation(AppExecFwk::RunningProcessInfo &info); 365 366 /** 367 * @brief Get all running instance keys for the current app 368 * 369 * @return error code 370 */ 371 int32_t GetAllRunningInstanceKeys(std::vector<std::string> &instanceKeys); 372 373 /** 374 * @brief Restart app 375 * 376 * @return error code 377 */ 378 int32_t RestartApp(const AAFwk::Want& want); 379 380 /** 381 * @brief Get the token witch the app launched. 382 * 383 * @return token The token which the is launched by app. 384 */ 385 std::shared_ptr<AppExecFwk::Configuration> GetConfiguration() const override; 386 387 /** 388 * @brief Obtains the application base directory on the device's internal storage. 389 * 390 * @return Returns the application base directory. 391 */ 392 std::string GetBaseDir() const override; 393 394 /** 395 * @brief Obtains the Device Type. 396 * 397 * @return Returns the Device Type. 398 */ 399 Global::Resource::DeviceType GetDeviceType() const override; 400 401 /** 402 * @brief Create a area mode context. 403 * 404 * @param areaMode Indicates the area mode. 405 * 406 * @return Returns the context with the specified area mode. 407 */ 408 std::shared_ptr<Context> CreateAreaModeContext(int areaMode) override; 409 410 #ifdef SUPPORT_GRAPHICS 411 /** 412 * @brief Create a context by displayId. This Context updates the density and direction properties 413 * based on the displayId, while other property values remain the same as in the original Context. 414 * 415 * @param displayId Indicates the displayId. 416 * 417 * @return Returns the context with the specified displayId. 418 */ 419 std::shared_ptr<Context> CreateDisplayContext(uint64_t displayId) override; 420 void RegisterGetDisplayConfig(GetDisplayConfigCallback getDisplayConfigCallback); 421 #endif 422 423 int32_t SetSupportedProcessCacheSelf(bool isSupport); 424 425 void PrintTokenInfo() const; 426 427 void AppHasDarkRes(bool &darkRes); 428 429 void SetProcessName(const std::string &processName); 430 431 static const int EL_DEFAULT = 1; 432 433 protected: 434 // Adding a new attribute requires adding a copy in the ShallowCopySelf function 435 sptr<IRemoteObject> token_; 436 437 private: 438 static const int64_t CONTEXT_CREATE_BY_SYSTEM_APP; 439 static const std::string CONTEXT_DATA_APP; 440 static const std::string CONTEXT_BUNDLE; 441 static const std::string CONTEXT_DISTRIBUTEDFILES_BASE_BEFORE; 442 static const std::string CONTEXT_DISTRIBUTEDFILES_BASE_MIDDLE; 443 static const std::string CONTEXT_DISTRIBUTEDFILES; 444 static const std::string CONTEXT_CLOUDFILE; 445 static const std::string CONTEXT_FILE_SEPARATOR; 446 static const std::string CONTEXT_DATA; 447 static const std::string CONTEXT_DATA_STORAGE; 448 static const std::string CONTEXT_BASE; 449 static const std::string CONTEXT_PRIVATE; 450 static const std::string CONTEXT_CACHE; 451 static const std::string CONTEXT_PREFERENCES; 452 static const std::string CONTEXT_GROUP; 453 static const std::string CONTEXT_DATABASE; 454 static const std::string CONTEXT_TEMP; 455 static const std::string CONTEXT_FILES; 456 static const std::string CONTEXT_HAPS; 457 static const std::string CONTEXT_ELS[]; 458 static const std::string CONTEXT_RESOURCE_END; 459 int flags_ = 0x00000000; 460 461 void InitResourceManager(const AppExecFwk::BundleInfo &bundleInfo, const std::shared_ptr<ContextImpl> &appContext, 462 bool currentBundle = false, const std::string &moduleName = "", 463 std::shared_ptr<Context> inputContext = nullptr); 464 bool IsCreateBySystemApp() const; 465 int GetCurrentAccountId() const; 466 void SetFlags(int64_t flags); 467 int GetCurrentActiveAccountId() const; 468 void CreateDirIfNotExist(const std::string& dirPath, const mode_t& mode) const; 469 470 int GetOverlayModuleInfos(const std::string &bundleName, const std::string &moduleName, 471 std::vector<AppExecFwk::OverlayModuleInfo> &overlayModuleInfos); 472 473 void OnOverlayChanged(const EventFwk::CommonEventData &data, 474 const std::shared_ptr<Global::Resource::ResourceManager> &resourceManager, const std::string &bundleName, 475 const std::string &moduleName, const std::string &loadPath); 476 477 std::vector<std::string> GetAddOverlayPaths( 478 const std::vector<AppExecFwk::OverlayModuleInfo> &overlayModuleInfos); 479 480 std::vector<std::string> GetRemoveOverlayPaths( 481 const std::vector<AppExecFwk::OverlayModuleInfo> &overlayModuleInfos); 482 483 void ChangeToLocalPath(const std::string &bundleName, 484 const std::string &sourcDir, std::string &localPath); 485 486 void CreateDirIfNotExistWithCheck(const std::string& dirPath, const mode_t& mode, bool checkExist = true); 487 int32_t GetDatabaseDirWithCheck(bool checkExist, std::string &databaseDir); 488 int32_t GetGroupDatabaseDirWithCheck(const std::string &groupId, bool checkExist, std::string &databaseDir); 489 int32_t GetPreferencesDirWithCheck(bool checkExist, std::string &preferencesDir); 490 int32_t GetGroupPreferencesDirWithCheck(const std::string &groupId, bool checkExist, std::string &preferencesDir); 491 int32_t GetGroupDirWithCheck(const std::string &groupId, bool checkExist, std::string &groupDir); 492 std::shared_ptr<Global::Resource::ResourceManager> InitOthersResourceManagerInner( 493 const AppExecFwk::BundleInfo &bundleInfo, bool currentBundle, const std::string& moduleName); 494 std::shared_ptr<Global::Resource::ResourceManager> InitResourceManagerInner( 495 const AppExecFwk::BundleInfo &bundleInfo, bool currentBundle, const std::string& moduleName, 496 std::shared_ptr<Context> inputContext = nullptr); 497 void GetOverlayPath(std::shared_ptr<Global::Resource::ResourceManager> &resourceManager, 498 const std::string &bundleName, const std::string &moduleName, std::string &loadPath, bool currentBundle, 499 std::shared_ptr<Context> inputContext = nullptr); 500 void AddPatchResource(std::shared_ptr<Global::Resource::ResourceManager> &resourceManager, 501 const std::string &loadPath, const std::string &hqfPath, bool isDebug, 502 std::shared_ptr<Context> inputContext = nullptr); 503 void SubscribeToOverlayEvents(std::shared_ptr<Global::Resource::ResourceManager> &resourceManager, 504 const std::string &name, const std::string &hapModuleName, std::string &loadPath, 505 std::vector<AppExecFwk::OverlayModuleInfo> overlayModuleInfos); 506 void UpdateResConfig(std::shared_ptr<Global::Resource::ResourceManager> &resourceManager); 507 void UpdateResConfig(std::shared_ptr<Global::Resource::ResourceManager> src, 508 std::shared_ptr<Global::Resource::ResourceManager> &resourceManager); 509 int32_t GetBundleInfo(const std::string &bundleName, AppExecFwk::BundleInfo &bundleInfo, bool ¤tBundle); 510 void GetBundleInfo(const std::string &bundleName, AppExecFwk::BundleInfo &bundleInfo, 511 std::shared_ptr<Context> inputContext = nullptr); 512 ErrCode GetOverlayMgrProxy(); 513 void UnsubscribeToOverlayEvents(); 514 void ShallowCopySelf(std::shared_ptr<ContextImpl> &contextImpl); 515 bool UpdateDisplayConfiguration(std::shared_ptr<ContextImpl> &contextImpl, uint64_t displayId, 516 float density, std::string direction); 517 #ifdef SUPPORT_GRAPHICS 518 bool GetDisplayConfig(uint64_t displayId, float &density, std::string &directionStr); 519 #endif 520 521 // Adding a new attribute requires adding a copy in the ShallowCopySelf function 522 static Global::Resource::DeviceType deviceType_; 523 std::shared_ptr<AppExecFwk::ApplicationInfo> applicationInfo_ = nullptr; 524 std::shared_ptr<Context> parentContext_ = nullptr; 525 std::shared_ptr<Global::Resource::ResourceManager> resourceManager_ = nullptr; 526 std::shared_ptr<AppExecFwk::HapModuleInfo> hapModuleInfo_ = nullptr; 527 std::shared_ptr<AppExecFwk::Configuration> config_ = nullptr; 528 std::string currArea_ = "el2"; 529 std::vector<AppExecFwk::OverlayModuleInfo> overlayModuleInfos_; 530 std::set<std::string> checkedDirSet_; 531 std::mutex checkedDirSetLock_; 532 533 std::mutex bundleManagerMutex_; 534 std::shared_ptr<AppExecFwk::BundleMgrHelper> bundleMgr_; 535 std::mutex overlayMgrProxyMutex_; 536 sptr<AppExecFwk::IOverlayManager> overlayMgrProxy_ = nullptr; 537 538 // True: need to get a new fms remote object, 539 // False: no need to get a new fms remote object. 540 volatile bool resetFlag_ = false; 541 542 std::mutex overlaySubscriberMutex_; 543 std::shared_ptr<AppExecFwk::OverlayEventSubscriber> overlaySubscriber_; 544 std::string processName_; 545 #ifdef SUPPORT_GRAPHICS 546 static std::mutex getDisplayConfigCallbackMutex_; 547 static GetDisplayConfigCallback getDisplayConfigCallback_; 548 #endif 549 }; 550 } // namespace AbilityRuntime 551 } // namespace OHOS 552 #endif // OHOS_ABILITY_RUNTIME_CONTEXT_IMPL_H 553