Signed-off-by: yangliu <yangliu178@huawei.com>
This commit is contained in:
yangliu 2024-11-18 21:45:21 +08:00
parent 0c4e82cd89
commit d17fe77b07
12 changed files with 137 additions and 166 deletions

View File

@ -78,12 +78,12 @@ int32_t FeatureStubImpl::OnAppInstall(const std::string &bundleName, int32_t use
}
int32_t FeatureStubImpl::ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam &param,
StoreMetaData meta, bool isTriple)
bool &isFindIdentifier)
{
if (featureImpl_ == nullptr) {
return -1;
}
return featureImpl_->ResolveAutoLaunch(identifier, param, meta, isTriple);
return featureImpl_->ResolveAutoLaunch(identifier, param, isFindIdentifier);
}
int32_t FeatureStubImpl::OnUserChange(uint32_t code, const std::string &user, const std::string &account)

View File

@ -36,7 +36,7 @@ public:
int32_t OnAppUpdate(const std::string &bundleName, int32_t user, int32_t index);
int32_t OnAppInstall(const std::string &bundleName, int32_t user, int32_t index);
int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam &param,
StoreMetaData meta = StoreMetaData(), bool isTriple = false);
bool &isFindIdentifier);
int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account);
int32_t Online(const std::string &device);
int32_t Offline(const std::string &device);

View File

