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