1 /* 2 * Copyright (c) 2021 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 KVSTORE_DATASERVICE_H 17 #define KVSTORE_DATASERVICE_H 18 19 #include <map> 20 #include <mutex> 21 #include <set> 22 23 #include "account_delegate.h" 24 #include "feature_stub_impl.h" 25 #include "ikvstore_data_service.h" 26 #include "ithread_pool.h" 27 #include "kvstore_device_listener.h" 28 #include "kvstore_meta_manager.h" 29 #include "kvstore_data_service_stub.h" 30 #include "metadata/store_meta_data.h" 31 #include "reporter.h" 32 #include "runtime_config.h" 33 #include "security/security.h" 34 #include "system_ability.h" 35 #include "executor_pool.h" 36 #include "types.h" 37 38 namespace OHOS::DistributedKv { 39 class KvStoreAccountObserver; 40 class KvStoreDataService : public SystemAbility, public KvStoreDataServiceStub { 41 DECLARE_SYSTEM_ABILITY(KvStoreDataService); 42 using Handler = std::function<void(int, std::map<std::string, std::vector<std::string>> &)>; 43 44 public: 45 struct UserInfo { 46 std::string userId; 47 std::set<std::string> bundles; 48 }; 49 struct BundleInfo { 50 std::string bundleName; 51 std::string appId; 52 std::string type; 53 int32_t uid; 54 uint32_t tokenId; 55 std::string userId; 56 std::set<std::string> storeIDs; 57 }; 58 using StoreMetaData = DistributedData::StoreMetaData; 59 // record kvstore meta version for compatible, should update when modify kvstore meta structure. 60 static constexpr uint32_t STORE_VERSION = 0x03000001; 61 62 explicit KvStoreDataService(bool runOnCreate = false); 63 explicit KvStoreDataService(int32_t systemAbilityId, bool runOnCreate = false); 64 virtual ~KvStoreDataService(); 65 66 void RegisterHandler(const std::string &name, Handler &handler); 67 void RegisterStoreInfo(); 68 bool IsExist(const std::string &infoName, std::map<std::string, std::vector<std::string>> &filterInfo, 69 std::string &metaParam); 70 void DumpStoreInfo(int fd, std::map<std::string, std::vector<std::string>> ¶ms); 71 void FilterData(std::vector<StoreMetaData> &metas, std::map<std::string, std::vector<std::string>> &filterInfo); 72 void PrintfInfo(int fd, const std::vector<StoreMetaData> &metas); 73 std::string GetIndentation(int size); 74 75 void RegisterUserInfo(); 76 void BuildData(std::map<std::string, UserInfo> &datas, const std::vector<StoreMetaData> &metas); 77 void PrintfInfo(int fd, const std::map<std::string, UserInfo> &datas); 78 void DumpUserInfo(int fd, std::map<std::string, std::vector<std::string>> ¶ms); 79 80 void RegisterBundleInfo(); 81 void BuildData(std::map<std::string, BundleInfo> &datas, const std::vector<StoreMetaData> &metas); 82 void PrintfInfo(int fd, const std::map<std::string, BundleInfo> &datas); 83 void DumpBundleInfo(int fd, std::map<std::string, std::vector<std::string>> ¶ms); 84 85 Status RegisterClientDeathObserver(const AppId &appId, sptr<IRemoteObject> observer) override; 86 87 sptr<IRemoteObject> GetFeatureInterface(const std::string &name) override; 88 89 int32_t ClearAppStorage(const std::string &bundleName, int32_t userId, int32_t appIndex, int32_t tokenId) override; 90 91 void OnDump() override; 92 93 int Dump(int fd, const std::vector<std::u16string> &args) override; 94 95 void OnStart() override; 96 97 void OnStop() override; 98 99 void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 100 101 void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; 102 103 void AccountEventChanged(const AccountEventInfo &eventInfo); 104 105 void SetCompatibleIdentify(const AppDistributedKv::DeviceInfo &info) const; 106 107 void OnDeviceOnline(const AppDistributedKv::DeviceInfo &info); 108 109 void OnDeviceOffline(const AppDistributedKv::DeviceInfo &info); 110 111 void OnDeviceOnReady(const AppDistributedKv::DeviceInfo &info); 112 113 void OnSessionReady(const AppDistributedKv::DeviceInfo &info); 114 115 int32_t OnUninstall(const std::string &bundleName, int32_t user, int32_t index); 116 117 int32_t OnUpdate(const std::string &bundleName, int32_t user, int32_t index); 118 119 int32_t OnInstall(const std::string &bundleName, int32_t user, int32_t index); 120 121 int32_t OnScreenUnlocked(int32_t user); 122 123 private: 124 void NotifyAccountEvent(const AccountEventInfo &eventInfo); 125 class KvStoreClientDeathObserverImpl { 126 public: 127 KvStoreClientDeathObserverImpl(const AppId &appId, KvStoreDataService &service, sptr<IRemoteObject> observer); 128 explicit KvStoreClientDeathObserverImpl(KvStoreDataService &service); 129 explicit KvStoreClientDeathObserverImpl(KvStoreClientDeathObserverImpl &&impl); 130 KvStoreClientDeathObserverImpl &operator=(KvStoreClientDeathObserverImpl &&impl); 131 132 virtual ~KvStoreClientDeathObserverImpl(); 133 134 pid_t GetPid() const; 135 136 private: 137 class KvStoreDeathRecipient : public IRemoteObject::DeathRecipient { 138 public: 139 explicit KvStoreDeathRecipient(KvStoreClientDeathObserverImpl &kvStoreClientDeathObserverImpl); 140 virtual ~KvStoreDeathRecipient(); 141 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 142 143 private: 144 KvStoreClientDeathObserverImpl &kvStoreClientDeathObserverImpl_; 145 }; 146 void NotifyClientDie(); 147 void Reset(); 148 pid_t uid_; 149 pid_t pid_; 150 uint32_t token_; 151 AppId appId_; 152 KvStoreDataService &dataService_; 153 sptr<IRemoteObject> observerProxy_; 154 sptr<KvStoreDeathRecipient> deathRecipient_; 155 }; 156 157 void Initialize(); 158 159 void LoadFeatures(); 160 161 void StartService(); 162 163 void InitSecurityAdapter(std::shared_ptr<ExecutorPool> executors); 164 165 void OnStoreMetaChanged(const std::vector<uint8_t> &key, const std::vector<uint8_t> &value, CHANGE_FLAG flag); 166 167 Status AppExit(pid_t uid, pid_t pid, uint32_t token, const AppId &appId); 168 169 void LoadConfigs(); 170 171 static constexpr int TEN_SEC = 10; 172 173 ConcurrentMap<uint32_t, std::map<int32_t, KvStoreClientDeathObserverImpl>> clients_; 174 std::shared_ptr<KvStoreAccountObserver> accountEventObserver_; 175 176 std::shared_ptr<Security> security_; 177 ConcurrentMap<std::string, sptr<DistributedData::FeatureStubImpl>> features_; 178 std::shared_ptr<KvStoreDeviceListener> deviceInnerListener_; 179 std::shared_ptr<ExecutorPool> executors_; 180 static constexpr int VERSION_WIDTH = 11; 181 static constexpr const char *INDENTATION = " "; 182 static constexpr int32_t FORMAT_BLANK_SIZE = 32; 183 static constexpr char FORMAT_BLANK_SPACE = ' '; 184 static constexpr int32_t PRINTF_COUNT_2 = 2; 185 static constexpr int MAXIMUM_PARAMETER_LIMIT = 3; 186 static constexpr pid_t INVALID_UID = -1; 187 static constexpr pid_t INVALID_PID = -1; 188 static constexpr uint32_t INVALID_TOKEN = 0; 189 }; 190 } 191 #endif // KVSTORE_DATASERVICE_H 192