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 "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 set the ResourceManager. 222 * 223 * @param the ResourceManager has been initialized. 224 */ 225 void SetResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> &resourceManager); 226 227 /** 228 * @brief Obtains a resource manager. 229 * 230 * @return Returns a ResourceManager object. 231 */ 232 std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const override; 233 234 /** 235 * @brief Creates a Context object for an application with the given bundle name. 236 * 237 * @param bundleName Indicates the bundle name of the application. 238 * 239 * @return Returns a Context object created for the specified application. 240 */ 241 std::shared_ptr<Context> CreateBundleContext(const std::string &bundleName) override; 242 243 int32_t CreateBundleContext(std::shared_ptr<Context> &context, const std::string &bundleName, 244 std::shared_ptr<Context> inputContext); 245 /** 246 * @brief Creates a ResourceManager object for a hap with the given hap name and app name. 247 * 248 * @param bundleName Indicates the app name of the application. 249 * 250 * @param moduleName Indicates the module name of the hap. 251 * 252 * @return Returns a ResourceManager object created for the specified hap and app. 253 */ 254 std::shared_ptr<Global::Resource::ResourceManager> CreateModuleResourceManager( 255 const std::string &bundleName, const std::string &moduleName) override; 256 257 int32_t CreateSystemHspModuleResourceManager(const std::string &bundleName, const std::string &moduleName, 258 std::shared_ptr<Global::Resource::ResourceManager> &resourceManager) override; 259 260 /** 261 * @brief Obtains an IBundleMgr instance. 262 * You can use this instance to obtain information about the application bundle. 263 * 264 * @return Returns an IBundleMgr instance. 265 */ 266 ErrCode GetBundleManager(); 267 268 /** 269 * @brief Set ApplicationInfo 270 * 271 * @param info ApplicationInfo instance. 272 */ 273 void SetApplicationInfo(const std::shared_ptr<AppExecFwk::ApplicationInfo> &info); 274 275 /** 276 * @brief Obtains information about the current application. The returned application information includes basic 277 * information such as the application name and application permissions. 278 * 279 * @return Returns the ApplicationInfo for the current application. 280 */ 281 std::shared_ptr<AppExecFwk::ApplicationInfo> GetApplicationInfo() const override; 282 283 /** 284 * @brief Set ApplicationInfo 285 * 286 * @param info ApplicationInfo instance. 287 */ 288 void SetParentContext(const std::shared_ptr<Context> &context); 289 290 /** 291 * @brief Obtains the path of the package containing the current ability. The returned path contains the resources, 292 * source code, and configuration files of a module. 293 * 294 * @return Returns the path of the package file. 295 */ 296 std::string GetBundleCodePath() const override; 297 298 /** 299 * @brief Obtains the HapModuleInfo object of the application. 300 * 301 * @return Returns the HapModuleInfo object of the application. 302 */ 303 std::shared_ptr<AppExecFwk::HapModuleInfo> GetHapModuleInfo() const override; 304 305 std::shared_ptr<AppExecFwk::HapModuleInfo> GetHapModuleInfoWithContext( 306 std::shared_ptr<Context> inputContext = nullptr) const; 307 308 /** 309 * @brief Set HapModuleInfo 310 * 311 * @param hapModuleInfo HapModuleInfo instance. 312 */ 313 void InitHapModuleInfo(const std::shared_ptr<AppExecFwk::AbilityInfo> &abilityInfo); 314 315 /** 316 * @brief Set HapModuleInfo 317 * 318 * @param hapModuleInfo HapModuleInfo instance. 319 */ 320 void InitHapModuleInfo(const AppExecFwk::HapModuleInfo &hapModuleInfo); 321 322 /** 323 * @brief Set the token witch the app launched. 324 * 325 * @param token The token which the is launched by app. 326 */ 327 void SetToken(const sptr<IRemoteObject> &token) override; 328 329 /** 330 * @brief Get the token witch the app launched. 331 * 332 * @return token The token which the is launched by app. 333 */ 334 sptr<IRemoteObject> GetToken() override; 335 336 /** 337 * @brief Get the token witch the app launched. 338 * 339 * @return token The token which the is launched by app. 340 */ 341 void SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &config); 342 343 /** 344 * @brief Kill process itself 345 * 346 * @return error code 347 */ 348 void KillProcessBySelf(const bool clearPageStack = true); 349 350 /** 351 * @brief Get running informationfor cuirrent process 352 * 353 * @return error code 354 */ 355 int32_t GetProcessRunningInformation(AppExecFwk::RunningProcessInfo &info); 356 357 /** 358 * @brief Get all running instance keys for the current app 359 * 360 * @return error code 361 */ 362 int32_t GetAllRunningInstanceKeys(std::vector<std::string> &instanceKeys); 363 364 /** 365 * @brief Restart app 366 * 367 * @return error code 368 */ 369 int32_t RestartApp(const AAFwk::Want& want); 370 371 /** 372 * @brief Get the token witch the app launched. 373 * 374 * @return token The token which the is launched by app. 375 */ 376 std::shared_ptr<AppExecFwk::Configuration> GetConfiguration() const override; 377 378 /** 379 * @brief Obtains the application base directory on the device's internal storage. 380 * 381 * @return Returns the application base directory. 382 */ 383 std::string GetBaseDir() const override; 384 385 /** 386 * @brief Obtains the Device Type. 387 * 388 * @return Returns the Device Type. 389 */ 390 Global::Resource::DeviceType GetDeviceType() const override; 391 392 #ifdef SUPPORT_GRAPHICS 393 /** 394 * @brief Create a context by displayId. This Context updates the density and direction properties 395 * based on the displayId, while other property values remain the same as in the original Context. 396 * 397 * @param displayId Indicates the displayId. 398 * 399 * @return Returns the context with the specified displayId. 400 */ 401 std::shared_ptr<Context> CreateDisplayContext(uint64_t displayId) override; 402 void RegisterGetDisplayConfig(GetDisplayConfigCallback getDisplayConfigCallback); 403 #endif 404 405 int32_t SetSupportedProcessCacheSelf(bool isSupport); 406 407 void PrintTokenInfo() const; 408 409 void AppHasDarkRes(bool &darkRes); 410 411 static const int EL_DEFAULT = 1; 412 413 protected: 414 // Adding a new attribute requires adding a copy in the ShallowCopySelf function 415 sptr<IRemoteObject> token_; 416 417 private: 418 static const int64_t CONTEXT_CREATE_BY_SYSTEM_APP; 419 static const std::string CONTEXT_DATA_APP; 420 static const std::string CONTEXT_BUNDLE; 421 static const std::string CONTEXT_DISTRIBUTEDFILES_BASE_BEFORE; 422 static const std::string CONTEXT_DISTRIBUTEDFILES_BASE_MIDDLE; 423 static const std::string CONTEXT_DISTRIBUTEDFILES; 424 static const std::string CONTEXT_CLOUDFILE; 425 static const std::string CONTEXT_FILE_SEPARATOR; 426 static const std::string CONTEXT_DATA; 427 static const std::string CONTEXT_DATA_STORAGE; 428 static const std::string CONTEXT_BASE; 429 static const std::string CONTEXT_PRIVATE; 430 static const std::string CONTEXT_CACHE; 431 static const std::string CONTEXT_PREFERENCES; 432 static const std::string CONTEXT_GROUP; 433 static const std::string CONTEXT_DATABASE; 434 static const std::string CONTEXT_TEMP; 435 static const std::string CONTEXT_FILES; 436 static const std::string CONTEXT_HAPS; 437 static const std::string CONTEXT_ELS[]; 438 static const std::string CONTEXT_RESOURCE_END; 439 int flags_ = 0x00000000; 440 441 void InitResourceManager(const AppExecFwk::BundleInfo &bundleInfo, const std::shared_ptr<ContextImpl> &appContext, 442 bool currentBundle = false, const std::string &moduleName = "", 443 std::shared_ptr<Context> inputContext = nullptr); 444 bool IsCreateBySystemApp() const; 445 int GetCurrentAccountId() const; 446 void SetFlags(int64_t flags); 447 int GetCurrentActiveAccountId() const; 448 void CreateDirIfNotExist(const std::string& dirPath, const mode_t& mode) const; 449 450 int GetOverlayModuleInfos(const std::string &bundleName, const std::string &moduleName, 451 std::vector<AppExecFwk::OverlayModuleInfo> &overlayModuleInfos); 452 453 void OnOverlayChanged(const EventFwk::CommonEventData &data, 454 const std::shared_ptr<Global::Resource::ResourceManager> &resourceManager, const std::string &bundleName, 455 const std::string &moduleName, const std::string &loadPath); 456 457 std::vector<std::string> GetAddOverlayPaths( 458 const std::vector<AppExecFwk::OverlayModuleInfo> &overlayModuleInfos); 459 460 std::vector<std::string> GetRemoveOverlayPaths( 461 const std::vector<AppExecFwk::OverlayModuleInfo> &overlayModuleInfos); 462 463 void ChangeToLocalPath(const std::string &bundleName, 464 const std::string &sourcDir, std::string &localPath); 465 466 void CreateDirIfNotExistWithCheck(const std::string& dirPath, const mode_t& mode, bool checkExist = true); 467 int32_t GetDatabaseDirWithCheck(bool checkExist, std::string &databaseDir); 468 int32_t GetGroupDatabaseDirWithCheck(const std::string &groupId, bool checkExist, std::string &databaseDir); 469 int32_t GetPreferencesDirWithCheck(bool checkExist, std::string &preferencesDir); 470 int32_t GetGroupPreferencesDirWithCheck(const std::string &groupId, bool checkExist, std::string &preferencesDir); 471 int32_t GetGroupDirWithCheck(const std::string &groupId, bool checkExist, std::string &groupDir); 472 std::shared_ptr<Global::Resource::ResourceManager> InitOthersResourceManagerInner( 473 const AppExecFwk::BundleInfo &bundleInfo, bool currentBundle, const std::string& moduleName); 474 std::shared_ptr<Global::Resource::ResourceManager> InitResourceManagerInner( 475 const AppExecFwk::BundleInfo &bundleInfo, bool currentBundle, const std::string& moduleName, 476 std::shared_ptr<Context> inputContext = nullptr); 477 void GetOverlayPath(std::shared_ptr<Global::Resource::ResourceManager> &resourceManager, 478 const std::string &bundleName, const std::string &moduleName, std::string &loadPath, bool currentBundle, 479 std::shared_ptr<Context> inputContext = nullptr); 480 void SubscribeToOverlayEvents(std::shared_ptr<Global::Resource::ResourceManager> &resourceManager, 481 const std::string &name, const std::string &hapModuleName, std::string &loadPath, 482 std::vector<AppExecFwk::OverlayModuleInfo> overlayModuleInfos); 483 void UpdateResConfig(std::shared_ptr<Global::Resource::ResourceManager> &resourceManager); 484 void UpdateResConfig(std::shared_ptr<Global::Resource::ResourceManager> src, 485 std::shared_ptr<Global::Resource::ResourceManager> &resourceManager); 486 int32_t GetBundleInfo(const std::string &bundleName, AppExecFwk::BundleInfo &bundleInfo, bool ¤tBundle); 487 void GetBundleInfo(const std::string &bundleName, AppExecFwk::BundleInfo &bundleInfo, const int &accountId, 488 std::shared_ptr<Context> inputContext = nullptr); 489 ErrCode GetOverlayMgrProxy(); 490 void UnsubscribeToOverlayEvents(); 491 void ShallowCopySelf(std::shared_ptr<ContextImpl> &contextImpl); 492 bool UpdateDisplayConfiguration(std::shared_ptr<ContextImpl> &contextImpl, uint64_t displayId, 493 float density, std::string direction); 494 #ifdef SUPPORT_GRAPHICS 495 bool GetDisplayConfig(uint64_t displayId, float &density, std::string &directionStr); 496 #endif 497 498 // Adding a new attribute requires adding a copy in the ShallowCopySelf function 499 static Global::Resource::DeviceType deviceType_; 500 std::shared_ptr<AppExecFwk::ApplicationInfo> applicationInfo_ = nullptr; 501 std::shared_ptr<Context> parentContext_ = nullptr; 502 std::shared_ptr<Global::Resource::ResourceManager> resourceManager_ = nullptr; 503 std::shared_ptr<AppExecFwk::HapModuleInfo> hapModuleInfo_ = nullptr; 504 std::shared_ptr<AppExecFwk::Configuration> config_ = nullptr; 505 std::string currArea_ = "el2"; 506 std::vector<AppExecFwk::OverlayModuleInfo> overlayModuleInfos_; 507 std::set<std::string> checkedDirSet_; 508 std::mutex checkedDirSetLock_; 509 510 std::mutex bundleManagerMutex_; 511 std::shared_ptr<AppExecFwk::BundleMgrHelper> bundleMgr_; 512 std::mutex overlayMgrProxyMutex_; 513 sptr<AppExecFwk::IOverlayManager> overlayMgrProxy_ = nullptr; 514 515 // True: need to get a new fms remote object, 516 // False: no need to get a new fms remote object. 517 volatile bool resetFlag_ = false; 518 519 std::shared_ptr<AppExecFwk::OverlayEventSubscriber> overlaySubscriber_; 520 std::string processName_; 521 #ifdef SUPPORT_GRAPHICS 522 static std::mutex getDisplayConfigCallbackMutex_; 523 static GetDisplayConfigCallback getDisplayConfigCallback_; 524 #endif 525 }; 526 } // namespace AbilityRuntime 527 } // namespace OHOS 528 #endif // OHOS_ABILITY_RUNTIME_CONTEXT_IMPL_H 529