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 DRIVER_BUNDLE_STATUS_CALLBACK_H
17 #define DRIVER_BUNDLE_STATUS_CALLBACK_H
18 
19 #include <stdint.h>
20 #include <vector>
21 #include <map>
22 #include <iostream>
23 
24 #include "bundle_info.h"
25 #include "bundle_mgr_proxy.h"
26 #include "extension_ability_info.h"
27 #include "ibus_extension.h"
28 #include "pkg_tables.h"
29 #include "ibundle_update_callback.h"
30 #include <future>
31 namespace OHOS {
32 namespace ExternalDeviceManager {
33 using namespace std;
34 using namespace OHOS;
35 using namespace OHOS::AAFwk;
36 using namespace OHOS::AppExecFwk;
37 using namespace OHOS::ExternalDeviceManager;
38 
39 enum {
40     ERR_DRV_STATUS_CALLBACK_ERROR = 1,
41 };
42 
43 enum ON_BUNDLE_STATUS {
44     BUNDLE_NULL,
45     BUNDLE_ADDED = 1,
46     BUNDLE_UPDATED,
47     BUNDLE_REMOVED,
48 };
49 
50 typedef int32_t(*PCALLBACKFUN)(int, int, const string &, const string &);
51 
52 class DrvBundleStateCallback : public IBundleStatusCallback {
53 public:
54     DrvBundleStateCallback();
55     DrvBundleStateCallback(shared_future<int32_t> bmsFuture, shared_future<int32_t> accountFuture,
56         shared_future<int32_t> commEventFuture);
57     ~DrvBundleStateCallback();
58 
59     void PrintTest();
60 
61     virtual void OnBundleStateChanged(const uint8_t installType, const int32_t resultCode,
62         const std::string &resultMsg, const std::string &bundleName) override;
63 
64     /**
65      * @brief Called when a new application package has been installed on the device.
66      * @param bundleName Indicates the name of the bundle whose state has been installed.
67      * @param userId Indicates the id of the bundle whose state has been installed.
68      */
69     virtual void OnBundleAdded(const std::string &bundleName, const int userId) override;
70     /**
71      * @brief Called when a new application package has been Updated on the device.
72      * @param bundleName Indicates the name of the bundle whose state has been Updated.
73      * @param userId Indicates the id of the bundle whose state has been Updated.
74      */
75     virtual void OnBundleUpdated(const std::string &bundleName, const int userId) override;
76     /**
77      * @brief Called when a new application package has been Removed on the device.
78      * @param bundleName Indicates the name of the bundle whose state has been Removed.
79      * @param userId Indicates the id of the bundle whose state has been Removed.
80      */
81     virtual void OnBundleRemoved(const std::string &bundleName, const int userId) override;
82 
83     virtual sptr<IRemoteObject> AsObject() override;
84 
85     bool GetAllDriverInfos(bool isExecCallback = true);
86     void GetAllDriverInfosAsync(bool isExecCallback = true);
87 
88     bool CheckBundleMgrProxyPermission();
89 
90     string GetStiching();
91 
92     PCALLBACKFUN m_pFun = nullptr;
93     std::shared_ptr<IBundleUpdateCallback> bundleUpdateCallback_ = nullptr;
94 
95     void ResetInitOnce();
96     void ResetMatchedBundles(const int32_t userId);
97 
98 private:
99     std::mutex bundleMgrMutex_;
100     std::mutex initOnceMutex_;
101     sptr<IBundleMgr> bundleMgr_ = nullptr;
102     string stiching = "This is used for Name Stiching";
103     bool initOnce = false;
104 
105     shared_future<int32_t> bmsFuture_;
106     shared_future<int32_t> accountFuture_;
107     shared_future<int32_t> commEventFuture_;
108 
109     bool QueryDriverInfos(const std::string &bundleName, const int userId,
110         std::vector<ExtensionAbilityInfo> &driverInfos);
111     bool UpdateToRdb(const std::vector<ExtensionAbilityInfo> &driverInfos, const std::string &bundleName = "",
112         bool isExecCallback = true);
113     void ClearDriverInfo(DriverInfo &tmpDrvInfo);
114     sptr<OHOS::AppExecFwk::IBundleMgr> GetBundleMgrProxy();
115     int32_t GetCurrentActiveUserId();
116     void ChangeValue(DriverInfo &tmpDrvInfo, const map<string, string> &metadata);
117     std::string GetBundleSize(const std::string &bundleName);
118     void ParseToPkgInfoTables(
119         const std::vector<ExtensionAbilityInfo> &driverInfos, std::vector<PkgInfoTable> &pkgInfoTables);
120     PkgInfoTable CreatePkgInfoTable(const ExtensionAbilityInfo &driverInfo, string driverInfoStr);
121     bool IsCurrentUserId(const int userId);
122 
123     void OnBundleDrvAdded(int bundleStatus);
124     void OnBundleDrvUpdated(int bundleStatus);
125     void OnBundleDrvRemoved(const std::string &bundleName);
126 };
127 } // namespace
128 }
129 #endif // DRIVER_BUNDLE_STATUS_CALLBACK_H