• 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 "configuration.h"
22 #include "bundle_mgr_interface.h"
23 
24 namespace OHOS {
25 namespace AppExecFwk {
26 struct RunningProcessInfo;
27 }
28 namespace AbilityRuntime {
29 class ContextImpl : public Context {
30 public:
31     ContextImpl() = default;
32     virtual ~ContextImpl() = default;
33 
34     /**
35      * @brief Obtains the bundle name of the current ability.
36      *
37      * @return Returns the bundle name of the current ability.
38      */
39     std::string GetBundleName() const override;
40 
41     /**
42      * @brief Obtains the path of the package containing the current ability. The returned path contains the resources,
43      *  source code, and configuration files of a module.
44      *
45      * @return Returns the path of the package file.
46      */
47     std::string GetBundleCodeDir() override;
48 
49     /**
50      * @brief Obtains the application-specific cache directory on the device's internal storage. The system
51      * automatically deletes files from the cache directory if disk space is required elsewhere on the device.
52      * Older files are always deleted first.
53      *
54      * @return Returns the application-specific cache directory.
55      */
56     std::string GetCacheDir() override;
57 
58     /**
59      * @brief Checks whether the configuration of this ability is changing.
60      *
61      * @return Returns true if the configuration of this ability is changing and false otherwise.
62      */
63     bool IsUpdatingConfigurations() override;
64 
65     /**
66      * @brief Informs the system of the time required for drawing this Page ability.
67      *
68      * @return Returns the notification is successful or fail
69      */
70     bool PrintDrawnCompleted() override;
71 
72     /**
73      * @brief Obtains the temporary directory.
74      *
75      * @return Returns the application temporary directory.
76      */
77     std::string GetTempDir() override;
78 
79     /**
80      * @brief Obtains the directory for storing files for the application on the device's internal storage.
81      *
82      * @return Returns the application file directory.
83      */
84     std::string GetFilesDir() override;
85 
86     /**
87      * @brief Obtains the local database path.
88      * If the local database path does not exist, the system creates one and returns the created path.
89      *
90      * @return Returns the local database file.
91      */
92     std::string GetDatabaseDir() override;
93 
94     /**
95      * @brief Obtains the local system database path.
96      * If the local group database path does not exist, the system creates one and returns the created path.
97      *
98      * @return Returns the local group database file.
99      */
100     int32_t GetSystemDatabaseDir(const std::string &groupId, bool checkExist, std::string &databaseDir) override;
101 
102     /**
103      * @brief Obtains the path storing the storage file of the application.
104      *
105      * @return Returns the local storage file.
106      */
107     std::string GetPreferencesDir() override;
108 
109     /**
110      * @brief Obtains the path storing the system storage file of the application.
111      *
112      * @return Returns the local system storage file.
113      */
114     int32_t GetSystemPreferencesDir(const std::string &groupId, bool checkExist, std::string &preferencesDir) override;
115 
116     /**
117      * @brief Obtains the path storing the group file of the application by the groupId.
118      *
119      * @return Returns the local group file.
120      */
121     std::string GetGroupDir(std::string groupId) override;
122 
123     /**
124      * @brief Obtains the path distributed file of the application
125      *
126      * @return Returns the distributed file.
127      */
128     std::string GetDistributedFilesDir() override;
129 
130     /**
131      * @brief Switch file area
132      *
133      * @param mode file area.
134      */
135     void SwitchArea(int mode) override;
136 
137     /**
138      * @brief Creates a Context object for a hap with the given module name.
139      *
140      * @param moduleName Indicates the module name of the hap.
141      *
142      * @return Returns a Context object created for the specified hap and app.
143      */
144     std::shared_ptr<Context> CreateModuleContext(const std::string &moduleName) override;
145 
146     /**
147      * @brief Creates a Context object for a hap with the given hap name and app name.
148      *
149      * @param bundleName Indicates the app name of the application.
150      *
151      * @param moduleName Indicates the module name of the hap.
152      *
153      * @return Returns a Context object created for the specified hap and app.
154      */
155     std::shared_ptr<Context> CreateModuleContext(const std::string &bundleName, const std::string &moduleName) override;
156 
157     /**
158      * @brief Get file area
159      *
160      * @return file area.
161      */
162     int GetArea() override;
163 
164     /**
165      * @brief set the ResourceManager.
166      *
167      * @param the ResourceManager has been initialized.
168      */
169     void SetResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> &resourceManager);
170 
171     /**
172     * @brief Obtains a resource manager.
173     *
174     * @return Returns a ResourceManager object.
175     */
176     std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const override;
177 
178     /**
179      * @brief Creates a Context object for an application with the given bundle name.
180      *
181      * @param bundleName Indicates the bundle name of the application.
182      *
183      * @return Returns a Context object created for the specified application.
184      */
185     std::shared_ptr<Context> CreateBundleContext(const std::string &bundleName) override;
186 
187     /**
188     * @brief Obtains an IBundleMgr instance.
189     * You can use this instance to obtain information about the application bundle.
190     *
191     * @return Returns an IBundleMgr instance.
192     */
193     ErrCode GetBundleManager();
194 
195     /**
196      * @brief Set ApplicationInfo
197      *
198      * @param info ApplicationInfo instance.
199      */
200     void SetApplicationInfo(const std::shared_ptr<AppExecFwk::ApplicationInfo> &info);
201 
202     /**
203      * @brief Obtains information about the current application. The returned application information includes basic
204      * information such as the application name and application permissions.
205      *
206      * @return Returns the ApplicationInfo for the current application.
207      */
208     std::shared_ptr<AppExecFwk::ApplicationInfo> GetApplicationInfo() const override;
209 
210     /**
211      * @brief Set ApplicationInfo
212      *
213      * @param info ApplicationInfo instance.
214      */
215     void SetParentContext(const std::shared_ptr<Context> &context);
216 
217     /**
218      * @brief Obtains the path of the package containing the current ability. The returned path contains the resources,
219      *  source code, and configuration files of a module.
220      *
221      * @return Returns the path of the package file.
222      */
223     std::string GetBundleCodePath() const override;
224 
225     /**
226      * @brief Obtains the HapModuleInfo object of the application.
227      *
228      * @return Returns the HapModuleInfo object of the application.
229      */
230     std::shared_ptr<AppExecFwk::HapModuleInfo> GetHapModuleInfo() const override;
231 
232     /**
233      * @brief Set HapModuleInfo
234      *
235      * @param hapModuleInfo HapModuleInfo instance.
236      */
237     void InitHapModuleInfo(const std::shared_ptr<AppExecFwk::AbilityInfo> &abilityInfo);
238 
239     /**
240      * @brief Set HapModuleInfo
241      *
242      * @param hapModuleInfo HapModuleInfo instance.
243      */
244     void InitHapModuleInfo(const AppExecFwk::HapModuleInfo &hapModuleInfo);
245 
246     /**
247      * @brief Set the token witch the app launched.
248      *
249      * @param token The token which the is launched by app.
250      */
251     void SetToken(const sptr<IRemoteObject> &token) override;
252 
253     /**
254      * @brief Get the token witch the app launched.
255      *
256      * @return token The token which the is launched by app.
257      */
258     sptr<IRemoteObject> GetToken() override;
259 
260     /**
261      * @brief Get the token witch the app launched.
262      *
263      * @return token The token which the is launched by app.
264      */
265     void SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &config);
266 
267     /**
268      * @brief Kill process itself
269      *
270      * @return error code
271      */
272     void KillProcessBySelf();
273 
274     /**
275      * @brief Get running informationfor cuirrent process
276      *
277      * @return error code
278      */
279     int32_t GetProcessRunningInformation(AppExecFwk::RunningProcessInfo &info);
280 
281     /**
282      * @brief Get the token witch the app launched.
283      *
284      * @return token The token which the is launched by app.
285      */
286     std::shared_ptr<AppExecFwk::Configuration> GetConfiguration() const override;
287 
288     /**
289      * @brief Obtains the application base directory on the device's internal storage.
290      *
291      * @return Returns the application base directory.
292      */
293     std::string GetBaseDir() const override;
294 
295     /**
296      * @brief Obtains the Device Type.
297      *
298      * @return Returns the Device Type.
299      */
300     Global::Resource::DeviceType GetDeviceType() const override;
301 
302     static const int EL_DEFAULT = 1;
303 
304 protected:
305     sptr<IRemoteObject> token_;
306 
307 private:
308     static const int64_t CONTEXT_CREATE_BY_SYSTEM_APP;
309     static const std::string CONTEXT_DATA_APP;
310     static const std::string CONTEXT_BUNDLE;
311     static const std::string CONTEXT_DISTRIBUTEDFILES_BASE_BEFORE;
312     static const std::string CONTEXT_DISTRIBUTEDFILES_BASE_MIDDLE;
313     static const std::string CONTEXT_DISTRIBUTEDFILES;
314     static const std::string CONTEXT_FILE_SEPARATOR;
315     static const std::string CONTEXT_DATA;
316     static const std::string CONTEXT_DATA_STORAGE;
317     static const std::string CONTEXT_BASE;
318     static const std::string CONTEXT_PRIVATE;
319     static const std::string CONTEXT_CACHE;
320     static const std::string CONTEXT_PREFERENCES;
321     static const std::string CONTEXT_GROUP;
322     static const std::string CONTEXT_DATABASE;
323     static const std::string CONTEXT_TEMP;
324     static const std::string CONTEXT_FILES;
325     static const std::string CONTEXT_HAPS;
326     static const std::string CONTEXT_ELS[];
327     int flags_ = 0x00000000;
328 
329     void InitResourceManager(const AppExecFwk::BundleInfo &bundleInfo, const std::shared_ptr<ContextImpl> &appContext,
330                              bool currentBundle = false, const std::string &moduleName = "");
331     bool IsCreateBySystemApp() const;
332     int GetCurrentAccountId() const;
333     void SetFlags(int64_t flags);
334     int GetCurrentActiveAccountId() const;
335     void CreateDirIfNotExist(const std::string& dirPath, const mode_t& mode) const;
336 
337     int GetOverlayModuleInfos(const std::string &bundleName, const std::string &moduleName,
338         std::vector<AppExecFwk::OverlayModuleInfo> &overlayModuleInfos);
339 
340     void OnOverlayChanged(const EventFwk::CommonEventData &data,
341         const std::shared_ptr<Global::Resource::ResourceManager> &resourceManager, const std::string &bundleName,
342         const std::string &moduleName, const std::string &loadPath);
343 
344     std::vector<std::string> GetAddOverlayPaths(
345         const std::vector<AppExecFwk::OverlayModuleInfo> &overlayModuleInfos);
346 
347     std::vector<std::string> GetRemoveOverlayPaths(
348         const std::vector<AppExecFwk::OverlayModuleInfo> &overlayModuleInfos);
349 
350     void ChangeToLocalPath(const std::string &bundleName,
351         const std::string &sourcDir, std::string &localPath);
352 
353     void CreateDirIfNotExistWithCheck(const std::string& dirPath, const mode_t& mode, bool checkExist = true);
354     int32_t GetDatabaseDirWithCheck(bool checkExist, std::string &databaseDir);
355     int32_t GetGroupDatabaseDirWithCheck(const std::string &groupId, bool checkExist, std::string &databaseDir);
356     int32_t GetPreferencesDirWithCheck(bool checkExist, std::string &preferencesDir);
357     int32_t GetGroupPreferencesDirWithCheck(const std::string &groupId, bool checkExist, std::string &preferencesDir);
358     int32_t GetGroupDirWithCheck(const std::string &groupId, bool checkExist, std::string &groupDir);
359 
360     static Global::Resource::DeviceType deviceType_;
361     std::shared_ptr<AppExecFwk::ApplicationInfo> applicationInfo_ = nullptr;
362     std::shared_ptr<Context> parentContext_ = nullptr;
363     std::shared_ptr<Global::Resource::ResourceManager> resourceManager_ = nullptr;
364     std::shared_ptr<AppExecFwk::HapModuleInfo> hapModuleInfo_ = nullptr;
365     std::shared_ptr<AppExecFwk::Configuration> config_ = nullptr;
366     std::string currArea_ = "el2";
367     std::vector<AppExecFwk::OverlayModuleInfo> overlayModuleInfos_;
368     std::set<std::string> checkedDirSet_;
369     std::mutex checkedDirSetLock_;
370 
371     std::mutex bundleManagerMutex_;
372     sptr<AppExecFwk::IBundleMgr> bundleMgr_;
373 
374     // True: need to get a new fms remote object,
375     // False: no need to get a new fms remote object.
376     volatile bool resetFlag_ = false;
377 };
378 }  // namespace AbilityRuntime
379 }  // namespace OHOS
380 #endif  // OHOS_ABILITY_RUNTIME_CONTEXT_IMPL_H
381