• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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();
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 Restart app
356      *
357      * @return error code
358      */
359     int32_t RestartApp(const AAFwk::Want& want);
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     std::shared_ptr<AppExecFwk::Configuration> GetConfiguration() const override;
367 
368     /**
369      * @brief Obtains the application base directory on the device's internal storage.
370      *
371      * @return Returns the application base directory.
372      */
373     std::string GetBaseDir() const override;
374 
375     /**
376      * @brief Obtains the Device Type.
377      *
378      * @return Returns the Device Type.
379      */
380     Global::Resource::DeviceType GetDeviceType() const override;
381 
382     int32_t SetSupportedProcessCacheSelf(bool isSupport);
383 
384     void PrintTokenInfo() const;
385 
386     void AppHasDarkRes(bool &darkRes);
387 
388     static const int EL_DEFAULT = 1;
389 
390 protected:
391     sptr<IRemoteObject> token_;
392 
393 private:
394     static const int64_t CONTEXT_CREATE_BY_SYSTEM_APP;
395     static const std::string CONTEXT_DATA_APP;
396     static const std::string CONTEXT_BUNDLE;
397     static const std::string CONTEXT_DISTRIBUTEDFILES_BASE_BEFORE;
398     static const std::string CONTEXT_DISTRIBUTEDFILES_BASE_MIDDLE;
399     static const std::string CONTEXT_DISTRIBUTEDFILES;
400     static const std::string CONTEXT_CLOUDFILE;
401     static const std::string CONTEXT_FILE_SEPARATOR;
402     static const std::string CONTEXT_DATA;
403     static const std::string CONTEXT_DATA_STORAGE;
404     static const std::string CONTEXT_BASE;
405     static const std::string CONTEXT_PRIVATE;
406     static const std::string CONTEXT_CACHE;
407     static const std::string CONTEXT_PREFERENCES;
408     static const std::string CONTEXT_GROUP;
409     static const std::string CONTEXT_DATABASE;
410     static const std::string CONTEXT_TEMP;
411     static const std::string CONTEXT_FILES;
412     static const std::string CONTEXT_HAPS;
413     static const std::string CONTEXT_ELS[];
414     static const std::string CONTEXT_RESOURCE_END;
415     int flags_ = 0x00000000;
416 
417     void InitResourceManager(const AppExecFwk::BundleInfo &bundleInfo, const std::shared_ptr<ContextImpl> &appContext,
418                              bool currentBundle = false, const std::string &moduleName = "",
419                              std::shared_ptr<Context> inputContext = nullptr);
420     bool IsCreateBySystemApp() const;
421     int GetCurrentAccountId() const;
422     void SetFlags(int64_t flags);
423     int GetCurrentActiveAccountId() const;
424     void CreateDirIfNotExist(const std::string& dirPath, const mode_t& mode) const;
425 
426     int GetOverlayModuleInfos(const std::string &bundleName, const std::string &moduleName,
427         std::vector<AppExecFwk::OverlayModuleInfo> &overlayModuleInfos);
428 
429     void OnOverlayChanged(const EventFwk::CommonEventData &data,
430         const std::shared_ptr<Global::Resource::ResourceManager> &resourceManager, const std::string &bundleName,
431         const std::string &moduleName, const std::string &loadPath);
432 
433     std::vector<std::string> GetAddOverlayPaths(
434         const std::vector<AppExecFwk::OverlayModuleInfo> &overlayModuleInfos);
435 
436     std::vector<std::string> GetRemoveOverlayPaths(
437         const std::vector<AppExecFwk::OverlayModuleInfo> &overlayModuleInfos);
438 
439     void ChangeToLocalPath(const std::string &bundleName,
440         const std::string &sourcDir, std::string &localPath);
441 
442     void CreateDirIfNotExistWithCheck(const std::string& dirPath, const mode_t& mode, bool checkExist = true);
443     int32_t GetDatabaseDirWithCheck(bool checkExist, std::string &databaseDir);
444     int32_t GetGroupDatabaseDirWithCheck(const std::string &groupId, bool checkExist, std::string &databaseDir);
445     int32_t GetPreferencesDirWithCheck(bool checkExist, std::string &preferencesDir);
446     int32_t GetGroupPreferencesDirWithCheck(const std::string &groupId, bool checkExist, std::string &preferencesDir);
447     int32_t GetGroupDirWithCheck(const std::string &groupId, bool checkExist, std::string &groupDir);
448     std::shared_ptr<Global::Resource::ResourceManager> InitOthersResourceManagerInner(
449         const AppExecFwk::BundleInfo &bundleInfo, bool currentBundle, const std::string& moduleName);
450     std::shared_ptr<Global::Resource::ResourceManager> InitResourceManagerInner(
451         const AppExecFwk::BundleInfo &bundleInfo, bool currentBundle, const std::string& moduleName,
452         std::shared_ptr<Context> inputContext = nullptr);
453     void GetOverlayPath(std::shared_ptr<Global::Resource::ResourceManager> &resourceManager,
454         const std::string &bundleName, const std::string &moduleName, std::string &loadPath, bool currentBundle,
455         std::shared_ptr<Context> inputContext = nullptr);
456     void SubscribeToOverlayEvents(std::shared_ptr<Global::Resource::ResourceManager> &resourceManager,
457         const std::string &name, const std::string &hapModuleName, std::string &loadPath,
458         std::vector<AppExecFwk::OverlayModuleInfo> overlayModuleInfos);
459     void UpdateResConfig(std::shared_ptr<Global::Resource::ResourceManager> &resourceManager);
460     void UpdateResConfig(std::shared_ptr<Global::Resource::ResourceManager> src,
461         std::shared_ptr<Global::Resource::ResourceManager> &resourceManager);
462     int32_t GetBundleInfo(const std::string &bundleName, AppExecFwk::BundleInfo &bundleInfo, bool &currentBundle);
463     void GetBundleInfo(const std::string &bundleName, AppExecFwk::BundleInfo &bundleInfo, const int &accountId,
464         std::shared_ptr<Context> inputContext = nullptr);
465     ErrCode GetOverlayMgrProxy();
466     void UnsubscribeToOverlayEvents();
467 
468     static Global::Resource::DeviceType deviceType_;
469     std::shared_ptr<AppExecFwk::ApplicationInfo> applicationInfo_ = nullptr;
470     std::shared_ptr<Context> parentContext_ = nullptr;
471     std::shared_ptr<Global::Resource::ResourceManager> resourceManager_ = nullptr;
472     std::shared_ptr<AppExecFwk::HapModuleInfo> hapModuleInfo_ = nullptr;
473     std::shared_ptr<AppExecFwk::Configuration> config_ = nullptr;
474     std::string currArea_ = "el2";
475     std::vector<AppExecFwk::OverlayModuleInfo> overlayModuleInfos_;
476     std::set<std::string> checkedDirSet_;
477     std::mutex checkedDirSetLock_;
478 
479     std::mutex bundleManagerMutex_;
480     std::shared_ptr<AppExecFwk::BundleMgrHelper> bundleMgr_;
481     std::mutex overlayMgrProxyMutex_;
482     sptr<AppExecFwk::IOverlayManager> overlayMgrProxy_ = nullptr;
483 
484     // True: need to get a new fms remote object,
485     // False: no need to get a new fms remote object.
486     volatile bool resetFlag_ = false;
487 
488     std::shared_ptr<AppExecFwk::OverlayEventSubscriber> overlaySubscriber_;
489 };
490 }  // namespace AbilityRuntime
491 }  // namespace OHOS
492 #endif  // OHOS_ABILITY_RUNTIME_CONTEXT_IMPL_H
493