@ -363,14 +363,13 @@ void KvStoreDataService::StartService()
// subscribe account event listener to EventNotificationMgr
auto autoLaunch = [this](const std::string &identifier, DistributedDB::AutoLaunchParam &param) -> bool {
StoreMetaData metaData;
bool isTriple = false;
auto status = ResolveAutoLaunchParamByIdentifier(identifier, param, metaData, isTriple);
features_.ForEachCopies([&identifier, &param, &metaData, &isTriple](const auto &,
bool isFindIdentifier = false;
features_.ForEachCopies([&identifier, &param, &isFindIdentifier](const auto &,
sptr<DistributedData::FeatureStubImpl> &value) {
value->ResolveAutoLaunch(identifier, param, metaData, isTriple);
value->ResolveAutoLaunch(identifier, param, isFindIdentifier);
return false;
});
return status;
return isFindIdentifier;
};
KvStoreDelegateManager::SetAutoLaunchRequestCallback(autoLaunch);
ZLOGI("Start distributedata Success, Publish ret: %{public}d", static_cast<int>(ret));
@ -398,134 +397,6 @@ void KvStoreDataService::OnStoreMetaChanged(
ZLOGI("dirty kv store. storeId:%{public}s", Anonymous::Change(metaData.storeId).c_str());
}
bool KvStoreDataService::CompareTripleIdentifier(const std::string &accountId, const std::string &identifier,
const StoreMetaData &storeMeta)
{
std::vector<std::string> accountIds { accountId, "ohosAnonymousUid", "default" };
for (auto &id : accountIds) {
auto convertedIds =
AppIdMappingConfigManager::GetInstance().Convert(storeMeta.appId, storeMeta.user);
const std::string &tempTripleIdentifier =
DistributedDB::KvStoreDelegateManager::GetKvStoreIdentifier(id, convertedIds.first,
storeMeta.storeId, false);
if (tempTripleIdentifier == identifier) {
ZLOGI("find triple identifier,storeId:%{public}s,id:%{public}s",
Anonymous::Change(storeMeta.storeId).c_str(), Anonymous::Change(id).c_str());
return true;
}
}
return false;
}
bool KvStoreDataService::ResolveAutoLaunchParamByIdentifier(
const std::string &identifier, DistributedDB::AutoLaunchParam &param, StoreMetaData &metaData, bool &isTriple)
{
std::vector<StoreMetaData> entries;
std::string localDeviceId = DmAdapter::GetInstance().GetLocalDevice().uuid;
if (!MetaDataManager::GetInstance().LoadMeta(StoreMetaData::GetPrefix({ localDeviceId }), entries)) {
ZLOGE("get full meta failed");
return false;
}
auto accountId = AccountDelegate::GetInstance()->GetUnencryptedAccountId();
for (const auto &storeMeta : entries) {
if ((!param.userId.empty() && (param.userId != storeMeta.user)) || (localDeviceId != storeMeta.deviceId) ||
((StoreMetaData::STORE_RELATIONAL_BEGIN <= storeMeta.storeType) &&
(StoreMetaData::STORE_RELATIONAL_END >= storeMeta.storeType))) {
// judge local userid and local meta
continue;
}
bool isTripleIdentifierEqual = CompareTripleIdentifier(accountId, identifier, storeMeta);
const std::string &itemDualIdentifier =
DistributedDB::KvStoreDelegateManager::GetKvStoreIdentifier("", storeMeta.appId, storeMeta.storeId, true);
if (isTripleIdentifierEqual && storeMeta.bundleName != Bootstrap::GetInstance().GetProcessLabel()) {
isTriple = isTripleIdentifierEqual;
metaData = storeMeta;
metaData.account = accountId;
}
if (identifier == itemDualIdentifier || isTripleIdentifierEqual) {
ZLOGI("identifier find");
DistributedDB::AutoLaunchOption option;
option.createIfNecessary = false;
option.isEncryptedDb = storeMeta.isEncrypt;
SecretKeyMeta secretKey;
if (storeMeta.isEncrypt && MetaDataManager::GetInstance().LoadMeta(storeMeta.GetSecretKey(), secretKey)) {
std::vector<uint8_t> decryptKey;
CryptoManager::GetInstance().Decrypt(secretKey.sKey, decryptKey);
option.passwd.SetValue(decryptKey.data(), decryptKey.size());
std::fill(decryptKey.begin(), decryptKey.end(), 0);
}
if (storeMeta.bundleName == Bootstrap::GetInstance().GetProcessLabel()) {
param.userId = storeMeta.user;
}
option.schema = storeMeta.schema;
option.createDirByStoreIdOnly = true;
option.dataDir = storeMeta.dataDir;
option.secOption = ConvertSecurity(storeMeta.securityLevel);
option.isAutoSync = storeMeta.isAutoSync;
option.syncDualTupleMode = true; // dual tuple flag
param.appId = storeMeta.appId;
param.storeId = storeMeta.storeId;
param.option = option;
return true;
}
}
ZLOGI("not find identifier");
return false;
}
DistributedDB::SecurityOption KvStoreDataService::ConvertSecurity(int securityLevel)
{
if (securityLevel < SecurityLevel::NO_LABEL || securityLevel > SecurityLevel::S4) {
return {DistributedDB::NOT_SET, DistributedDB::ECE};
}
switch (securityLevel) {
case SecurityLevel::S3:
return {DistributedDB::S3, DistributedDB::SECE};
case SecurityLevel::S4:
return {DistributedDB::S4, DistributedDB::ECE};
default:
return {securityLevel, DistributedDB::ECE};
}
}
Status KvStoreDataService::InitNbDbOption(const Options &options, const std::vector<uint8_t> &cipherKey,
DistributedDB::KvStoreNbDelegate::Option &dbOption)
{
DistributedDB::CipherPassword password;
auto status = password.SetValue(cipherKey.data(), cipherKey.size());
if (status != DistributedDB::CipherPassword::ErrorCode::OK) {
ZLOGE("Failed to set the passwd.");
return Status::DB_ERROR;
}
dbOption.syncDualTupleMode = true; // tuple of (appid+storeid)
dbOption.createIfNecessary = options.createIfMissing;
dbOption.isMemoryDb = (!options.persistent);
dbOption.isEncryptedDb = options.encrypt;
dbOption.isNeedCompressOnSync = options.isNeedCompress;
if (options.encrypt) {
dbOption.cipher = DistributedDB::CipherType::AES_256_GCM;
dbOption.passwd = password;
}
if (options.kvStoreType == KvStoreType::SINGLE_VERSION) {
dbOption.conflictResolvePolicy = DistributedDB::LAST_WIN;
} else if (options.kvStoreType == KvStoreType::DEVICE_COLLABORATION) {
dbOption.conflictResolvePolicy = DistributedDB::DEVICE_COLLABORATION;
} else {
ZLOGE("kvStoreType is invalid");
return Status::INVALID_ARGUMENT;
}
dbOption.schema = options.schema;
dbOption.createDirByStoreIdOnly = true;
dbOption.secOption = ConvertSecurity(options.securityLevel);
return Status::SUCCESS;
}
void KvStoreDataService::OnStop()
{
ZLOGI("begin.");

View File

@ -171,12 +171,6 @@ private:
void LoadConfigs();
bool CompareTripleIdentifier(const std::string &accountId, const std::string &identifier,
const StoreMetaData &storeMeta);
static DistributedDB::SecurityOption ConvertSecurity(int securityLevel);
static Status InitNbDbOption(const Options &options, const std::vector<uint8_t> &cipherKey,
DistributedDB::KvStoreNbDelegate::Option &dbOption);
static constexpr int TEN_SEC = 10;
ConcurrentMap<uint32_t, std::map<int32_t, KvStoreClientDeathObserverImpl>> clients_;

View File

@ -92,7 +92,7 @@ int32_t FeatureSystem::Feature::OnAppInstall(const std::string &bundleName, int3
}
int32_t FeatureSystem::Feature::ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam &param,
StoreMetaData meta, bool isTriple)
bool &isFindIdentifier)
{
return E_OK;
}

View File

@ -52,7 +52,7 @@ public:
virtual int32_t OnAppUpdate(const std::string &bundleName, int32_t user, int32_t index);
virtual int32_t OnAppInstall(const std::string &bundleName, int32_t user, int32_t index);
virtual int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam &param,
StoreMetaData meta = StoreMetaData(), bool isTriple = false);
bool &isFindIdentifier);
virtual int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account);
virtual int32_t Online(const std::string &device);
virtual int32_t Offline(const std::string &device);

