1 /*
2  * Copyright (c) 2023 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 FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_SHARED_BUNDLE_INSTALLER_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_SHARED_BUNDLE_INSTALLER_H
18 
19 #include <memory>
20 #include <string>
21 #include <unordered_map>
22 
23 #include "event_report.h"
24 #include "inner_shared_bundle_installer.h"
25 #include "install_param.h"
26 #include "inner_bundle_info.h"
27 #include "nocopyable.h"
28 
29 namespace OHOS {
30 namespace AppExecFwk {
31 class SharedBundleInstaller {
32 public:
33     /**
34      * @brief Cross-app shared bundle installer.
35      * @param installParam Indicates the install param.
36      * @param appType Indicates the type of the application.
37      */
38     SharedBundleInstaller(const InstallParam &installParam, const Constants::AppType appType);
39     virtual ~SharedBundleInstaller();
40 
41     /**
42      * @brief Parse cross-app hsp files.
43      * @return Returns ERR_OK if the files are parsed successfully; returns error code otherwise.
44      */
45     ErrCode ParseFiles();
46 
47     /**
48      * @brief Checks whether to install cross-app shared bundles.
49      * @return Returns true if the files need to be installed; returns false otherwise.
50      */
NeedToInstall()51     inline bool NeedToInstall() const
52     {
53         return !innerInstallers_.empty();
54     }
55 
56     /**
57      * @brief Checks whether the dependencies of a bundle are satisfied in installing or installed shared bundles.
58      * @param innerBundleInfo Indicates the InnerBundleInfo object to be checked.
59      * @return Returns true if the dependencies are satisfied; returns false otherwise.
60      */
61     bool CheckDependency(const InnerBundleInfo &innerBundleInfo) const;
62 
63     /**
64      * @brief Install cross-app shared bundles.
65      * @param eventTemplate Indicates the template of EventInfo to send after installation.
66      * @return Returns ERR_OK if the files are installed successfully; returns error code otherwise.
67      */
68     ErrCode Install(const EventInfo &eventTemplate);
69 
70 private:
71     bool FindDependencyInInstalledBundles(const Dependency &dependency) const;
72     void SendBundleSystemEvent(const EventInfo &eventTemplate, ErrCode errCode);
73     void GetCallingEventInfo(EventInfo &eventInfo);
74 
75     InstallParam installParam_;
76     const Constants::AppType appType_;
77     // the key is the bundle name of cross-app shared bundle to be installed
78     std::unordered_map<std::string, std::shared_ptr<InnerSharedBundleInstaller>> innerInstallers_;
79 
80     DISALLOW_COPY_AND_MOVE(SharedBundleInstaller);
81 
82 #define CHECK_RESULT(errcode, errmsg)                                              \
83     do {                                                                           \
84         if ((errcode) != ERR_OK) {                                                   \
85             APP_LOGE(errmsg, errcode);                                             \
86             return errcode;                                                        \
87         }                                                                          \
88     } while (0)
89 };
90 }  // namespace AppExecFwk
91 }  // namespace OHOS
92 #endif  // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_SHARED_BUNDLE_INSTALLER_H