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