View File

@ -59,6 +59,7 @@ using system_clock = std::chrono::system_clock;
using DMAdapter = DistributedData::DeviceManagerAdapter;
using DumpManager = OHOS::DistributedData::DumpManager;
using CommContext = OHOS::DistributedData::CommunicatorContext;
using SecretKeyMeta = DistributedData::SecretKeyMetaData;
static constexpr const char *DEFAULT_USER_ID = "0";
__attribute__((used)) KVDBServiceImpl::Factory KVDBServiceImpl::factory_;
KVDBServiceImpl::Factory::Factory()
@ -754,24 +755,109 @@ int32_t KVDBServiceImpl::OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const
return SUCCESS;
}
int32_t KVDBServiceImpl::ResolveAutoLaunch(const std::string &identifier, DBLaunchParam &param,
StoreMetaData meta, bool isTriple)
bool KVDBServiceImpl::CompareTripleIdentifier(const std::string &accountId, const std::string &identifier,
const StoreMetaData &storeMeta)
{
std::vector<std::string> accountIds { accountId, "ohosAnonymousUid", "default" };
for (auto &id : accountIds) {
auto convertedIds =
AppIdMappingConfigManager::GetInstance().Convert(storeMeta.appId, storeMeta.user);
const std::string &tempTripleIdentifier =
DistributedDB::KvStoreDelegateManager::GetKvStoreIdentifier(id, convertedIds.first,
storeMeta.storeId, false);
if (tempTripleIdentifier == identifier) {
ZLOGI("find triple identifier,storeId:%{public}s,id:%{public}s",
Anonymous::Change(storeMeta.storeId).c_str(), Anonymous::Change(id).c_str());
return true;
}
}
return false;
}
bool KVDBServiceImpl::IsTripleAutoLaunch(
const std::string &identifier, DistributedDB::AutoLaunchParam &param, StoreMetaData &meta, bool &isFindIdentifier)
{
std::vector<StoreMetaData> entries;
std::string localDeviceId = DmAdapter::GetInstance().GetLocalDevice().uuid;
if (!MetaDataManager::GetInstance().LoadMeta(StoreMetaData::GetPrefix({ localDeviceId }), entries)) {
ZLOGE("get full meta failed");
return false;
}
auto accountId = AccountDelegate::GetInstance()->GetUnencryptedAccountId();
for (const auto &storeMeta : entries) {
if ((!param.userId.empty() && (param.userId != storeMeta.user)) || (localDeviceId != storeMeta.deviceId) ||
((StoreMetaData::STORE_RELATIONAL_BEGIN <= storeMeta.storeType) &&
(StoreMetaData::STORE_RELATIONAL_END >= storeMeta.storeType))) {
// judge local userid and local meta
continue;
}
bool isTripleIdentifierEqual = CompareTripleIdentifier(accountId, identifier, storeMeta);
const std::string &itemDualIdentifier =
DistributedDB::KvStoreDelegateManager::GetKvStoreIdentifier("", storeMeta.appId, storeMeta.storeId, true);
if (isTripleIdentifierEqual && storeMeta.bundleName != Bootstrap::GetInstance().GetProcessLabel()) {
meta = storeMeta;
meta.account = accountId;
}
if (identifier == itemDualIdentifier || isTripleIdentifierEqual) {
ZLOGI("identifier find:%{public}d", isTripleIdentifierEqual);
DistributedDB::AutoLaunchOption option;
option.createIfNecessary = false;
option.isEncryptedDb = storeMeta.isEncrypt;
SecretKeyMeta secretKey;
if (storeMeta.isEncrypt && MetaDataManager::GetInstance().LoadMeta(storeMeta.GetSecretKey(), secretKey)) {
std::vector<uint8_t> decryptKey;
CryptoManager::GetInstance().Decrypt(secretKey.sKey, decryptKey);
option.passwd.SetValue(decryptKey.data(), decryptKey.size());
std::fill(decryptKey.begin(), decryptKey.end(), 0);
}
if (storeMeta.bundleName == Bootstrap::GetInstance().GetProcessLabel()) {
param.userId = storeMeta.user;
}
option.schema = storeMeta.schema;
option.createDirByStoreIdOnly = true;
option.dataDir = storeMeta.dataDir;
option.secOption = ConvertSecurity(storeMeta.securityLevel);
option.isAutoSync = storeMeta.isAutoSync;
option.syncDualTupleMode = true; // dual tuple flag
param.appId = storeMeta.appId;
param.storeId = storeMeta.storeId;
param.option = option;
isFindIdentifier = true;
return isTripleIdentifierEqual;
}
}
ZLOGI("not find identifier");
return false;
}
int32_t KVDBServiceImpl::DoTripleAutoLaunch(StoreMetaData &meta)
{
auto watchers = GetWatchers(meta.tokenId, meta.storeId);
ZLOGI("triple autolaunch.appId:%{public}s storeId:%{public}s tokenid:0x%{public}x size:%{public}zu",
meta.bundleName.c_str(), Anonymous::Change(meta.storeId).c_str(), meta.tokenId, watchers.size());
auto store = AutoCache::GetInstance().GetStore(meta, watchers);
if (store == nullptr) {
ZLOGE("store null, storeId:%{public}s", Anonymous::Change(meta.storeId).c_str());
return STORE_NOT_OPEN;
}
store->SetEqualIdentifier(meta.appId, meta.storeId, meta.account);
return SUCCESS;
}
int32_t KVDBServiceImpl::ResolveAutoLaunch(const std::string &identifier, DBLaunchParam &param, bool &isFindIdentifier)
{
ZLOGI("user:%{public}s appId:%{public}s storeId:%{public}s identifier:%{public}s", param.userId.c_str(),
param.appId.c_str(), Anonymous::Change(param.storeId).c_str(), Anonymous::Change(identifier).c_str());
if (isTriple) {
auto watchers = GetWatchers(meta.tokenId, meta.storeId);
ZLOGI("triple autolaunch.appId:%{public}s storeId:%{public}s tokenid:0x%{public}x size:%{public}zu",
meta.bundleName.c_str(), Anonymous::Change(meta.storeId).c_str(), meta.tokenId, watchers.size());
auto store = AutoCache::GetInstance().GetStore(meta, watchers);
if (store == nullptr) {
ZLOGE("store null, storeId:%{public}s", Anonymous::Change(meta.storeId).c_str());
return STORE_NOT_OPEN;
}
store->SetEqualIdentifier(meta.appId, meta.storeId, meta.account);
return SUCCESS;
}
StoreMetaData meta;
if (IsTripleAutoLaunch(identifier, param, meta, isFindIdentifier)) {
ZLOGI("do triple autolaunch. storeId:%{public}s", Anonymous::Change(meta.storeId).c_str());
return DoTripleAutoLaunch(meta);
}
std::vector<StoreMetaData> metaData;
auto prefix = StoreMetaData::GetPrefix({ DMAdapter::GetInstance().GetLocalDevice().uuid, param.userId });
if (!MetaDataManager::GetInstance().LoadMeta(prefix, metaData)) {
@ -797,6 +883,21 @@ int32_t KVDBServiceImpl::ResolveAutoLaunch(const std::string &identifier, DBLaun
return SUCCESS;
}
DistributedDB::SecurityOption KVDBServiceImpl::ConvertSecurity(int securityLevel)
{
if (securityLevel < SecurityLevel::NO_LABEL || securityLevel > SecurityLevel::S4) {
return {DistributedDB::NOT_SET, DistributedDB::ECE};
}
switch (securityLevel) {
case SecurityLevel::S3:
return {DistributedDB::S3, DistributedDB::SECE};
case SecurityLevel::S4:
return {DistributedDB::S4, DistributedDB::ECE};
default:
return {securityLevel, DistributedDB::ECE};
}
}
int32_t KVDBServiceImpl::OnUserChange(uint32_t code, const std::string &user, const std::string &account)
{
(void)code;

View File

@ -73,8 +73,7 @@ public:
int32_t OnBind(const BindInfo &bindInfo) override;
int32_t OnInitialize() override;
int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &appId) override;
int32_t ResolveAutoLaunch(const std::string &identifier, DBLaunchParam &param,
StoreMetaData meta = StoreMetaData(), bool isTriple = false) override;
int32_t ResolveAutoLaunch(const std::string &identifier, DBLaunchParam &param, bool &isFindIdentifier) override;
int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account) override;
Status RemoveDeviceData(const AppId &appId, const StoreId &storeId, const std::string &device) override;
@ -151,6 +150,12 @@ private:
void TryToSync(const StoreMetaData &metaData, bool force = false);
bool IsRemoteChange(const StoreMetaData &metaData, const std::string &device);
bool IsOHOSType(const std::vector<std::string> &ids);
bool CompareTripleIdentifier(const std::string &accountId, const std::string &identifier,
const StoreMetaData &storeMeta);
bool IsTripleAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam &param,
StoreMetaData &meta, bool &isFindIdentifier);
int32_t DoTripleAutoLaunch(StoreMetaData &meta);
DistributedDB::SecurityOption ConvertSecurity(int securityLevel);
static Factory factory_;
ConcurrentMap<uint32_t, SyncAgent> syncAgents_;
std::shared_ptr<ExecutorPool> executors_;

