diff --git a/services/distributeddataservice/service/common/BUILD.gn b/services/distributeddataservice/service/common/BUILD.gn index 50a08c56e6e2ee8ef72a754ae62274ca1a5c89fd..98a8c1c55a479c8b1aa7f7a37e936f5af2327261 100644 --- a/services/distributeddataservice/service/common/BUILD.gn +++ b/services/distributeddataservice/service/common/BUILD.gn @@ -23,6 +23,7 @@ ohos_source_set("distributeddata_common") { ubsan = true } sources = [ + "bundle_utils.cpp", "common_types_utils.cpp", "value_proxy.cpp", "xcollie.cpp", diff --git a/services/distributeddataservice/service/common/bundle_utils.cpp b/services/distributeddataservice/service/common/bundle_utils.cpp new file mode 100644 index 0000000000000000000000000000000000000000..afe0b987804791d1465294f86ff88dc5522c73a2 --- /dev/null +++ b/services/distributeddataservice/service/common/bundle_utils.cpp @@ -0,0 +1,39 @@ +/* +* Copyright (c) 2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include "bundle_utils.h" + +namespace OHOS::DistributedData { +BundleUtils &BundleUtils::GetInstance() +{ + static BundleUtils instance; + return instance; +} + +void BundleUtils::SetBundleInfoCallback(Callback callback) +{ + std::lock_guard lock(lock_); + bundleInfoCallback_ = callback; +} + +std::pair BundleUtils::IsConfigSilentProxy(const std::string &bundleName, int32_t userId, + const std::string &storeName) +{ + if (bundleInfoCallback_ == nullptr) { + return std::make_pair(-1, false); + } + return bundleInfoCallback_(bundleName, userId, storeName); +} +} \ No newline at end of file diff --git a/services/distributeddataservice/service/common/bundle_utils.h b/services/distributeddataservice/service/common/bundle_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..299ca62ac5016b59393f00a671852dd6da5484eb --- /dev/null +++ b/services/distributeddataservice/service/common/bundle_utils.h @@ -0,0 +1,43 @@ +/* +* Copyright (c) 2025 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_BUNDLEINFO_UTILS_H +#define OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_BUNDLEINFO_UTILS_H + +#include +#include +#include +#include +#include + +namespace OHOS::DistributedData { +using Callback = + std::function(const std::string &bundleName, int32_t userId, const std::string &storeName)>; +class BundleUtils { +public: + BundleUtils() = default; + static BundleUtils &GetInstance(); + + std::pair IsConfigSilentProxy(const std::string &bundleName, int32_t userId, + const std::string &storeName); + + void SetBundleInfoCallback(Callback callback); + +private: + std::mutex lock_; + Callback bundleInfoCallback_; +}; +} +#endif // OHOS_DISTRIBUTED_DATA_DATAMGR_SERVICE_BUNDLEINFO_UTILS_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp index afd7e1f4f8523c9f76f3fdd4738ced256f1efcd3..f4056740acad44b790f6397e716d3b0ca14be453 100644 --- a/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp +++ b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.cpp @@ -192,6 +192,7 @@ void BundleMgrProxy::Delete(const std::string &bundleName, int32_t userId, int32 bundleCache_.Erase(bundleName + std::to_string(userId)); } callerInfoCache_.Erase(bundleName + std::to_string(userId)); + isSilent_.Delete(SilentBundleInfo(bundleName, userId)); return; } @@ -290,4 +291,63 @@ std::pair> BundleMgrProxy::ConvertHapModuleInfo( } return std::make_pair(E_OK, hapModuleInfos); } + +void BundleMgrProxy::UpdateSilentConfig(const SilentBundleInfo &silentBundleInfo, const std::string &storeName, + bool isSilent) +{ + std::map storeNameMap; + if (isSilent_.Get(silentBundleInfo, storeNameMap)) { + storeNameMap.insert_or_assign(storeName, isSilent); + } else { + storeNameMap.emplace(storeName, isSilent); + } + isSilent_.Set(silentBundleInfo, storeNameMap); +} + +std::pair BundleMgrProxy::IsConfigSilentProxy(const std::string &bundleName, int32_t userId, + const std::string &storeName) +{ + std::map storeNameMap; + SilentBundleInfo silentBundleInfo(bundleName, userId); + if (isSilent_.Get(silentBundleInfo, storeNameMap)) { + auto it = storeNameMap.find(storeName); + if (it != storeNameMap.end()) { + auto flag = it->second ? E_OK : E_SILENT_PROXY_DISABLE; + return {flag, it->second}; + } + } + + auto bmsClient = GetBundleMgrProxy(); + if (bmsClient == nullptr) { + ZLOGE("GetBundleMgrProxy is nullptr! bundle is %{public}s, user is %{public}d.", bundleName.c_str(), userId); + return std::make_pair(E_BMS_NOT_READY, false); + } + + AppExecFwk::BundleInfo bundleInfo; + bool result = bmsClient->GetBundleInfo(bundleName, + static_cast(AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_HAP_MODULE), bundleInfo, userId); + if (!result) { + ZLOGE("Get bundleInfo failed! ret: %{public}d, bundle is %{public}s, user is %{public}d.", + result, bundleName.c_str(), userId); + return std::make_pair(E_ERROR, false); + } + + auto [errCode, hapModuleInfos] = ConvertHapModuleInfo(bundleInfo); + for (auto &hapModuleInfo : hapModuleInfos) { + for (auto &data : hapModuleInfo.proxyDatas) { + const auto &profileInfo = data.profileInfo; + if (profileInfo.resultCode == ERROR) { + ZLOGE("Profile unmarshall error.bundleName: %{public}s, userId: %{public}d", + bundleName.c_str(), userId); + return std::make_pair(E_SILENT_PROXY_DISABLE, false); + } + if (profileInfo.profile.storeName == storeName) { + UpdateSilentConfig(silentBundleInfo, storeName, true); + return std::make_pair(E_OK, true); + } + } + } + UpdateSilentConfig(silentBundleInfo, storeName, false); + return std::make_pair(E_SILENT_PROXY_DISABLE, false); +} } // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.h b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.h index 7198715f13ab791ed66c14ba7dfca73b089f6d95..ded6e358568ffbd64cfc02ba1edc78c631170383 100644 --- a/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.h +++ b/services/distributeddataservice/service/data_share/common/bundle_mgr_proxy.h @@ -18,11 +18,13 @@ #include #include +#include #include "bundle_info.h" #include "bundlemgr/bundle_mgr_proxy.h" #include "concurrent_map.h" #include "data_share_profile_config.h" +#include "lru_bucket.h" namespace OHOS::DataShare { struct ProfileConfig { @@ -76,6 +78,19 @@ struct BundleInfo { uint32_t tokenId; }; +struct SilentBundleInfo { + std::string bundleName; + int32_t userId; + SilentBundleInfo(const std::string &name, int32_t userId) : bundleName(name), userId(userId) {} + bool operator<(const SilentBundleInfo &other) const + { + if (bundleName == other.bundleName) { + return userId < other.userId; + } + return bundleName < other.bundleName; + } +}; + class BundleMgrProxy final : public std::enable_shared_from_this { public: BundleMgrProxy() = default; @@ -85,6 +100,8 @@ public: BundleConfig &bundleConfig, int32_t appIndex = 0); int GetBundleInfoFromBMSWithCheck(const std::string &bundleName, int32_t userId, BundleConfig &bundleConfig, int32_t appIndex = 0); + std::pair IsConfigSilentProxy(const std::string &bundleName, int32_t userId, + const std::string &storeName); void Delete(const std::string &bundleName, int32_t userId, int32_t appIndex); sptr CheckBMS(); std::pair GetCallerAppIdentifier(const std::string &bundleName, int32_t userId); @@ -108,6 +125,8 @@ private: std::pair ConvertToDataShareBundle(AppExecFwk::BundleInfo &bundleInfo); std::pair> ConvertExtensionAbility(AppExecFwk::BundleInfo &bundleInfo); std::pair> ConvertHapModuleInfo(AppExecFwk::BundleInfo &bundleInfo); + void UpdateSilentConfig(const SilentBundleInfo &silentBundleInfo, const std::string &storeName, + bool isSilent); std::mutex mutex_; sptr proxy_; sptr deathRecipient_; @@ -115,6 +134,8 @@ private: ConcurrentMap callerInfoCache_; static constexpr const char *DATA_SHARE_EXTENSION_META = "ohos.extension.dataShare"; static constexpr const char *DATA_SHARE_PROPERTIES_META = "dataProperties"; + static constexpr size_t CACHE_SIZE = 32; + LRUBucket> isSilent_ {CACHE_SIZE}; }; } // namespace OHOS::DataShare #endif // DATASHARESERVICE_BUNDLEMGR_PROXY_H diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp index 0b3e7c81b499d48ccdcf57dac0174ae10c8379ca..4e3f39e70613de62544df290a49e3c6ef3bb12de 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -22,6 +22,8 @@ #include "account/account_delegate.h" #include "app_connect_manager.h" +#include "bundle_mgr_proxy.h" +#include "bundle_utils.h" #include "common_event_manager.h" #include "common_event_support.h" #include "concurrent_task_client.h" @@ -610,6 +612,10 @@ int32_t DataShareServiceImpl::OnBind(const BindInfo &binderInfo) SubscribeConcurrentTask(); SubscribeTimeChanged(); SubscribeChange(); + auto task = [](const std::string &bundleName, int32_t userId, const std::string &storeName) { + return BundleMgrProxy::GetInstance()->IsConfigSilentProxy(bundleName, userId, storeName); + }; + BundleUtils::GetInstance().SetBundleInfoCallback(task); ZLOGI("end"); return E_OK; } diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index b62f047ea50a692578e343317ac41748c4191696..c580433a9d669eda56a3feba7be36edca5b10389 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -19,6 +19,7 @@ #include "accesstoken_kit.h" #include "account/account_delegate.h" #include "bootstrap.h" +#include "bundle_utils.h" #include "changeevent/remote_change_event.h" #include "checker/checker_manager.h" #include "cloud/change_event.h" @@ -880,6 +881,20 @@ int32_t RdbServiceImpl::BeforeOpen(RdbSyncerParam ¶m) return RDB_OK; } +std::pair RdbServiceImpl::IsSupportSilent(const RdbSyncerParam ¶m) +{ + XCollie xcollie(__FUNCTION__, XCollie::XCOLLIE_LOG | XCollie::XCOLLIE_RECOVERY); + if (!IsValidParam(param) || !IsValidAccess(param.bundleName_, param.storeName_)) { + ZLOGE("bundleName:%{public}s, storeName:%{public}s. Permission error", param.bundleName_.c_str(), + Anonymous::Change(param.storeName_).c_str()); + return {RDB_ERROR, false}; + } + auto meta = GetStoreMetaData(param); + auto [err, flag] = + BundleUtils::GetInstance().IsConfigSilentProxy(meta.bundleName, std::atoi(meta.user.c_str()), meta.storeId); + return {RDB_OK, flag}; +} + void RdbServiceImpl::SetReturnParam(const StoreMetaData &metadata, RdbSyncerParam ¶m) { param.bundleName_ = metadata.bundleName; diff --git a/services/distributeddataservice/service/rdb/rdb_service_impl.h b/services/distributeddataservice/service/rdb/rdb_service_impl.h index b7fe3c03c401819504d293ea15f227ef7ceed66d..6db6dfa4b0a31a6556a5bb8a1525fa40125a1f0d 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -106,6 +106,8 @@ public: int32_t BeforeOpen(RdbSyncerParam ¶m) override; + std::pair IsSupportSilent(const RdbSyncerParam ¶m) override; + int32_t AfterOpen(const RdbSyncerParam ¶m) override; int32_t ReportStatistic(const RdbSyncerParam ¶m, const RdbStatEvent &statEvent) override; diff --git a/services/distributeddataservice/service/rdb/rdb_service_stub.cpp b/services/distributeddataservice/service/rdb/rdb_service_stub.cpp index f52549c1f7aee6a8e0bcd8f23b604a9ec7585fca..fab63339060b243b2ded6d8fbf1f566a95835738 100644 --- a/services/distributeddataservice/service/rdb/rdb_service_stub.cpp +++ b/services/distributeddataservice/service/rdb/rdb_service_stub.cpp @@ -61,6 +61,23 @@ int32_t RdbServiceStub::OnBeforeOpen(MessageParcel &data, MessageParcel &reply) return RDB_OK; } +int32_t RdbServiceStub::OnIsSupportSilent(MessageParcel &data, MessageParcel &reply) +{ + RdbSyncerParam param; + bool isSilent = false; + if (!ITypesUtil::Unmarshal(data, param, isSilent)) { + ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(), + Anonymous::Change(param.storeName_).c_str()); + return IPC_STUB_INVALID_DATA_ERR; + } + auto [status, ret] = IsSupportSilent(param); + if (!ITypesUtil::Marshal(reply, status, ret)) { + ZLOGE("Marshal status:0x%{public}x", status); + return IPC_STUB_WRITE_PARCEL_ERR; + } + return RDB_OK; +} + int32_t RdbServiceStub::OnAfterOpen(MessageParcel &data, MessageParcel &reply) { RdbSyncerParam param; diff --git a/services/distributeddataservice/service/rdb/rdb_service_stub.h b/services/distributeddataservice/service/rdb/rdb_service_stub.h index 1f9a75d15ca2973353000dca12d1b6f259fa0d26..f52a6798177024823988e7eb78ef9297de1f23d8 100755 --- a/services/distributeddataservice/service/rdb/rdb_service_stub.h +++ b/services/distributeddataservice/service/rdb/rdb_service_stub.h @@ -1,130 +1,133 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef DISTRIBUTED_RDB_SERVICE_STUB_H -#define DISTRIBUTED_RDB_SERVICE_STUB_H - -#include "iremote_stub.h" -#include "rdb_service.h" -#include "rdb_notifier.h" -#include "feature/feature_system.h" -#include "rdb_result_set_impl.h" - -namespace OHOS::DistributedRdb { -using RdbServiceCode = OHOS::DistributedRdb::RelationalStore::RdbServiceInterfaceCode; - -class RdbServiceStub : public RdbService, public DistributedData::FeatureSystem::Feature { -public: - DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedRdb.IRdbService"); - int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply) override; - -private: - static bool CheckInterfaceToken(MessageParcel& data); - - int32_t OnRemoteObtainDistributedTableName(MessageParcel& data, MessageParcel& reply); - - int32_t OnDelete(MessageParcel& data, MessageParcel& reply); - - int32_t OnRemoteInitNotifier(MessageParcel& data, MessageParcel& reply); - - int32_t OnRemoteSetDistributedTables(MessageParcel& data, MessageParcel& reply); - - int32_t OnRemoteDoSync(MessageParcel& data, MessageParcel& reply); - - int32_t OnRemoteDoAsync(MessageParcel& data, MessageParcel& reply); - - int32_t OnRemoteDoSubscribe(MessageParcel& data, MessageParcel& reply); - - int32_t OnRemoteDoUnSubscribe(MessageParcel& data, MessageParcel& reply); - - int32_t OnRemoteRegisterDetailProgressObserver(MessageParcel& data, MessageParcel& reply); - - int32_t OnRemoteUnregisterDetailProgressObserver(MessageParcel& data, MessageParcel& reply); - - int32_t OnRemoteDoRemoteQuery(MessageParcel& data, MessageParcel& reply); - - int32_t OnRemoteNotifyDataChange(MessageParcel& data, MessageParcel& reply); - - int32_t OnRemoteSetSearchable(MessageParcel& data, MessageParcel& reply); - - int32_t OnRemoteQuerySharingResource(MessageParcel& data, MessageParcel& reply); - - int32_t OnBeforeOpen(MessageParcel& data, MessageParcel& reply); - - int32_t OnAfterOpen(MessageParcel& data, MessageParcel& reply); - - int32_t OnReportStatistic(MessageParcel& data, MessageParcel& reply); - - int32_t OnDisable(MessageParcel& data, MessageParcel& reply); - - int32_t OnEnable(MessageParcel& data, MessageParcel& reply); - - int32_t OnGetPassword(MessageParcel& data, MessageParcel& reply); - - int32_t OnLockCloudContainer(MessageParcel& data, MessageParcel& reply); - - int32_t OnUnlockCloudContainer(MessageParcel& data, MessageParcel& reply); - - int32_t OnGetDebugInfo(MessageParcel& data, MessageParcel& reply); - - int32_t OnGetDfxInfo(MessageParcel& data, MessageParcel& reply); - - int32_t OnVerifyPromiseInfo(MessageParcel& data, MessageParcel& reply); - - using RequestHandle = int (RdbServiceStub::*)(MessageParcel &, MessageParcel &); -RDB_UTILS_PUSH_WARNING -RDB_UTILS_DISABLE_WARNING("-Wc99-designator") - static constexpr RequestHandle HANDLERS[static_cast(RdbServiceCode::RDB_SERVICE_CMD_MAX)] = { - [static_cast(RdbServiceCode::RDB_SERVICE_CMD_OBTAIN_TABLE)] = - &RdbServiceStub::OnRemoteObtainDistributedTableName, - [static_cast(RdbServiceCode::RDB_SERVICE_CMD_INIT_NOTIFIER)] = &RdbServiceStub::OnRemoteInitNotifier, - [static_cast(RdbServiceCode::RDB_SERVICE_CMD_SET_DIST_TABLE)] = - &RdbServiceStub::OnRemoteSetDistributedTables, - [static_cast(RdbServiceCode::RDB_SERVICE_CMD_SYNC)] = &RdbServiceStub::OnRemoteDoSync, - [static_cast(RdbServiceCode::RDB_SERVICE_CMD_ASYNC)] = &RdbServiceStub::OnRemoteDoAsync, - [static_cast(RdbServiceCode::RDB_SERVICE_CMD_SUBSCRIBE)] = &RdbServiceStub::OnRemoteDoSubscribe, - [static_cast(RdbServiceCode::RDB_SERVICE_CMD_UNSUBSCRIBE)] = &RdbServiceStub::OnRemoteDoUnSubscribe, - [static_cast(RdbServiceCode::RDB_SERVICE_CMD_REMOTE_QUERY)] = &RdbServiceStub::OnRemoteDoRemoteQuery, - [static_cast(RdbServiceCode::RDB_SERVICE_CMD_DELETE)] = &RdbServiceStub::OnDelete, - [static_cast(RdbServiceCode::RDB_SERVICE_CMD_REGISTER_AUTOSYNC_PROGRESS_OBSERVER)] = - &RdbServiceStub::OnRemoteRegisterDetailProgressObserver, - [static_cast(RdbServiceCode::RDB_SERVICE_CMD_UNREGISTER_AUTOSYNC_PROGRESS_OBSERVER)] = - &RdbServiceStub::OnRemoteUnregisterDetailProgressObserver, - [static_cast(RdbServiceCode::RDB_SERVICE_CMD_NOTIFY_DATA_CHANGE)] = - &RdbServiceStub::OnRemoteNotifyDataChange, - [static_cast(RdbServiceCode::RDB_SERVICE_CMD_SET_SEARCHABLE)] = - &RdbServiceStub::OnRemoteSetSearchable, - [static_cast(RdbServiceCode::RDB_SERVICE_CMD_QUERY_SHARING_RESOURCE)] = - &RdbServiceStub::OnRemoteQuerySharingResource, - [static_cast(RdbServiceCode::RDB_SERVICE_CMD_DISABLE)] = &RdbServiceStub::OnDisable, - [static_cast(RdbServiceCode::RDB_SERVICE_CMD_ENABLE)] = &RdbServiceStub::OnEnable, - [static_cast(RdbServiceCode::RDB_SERVICE_CMD_BEFORE_OPEN)] = &RdbServiceStub::OnBeforeOpen, - [static_cast(RdbServiceCode::RDB_SERVICE_CMD_AFTER_OPEN)] = &RdbServiceStub::OnAfterOpen, - [static_cast(RdbServiceCode::RDB_SERVICE_CMD_GET_PASSWORD)] = &RdbServiceStub::OnGetPassword, - [static_cast(RdbServiceCode::RDB_SERVICE_CMD_LOCK_CLOUD_CONTAINER)] = - &RdbServiceStub::OnLockCloudContainer, - [static_cast(RdbServiceCode::RDB_SERVICE_CMD_UNLOCK_CLOUD_CONTAINER)] = - &RdbServiceStub::OnUnlockCloudContainer, - [static_cast(RdbServiceCode::RDB_SERVICE_CMD_GET_DEBUG_INFO)] = &RdbServiceStub::OnGetDebugInfo, - [static_cast(RdbServiceCode::RDB_SERVICE_CMD_VERIFY_PROMISE_INFO)] = - &RdbServiceStub::OnVerifyPromiseInfo, - [static_cast(RdbServiceCode::RDB_SERVICE_CMD_REPORT_STAT)] = - &RdbServiceStub::OnReportStatistic, - [static_cast(RdbServiceCode::RDB_SERVICE_CMD_GET_DFX_INFO)] = &RdbServiceStub::OnGetDfxInfo, - }; -RDB_UTILS_POP_WARNING -}; -} // namespace OHOS::DistributedRdb -#endif +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DISTRIBUTED_RDB_SERVICE_STUB_H +#define DISTRIBUTED_RDB_SERVICE_STUB_H + +#include "iremote_stub.h" +#include "rdb_service.h" +#include "rdb_notifier.h" +#include "feature/feature_system.h" +#include "rdb_result_set_impl.h" + +namespace OHOS::DistributedRdb { +using RdbServiceCode = OHOS::DistributedRdb::RelationalStore::RdbServiceInterfaceCode; + +class RdbServiceStub : public RdbService, public DistributedData::FeatureSystem::Feature { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedRdb.IRdbService"); + int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply) override; + +private: + static bool CheckInterfaceToken(MessageParcel& data); + + int32_t OnRemoteObtainDistributedTableName(MessageParcel& data, MessageParcel& reply); + + int32_t OnDelete(MessageParcel& data, MessageParcel& reply); + + int32_t OnRemoteInitNotifier(MessageParcel& data, MessageParcel& reply); + + int32_t OnRemoteSetDistributedTables(MessageParcel& data, MessageParcel& reply); + + int32_t OnRemoteDoSync(MessageParcel& data, MessageParcel& reply); + + int32_t OnRemoteDoAsync(MessageParcel& data, MessageParcel& reply); + + int32_t OnRemoteDoSubscribe(MessageParcel& data, MessageParcel& reply); + + int32_t OnRemoteDoUnSubscribe(MessageParcel& data, MessageParcel& reply); + + int32_t OnRemoteRegisterDetailProgressObserver(MessageParcel& data, MessageParcel& reply); + + int32_t OnRemoteUnregisterDetailProgressObserver(MessageParcel& data, MessageParcel& reply); + + int32_t OnRemoteDoRemoteQuery(MessageParcel& data, MessageParcel& reply); + + int32_t OnRemoteNotifyDataChange(MessageParcel& data, MessageParcel& reply); + + int32_t OnRemoteSetSearchable(MessageParcel& data, MessageParcel& reply); + + int32_t OnRemoteQuerySharingResource(MessageParcel& data, MessageParcel& reply); + + int32_t OnBeforeOpen(MessageParcel& data, MessageParcel& reply); + + int32_t OnAfterOpen(MessageParcel& data, MessageParcel& reply); + + int32_t OnIsSupportSilent(MessageParcel& data, MessageParcel& reply); + + int32_t OnReportStatistic(MessageParcel& data, MessageParcel& reply); + + int32_t OnDisable(MessageParcel& data, MessageParcel& reply); + + int32_t OnEnable(MessageParcel& data, MessageParcel& reply); + + int32_t OnGetPassword(MessageParcel& data, MessageParcel& reply); + + int32_t OnLockCloudContainer(MessageParcel& data, MessageParcel& reply); + + int32_t OnUnlockCloudContainer(MessageParcel& data, MessageParcel& reply); + + int32_t OnGetDebugInfo(MessageParcel& data, MessageParcel& reply); + + int32_t OnGetDfxInfo(MessageParcel& data, MessageParcel& reply); + + int32_t OnVerifyPromiseInfo(MessageParcel& data, MessageParcel& reply); + + using RequestHandle = int (RdbServiceStub::*)(MessageParcel &, MessageParcel &); +RDB_UTILS_PUSH_WARNING +RDB_UTILS_DISABLE_WARNING("-Wc99-designator") + static constexpr RequestHandle HANDLERS[static_cast(RdbServiceCode::RDB_SERVICE_CMD_MAX)] = { + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_OBTAIN_TABLE)] = + &RdbServiceStub::OnRemoteObtainDistributedTableName, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_INIT_NOTIFIER)] = &RdbServiceStub::OnRemoteInitNotifier, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_SET_DIST_TABLE)] = + &RdbServiceStub::OnRemoteSetDistributedTables, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_SYNC)] = &RdbServiceStub::OnRemoteDoSync, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_ASYNC)] = &RdbServiceStub::OnRemoteDoAsync, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_SUBSCRIBE)] = &RdbServiceStub::OnRemoteDoSubscribe, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_UNSUBSCRIBE)] = &RdbServiceStub::OnRemoteDoUnSubscribe, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_REMOTE_QUERY)] = &RdbServiceStub::OnRemoteDoRemoteQuery, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_DELETE)] = &RdbServiceStub::OnDelete, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_REGISTER_AUTOSYNC_PROGRESS_OBSERVER)] = + &RdbServiceStub::OnRemoteRegisterDetailProgressObserver, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_UNREGISTER_AUTOSYNC_PROGRESS_OBSERVER)] = + &RdbServiceStub::OnRemoteUnregisterDetailProgressObserver, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_NOTIFY_DATA_CHANGE)] = + &RdbServiceStub::OnRemoteNotifyDataChange, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_SET_SEARCHABLE)] = + &RdbServiceStub::OnRemoteSetSearchable, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_QUERY_SHARING_RESOURCE)] = + &RdbServiceStub::OnRemoteQuerySharingResource, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_DISABLE)] = &RdbServiceStub::OnDisable, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_ENABLE)] = &RdbServiceStub::OnEnable, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_BEFORE_OPEN)] = &RdbServiceStub::OnBeforeOpen, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_AFTER_OPEN)] = &RdbServiceStub::OnAfterOpen, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_GET_PASSWORD)] = &RdbServiceStub::OnGetPassword, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_LOCK_CLOUD_CONTAINER)] = + &RdbServiceStub::OnLockCloudContainer, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_UNLOCK_CLOUD_CONTAINER)] = + &RdbServiceStub::OnUnlockCloudContainer, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_GET_DEBUG_INFO)] = &RdbServiceStub::OnGetDebugInfo, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_VERIFY_PROMISE_INFO)] = + &RdbServiceStub::OnVerifyPromiseInfo, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_REPORT_STAT)] = + &RdbServiceStub::OnReportStatistic, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_GET_DFX_INFO)] = &RdbServiceStub::OnGetDfxInfo, + [static_cast(RdbServiceCode::RDB_SERVICE_CMD_GET_ISSILENT)] = &RdbServiceStub::OnIsSupportSilent, + }; +RDB_UTILS_POP_WARNING +}; +} // namespace OHOS::DistributedRdb +#endif diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 98c13fef7209ebd20d4b87ac18c56c8738452f45..5997cb1891cf37124dbf501bc66995220fe156ca 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -1191,6 +1191,7 @@ ohos_unittest("DataShareServiceImplTest") { ] sources = [ + "${data_service_path}/service/common/bundle_utils.cpp", "${data_service_path}/service/common/xcollie.cpp", "${data_service_path}/service/data_share/common/app_connect_manager.cpp", "${data_service_path}/service/data_share/common/bundle_mgr_proxy.cpp", @@ -1322,6 +1323,7 @@ ohos_unittest("DataShareServiceImplMockTest") { include_dirs = [ "${data_service_path}/service/config/include" ] sources = [ + "${data_service_path}/service/common/bundle_utils.cpp", "${data_service_path}/service/common/xcollie.cpp", "${data_service_path}/service/config/src/model/app_access_check_config.cpp", "${data_service_path}/service/config/src/model/app_id_mapping_config.cpp", diff --git a/services/distributeddataservice/service/test/data_share_service_impl_test.cpp b/services/distributeddataservice/service/test/data_share_service_impl_test.cpp index 5d9273bed5c60e9553c5b654f0cc5cf06644b5ab..8c66ddb294d8545bcb389194d8ef07ff74f803f8 100644 --- a/services/distributeddataservice/service/test/data_share_service_impl_test.cpp +++ b/services/distributeddataservice/service/test/data_share_service_impl_test.cpp @@ -21,6 +21,8 @@ #include "accesstoken_kit.h" #include "account_delegate_mock.h" +#include "bundle_mgr_proxy.h" +#include "bundle_utils.h" #include "data_share_service_stub.h" #include "device_manager_adapter.h" #include "dump/dump_manager.h" @@ -563,4 +565,93 @@ HWTEST_F(DataShareServiceImplTest, UpdateLaunchInfo001, TestSize.Level1) EXPECT_EQ(storeMetaData.size(), 0); ZLOGI("DataShareServiceImplTest UpdateLaunchInfo001 end"); } + +/** +* @tc.name: BundleMgrProxyTest001 +* @tc.desc: Test the IsConfigSilentProxy method of BundleMgrProxy +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DataShareServiceImplTest, BundleMgrProxyTest001, TestSize.Level1) +{ + ZLOGI("DataShareServiceImplTest BundleMgrProxyTest001 start"); + int32_t user = static_cast(USER_TEST); + std::string storeName = ""; + auto bundleMgr = BundleMgrProxy::GetInstance(); + ASSERT_NE(bundleMgr, nullptr); + auto [err, ret] = bundleMgr->IsConfigSilentProxy(BUNDLE_NAME, user, storeName); + EXPECT_EQ(err, OHOS::DataShare::E_SILENT_PROXY_DISABLE); + EXPECT_EQ(ret, false); + auto [err1, ret1] = bundleMgr->IsConfigSilentProxy("", user, storeName); + EXPECT_NE(err1, OHOS::DataShare::E_OK); + EXPECT_EQ(ret1, false); + + // wirte data to LRU cache + storeName = "test"; + DataShare::SilentBundleInfo silentBundleInfo(BUNDLE_NAME, user); + bundleMgr->UpdateSilentConfig(silentBundleInfo, storeName, true); + auto [err2, ret2] = bundleMgr->IsConfigSilentProxy(BUNDLE_NAME, user, storeName); + EXPECT_EQ(err2, OHOS::DataShare::E_OK); + EXPECT_EQ(ret2, true); + + bundleMgr->isSilent_.Delete(silentBundleInfo); + EXPECT_EQ(bundleMgr->isSilent_.Size(), 0); + ZLOGI("DataShareServiceImplTest BundleMgrProxyTest001 end"); +} + +/** +* @tc.name: BundleMgrProxyTest002 +* @tc.desc: Test the UpdateSilentConfig method of BundleMgrProxy +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DataShareServiceImplTest, BundleMgrProxyTest002, TestSize.Level1) +{ + ZLOGI("DataShareServiceImplTest BundleMgrProxyTest002 start"); + + int32_t user = static_cast(USER_TEST); + std::string storeName = "test"; + DataShare::SilentBundleInfo silentBundleInfo(BUNDLE_NAME, user); + + auto bundleMgr = BundleMgrProxy::GetInstance(); + ASSERT_NE(bundleMgr, nullptr); + + // insert data to isSilent_ + bundleMgr->UpdateSilentConfig(silentBundleInfo, storeName, true); + EXPECT_EQ(bundleMgr->isSilent_.Size(), 1); + bundleMgr->UpdateSilentConfig(silentBundleInfo, storeName + "1", true); + EXPECT_EQ(bundleMgr->isSilent_.Size(), 1); + // Update isSilent_ + bundleMgr->UpdateSilentConfig(silentBundleInfo, storeName, false); + EXPECT_EQ(bundleMgr->isSilent_.Size(), 1); + // delete data + bundleMgr->isSilent_.Delete(DataShare::SilentBundleInfo(BUNDLE_NAME, user)); + EXPECT_EQ(bundleMgr->isSilent_.Size(), 0); + + ZLOGI("DataShareServiceImplTest BundleMgrProxyTest002 end"); +} + +/** +* @tc.name: BundleUtilsTest001 +* @tc.desc: Test the SetBundleInfoCallback and IsConfigSilentProxy methods of BundleUtils +* @tc.type: FUNC +* @tc.require: +*/ +HWTEST_F(DataShareServiceImplTest, BundleUtilsTest001, TestSize.Level1) +{ + ZLOGI("DataShareServiceImplTest BundleUtilsTest001 start"); + + auto [err, ret] = BundleUtils::GetInstance().IsConfigSilentProxy("", 0, ""); + EXPECT_EQ(err, -1); + EXPECT_EQ(ret, false); + + auto task = [](const std::string &bundleName, int32_t userId, const std::string &storeName) { + return std::make_pair(0, true); + }; + BundleUtils::GetInstance().SetBundleInfoCallback(task); + auto [err2, ret2] = BundleUtils::GetInstance().IsConfigSilentProxy("", 0, ""); + EXPECT_EQ(err2, 0); + EXPECT_EQ(ret2, true); + ZLOGI("DataShareServiceImplTest BundleUtilsTest001 end"); +} } // namespace OHOS::Test \ No newline at end of file diff --git a/services/distributeddataservice/service/test/fuzztest/datashareserviceimpl_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/datashareserviceimpl_fuzzer/BUILD.gn index baa4fa6efdd1a20dfbe11b2d162431452b184ad8..4620f46bfab4cee677c45c9eb8f0c56c6313deb4 100644 --- a/services/distributeddataservice/service/test/fuzztest/datashareserviceimpl_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/datashareserviceimpl_fuzzer/BUILD.gn @@ -50,6 +50,7 @@ ohos_fuzztest("DataShareServiceImplFuzzTest") { ] sources = [ + "${data_service_path}/service/common/bundle_utils.cpp", "${data_service_path}/service/common/xcollie.cpp", "${data_service_path}/service/data_share/common/app_connect_manager.cpp", "${data_service_path}/service/data_share/common/bundle_mgr_proxy.cpp", diff --git a/services/distributeddataservice/service/test/fuzztest/datashareservicestub_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/datashareservicestub_fuzzer/BUILD.gn index 5824983758ed23c0ab72fde32937e49601f7e9c2..3585f18e99324f1339b8ee8915d5ff2075d67695 100644 --- a/services/distributeddataservice/service/test/fuzztest/datashareservicestub_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/datashareservicestub_fuzzer/BUILD.gn @@ -48,6 +48,7 @@ ohos_fuzztest("DataShareServiceStubFuzzTest") { ] sources = [ + "${data_service_path}/service/common/bundle_utils.cpp", "${data_service_path}/service/common/xcollie.cpp", "${data_service_path}/service/data_share/common/app_connect_manager.cpp", "${data_service_path}/service/data_share/common/bundle_mgr_proxy.cpp", diff --git a/services/distributeddataservice/service/test/fuzztest/datasharesubscriber_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/datasharesubscriber_fuzzer/BUILD.gn index ddfb2cd7bb57b934d81490aeb75a9739e50a4f59..ee23c45188d215a44e9a66c55478f024750a931e 100644 --- a/services/distributeddataservice/service/test/fuzztest/datasharesubscriber_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/datasharesubscriber_fuzzer/BUILD.gn @@ -50,6 +50,7 @@ ohos_fuzztest("DataShareSubscriberFuzzTest") { ] sources = [ + "${data_service_path}/service/common/bundle_utils.cpp", "${data_service_path}/service/common/xcollie.cpp", "${data_service_path}/service/data_share/common/app_connect_manager.cpp", "${data_service_path}/service/data_share/common/bundle_mgr_proxy.cpp", diff --git a/services/distributeddataservice/service/test/fuzztest/syncstrategies_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/syncstrategies_fuzzer/BUILD.gn index 1be915e1eeb8cb45f524c5c23150e5c0921aeb27..2beeabbef2b7697a32dea655d744907efa9ee930 100644 --- a/services/distributeddataservice/service/test/fuzztest/syncstrategies_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/syncstrategies_fuzzer/BUILD.gn @@ -20,6 +20,7 @@ ohos_fuzztest("SyncStrategiesFuzzTest") { module_out_path = "datamgr_service/datamgr_service" include_dirs = [ + "${data_service_path}/adapter/network", "${data_service_path}/framework/include", "${data_service_path}/service/cloud", "${data_service_path}/service/common", @@ -40,6 +41,7 @@ ohos_fuzztest("SyncStrategiesFuzzTest") { deps = [ "${data_service_path}/adapter/dfx:distributeddata_dfx", + "${data_service_path}/adapter/network:distributeddata_network", "${data_service_path}/framework:distributeddatasvcfwk", "${data_service_path}/service/cloud:distributeddata_cloud", "${data_service_path}/service/common:distributeddata_common", @@ -52,6 +54,7 @@ ohos_fuzztest("SyncStrategiesFuzzTest") { "access_token:libtoken_setproc", "access_token:libtokenid_sdk", "c_utils:utils", + "device_manager:devicemanagersdk", "hilog:libhilog", "json:nlohmann_json_static", "kv_store:datamgr_common", diff --git a/services/distributeddataservice/service/test/fuzztest/syncstrategies_fuzzer/syncstrategies_fuzzer.cpp b/services/distributeddataservice/service/test/fuzztest/syncstrategies_fuzzer/syncstrategies_fuzzer.cpp index 2cf4591d3c4c8b83f6508c7d232574e4631d37e3..6a845885ebea47efaa32e302b88d59197578235e 100644 --- a/services/distributeddataservice/service/test/fuzztest/syncstrategies_fuzzer/syncstrategies_fuzzer.cpp +++ b/services/distributeddataservice/service/test/fuzztest/syncstrategies_fuzzer/syncstrategies_fuzzer.cpp @@ -14,6 +14,7 @@ */ #include +#include "src/network_delegate_normal_impl.h" #include "syncstrategies_fuzzer.h" #include "sync_strategies/network_sync_strategy.h" @@ -25,6 +26,8 @@ void SyncStrategiesFuzz001(FuzzedDataProvider &provider) { int32_t user = provider.ConsumeIntegral(); std::string bundleName = provider.ConsumeRandomLengthString(100); + static NetworkDelegateNormalImpl delegate; + NetworkDelegate::RegisterNetworkInstance(&delegate); NetworkSyncStrategy strategy; StoreInfo storeInfo; storeInfo.user = user;