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 #include "iremote_object.h" 32 33 namespace OHOS { 34 namespace ExternalDeviceManager { 35 using namespace std; 36 using namespace OHOS; 37 using namespace OHOS::AAFwk; 38 using namespace OHOS::AppExecFwk; 39 using namespace OHOS::ExternalDeviceManager; 40 41 enum { 42 ERR_DRV_STATUS_CALLBACK_ERROR = 1, 43 }; 44 45 enum ON_BUNDLE_STATUS { 46 BUNDLE_NULL, 47 BUNDLE_ADDED = 1, 48 BUNDLE_UPDATED, 49 BUNDLE_REMOVED, 50 }; 51 52 typedef int32_t(*PCALLBACKFUN)(int, int, const string &, const string &); 53 54 class DrvBundleStateCallback : public IBundleStatusCallback { 55 public: 56 DrvBundleStateCallback(); 57 DrvBundleStateCallback(shared_future<int32_t> bmsFuture, shared_future<int32_t> accountFuture, 58 shared_future<int32_t> commEventFuture); 59 ~DrvBundleStateCallback(); 60 61 void PrintTest(); 62 63 virtual void OnBundleStateChanged(const uint8_t installType, const int32_t resultCode, 64 const std::string &resultMsg, const std::string &bundleName) override; 65 66 /** 67 * @brief Called when a new application package has been installed on the device. 68 * @param bundleName Indicates the name of the bundle whose state has been installed. 69 * @param userId Indicates the id of the bundle whose state has been installed. 70 */ 71 virtual void OnBundleAdded(const std::string &bundleName, const int userId) override; 72 /** 73 * @brief Called when a new application package has been Updated on the device. 74 * @param bundleName Indicates the name of the bundle whose state has been Updated. 75 * @param userId Indicates the id of the bundle whose state has been Updated. 76 */ 77 virtual void OnBundleUpdated(const std::string &bundleName, const int userId) override; 78 /** 79 * @brief Called when a new application package has been Removed on the device. 80 * @param bundleName Indicates the name of the bundle whose state has been Removed. 81 * @param userId Indicates the id of the bundle whose state has been Removed. 82 */ 83 virtual void OnBundleRemoved(const std::string &bundleName, const int userId) override; 84 85 virtual sptr<IRemoteObject> AsObject() override; 86 87 bool GetAllDriverInfos(); 88 void GetAllDriverInfosAsync(); 89 90 bool CheckBundleMgrProxyPermission(); 91 92 string GetStiching(); 93 94 PCALLBACKFUN m_pFun = nullptr; 95 std::shared_ptr<IBundleUpdateCallback> bundleUpdateCallback_ = nullptr; 96 97 void ResetInitOnce(); 98 void ResetMatchedBundles(const int32_t userId); 99 void ReportBundleSysEvent(const std::vector<ExtensionAbilityInfo> &driverInfos, 100 const std::string &bundleName, std::string driverEventName); 101 102 private: 103 std::mutex bundleMgrMutex_; 104 std::mutex initOnceMutex_; 105 sptr<IBundleMgr> bundleMgr_ = nullptr; 106 sptr<IRemoteObject::DeathRecipient> bmsDeathRecipient_ = nullptr; 107 string stiching = "This is used for Name Stiching"; 108 bool initOnce = false; 109 110 shared_future<int32_t> bmsFuture_; 111 shared_future<int32_t> accountFuture_; 112 shared_future<int32_t> commEventFuture_; 113 114 bool QueryDriverInfos(const std::string &bundleName, const int userId, 115 std::vector<ExtensionAbilityInfo> &driverInfos); 116 bool UpdateToRdb(const std::vector<ExtensionAbilityInfo> &driverInfos, const std::string &bundleName = ""); 117 void ClearDriverInfo(DriverInfo &tmpDrvInfo); 118 bool GetBundleMgrProxy(); 119 int32_t GetCurrentActiveUserId(); 120 void ChangeValue(DriverInfo &tmpDrvInfo, const map<string, string> &metadata); 121 std::string GetBundleSize(const std::string &bundleName); 122 void ParseToPkgInfoTables( 123 const std::vector<ExtensionAbilityInfo> &driverInfos, std::vector<PkgInfoTable> &pkgInfoTables); 124 PkgInfoTable CreatePkgInfoTable(const ExtensionAbilityInfo &driverInfo, string driverInfoStr); 125 bool IsCurrentUserId(const int userId); 126 127 void OnBundleDrvAdded(int bundleStatus); 128 void OnBundleDrvUpdated(int bundleStatus); 129 void OnBundleDrvRemoved(const std::string &bundleName); 130 void ResetBundleMgr(); 131 std::string ParseIdVector(std::vector<uint16_t> ids); 132 int ParseVersionCode(const std::vector<ExtensionAbilityInfo> &driverInfos, const std::string &bundleName); 133 }; 134 135 class BundleMgrDeathRecipient : public IRemoteObject::DeathRecipient { 136 public: BundleMgrDeathRecipient(std::function<void ()> callback)137 explicit BundleMgrDeathRecipient(std::function<void()> callback) 138 { 139 callback_ = callback; 140 } ~BundleMgrDeathRecipient()141 ~BundleMgrDeathRecipient() 142 { 143 callback_ = nullptr; 144 } 145 OnRemoteDied(const wptr<IRemoteObject> & remote)146 void OnRemoteDied(const wptr<IRemoteObject> &remote) override 147 { 148 if (callback_ != nullptr) { 149 callback_(); 150 } 151 } 152 private: 153 std::function<void()> callback_ = nullptr; 154 }; 155 156 } // namespace 157 } 158 #endif // DRIVER_BUNDLE_STATUS_CALLBACK_H