• 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_DEAL_H
17 #define OHOS_ABILITY_RUNTIME_CONTEXT_DEAL_H
18 
19 #include "context.h"
20 #include "profile.h"
21 #include "lifecycle_state_info.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 class ContextDeal : public Context, public std::enable_shared_from_this<ContextDeal> {
26 public:
27     ContextDeal() = default;
28     explicit ContextDeal(bool isCreateBySystemApp);
29     virtual ~ContextDeal() = default;
30 
31     /**
32      * Called when getting the ProcessInfo
33      *
34      * @return ProcessInfo
35      */
36     std::shared_ptr<ProcessInfo> GetProcessInfo() const override;
37 
38     /**
39      * Called when setting the ProcessInfo
40      *
41      * @param info ProcessInfo instance
42      */
43     void SetProcessInfo(const std::shared_ptr<ProcessInfo> &info);
44 
45     /**
46      * @brief Obtains information about the current application. The returned application information includes basic
47      * information such as the application name and application permissions.
48      *
49      * @return Returns the ApplicationInfo for the current application.
50      */
51     std::shared_ptr<ApplicationInfo> GetApplicationInfo() const override;
52 
53     /**
54      * @brief Set ApplicationInfo
55      *
56      * @param info ApplicationInfo instance.
57      */
58     void SetApplicationInfo(const std::shared_ptr<ApplicationInfo> &info);
59 
60     /**
61      * @brief Obtains the Context object of the application.
62      *
63      * @return Returns the Context object of the application.
64      */
65     std::shared_ptr<Context> GetApplicationContext() const override;
66 
67     /**
68      * @brief Set ApplicationContext
69      *
70      * @param context ApplicationContext instance.
71      */
72     void SetApplicationContext(const std::shared_ptr<Context> &context);
73 
74     /**
75      * @brief Obtains the path of the package containing the current ability. The returned path contains the resources,
76      *  source code, and configuration files of a module.
77      *
78      * @return Returns the path of the package file.
79      */
80     std::string GetBundleCodePath() override;
81 
82     /**
83      * @brief SetBundleCodePath
84      *
85      * @param Returns string path
86      */
87     void SetBundleCodePath(std::string &path);
88 
89     /**
90      * @brief Obtains information about the current ability.
91      * The returned information includes the class name, bundle name, and other information about the current ability.
92      *
93      * @return Returns the AbilityInfo object for the current ability.
94      */
95     const std::shared_ptr<AbilityInfo> GetAbilityInfo() override;
96 
97     /**
98      * @brief Set AbilityInfo
99      *
100      * @param info AbilityInfo instance.
101      */
102     void SetAbilityInfo(const std::shared_ptr<AbilityInfo> &info);
103 
104     /**
105      * @brief Obtains the Context object of the ability.
106      *
107      * @return Returns the Context object of the ability.
108      */
109     std::shared_ptr<Context> GetContext() override;
110 
111     /**
112      * @brief Set Ability context
113      *
114      * @param context Ability object
115      */
116     void SetContext(const std::shared_ptr<Context> &context);
117 
118     /**
119      * @brief Obtains an IBundleMgr instance.
120      * You can use this instance to obtain information about the application bundle.
121      *
122      * @return Returns an IBundleMgr instance.
123      */
124     sptr<IBundleMgr> GetBundleManager() const override;
125 
126     /**
127      * @brief Obtains a resource manager.
128      *
129      * @return Returns a ResourceManager object.
130      */
131     std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const override;
132 
133     /**
134      * @brief Set Profile instance.
135      *
136      * @param Profile instance.
137      */
138     void SetProfile(const std::shared_ptr<Profile> &profile);
139 
140     /**
141      * @brief Obtains an Profile instance.
142      *
143      * @return Returns an Profile instance.
144      */
145     std::shared_ptr<Profile> GetProfile() const;
146 
147     /**
148      * @brief Deletes the specified private file associated with the application.
149      *
150      * @param fileName Indicates the name of the file to delete. The file name cannot contain path separators.
151      *
152      * @return Returns true if the file is deleted successfully; returns false otherwise.
153      */
154     bool DeleteFile(const std::string &fileName) override;
155 
156     /**
157      * @brief Destroys another ability that uses the AbilityInfo.AbilityType.SERVICE template.
158      * The current ability using either the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE
159      * template can call this method to destroy another ability that uses the AbilityInfo.AbilityType.SERVICE
160      * template. The current ability itself can be destroyed by calling the terminateAbility() method.
161      *
162      * @param want Indicates the Want containing information about the ability to destroy.
163      *
164      * @return Returns true if the ability is destroyed successfully; returns false otherwise.
165      */
166     bool StopAbility(const AAFwk::Want &want) override;
167 
168     /**
169      * @brief Obtains the application-specific cache directory on the device's internal storage. The system
170      * automatically deletes files from the cache directory if disk space is required elsewhere on the device.
171      * Older files are always deleted first.
172      *
173      * @return Returns the application-specific cache directory.
174      */
175     std::string GetCacheDir() override;
176 
177     /**
178      * @brief Checks whether the configuration of this ability is changing.
179      *
180      * @return Returns true if the configuration of this ability is changing and false otherwise.
181      */
182     bool IsUpdatingConfigurations() override;
183 
184     /**
185      * @brief Informs the system of the time required for drawing this Page ability.
186      *
187      * @return Returns the notification is successful or fail
188      */
189     bool PrintDrawnCompleted() override;
190 
191     /**
192      * @brief Obtains the application-specific code-cache directory on the device's internal storage.
193      * The system will delete any files stored in this location both when your specific application is upgraded,
194      * and when the entire platform is upgraded.
195      *
196      * @return Returns the application-specific code-cache directory.
197      */
198     std::string GetCodeCacheDir() override;
199 
200     /**
201      * @brief Obtains the local database path.
202      * If the local database path does not exist, the system creates one and returns the created path.
203      *
204      * @return Returns the local database file.
205      */
206     std::string GetDatabaseDir() override;
207 
208     /**
209      * @brief Obtains the absolute path where all private data files of this application are stored.
210      *
211      * @return Returns the absolute path storing all private data files of this application.
212      */
213     std::string GetDataDir() override;
214 
215     /**
216      * @brief Obtains the directory for storing custom data files of the application.
217      * You can use the returned File object to create and access files in this directory. The files
218      * can be accessible only by the current application.
219      *
220      * @param name Indicates the name of the directory to retrieve. This directory is created as part
221      * of your application data.
222      * @param mode Indicates the file operating mode. The value can be 0 or a combination of MODE_PRIVATE.
223      *
224      * @return Returns a File object for the requested directory.
225      */
226     std::string GetDir(const std::string &name, int mode) override;
227 
228     /**
229      * @brief Obtains the absolute path to the application-specific cache directory
230      * on the primary external or shared storage device.
231      *
232      * @return Returns the absolute path to the application-specific cache directory on the external or
233      * shared storage device; returns null if the external or shared storage device is temporarily unavailable.
234      */
235     std::string GetExternalCacheDir() override;
236 
237     /**
238      * @brief Obtains the absolute path to the directory for storing files for the application on the
239      * primary external or shared storage device.
240      *
241      * @param type Indicates the type of the file directory to return
242      *
243      * @return Returns the absolute path to the application file directory on the external or shared storage
244      * device; returns null if the external or shared storage device is temporarily unavailable.
245      */
246     std::string GetExternalFilesDir(std::string &type) override;
247 
248     /**
249      * @brief Obtains the directory for storing files for the application on the device's internal storage.
250      *
251      * @return Returns the application file directory.
252      */
253     std::string GetFilesDir() override;
254 
255     /**
256      * @brief Obtains the absolute path which app created and will be excluded from automatic backup to remote storage.
257      * The returned path maybe changed if the application is moved to an adopted storage device.
258      *
259      * @return The path of the directory holding application files that will not be automatically backed up to remote
260      * storage.
261      */
262     std::string GetNoBackupFilesDir() override;
263 
264     /**
265      * @brief Checks whether the current process has the given permission.
266      * You need to call requestPermissionsFromUser(std::vector<std::string>,std::vector<int>, int) to request
267      * a permission only if the current process does not have the specific permission.
268      *
269      * @param permission Indicates the permission to check. This parameter cannot be null.
270      *
271      * @return Returns 0 (IBundleManager.PERMISSION_GRANTED) if the current process has the permission;
272      * returns -1 (IBundleManager.PERMISSION_DENIED) otherwise.
273      */
274     int VerifySelfPermission(const std::string &permission) override;
275 
276     /**
277      * @brief Obtains the bundle name of the current ability.
278      *
279      * @return Returns the bundle name of the current ability.
280      */
281     std::string GetBundleName() const override;
282 
283     /**
284      * @brief Obtains the path of the OHOS Ability Package (HAP} containing this ability.
285      *
286      * @return Returns the path of the HAP containing this ability.
287      */
288     std::string GetBundleResourcePath() override;
289 
290     /**
291      * @brief Starts a new ability.
292      * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method
293      * to start a specific ability. The system locates the target ability from installed abilities based on the value
294      * of the want parameter and then starts it. You can specify the ability to start using the want parameter.
295      *
296      * @param want Indicates the Want containing information about the target ability to start.
297      *
298      * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE
299      * template is started. You can define the request code to identify the results returned by abilities. The value
300      * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE
301      * template.
302      *
303      * @return errCode ERR_OK on success, others on failure.
304      */
305     ErrCode StartAbility(const AAFwk::Want &want, int requestCode) override;
306 
307     /**
308      * @brief Remove permissions for all users who have access to specific permissions
309      *
310      * @param permission Indicates the permission to unauth. This parameter cannot be null.
311      * @param uri Indicates the URI to unauth. This parameter cannot be null.
312      * @param uid Indicates the UID of the unauth to check.
313      *
314      */
315     void UnauthUriPermission(const std::string &permission, const Uri &uri, int uid) override;
316 
317     /**
318      * @brief Obtains an ability manager.
319      * The ability manager provides information about running processes and memory usage of an application.
320      *
321      * @return Returns an IAbilityManager instance.
322      */
323     sptr<AAFwk::IAbilityManager> GetAbilityManager() override;
324 
325     /**
326      * @brief Obtains the type of this application.
327      *
328      * @return Returns system if this application is a system application;
329      * returns normal if it is released in OHOS AppGallery;
330      * returns other if it is released by a third-party vendor;
331      * returns an empty string if the query fails.
332      */
333     std::string GetAppType() override;
334 
335     /**
336      * @brief Destroys another ability you had previously started by calling Ability.startAbilityForResult
337      * (ohos.aafwk.content.Want, int, ohos.aafwk.ability.startsetting.AbilityStartSetting) with the same requestCode
338      * passed.
339      *
340      * @param requestCode Indicates the request code passed for starting the ability.
341      *
342      * @return errCode ERR_OK on success, others on failure.
343      */
344     ErrCode TerminateAbility(int requestCode) override;
345 
346     /**
347      * @brief Query whether the application of the specified PID and UID has been granted a certain permission
348      *
349      * @param permissions Indicates the list of permissions to be requested. This parameter cannot be null.
350      * @param pid Process id
351      * @param uid
352      * @return Returns 0 (IBundleManager.PERMISSION_GRANTED) if the current process has the permission;
353      * returns -1 (IBundleManager.PERMISSION_DENIED) otherwise.
354      */
355     virtual int VerifyPermission(const std::string &permission, int pid, int uid) override;
356 
357     /**
358      * @brief Obtains the distributed file path.
359      * If the distributed file path does not exist, the system creates one and returns the created path. This method is
360      * applicable only to the context of an ability rather than that of an application.
361      *
362      * @return Returns the distributed file.
363      */
364     std::string GetDistributedDir() override;
365 
366     /**
367      * @brief Sets the pattern of this Context based on the specified pattern ID.
368      *
369      * @param patternId Indicates the resource ID of the pattern to set.
370      */
371     void SetPattern(int patternId) override;
372 
373     /**
374      * @brief Obtains the Context object of this ability.
375      *
376      * @return Returns the Context object of this ability.
377      */
378     std::shared_ptr<Context> GetAbilityPackageContext() override;
379 
380     /**
381      * @brief Obtains the HapModuleInfo object of the application.
382      *
383      * @return Returns the HapModuleInfo object of the application.
384      */
385     std::shared_ptr<HapModuleInfo> GetHapModuleInfo() override;
386 
387     /**
388      * @brief Obtains the name of the current process.
389      *
390      * @return Returns the current process name.
391      */
392     std::string GetProcessName() override;
393 
394     /**
395      * @brief Obtains the bundle name of the ability that called the current ability.
396      * You can use the obtained bundle name to check whether the calling ability is allowed to receive the data you will
397      * send. If you did not use Ability.startAbilityForResult(ohos.aafwk.content.Want, int,
398      * ohos.aafwk.ability.startsetting.AbilityStartSetting) to start the calling ability, null is returned.
399      *
400      * @return Returns the bundle name of the calling ability; returns null if no calling ability is available.
401      */
402     std::string GetCallingBundle() override;
403 
404     /**
405      * @brief Requests certain permissions from the system.
406      * This method is called for permission request. This is an asynchronous method. When it is executed,
407      * the task will be called back.
408      *
409      * @param permissions Indicates the list of permissions to be requested. This parameter cannot be null.
410      * @param permissionsState Indicates the list of permissions' state to be requested. This parameter cannot be null.
411      * @param task The callback or promise fo js interface.
412      */
413     void RequestPermissionsFromUser(std::vector<std::string> &permissions, std::vector<int> &permissionsState,
414         PermissionRequestTask &&task) override;
415 
416     /**
417      * @brief Starts a new ability with special ability start setting.
418      *
419      * @param want Indicates the Want containing information about the target ability to start.
420      * @param requestCode Indicates the request code returned after the ability is started. You can define the request
421      * code to identify the results returned by abilities. The value ranges from 0 to 65535.
422      * @param abilityStartSetting Indicates the special start setting used in starting ability.
423      *
424      * @return errCode ERR_OK on success, others on failure.
425      */
426     ErrCode StartAbility(const Want &want, int requestCode, const AbilityStartSetting &abilityStartSetting) override;
427 
428     /**
429      * @brief Destroys the current ability.
430      *
431      * @return errCode ERR_OK on success, others on failure.
432      */
433     ErrCode TerminateAbility() override;
434 
435     /**
436      * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template.
437      *
438      * @param want Indicates the want containing information about the ability to connect
439      *
440      * @param conn Indicates the callback object when the target ability is connected.
441      *
442      * @return True means success and false means failure
443      */
444     bool ConnectAbility(const Want &want, const sptr<AAFwk::IAbilityConnection> &conn) override;
445 
446     /**
447      * @brief Disconnects the current ability from an ability
448      *
449      * @param conn Indicates the IAbilityConnection callback object passed by connectAbility after the connection
450      *              is set up. The IAbilityConnection object uniquely identifies a connection between two abilities.
451      *
452      * @return errCode ERR_OK on success, others on failure.
453      */
454     ErrCode DisconnectAbility(const sptr<AAFwk::IAbilityConnection> &conn) override;
455 
456     /**
457      * @brief init the ResourceManager for ContextDeal.
458      *
459      * @param the ResourceManager has been inited.
460      *
461      */
462     void initResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> &resourceManager);
463 
464     /**
465      * @brief Obtains information about the caller of this ability.
466      *
467      * @return Returns the caller information.
468      */
469     Uri GetCaller() override;
470 
471     /**
472      * @brief SerUriString
473      */
474     void SerUriString(const std::string &uri);
475 
476     /**
477      * @brief Get the string of this Context based on the specified resource ID.
478      *
479      * @param resId Indicates the resource ID of the string to get.
480      *
481      * @return Returns the string of this Context.
482      */
483     std::string GetString(int resId) override;
484 
485     /**
486      * @brief Get the string array of this Context based on the specified resource ID.
487      *
488      * @param resId Indicates the resource ID of the string array to get.
489      *
490      * @return Returns the string array of this Context.
491      */
492     std::vector<std::string> GetStringArray(int resId) override;
493 
494     /**
495      * @brief Get the integer array of this Context based on the specified resource ID.
496      *
497      * @param resId Indicates the resource ID of the integer array to get.
498      *
499      * @return Returns the integer array of this Context.
500      */
501     std::vector<int> GetIntArray(int resId) override;
502 
503     /**
504      * @brief Obtains the theme of this Context.
505      *
506      * @return theme Returns the theme of this Context.
507      */
508     std::map<std::string, std::string> GetTheme() override;
509 
510     /**
511      * @brief Sets the theme of this Context based on the specified theme ID.
512      *
513      * @param themeId Indicates the resource ID of the theme to set.
514      */
515     void SetTheme(int themeId) override;
516 
517     /**
518      * @brief Obtains the pattern of this Context.
519      *
520      * @return getPattern in interface Context
521      */
522     std::map<std::string, std::string> GetPattern() override;
523 
524     /**
525      * @brief Get the color of this Context based on the specified resource ID.
526      *
527      * @param resId Indicates the resource ID of the color to get.
528      *
529      * @return Returns the color value of this Context.
530      */
531     int GetColor(int resId) override;
532 
533     /**
534      * @brief Obtains the theme id of this Context.
535      *
536      * @return int Returns the theme id of this Context.
537      */
538     int GetThemeId() override;
539 
540     /**
541      * @brief
542      * Destroys this Service ability if the number of times it has been started equals the number represented by the
543      * given {@code startId}. This method is the same as calling {@link #terminateAbility} to destroy this Service
544      * ability, except that this method helps you avoid destroying it if a client has requested a Service
545      * ability startup in {@link ohos.aafwk.ability.Ability#onCommand} but you are unaware of it.
546      *
547      * @param startId Indicates the number of startup times of this Service ability passed to
548      *                {@link ohos.aafwk.ability.Ability#onCommand}. The {@code startId} is
549      *                incremented by 1 every time this ability is started. For example,
550      *                if this ability has been started for six times, the value of {@code startId} is {@code 6}.
551      *
552      * @return Returns {@code true} if the {@code startId} matches the number of startup times
553      *         and this Service ability will be destroyed; returns {@code false} otherwise.
554      */
555     bool TerminateAbilityResult(int startId) override;
556 
557     /**
558      * @brief Obtains the current display orientation of this ability.
559      *
560      * @return Returns the current display orientation.
561      */
562     int GetDisplayOrientation() override;
563 
564     /**
565      * @brief Obtains the path storing the preference file of the application.
566      *        If the preference file path does not exist, the system creates one and returns the created path.
567      *
568      * @return Returns the preference file path .
569      */
570     std::string GetPreferencesDir() override;
571 
572     /**
573      * @brief Set color mode
574      *
575      * @param the value of color mode.
576      */
577     void SetColorMode(int mode) override;
578 
579     /**
580      * @brief Obtains color mode.
581      *
582      * @return Returns the color mode value.
583      */
584     int GetColorMode() override;
585 
586     /**
587      * @brief Set the LifeCycleStateInfo to the deal.
588      *
589      * @param info the info to set.
590      */
591     void SetLifeCycleStateInfo(const AAFwk::LifeCycleStateInfo &info);
592 
593     /**
594      * @brief Obtains the unique ID of the mission containing this ability.
595      *
596      * @return Returns the unique mission ID.
597      */
598     int GetMissionId() override;
599 
600     /**
601      * @brief Starts multiple abilities.
602      *
603      * @param wants Indicates the Want containing information array about the target ability to start.
604      */
605     void StartAbilities(const std::vector<AAFwk::Want> &wants) override;
606 
607     /**
608      * @brief Set EventRunner for main thread.
609      *
610      * @param runner The EventRunner.
611      */
612     void SetRunner(const std::shared_ptr<EventRunner> &runner);
613 
614     /**
615      * @brief Obtains the application base directory on the device's internal storage.
616      *
617      * @return Returns the application base directory.
618      */
619     std::string GetBaseDir() const;
620 
621     /**
622      * @brief Obtains the lifecycle state info.
623      *
624      * @return Returns the lifecycle state info.
625      */
626     AAFwk::LifeCycleStateInfo GetLifeCycleStateInfo() const;
627 public:
628     static const int64_t CONTEXT_CREATE_BY_SYSTEM_APP;
629     static const std::string CONTEXT_DEAL_FILE_SEPARATOR;
630     static const std::string CONTEXT_DEAL_CODE_CACHE;
631     static const std::string CONTEXT_DEAL_Files;
632     static const std::string CONTEXT_DEAL_NO_BACKUP_Files;
633     static const std::string CONTEXT_DEAL_DIRNAME;
634     static const std::string CONTEXT_DISTRIBUTED_BASE_BEFORE;
635     static const std::string CONTEXT_DISTRIBUTED_BASE_MIDDLE;
636     static const std::string CONTEXT_DISTRIBUTED;
637     static const std::string CONTEXT_DATA_STORAGE;
638     static const std::string CONTEXT_ELS[];
639     static const std::string CONTEXT_DEAL_DATA_APP;
640     static const std::string CONTEXT_DEAL_BASE;
641     static const std::string CONTEXT_DEAL_DATABASE;
642     static const std::string CONTEXT_DEAL_PREFERENCES;
643     static const std::string CONTEXT_DEAL_DISTRIBUTEDFILES;
644     static const std::string CONTEXT_DEAL_CACHE;
645     static const std::string CONTEXT_DEAL_DATA;
646     static const int EL_DEFAULT = 1;
647     int flags_ = 0x00000000;
648 
649 protected:
650     sptr<IRemoteObject> GetToken() override;
651     bool HapModuleInfoRequestInit();
652 
653 private:
654     bool IsCreateBySystemApp() const;
655     int GetCurrentAccountId() const;
656     void CreateDirIfNotExist(const std::string& dirPath) const;
657 
658     std::shared_ptr<ProcessInfo> processInfo_ = nullptr;
659     std::shared_ptr<ApplicationInfo> applicationInfo_ = nullptr;
660     std::shared_ptr<AbilityInfo> abilityInfo_ = nullptr;
661     std::shared_ptr<Profile> profile_ = nullptr;
662     std::shared_ptr<Context> appContext_ = nullptr;
663     std::shared_ptr<Context> abilityContext_ = nullptr;
664     std::shared_ptr<Global::Resource::ResourceManager> resourceManager_ = nullptr;
665     std::string path_ = "";
666     std::string uriString_ = "";
667     std::string preferenceDir_ = "";
668     std::map<std::string, std::string> pattern_;
669     std::map<std::string, std::string> theme_;
670     AAFwk::LifeCycleStateInfo lifeCycleStateInfo_;
671     std::shared_ptr<EventRunner> mainEventRunner_;
672     std::shared_ptr<HapModuleInfo> hapModuleInfoLocal_ = nullptr;
673     bool isCreateBySystemApp_ = false;
674     std::string currArea_ = "el2";
675 };
676 }  // namespace AppExecFwk
677 }  // namespace OHOS
678 #endif  // OHOS_ABILITY_RUNTIME_CONTEXT_DEAL_H
679