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 100 private: 101 std::mutex bundleMgrMutex_; 102 std::mutex initOnceMutex_; 103 sptr<IBundleMgr> bundleMgr_ = nullptr; 104 sptr<IRemoteObject::DeathRecipient> bmsDeathRecipient_ = nullptr; 105 string stiching = "This is used for Name Stiching"; 106 bool initOnce = false; 107 108 shared_future<int32_t> bmsFuture_; 109 shared_future<int32_t> accountFuture_; 110 shared_future<int32_t> commEventFuture_; 111 112 bool QueryDriverInfos(const std::string &bundleName, const int userId, 113 std::vector<ExtensionAbilityInfo> &driverInfos); 114 bool UpdateToRdb(const std::vector<ExtensionAbilityInfo> &driverInfos, const std::string &bundleName = "", 115 const std::string &interfaceName = ""); 116 void ClearDriverInfo(DriverInfo &tmpDrvInfo); 117 bool GetBundleMgrProxy(); 118 int32_t GetCurrentActiveUserId(); 119 void ChangeValue(DriverInfo &tmpDrvInfo, const map<string, string> &metadata); 120 std::string GetBundleSize(const std::string &bundleName); 121 std::vector<DriverInfo> ParseToPkgInfoTables( 122 const std::vector<ExtensionAbilityInfo> &driverInfos, std::vector<PkgInfoTable> &pkgInfoTables); 123 PkgInfoTable CreatePkgInfoTable(const ExtensionAbilityInfo &driverInfo, string driverInfoStr); 124 bool IsCurrentUserId(const int userId); 125 void OnBundleDrvRemoved(const std::string &bundleName, const std::string &interfaceName); 126 void ResetBundleMgr(); 127 }; 128 129 class BundleMgrDeathRecipient : public IRemoteObject::DeathRecipient { 130 public: BundleMgrDeathRecipient(std::function<void ()> callback)131 explicit BundleMgrDeathRecipient(std::function<void()> callback) 132 { 133 callback_ = callback; 134 } ~BundleMgrDeathRecipient()135 ~BundleMgrDeathRecipient() 136 { 137 callback_ = nullptr; 138 } 139 OnRemoteDied(const wptr<IRemoteObject> & remote)140 void OnRemoteDied(const wptr<IRemoteObject> &remote) override 141 { 142 if (callback_ != nullptr) { 143 callback_(); 144 } 145 } 146 private: 147 std::function<void()> callback_ = nullptr; 148 }; 149 150 } // namespace 151 } 152 #endif // DRIVER_BUNDLE_STATUS_CALLBACK_H