• 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, public std::enable_shared_from_this<ContextImpl> {
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 path storing the storage file of the application.
96      *
97      * @return Returns the local storage file.
98      */
99     std::string GetPreferencesDir() override;
100 
101     /**
102      * @brief Obtains the path distributed file of the application
103      *
104      * @return Returns the distributed file.
105      */
106     std::string GetDistributedFilesDir() override;
107 
108     /**
109      * @brief Switch file area
110      *
111      * @param mode file area.
112      */
113     void SwitchArea(int mode) override;
114 
115     /**
116      * @brief Creates a Context object for a hap with the given module name.
117      *
118      * @param moduleName Indicates the module name of the hap.
119      *
120      * @return Returns a Context object created for the specified hap and app.
121      */
122     std::shared_ptr<Context> CreateModuleContext(const std::string &moduleName) override;
123 
124     /**
125      * @brief Creates a Context object for a hap with the given hap name and app name.
126      *
127      * @param bundleName Indicates the app name of the application.
128      *
129      * @param moduleName Indicates the module name of the hap.
130      *
131      * @return Returns a Context object created for the specified hap and app.
132      */
133     std::shared_ptr<Context> CreateModuleContext(const std::string &bundleName, const std::string &moduleName) override;
134 
135     /**
136      * @brief Get file area
137      *
138      * @return file area.
139      */
140     int GetArea() override;
141 
142     /**
143      * @brief set the ResourceManager.
144      *
145      * @param the ResourceManager has been initialized.
146      */
147     void SetResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> &resourceManager);
148 
149     /**
150     * @brief Obtains a resource manager.
151     *
152     * @return Returns a ResourceManager object.
153     */
154     std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const override;
155 
156     /**
157      * @brief Creates a Context object for an application with the given bundle name.
158      *
159      * @param bundleName Indicates the bundle name of the application.
160      *
161      * @return Returns a Context object created for the specified application.
162      */
163     std::shared_ptr<Context> CreateBundleContext(const std::string &bundleName) override;
164 
165     /**
166     * @brief Obtains an IBundleMgr instance.
167     * You can use this instance to obtain information about the application bundle.
168     *
169     * @return Returns an IBundleMgr instance.
170     */
171     sptr<AppExecFwk::IBundleMgr> GetBundleManager() const;
172 
173     /**
174      * @brief Set ApplicationInfo
175      *
176      * @param info ApplicationInfo instance.
177      */
178     void SetApplicationInfo(const std::shared_ptr<AppExecFwk::ApplicationInfo> &info);
179 
180     /**
181      * @brief Obtains information about the current application. The returned application information includes basic
182      * information such as the application name and application permissions.
183      *
184      * @return Returns the ApplicationInfo for the current application.
185      */
186     std::shared_ptr<AppExecFwk::ApplicationInfo> GetApplicationInfo() const override;
187 
188     /**
189      * @brief Set ApplicationInfo
190      *
191      * @param info ApplicationInfo instance.
192      */
193     void SetParentContext(const std::shared_ptr<Context> &context);
194 
195     /**
196      * @brief Obtains the path of the package containing the current ability. The returned path contains the resources,
197      *  source code, and configuration files of a module.
198      *
199      * @return Returns the path of the package file.
200      */
201     std::string GetBundleCodePath() const override;
202 
203     /**
204      * @brief Obtains the HapModuleInfo object of the application.
205      *
206      * @return Returns the HapModuleInfo object of the application.
207      */
208     std::shared_ptr<AppExecFwk::HapModuleInfo> GetHapModuleInfo() const override;
209 
210     /**
211      * @brief Set HapModuleInfo
212      *
213      * @param hapModuleInfo HapModuleInfo instance.
214      */
215     void InitHapModuleInfo(const std::shared_ptr<AppExecFwk::AbilityInfo> &abilityInfo);
216 
217     /**
218      * @brief Set HapModuleInfo
219      *
220      * @param hapModuleInfo HapModuleInfo instance.
221      */
222     void InitHapModuleInfo(const AppExecFwk::HapModuleInfo &hapModuleInfo);
223 
224     /**
225      * @brief Set the token witch the app launched.
226      *
227      * @param token The token which the is launched by app.
228      */
229     void SetToken(const sptr<IRemoteObject> &token) override;
230 
231     /**
232      * @brief Get the token witch the app launched.
233      *
234      * @return token The token which the is launched by app.
235      */
236     sptr<IRemoteObject> GetToken() override;
237 
238     /**
239      * @brief Get the token witch the app launched.
240      *
241      * @return token The token which the is launched by app.
242      */
243     void SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &config);
244 
245     /**
246      * @brief Kill process itself
247      *
248      * @return error code
249      */
250     void KillProcessBySelf();
251 
252     /**
253      * @brief Get running informationfor cuirrent process
254      *
255      * @return error code
256      */
257     int32_t GetProcessRunningInformation(AppExecFwk::RunningProcessInfo &info);
258 
259     /**
260      * @brief Get the token witch the app launched.
261      *
262      * @return token The token which the is launched by app.
263      */
264     std::shared_ptr<AppExecFwk::Configuration> GetConfiguration() const override;
265 
266     /**
267      * @brief Obtains the application base directory on the device's internal storage.
268      *
269      * @return Returns the application base directory.
270      */
271     std::string GetBaseDir() const override;
272 
273     /**
274      * @brief Obtains the Device Type.
275      *
276      * @return Returns the Device Type.
277      */
278     Global::Resource::DeviceType GetDeviceType() const override;
279 
280     static const int EL_DEFAULT = 1;
281 
282 protected:
283     sptr<IRemoteObject> token_;
284 
285 private:
286     static const int64_t CONTEXT_CREATE_BY_SYSTEM_APP;
287     static const std::string CONTEXT_DATA_APP;
288     static const std::string CONTEXT_BUNDLE;
289     static const std::string CONTEXT_DISTRIBUTEDFILES_BASE_BEFORE;
290     static const std::string CONTEXT_DISTRIBUTEDFILES_BASE_MIDDLE;
291     static const std::string CONTEXT_DISTRIBUTEDFILES;
292     static const std::string CONTEXT_FILE_SEPARATOR;
293     static const std::string CONTEXT_DATA;
294     static const std::string CONTEXT_DATA_STORAGE;
295     static const std::string CONTEXT_BASE;
296     static const std::string CONTEXT_PRIVATE;
297     static const std::string CONTEXT_CACHE;
298     static const std::string CONTEXT_PREFERENCES;
299     static const std::string CONTEXT_DATABASE;
300     static const std::string CONTEXT_TEMP;
301     static const std::string CONTEXT_FILES;
302     static const std::string CONTEXT_HAPS;
303     static const std::string CONTEXT_ELS[];
304     int flags_ = 0x00000000;
305 
306     void InitResourceManager(const AppExecFwk::BundleInfo &bundleInfo, const std::shared_ptr<ContextImpl> &appContext,
307                              bool currentBundle = false, const std::string &moduleName = "") const;
308     bool IsCreateBySystemApp() const;
309     int GetCurrentAccountId() const;
310     void SetFlags(int64_t flags);
311     int GetCurrentActiveAccountId() const;
312     void CreateDirIfNotExist(const std::string& dirPath, const mode_t& mode) const;
313 
314     static Global::Resource::DeviceType deviceType_;
315     std::shared_ptr<AppExecFwk::ApplicationInfo> applicationInfo_ = nullptr;
316     std::shared_ptr<Context> parentContext_ = nullptr;
317     std::shared_ptr<Global::Resource::ResourceManager> resourceManager_ = nullptr;
318     std::shared_ptr<AppExecFwk::HapModuleInfo> hapModuleInfo_ = nullptr;
319     std::shared_ptr<AppExecFwk::Configuration> config_ = nullptr;
320     std::string currArea_ = "el2";
321 };
322 }  // namespace AbilityRuntime
323 }  // namespace OHOS
324 #endif  // OHOS_ABILITY_RUNTIME_CONTEXT_IMPL_H
325