diff --git a/services/distributeddataservice/service/BUILD.gn b/services/distributeddataservice/service/BUILD.gn index 42b0e4b6848986a26944cf6f13d3907d941ae14b..ff073e3ea39fe45351ee84c70e909b2e4b584501 100644 --- a/services/distributeddataservice/service/BUILD.gn +++ b/services/distributeddataservice/service/BUILD.gn @@ -119,10 +119,6 @@ ohos_shared_library("distributeddatasvc") { ] external_deps = [ - "ability_base:want", - "ability_base:zuri", - "ability_runtime:ability_manager", - "ability_runtime:dataobs_manager", "access_token:libaccesstoken_sdk", "access_token:libtokenid_sdk", "c_utils:utils", diff --git a/services/distributeddataservice/service/data_share/BUILD.gn b/services/distributeddataservice/service/data_share/BUILD.gn index 8cb08cb8e615c4a2a1d75cc6f780e9417b00f9d2..35f0aa3524c73616e586879a6917587471db1b6c 100644 --- a/services/distributeddataservice/service/data_share/BUILD.gn +++ b/services/distributeddataservice/service/data_share/BUILD.gn @@ -40,6 +40,7 @@ ohos_shared_library("data_share_service") { "common/db_delegate.cpp", "common/div_strategy.cpp", "common/extension_connect_adaptor.cpp", + "common/extension_mgr_proxy.cpp", "common/kv_delegate.cpp", "common/rdb_delegate.cpp", "common/scheduler_manager.cpp", @@ -91,8 +92,8 @@ ohos_shared_library("data_share_service") { external_deps = [ "ability_base:want", "ability_base:zuri", - "ability_runtime:ability_manager", "ability_runtime:dataobs_manager", + "ability_runtime:extension_manager", "access_token:libaccesstoken_sdk", "access_token:libtokenid_sdk", "bundle_framework:appexecfwk_base", diff --git a/services/distributeddataservice/service/data_share/common/callback_impl.h b/services/distributeddataservice/service/data_share/common/callback_impl.h index 2eb22014dd894994d06744fc6204405c81f82870..451f375fa18526d6ac8afeb9425276ed9ee526ad 100644 --- a/services/distributeddataservice/service/data_share/common/callback_impl.h +++ b/services/distributeddataservice/service/data_share/common/callback_impl.h @@ -16,7 +16,6 @@ #define DATASHARESERVICE_CALLBACK_IMPL_H #include "ability_connect_callback_stub.h" -#include "ability_manager_client.h" #include "block_data.h" namespace OHOS::DataShare { diff --git a/services/distributeddataservice/service/data_share/common/extension_connect_adaptor.cpp b/services/distributeddataservice/service/data_share/common/extension_connect_adaptor.cpp index 44725a692b38f3af26b68f694f152dcc738fb0c8..a0deba23e9859a342af0a42f719259b37cb2b9ad 100644 --- a/services/distributeddataservice/service/data_share/common/extension_connect_adaptor.cpp +++ b/services/distributeddataservice/service/data_share/common/extension_connect_adaptor.cpp @@ -20,6 +20,7 @@ #include "app_connect_manager.h" #include "callback_impl.h" +#include "extension_mgr_proxy.h" #include "log_print.h" namespace OHOS::DataShare { @@ -37,8 +38,6 @@ ExtensionConnectAdaptor::ExtensionConnectAdaptor() : data_(1) {} bool ExtensionConnectAdaptor::DoConnect(std::shared_ptr context) { - AAFwk::Want want; - want.SetUri(context->uri); data_.Clear(); callback_ = new (std::nothrow) CallbackImpl(data_); if (callback_ == nullptr) { @@ -46,7 +45,7 @@ bool ExtensionConnectAdaptor::DoConnect(std::shared_ptr context) return false; } ZLOGI("Start connect %{public}s", context->uri.c_str()); - ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->ConnectAbility(want, callback_, nullptr); + ErrCode ret = ExtensionMgrProxy::GetInstance()->Connect(context->uri, callback_->AsObject(), nullptr); if (ret != ERR_OK) { ZLOGE("connect ability failed, ret = %{public}d", ret); return false; @@ -80,7 +79,7 @@ void ExtensionConnectAdaptor::Disconnect() } data_.Clear(); ZLOGI("Start disconnect"); - AAFwk::AbilityManagerClient::GetInstance()->DisconnectAbility(callback_); + ExtensionMgrProxy::GetInstance()->DisConnect(callback_->AsObject()); if (!data_.GetValue()) { ZLOGI("disconnect ability ended successfully"); } diff --git a/services/distributeddataservice/service/data_share/common/extension_mgr_proxy.cpp b/services/distributeddataservice/service/data_share/common/extension_mgr_proxy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e6081f85bebf6dfcc41cdec17cab8b23761360b9 --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/extension_mgr_proxy.cpp @@ -0,0 +1,102 @@ +/* + * Copyright (c) 2023 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. + */ + +#define LOG_TAG "AppConnectManager" +#include "extension_mgr_proxy.h" + +#include "app_connect_manager.h" +#include "extension_ability_info.h" +#include "if_system_ability_manager.h" +#include "iservice_registry.h" +#include "log_print.h" +#include "system_ability_definition.h" +#include "want.h" +namespace OHOS::DataShare { +void ExtensionMgrProxy::OnProxyDied() +{ + std::lock_guard lock(mutex_); + if (sa_ != nullptr) { + sa_->RemoveDeathRecipient(deathRecipient_); + } + deathRecipient_ = nullptr; + proxy_ = nullptr; +} + +ExtensionMgrProxy::~ExtensionMgrProxy() +{ + std::lock_guard lock(mutex_); + if (sa_ != nullptr) { + sa_->RemoveDeathRecipient(deathRecipient_); + } +} + +std::shared_ptr ExtensionMgrProxy::GetInstance() +{ + static std::shared_ptr proxy = std::make_shared(); + return proxy; +} + +bool ExtensionMgrProxy::Connect( + const std::string &uri, const sptr &connect, const sptr &callerToken) +{ + AAFwk::Want want; + want.SetUri(uri); + std::lock_guard lock(mutex_); + if (ConnectSA()) { + return proxy_->ConnectAbilityCommon(want, connect, callerToken, AppExecFwk::ExtensionAbilityType::DATASHARE); + } + return false; +} + +bool ExtensionMgrProxy::ConnectSA() +{ + if (proxy_ != nullptr) { + return true; + } + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityManager == nullptr) { + ZLOGE("Failed to get system ability mgr."); + return false; + } + + sa_ = systemAbilityManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID); + if (sa_ == nullptr) { + ZLOGE("Failed to GetSystemAbility."); + return false; + } + deathRecipient_ = new (std::nothrow) ExtensionMgrProxy::ServiceDeathRecipient(weak_from_this()); + if (deathRecipient_ == nullptr) { + ZLOGE("deathRecipient alloc failed."); + return false; + } + sa_->AddDeathRecipient(deathRecipient_); + proxy_ = new (std::nothrow)Proxy(sa_); + if (proxy_ == nullptr) { + ZLOGE("proxy_ null, new failed"); + return false; + } + return true; +} + +bool ExtensionMgrProxy::DisConnect(sptr connect) +{ + std::lock_guard lock(mutex_); + if (ConnectSA()) { + return proxy_->DisconnectAbility(connect); + } + return false; +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/common/extension_mgr_proxy.h b/services/distributeddataservice/service/data_share/common/extension_mgr_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..d107cde68d21696105e62896b178c7bd950020a7 --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/extension_mgr_proxy.h @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2023 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 DATASHARESERVICE_EXTENSION_PROXY_H +#define DATASHARESERVICE_EXTENSION_PROXY_H + +#include +#include + +#include "extension_manager_proxy.h" +namespace OHOS::DataShare { +class ExtensionMgrProxy final : public std::enable_shared_from_this { +public: + // do not use + ExtensionMgrProxy() = default; + ~ExtensionMgrProxy(); + static std::shared_ptr GetInstance(); + bool Connect(const std::string &uri, const sptr &connect, const sptr &callerToken); + bool DisConnect(sptr connect); +private: + using Proxy = AAFwk::ExtensionManagerProxy; + class ServiceDeathRecipient : public IRemoteObject::DeathRecipient { + public: + explicit ServiceDeathRecipient(std::weak_ptr owner) : owner_(owner) + { + } + void OnRemoteDied(const wptr &object) override + { + auto owner = owner_.lock(); + if (owner != nullptr) { + owner->OnProxyDied(); + } + } + + private: + std::weak_ptr owner_; + }; + void OnProxyDied(); + bool ConnectSA(); + std::mutex mutex_; + sptr sa_; + sptr proxy_; + sptr deathRecipient_; +}; +} // namespace OHOS::DataShare +#endif // DATASHARESERVICE_BUNDLEMGR_PROXY_H diff --git a/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp b/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp index c0c96fc1d93d97501d381b2cb796734bc4875018..694771d7dd6f12503196145183b8ff4b2f4f3408 100644 --- a/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp +++ b/services/distributeddataservice/service/data_share/common/scheduler_manager.cpp @@ -39,9 +39,6 @@ void SchedulerManager::Execute(const std::string &uri, const int32_t userId, con } std::vector keys = RdbSubscriberManager::GetInstance().GetKeysByUri(uri); for (auto const &key : keys) { - if (RdbSubscriberManager::GetInstance().GetCount(key) == 0) { - continue; - } ExecuteSchedulerSQL(rdbDir, userId, version, key, delegate); } } @@ -64,23 +61,32 @@ void SchedulerManager::SetTimer( ZLOGE("executor_ is nullptr"); return; } + // reminder time must is in future + auto now = time(nullptr); + if (reminderTime <= now) { + ZLOGE("reminderTime is not in future, %{public}" PRId64 "%{public}" PRId64, reminderTime, now); + return; + } + auto duration = std::chrono::seconds(reminderTime - now); + ZLOGI("reminderTime will notify in %{public}" PRId64 " seconds", reminderTime - now); auto it = timerCache_.find(key); if (it != timerCache_.end()) { // has current timer, reset time - ZLOGD("has current taskId, uri is %{public}s, subscriberId is %{public}" PRId64 ", bundleName is %{public}s", - DistributedData::Anonymous::Change(key.uri).c_str(), key.subscriberId, key.bundleName.c_str()); - executor_->Reset(it->second, std::chrono::seconds(reminderTime - time(nullptr))); + ZLOGD("has current taskId, uri is %{private}s, subscriberId is %{public}" PRId64 ", bundleName is %{public}s", + key.uri.c_str(), key.subscriberId, key.bundleName.c_str()); + executor_->Reset(it->second, duration); return; } // not find task in map, create new timer - auto taskId = executor_->Schedule(std::chrono::seconds(reminderTime - time(nullptr)), - [key, dbPath, version, userId, this]() { - timerCache_.erase(key); - // 1. execute schedulerSQL in next time - Execute(key, userId, dbPath, version); - // 2. notify - RdbSubscriberManager::GetInstance().EmitByKey(key, userId, dbPath, version); - }); + auto taskId = executor_->Schedule(duration, [key, dbPath, version, userId, this]() { + ZLOGI("schedule notify start, uri is %{private}s, subscriberId is %{public}" PRId64 ", bundleName is " + "%{public}s", key.uri.c_str(), key.subscriberId, key.bundleName.c_str()); + timerCache_.erase(key); + // 1. execute schedulerSQL in next time + Execute(key, userId, dbPath, version); + // 2. notify + RdbSubscriberManager::GetInstance().EmitByKey(key, userId, dbPath, version); + }); if (taskId == ExecutorPool::INVALID_TASK_ID) { ZLOGE("create timer failed, over the max capacity"); return; diff --git a/services/distributeddataservice/service/data_share/data/resultset_json_formatter.cpp b/services/distributeddataservice/service/data_share/data/resultset_json_formatter.cpp index 04e2d908fc9c333a302a81b3301799b03633a43f..5bdfa4a721ef829efd413156ce670003c1ff79b6 100644 --- a/services/distributeddataservice/service/data_share/data/resultset_json_formatter.cpp +++ b/services/distributeddataservice/service/data_share/data/resultset_json_formatter.cpp @@ -21,6 +21,7 @@ namespace OHOS::DataShare { bool ResultSetJsonFormatter::Marshal(json &node) const { + node = json::array(); int columnCount = 0; auto result = resultSet->GetColumnCount(columnCount); if (result != NativeRdb::E_OK) { 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 f084a0fe45a7a8f3d8502c5549b01c4c475e1229..cb7b14f75e881f8e69251c5cda9e3c35787c9dee 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -52,8 +52,6 @@ int32_t DataShareServiceImpl::Insert(const std::string &uri, const DataShareValu if (ret) { NotifyChange(uri); RdbSubscriberManager::GetInstance().Emit(uri, context); - SchedulerManager::GetInstance().Execute( - uri, context->currentUserId, context->calledSourceDir, context->version); } return ret; } @@ -83,8 +81,6 @@ int32_t DataShareServiceImpl::Update(const std::string &uri, const DataSharePred if (ret) { NotifyChange(uri); RdbSubscriberManager::GetInstance().Emit(uri, context); - SchedulerManager::GetInstance().Execute( - uri, context->currentUserId, context->calledSourceDir, context->version); } return ret; } @@ -97,8 +93,6 @@ int32_t DataShareServiceImpl::Delete(const std::string &uri, const DataSharePred if (ret) { NotifyChange(uri); RdbSubscriberManager::GetInstance().Emit(uri, context); - SchedulerManager::GetInstance().Execute( - uri, context->currentUserId, context->calledSourceDir, context->version); } return ret; } diff --git a/services/distributeddataservice/service/data_share/strategies/rdb_notify_strategy.cpp b/services/distributeddataservice/service/data_share/strategies/rdb_notify_strategy.cpp index 2c13eb862d02c939bb3b68fa583d72c6a8ba296c..ab3c95d8711c4a94c1f24d2f49b829670394b8b8 100644 --- a/services/distributeddataservice/service/data_share/strategies/rdb_notify_strategy.cpp +++ b/services/distributeddataservice/service/data_share/strategies/rdb_notify_strategy.cpp @@ -35,7 +35,7 @@ bool RdbNotifyStrategy::Execute(std::shared_ptr context) return false; } if (context->callerBundleName != context->calledBundleName) { - ZLOGE("not your data, cannot notify, callerBundleName: %{public}s, calledBundleName: %{public}s", + ZLOGD("not your data, cannot notify, callerBundleName: %{public}s, calledBundleName: %{public}s", context->callerBundleName.c_str(), context->calledBundleName.c_str()); return false; } diff --git a/services/distributeddataservice/service/data_share/subscriber_managers/rdb_subscriber_manager.cpp b/services/distributeddataservice/service/data_share/subscriber_managers/rdb_subscriber_manager.cpp index cb8a9059a0aab964a26042cf03789f0f0dccb35c..9acd29a0c9d8370710ed9748ee609223852b4374 100644 --- a/services/distributeddataservice/service/data_share/subscriber_managers/rdb_subscriber_manager.cpp +++ b/services/distributeddataservice/service/data_share/subscriber_managers/rdb_subscriber_manager.cpp @@ -229,6 +229,8 @@ void RdbSubscriberManager::Emit(const std::string &uri, std::shared_ptr Notify(key, context->currentUserId, val, context->calledSourceDir, context->version); return false; }); + SchedulerManager::GetInstance().Execute( + uri, context->currentUserId, context->calledSourceDir, context->version); } std::vector RdbSubscriberManager::GetKeysByUri(const std::string &uri) @@ -255,15 +257,6 @@ void RdbSubscriberManager::EmitByKey(const Key &key, int32_t userId, const std:: }); } -int RdbSubscriberManager::GetCount(const Key &key) -{ - auto pair = rdbCache_.Find(key); - if (!pair.first) { - return 0; - } - return pair.second.size(); -} - int RdbSubscriberManager::GetEnableObserverCount(const Key &key) { auto pair = rdbCache_.Find(key); @@ -300,6 +293,9 @@ int RdbSubscriberManager::Notify(const Key &key, int32_t userId, const std::vect changeNode.templateId_.bundleName_ = key.bundleName; for (const auto &predicate : tpl.predicates_) { std::string result = delegate->Query(predicate.selectSql_); + if (result.empty()) { + continue; + } changeNode.data_.emplace_back("{\"" + predicate.key_ + "\":" + result + "}"); } diff --git a/services/distributeddataservice/service/data_share/subscriber_managers/rdb_subscriber_manager.h b/services/distributeddataservice/service/data_share/subscriber_managers/rdb_subscriber_manager.h index d388c5afaaae3adafdbaaf945c3415d42c046f9b..32f8471e0a68025e5c00b190e8337e2cca7fb172 100644 --- a/services/distributeddataservice/service/data_share/subscriber_managers/rdb_subscriber_manager.h +++ b/services/distributeddataservice/service/data_share/subscriber_managers/rdb_subscriber_manager.h @@ -60,7 +60,6 @@ public: int Enable(const Key &key, std::shared_ptr context); void Emit(const std::string &uri, std::shared_ptr context); void EmitByKey(const Key &key, int32_t userId, const std::string &rdbPath, int version); - int GetCount(const Key &key); std::vector GetKeysByUri(const std::string &uri); void Clear(); diff --git a/services/distributeddataservice/service/test/fuzztest/datashareservicestub_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/datashareservicestub_fuzzer/BUILD.gn index e2cbf7fe23f061823321c7592632b2ef2be0a6d9..fc781eb68247c76a580bf38c0892e099f7e0d884 100644 --- a/services/distributeddataservice/service/test/fuzztest/datashareservicestub_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/datashareservicestub_fuzzer/BUILD.gn @@ -50,6 +50,7 @@ ohos_fuzztest("DataShareServiceStubFuzzTest") { "${data_service_path}/service/data_share/common/db_delegate.cpp", "${data_service_path}/service/data_share/common/div_strategy.cpp", "${data_service_path}/service/data_share/common/extension_connect_adaptor.cpp", + "${data_service_path}/service/data_share/common/extension_mgr_proxy.cpp", "${data_service_path}/service/data_share/common/kv_delegate.cpp", "${data_service_path}/service/data_share/common/rdb_delegate.cpp", "${data_service_path}/service/data_share/common/scheduler_manager.cpp", @@ -97,8 +98,8 @@ ohos_fuzztest("DataShareServiceStubFuzzTest") { external_deps = [ "ability_base:want", "ability_base:zuri", - "ability_runtime:ability_manager", "ability_runtime:dataobs_manager", + "ability_runtime:extension_manager", "access_token:libaccesstoken_sdk", "access_token:libtokenid_sdk", "bundle_framework:appexecfwk_base",