View File

@ -295,8 +295,7 @@ int32_t ObjectServiceImpl::ObjectStatic::OnAppUninstall(const std::string &bundl
return result;
}
int32_t ObjectServiceImpl::ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam &param,
StoreMetaData meta, bool isTriple)
int32_t ObjectServiceImpl::ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam &param, bool &isFindIdentifier)
{
ZLOGI("start, user:%{public}s appId:%{public}s storeId:%{public}s identifier:%{public}s", param.userId.c_str(),
param.appId.c_str(), DistributedData::Anonymous::Change(param.storeId).c_str(),

View File

@ -42,7 +42,7 @@ public:
const std::string &bundleName, const std::string &sessionId, const uint32_t &tokenId);
void Clear();
int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam &param,
StoreMetaData meta = StoreMetaData(), bool isTriple = false) override;
bool &isFindIdentifier) override;
int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &appId) override;
int32_t OnInitialize() override;
int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account) override;

View File

@ -97,7 +97,8 @@ RdbServiceImpl::RdbServiceImpl()
ZLOGI("construct");
DistributedDB::RelationalStoreManager::SetAutoLaunchRequestCallback(
[this](const std::string& identifier, DistributedDB::AutoLaunchParam &param) {
return ResolveAutoLaunch(identifier, param);
bool isFindIdentifier = false;
return ResolveAutoLaunch(identifier, param, isFindIdentifier);
});
auto process = [this](const Event &event) {
auto &evt = static_cast<const CloudEvent &>(event);
@ -145,7 +146,7 @@ RdbServiceImpl::RdbServiceImpl()
}
int32_t RdbServiceImpl::ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam &param,
StoreMetaData meta, bool isTriple)
bool &isFindIdentifier)
{
std::string identifierHex = TransferStringToHex(identifier);
ZLOGI("%{public}.6s", identifierHex.c_str());

View File

@ -75,7 +75,7 @@ public:
std::shared_ptr<DetailProgressObserver> observer) override;
int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam &param,
StoreMetaData meta = StoreMetaData(), bool isTriple = false) override;
bool &isFindIdentifier) override;
int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName) override;