From 0f5e3f2ff4c33549ae3409d94981a8cff15284af Mon Sep 17 00:00:00 2001 From: wangyingli Date: Sun, 6 Jul 2025 16:04:02 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E7=9B=91=E5=90=AC=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E6=8B=89=E8=B5=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangyingli --- .../include/commonevent/back_search_event.h | 49 ++++ .../commonevent/pull_extension_event.h | 58 +++++ .../framework/include/eventcenter/event.h | 2 + .../service/data_share/BUILD.gn | 2 + .../common/search_callback_impl.cpp | 41 ++++ .../data_share/common/search_callback_impl.h | 38 ++++ .../common/search_connect_adaptor.cpp | 110 +++++++++ .../common/search_connect_adaptor.h | 38 ++++ .../data_share/data_share_service_impl.cpp | 3 + .../data_share/data_share_service_impl.h | 4 + .../service/test/data_share_common_test.cpp | 211 +++++++++++++----- 11 files changed, 503 insertions(+), 53 deletions(-) create mode 100644 services/distributeddataservice/framework/include/commonevent/back_search_event.h create mode 100644 services/distributeddataservice/framework/include/commonevent/pull_extension_event.h create mode 100644 services/distributeddataservice/service/data_share/common/search_callback_impl.cpp create mode 100644 services/distributeddataservice/service/data_share/common/search_callback_impl.h create mode 100644 services/distributeddataservice/service/data_share/common/search_connect_adaptor.cpp create mode 100644 services/distributeddataservice/service/data_share/common/search_connect_adaptor.h diff --git a/services/distributeddataservice/framework/include/commonevent/back_search_event.h b/services/distributeddataservice/framework/include/commonevent/back_search_event.h new file mode 100644 index 000000000..853fe2ed8 --- /dev/null +++ b/services/distributeddataservice/framework/include/commonevent/back_search_event.h @@ -0,0 +1,49 @@ +/* + * 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_SERVICES_FRAMEWORK_COMMON_EVENT_BACK_SEARCH_EVENT_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_COMMON_EVENT_BACK_SEARCH_EVENT_H + +#include "eventcenter/event.h" +#include "metadata/store_meta_data.h" +#include "visibility.h" +#include +#include +#include +#include + +namespace OHOS::DistributedData { +class API_EXPORT BackSearchEvent : public Event { +public: + struct BackInfo { + StoreMetaData meta; + std::string account; + }; + + BackSearchEvent(BackInfo backInfo, int32_t evtId = EVT_BACKSEARCH) + : Event(evtId), info_(backInfo) + { + } + const BackSearchEvent::BackInfo& GetInfo() const + { + return info_; + } + ~BackSearchEvent() override = default; + +private: + BackInfo info_; +}; +} // OHOS::DistributedData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_COMMON_EVENT_BACK_SEARCH_EVENT_H \ No newline at end of file diff --git a/services/distributeddataservice/framework/include/commonevent/pull_extension_event.h b/services/distributeddataservice/framework/include/commonevent/pull_extension_event.h new file mode 100644 index 000000000..7b52dd6d2 --- /dev/null +++ b/services/distributeddataservice/framework/include/commonevent/pull_extension_event.h @@ -0,0 +1,58 @@ +/* + * 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_SERVICES_FRAMEWORK_COMMON_EVENT_PULL_EXTENDION_EVENT_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_COMMON_EVENT_PULL_EXTENDION_EVENT_H + +#include "eventcenter/event.h" +#include "metadata/store_meta_data.h" +#include "visibility.h" +#include +#include +#include + +namespace OHOS::DistributedData { +class API_EXPORT PullExtensionEvent : public Event { +public: + using StoreMetaData = DistributedData::StoreMetaData; + struct EventInfo { + StoreMetaData meta; + std::string MeetimeUri; + std::string accountId; + + std::shared_ptr mutex_; + std::shared_ptr condition_; + bool conditionMet_ = false; + + ~EventInfo() {} + }; + + PullExtensionEvent(EventInfo evtInfo, int32_t evtId = EVT_SEARCH) + : Event(evtId), info_(std::move(evtInfo)) + { + } + + ~PullExtensionEvent() override = default; + + const PullExtensionEvent::EventInfo& GetInfo() const + { + return info_; + } + +private: + EventInfo info_; +}; +} // OHOS::DistributedData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_COMMON_EVENT_PULL_EXTENDION_EVENT_H \ No newline at end of file diff --git a/services/distributeddataservice/framework/include/eventcenter/event.h b/services/distributeddataservice/framework/include/eventcenter/event.h index 618f6fcb7..7aea323e1 100644 --- a/services/distributeddataservice/framework/include/eventcenter/event.h +++ b/services/distributeddataservice/framework/include/eventcenter/event.h @@ -31,6 +31,8 @@ public: EVT_CLOUD = 0x2000, EVT_BIND = 0X3000, EVT_CHANGE = 0X4000, + EVT_SEARCH = 0X5000, + EVT_BACKSEARCH = 0X6000 }; API_EXPORT Event(int32_t evtId); API_EXPORT Event(Event &&) noexcept = delete; diff --git a/services/distributeddataservice/service/data_share/BUILD.gn b/services/distributeddataservice/service/data_share/BUILD.gn index 68e54c60c..2f3851ca9 100644 --- a/services/distributeddataservice/service/data_share/BUILD.gn +++ b/services/distributeddataservice/service/data_share/BUILD.gn @@ -61,6 +61,8 @@ ohos_source_set("data_share_service") { "common/scheduler_manager.cpp", "common/seq_strategy.cpp", "common/uri_utils.cpp", + "common/search_connect_adaptor.cpp", + "common/search_callback_impl.cpp", "data/published_data.cpp", "data/resultset_json_formatter.cpp", "data/template_data.cpp", diff --git a/services/distributeddataservice/service/data_share/common/search_callback_impl.cpp b/services/distributeddataservice/service/data_share/common/search_callback_impl.cpp new file mode 100644 index 000000000..d86b2509a --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/search_callback_impl.cpp @@ -0,0 +1,41 @@ +/* + * 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. + */ +#define LOG_TAG "SearchCallbackimpl" + +#include "ability_connect_callback_stub.h" +#include "block_data.h" +#include "iremote_object.h" +#include "datashare_log.h" +#include "search_callback_impl.h" + +namespace OHOS::DataShare { +void SearchCallbackImpl::OnAbilityConnectDone(const AppExecFwk::ElementName &element, + const sptr &remoteObject, int resultCode) +{ + std::unique_lock lock(connectingMtx_); + isConnectdone = true; + remoteObject_ = remoteObject; + connectCondition_.notify_all(); +} + +void SearchCallbackImpl::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) +{ + remoteObject_ = nullptr; +} + +sptr SearchCallbackImpl::GetRemoteObject() const { + return remoteObject_; +} +} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/common/search_callback_impl.h b/services/distributeddataservice/service/data_share/common/search_callback_impl.h new file mode 100644 index 000000000..887fc3b23 --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/search_callback_impl.h @@ -0,0 +1,38 @@ +/* + * 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 DATASHARESERVICE_SEARCH_CALLBACK_IMPL_H +#define DATASHARESERVICE_SEARCH_CALLBACK_IMPL_H + +#include "ability_connect_callback_stub.h" +#include "block_data.h" +#include "iremote_object.h" + +namespace OHOS::DataShare { +class SearchCallbackImpl : public AAFwk::AbilityConnectionStub { +public: + explicit SearchCallbackImpl() : remoteObject_(nullptr){} + void OnAbilityConnectDone( + const AppExecFwk::ElementName &element, const sptr &remoteObject, int resultCode) override; + void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override; + sptr GetRemoteObject() const; + + std::mutex connectingMtx_; + std::condition_variable connectCondition_; + bool isConnectdone = true; +private: + sptr remoteObject_; +}; +} // namespace OHOS::DataShare +#endif // DATASHARESERVICE_SEARCH_CALLBACK_IMPL_H diff --git a/services/distributeddataservice/service/data_share/common/search_connect_adaptor.cpp b/services/distributeddataservice/service/data_share/common/search_connect_adaptor.cpp new file mode 100644 index 000000000..d49984915 --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/search_connect_adaptor.cpp @@ -0,0 +1,110 @@ +/* + * 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. + */ +#define LOG_TAG "SearchConnectAdaptor" +#include "search_connect_adaptor.h" +#include +#include "app_connect_manager.h" +#include "search_callback_impl.h" +#include "extension_ability_manager.h" +#include "extension_ability_info.h" +#include "extension_mgr_proxy.h" +#include "log_print.h" +#include "uri_utils.h" +#include "itypes_util.h" + +namespace OHOS::DataShare { + +const std::u16string INTERFACE_TOKEN = u"OHOS.DataShare.IDataShare"; +static constexpr int REQUEST_CODE = 13; +constexpr int64_t WAITTIME = 1; + +sptr SearchConnectAdaptor::callback_ = nullptr; +SearchConnectAdaptor::SearchConnectAdaptor() +{ + callback_ = new (std::nothrow) SearchCallbackImpl(); +} + +bool SearchConnectAdaptor::DoConnect(const std::string &uri, const std::string &bundleName, + AAFwk::WantParams &wantParams) +{ + if (callback_ == nullptr) { + return false; + } + ErrCode ret = ExtensionMgrProxy::GetInstance()->Connect(uri, callback_->AsObject(), nullptr, wantParams); + if (ret != ERR_OK) { + ZLOGE("connect ability failed, ret = %{public}d, uri: %{public}s", ret, + URIUtils::Anonymous(uri).c_str()); + return false; + } + ZLOGI("Do connect, uri: %{public}s", URIUtils::Anonymous(uri).c_str()); + return true; +} + +std::pair SearchConnectAdaptor::Connect(const std::string &uri, const StoreMetaData &meta, + AAFwk::WantParams &wantParams, int maxWaitTime) +{ + bool isConnectdone; + { + std::unique_lock lock(callback_->connectingMtx_); + isConnectdone = callback_->isConnectdone; + } + if (isConnectdone) { + std::unique_lock lock(callback_->connectingMtx_); + callback_->isConnectdone = false; + } else { + std::unique_lock lock(connectMutex); + callback_->connectCV.wait_for(lock, std::chrono::seconds(WAITTIME)); + std::unique_lock lck(callback_->connectingMtx_); + callback_->isConnectdone = false; + } + + SearchConnectAdaptor strategy; + auto bundleName = meta.bundleName; + auto errCode = AppConnectManager::Wait( + bundleName, maxWaitTime, + [&uri, &bundleName, &strategy, &wantParams]() { + auto connectCode = strategy.DoConnect(uri, bundleName, wantParams); + return connectCode; + }, + [&strategy]() {}); + + MessageParcel reply; + MessageOption option; + MessageParcel data; + data.WriteInterfaceToken(INTERFACE_TOKEN); + Uri dataDir(meta.dataDir); + sptr remote = callback_->GetRemoteObject(); + if (remote == nullptr) { + ZLOGE("remote is null"); + return std::make_pair(false, ""); + } + Uri result(""); + int32_t err = remote->SendRequest(static_cast(REQUEST_CODE), data, reply, option); + if (err != ERR_NONE) { + ZLOGE("NormalizeUri fail to SendRequest. err: %{public}d", err); + return std::make_pair(false, ""); + } + Disconnect(); + return std::make_pair(true, result.ToString()); +} + +void SearchConnectAdaptor::Disconnect() +{ + if (callback_ == nullptr) { + return; + } + callback_ = nullptr; +} +} // namespace OHOS::DataShare \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/common/search_connect_adaptor.h b/services/distributeddataservice/service/data_share/common/search_connect_adaptor.h new file mode 100644 index 000000000..bf9e7286d --- /dev/null +++ b/services/distributeddataservice/service/data_share/common/search_connect_adaptor.h @@ -0,0 +1,38 @@ +/* + * 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 DATASHARESERVICE_SEARCH_CONNECT_ADAPTOR_H +#define DATASHARESERVICE_SEARCH_CONNECT_ADAPTOR_H + +#include "ability_connect_callback_interface.h" +#include "block_data.h" +#include "metadata/store_meta_data.h" +#include "search_callback_impl.h" + +namespace OHOS::DataShare { +class SearchConnectAdaptor { +public: + using StoreMetaData = DistributedData::StoreMetaData; + static std::pair Connect(const std::string &uri, const StoreMetaData &meta, + AAFwk::WantParams &wantParams, int maxWaitTime = 2); + SearchConnectAdaptor(); +private: + void Disconnect(); + bool DoConnect(const std::string &uri, const std::string &bundleName, AAFwk::WantParams &wantParams); + static sptr callback_; + std::mutex connectMutex; +}; +} // namespace OHOS::DataShare +#endif // DATASHARESERVICE_SEARCH_CONNECT_ADAPTOR_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 4043d0511..ec39fe4b8 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -63,6 +63,8 @@ #include "proxy_data_manager.h" #include "datashare_observer.h" #include "subscriber_managers/proxy_data_subscriber_manager.h" +#include "search_connect_adaptor.h" +#include "commonevent/back_search_event.h" namespace OHOS::DataShare { using FeatureSystem = DistributedData::FeatureSystem; @@ -606,6 +608,7 @@ int32_t DataShareServiceImpl::OnBind(const BindInfo &binderInfo) SubscribeConcurrentTask(); SubscribeTimeChanged(); SubscribeChange(); + SubscribeSearch(); ZLOGI("end"); return E_OK; } diff --git a/services/distributeddataservice/service/data_share/data_share_service_impl.h b/services/distributeddataservice/service/data_share/data_share_service_impl.h index d39912924..5cfd63b6e 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.h +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.h @@ -41,10 +41,12 @@ #include "template_strategy.h" #include "uri_utils.h" #include "visibility.h" +#include "commonevent/pull_extension_event.h" namespace OHOS::DataShare { class DataShareServiceImpl : public DataShareServiceStub { public: + using StoreMetaData = DistributedData::StoreMetaData; using Handler = std::function> &)>; using ExecuteCallbackEx = std::function(DataProviderConfig::ProviderInfo &, DistributedData::StoreMetaData &, std::shared_ptr)>; @@ -138,7 +140,9 @@ private: void SubscribeConcurrentTask(); static void InitSubEvent(); void AutoLaunch(const DistributedData::Event &event); + void PullService(DistributedData::PullExtensionEvent::EventInfo &eventInfo); void SubscribeChange(); + void SubscribeSearch(); bool AllowCleanDataLaunchApp(const DistributedData::Event &event, bool launchForCleanData); static void SaveLaunchInfo(const std::string &bundleName, const std::string &userId, const std::string &deviceId); diff --git a/services/distributeddataservice/service/test/data_share_common_test.cpp b/services/distributeddataservice/service/test/data_share_common_test.cpp index a6db0130b..613b05b7c 100644 --- a/services/distributeddataservice/service/test/data_share_common_test.cpp +++ b/services/distributeddataservice/service/test/data_share_common_test.cpp @@ -17,6 +17,7 @@ #include #include #include "extension_connect_adaptor.h" +#include "search_connect_adaptor.h" #include "data_share_profile_config.h" #include "div_strategy.h" #include "log_print.h" @@ -25,17 +26,32 @@ #include "scheduler_manager.h" #include "seq_strategy.h" #include "strategy.h" +#include "metadata/store_meta_data.h" namespace OHOS::Test { using namespace testing::ext; using namespace OHOS::DataShare; +using StoreMetaData = DistributedData::StoreMetaData; + +class MockCallback : public SearchCallbackImpl { +public: + MockCallback() = default; + ~MockCallback() = default; +}; + class DataShareCommonTest : public testing::Test { public: static constexpr int64_t USER_TEST = 100; static void SetUpTestCase(void){}; static void TearDownTestCase(void){}; - void SetUp(){}; + void SetUp() override { + adaptor_ = std::make_shared(); + callback_ = new MockCallback(); + adaptor_->callback_ = callback_; + }; void TearDown(){}; + std::shared_ptr adaptor_; + MockCallback* callback_; }; /** @@ -163,8 +179,8 @@ HWTEST_F(DataShareCommonTest, InsertEx001, TestSize.Level1) bool registerFunction = false; std::string extUri = "uri"; std::string backup = "backup"; - RdbDelegate rdbDelegate; - rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); + RdbDelegate rdbDelegate; + rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); rdbDelegate.store_ = nullptr; std::string tableName = ""; DataShare::DataShareValuesBucket valuesBucket; @@ -197,8 +213,8 @@ HWTEST_F(DataShareCommonTest, UpdateEx001, TestSize.Level1) bool registerFunction = false; std::string extUri = "uri"; std::string backup = "backup"; - RdbDelegate rdbDelegate; - rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); + RdbDelegate rdbDelegate; + rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); rdbDelegate.store_ = nullptr; std::string tableName = ""; DataShare::DataShareValuesBucket valuesBucket; @@ -232,8 +248,8 @@ HWTEST_F(DataShareCommonTest, DeleteEx001, TestSize.Level1) bool registerFunction = false; std::string extUri = "uri"; std::string backup = "backup"; - RdbDelegate rdbDelegate; - rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); + RdbDelegate rdbDelegate; + rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); rdbDelegate.store_ = nullptr; std::string tableName = ""; DataSharePredicates predicate; @@ -264,8 +280,8 @@ HWTEST_F(DataShareCommonTest, Query001, TestSize.Level1) bool registerFunction = false; std::string extUri = "uri"; std::string backup = "backup"; - RdbDelegate rdbDelegate; - rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); + RdbDelegate rdbDelegate; + rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); rdbDelegate.store_ = nullptr; std::string tableName = ""; DataSharePredicates predicate; @@ -299,8 +315,8 @@ HWTEST_F(DataShareCommonTest, Query002, TestSize.Level1) bool registerFunction = false; std::string extUri = "uri"; std::string backup = "backup"; - RdbDelegate rdbDelegate; - rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); + RdbDelegate rdbDelegate; + rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); rdbDelegate.store_ = nullptr; std::string sql = "testsql"; std::vector selectionArgs; @@ -331,8 +347,8 @@ HWTEST_F(DataShareCommonTest, QuerySql001, TestSize.Level1) bool registerFunction = false; std::string extUri = "uri"; std::string backup = "backup"; - RdbDelegate rdbDelegate; - rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); + RdbDelegate rdbDelegate; + rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); rdbDelegate.store_ = nullptr; std::string sql = "testsql"; auto result = rdbDelegate.QuerySql(sql); @@ -362,8 +378,8 @@ HWTEST_F(DataShareCommonTest, UpdateSql001, TestSize.Level1) bool registerFunction = false; std::string extUri = "uri"; std::string backup = "backup"; - RdbDelegate rdbDelegate; - rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); + RdbDelegate rdbDelegate; + rdbDelegate.Init(metaData, version, registerFunction, extUri, backup); rdbDelegate.store_ = nullptr; std::string sql = "testsql"; auto result = rdbDelegate.UpdateSql(sql); @@ -571,42 +587,131 @@ HWTEST_F(DataShareCommonTest, ClearTimer003, TestSize.Level1) EXPECT_TRUE(manager.timerCache_.empty()); ZLOGI("ClearTimer003 end"); } - -/** - * @tc.name: DBDelegateTest001 - * @tc.desc: do nothing when delegate already inited - * @tc.type: FUNC - */ -HWTEST_F(DataShareCommonTest, DBDelegateTest001, TestSize.Level0) { - RdbDelegate delegate; - DistributedData::StoreMetaData meta; - meta.tokenId = 1; - - delegate.isInited_ = true; - delegate.Init({}, 0, false, "extUri", "backup"); - - EXPECT_TRUE(delegate.isInited_); - EXPECT_EQ(delegate.tokenId_, 0); - EXPECT_EQ(delegate.extUri_, ""); - EXPECT_EQ(delegate.backup_, ""); -} - -/** - * @tc.name : DBDelegateTest002 - * @tc.number: init members in delegate init - * @tc.type: FUNC - */ -HWTEST_F(DataShareCommonTest, DBDelegateTest002, TestSize.Level0) { - RdbDelegate delegate; - DistributedData::StoreMetaData meta; - meta.tokenId = 1; - - delegate.isInited_ = false; - delegate.Init(meta, 0, false, "extUri", "backup"); - - EXPECT_FALSE(delegate.isInited_); - EXPECT_EQ(delegate.tokenId_, meta.tokenId); - EXPECT_EQ(delegate.extUri_, "extUri"); - EXPECT_EQ(delegate.backup_, "backup"); -} + +/** + * @tc.name: DBDelegateTest001 + * @tc.desc: do nothing when delegate already inited + * @tc.type: FUNC + */ +HWTEST_F(DataShareCommonTest, DBDelegateTest001, TestSize.Level0) { + RdbDelegate delegate; + DistributedData::StoreMetaData meta; + meta.tokenId = 1; + + delegate.isInited_ = true; + delegate.Init({}, 0, false, "extUri", "backup"); + + EXPECT_TRUE(delegate.isInited_); + EXPECT_EQ(delegate.tokenId_, 0); + EXPECT_EQ(delegate.extUri_, ""); + EXPECT_EQ(delegate.backup_, ""); +} + +/** + * @tc.name : DBDelegateTest002 + * @tc.number: init members in delegate init + * @tc.type: FUNC + */ +HWTEST_F(DataShareCommonTest, DBDelegateTest002, TestSize.Level0) { + RdbDelegate delegate; + DistributedData::StoreMetaData meta; + meta.tokenId = 1; + + delegate.isInited_ = false; + delegate.Init(meta, 0, false, "extUri", "backup"); + + EXPECT_FALSE(delegate.isInited_); + EXPECT_EQ(delegate.tokenId_, meta.tokenId); + EXPECT_EQ(delegate.extUri_, "extUri"); + EXPECT_EQ(delegate.backup_, "backup"); +} + +/** +* @tc.name: DoConnect002 +* @tc.desc: test DoConnect function in SearchConnectAdaptor when callback_ is nullptr +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Creat a SearchConnectAdaptor object and callback = nullptr + 2.call DoConnect function +* @tc.experct: DoConnect failed and return false +*/ +HWTEST_F(DataShareCommonTest, DoConnect002, TestSize.Level1) +{ + ZLOGI("DoConnect002 start"); + std::string uri = "testUri"; + std::string bundleName = "testBundle"; + AAFwk::WantParams wantParams; + SearchConnectAdaptor searchConnectAdaptor; + bool result = adaptor_->DoConnect(uri, bundleName, wantParams); + EXPECT_FALSE(result); + ZLOGI("DoConnect002 end"); +} + +/** +* @tc.name: DisConnectTest +* @tc.desc: test DisConnect function in SearchConnectAdaptor when callback_ is not null +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Creat a SearchConnectAdaptor object and callback = nullptr + 2.call DisConnect function +* @tc.experct: callback_ is nullptr +*/ +HWTEST_F(DataShareCommonTest, DisConnectTest, TestSize.Level1) +{ + std::shared_ptr adaptor_ = std::make_shared(); + EXPECT_NE(adaptor_->callback, nullptr); + adaptor_->DisConnect(); + EXPECT_EQ(adaptor_->callback, nullptr); +} + +/** +* @tc.name: ConnectTest +* @tc.desc: test Connect function in SearchConnectAdaptor +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Prepare test data + 2.call Connect function +*/ +HWTEST_F(DataShareCommonTest, ConnectTest001, TestSize.Level1) +{ + const std::string uri = "testUri"; + StoreMetaData meta; + meta.dataDir = "testdir"; + meta.bundlename = "test_bundlename" + AAFwk::WantParams wantParams; + const int maxWaitTime = 1; + auto result = adaptor_->Connect(uri, meta, wantParams, maxWaitTime); + EXPECT_TRUE(result.first); + EXPECT_FALSE(result.second.empty()); +} + +/** +* @tc.name: ConnectTest +* @tc.desc: test Connect function in SearchConnectAdaptor +* @tc.type: FUNC +* @tc.require:issueIBX9E1 +* @tc.precon: None +* @tc.step: + 1.Prepare test data + 2.call Connect function +*/ +HWTEST_F(DataShareCommonTest, ConnectTest002, TestSize.Level1) +{ + const std::string uri = "testUri"; + StoreMetaData meta; + meta.dataDir = "testdir"; + meta.bundlename = "test_bundlename" + AAFwk::WantParams wantParams; + const int maxWaitTime = 1; + adaptor_->callback = nullptr; + auto result = adaptor_->Connect(uri, meta, wantParams, maxWaitTime); + EXPECT_FALSE(result.first); + EXPECT_TRUE(result.second.empty()); +} } // namespace OHOS::Test \ No newline at end of file -- Gitee From 8bbacbd3ab1e6d537a41846046e5749d185a81f0 Mon Sep 17 00:00:00 2001 From: wangyingli Date: Sun, 6 Jul 2025 16:21:57 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E7=9B=91=E5=90=AC=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E6=8B=89=E8=B5=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangyingli --- .../include/commonevent/back_search_event.h | 98 ++++---- .../commonevent/pull_extension_event.h | 116 ++++----- .../common/search_callback_impl.cpp | 80 ++++--- .../common/search_connect_adaptor.cpp | 221 +++++++++--------- .../common/search_connect_adaptor.h | 2 +- .../data_share/data_share_service_impl.cpp | 23 ++ 6 files changed, 281 insertions(+), 259 deletions(-) diff --git a/services/distributeddataservice/framework/include/commonevent/back_search_event.h b/services/distributeddataservice/framework/include/commonevent/back_search_event.h index 853fe2ed8..a8b35b306 100644 --- a/services/distributeddataservice/framework/include/commonevent/back_search_event.h +++ b/services/distributeddataservice/framework/include/commonevent/back_search_event.h @@ -1,49 +1,49 @@ -/* - * 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_SERVICES_FRAMEWORK_COMMON_EVENT_BACK_SEARCH_EVENT_H -#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_COMMON_EVENT_BACK_SEARCH_EVENT_H - -#include "eventcenter/event.h" -#include "metadata/store_meta_data.h" -#include "visibility.h" -#include -#include -#include -#include - -namespace OHOS::DistributedData { -class API_EXPORT BackSearchEvent : public Event { -public: - struct BackInfo { - StoreMetaData meta; - std::string account; - }; - - BackSearchEvent(BackInfo backInfo, int32_t evtId = EVT_BACKSEARCH) - : Event(evtId), info_(backInfo) - { - } - const BackSearchEvent::BackInfo& GetInfo() const - { - return info_; - } - ~BackSearchEvent() override = default; - -private: - BackInfo info_; -}; -} // OHOS::DistributedData -#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_COMMON_EVENT_BACK_SEARCH_EVENT_H \ No newline at end of file +/* + * 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_SERVICES_FRAMEWORK_COMMON_EVENT_BACK_SEARCH_EVENT_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_COMMON_EVENT_BACK_SEARCH_EVENT_H + +#include "eventcenter/event.h" +#include "metadata/store_meta_data.h" +#include "visibility.h" +#include +#include +#include +#include + +namespace OHOS::DistributedData { +class API_EXPORT BackSearchEvent : public Event { +public: + struct BackInfo { + StoreMetaData meta; + std::string account; + }; + + BackSearchEvent(BackInfo backInfo, int32_t evtId = EVT_BACKSEARCH) + : Event(evtId), info_(backInfo) + { + } + const BackSearchEvent::BackInfo& GetInfo() const + { + return info_; + } + ~BackSearchEvent() override = default; + +private: + BackInfo info_; +}; +} // OHOS::DistributedData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_COMMON_EVENT_BACK_SEARCH_EVENT_H diff --git a/services/distributeddataservice/framework/include/commonevent/pull_extension_event.h b/services/distributeddataservice/framework/include/commonevent/pull_extension_event.h index 7b52dd6d2..008ad144e 100644 --- a/services/distributeddataservice/framework/include/commonevent/pull_extension_event.h +++ b/services/distributeddataservice/framework/include/commonevent/pull_extension_event.h @@ -1,58 +1,58 @@ -/* - * 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_SERVICES_FRAMEWORK_COMMON_EVENT_PULL_EXTENDION_EVENT_H -#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_COMMON_EVENT_PULL_EXTENDION_EVENT_H - -#include "eventcenter/event.h" -#include "metadata/store_meta_data.h" -#include "visibility.h" -#include -#include -#include - -namespace OHOS::DistributedData { -class API_EXPORT PullExtensionEvent : public Event { -public: - using StoreMetaData = DistributedData::StoreMetaData; - struct EventInfo { - StoreMetaData meta; - std::string MeetimeUri; - std::string accountId; - - std::shared_ptr mutex_; - std::shared_ptr condition_; - bool conditionMet_ = false; - - ~EventInfo() {} - }; - - PullExtensionEvent(EventInfo evtInfo, int32_t evtId = EVT_SEARCH) - : Event(evtId), info_(std::move(evtInfo)) - { - } - - ~PullExtensionEvent() override = default; - - const PullExtensionEvent::EventInfo& GetInfo() const - { - return info_; - } - -private: - EventInfo info_; -}; -} // OHOS::DistributedData -#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_COMMON_EVENT_PULL_EXTENDION_EVENT_H \ No newline at end of file +/* + * 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_SERVICES_FRAMEWORK_COMMON_EVENT_PULL_EXTENDION_EVENT_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_COMMON_EVENT_PULL_EXTENDION_EVENT_H + +#include "eventcenter/event.h" +#include "metadata/store_meta_data.h" +#include "visibility.h" +#include +#include +#include + +namespace OHOS::DistributedData { +class API_EXPORT PullExtensionEvent : public Event { +public: + using StoreMetaData = DistributedData::StoreMetaData; + struct EventInfo { + StoreMetaData meta; + std::string Uri; + std::string accountId; + + std::shared_ptr mutex_; + std::shared_ptr condition_; + bool conditionMet_ = false; + + ~EventInfo() {} + }; + + PullExtensionEvent(EventInfo evtInfo, int32_t evtId = EVT_SEARCH) + : Event(evtId), info_(std::move(evtInfo)) + { + } + + ~PullExtensionEvent() override = default; + + const PullExtensionEvent::EventInfo& GetInfo() const + { + return info_; + } + +private: + EventInfo info_; +}; +} // OHOS::DistributedData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_COMMON_EVENT_PULL_EXTENDION_EVENT_H diff --git a/services/distributeddataservice/service/data_share/common/search_callback_impl.cpp b/services/distributeddataservice/service/data_share/common/search_callback_impl.cpp index d86b2509a..f82d61010 100644 --- a/services/distributeddataservice/service/data_share/common/search_callback_impl.cpp +++ b/services/distributeddataservice/service/data_share/common/search_callback_impl.cpp @@ -1,41 +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. - */ -#define LOG_TAG "SearchCallbackimpl" - -#include "ability_connect_callback_stub.h" -#include "block_data.h" -#include "iremote_object.h" -#include "datashare_log.h" -#include "search_callback_impl.h" - -namespace OHOS::DataShare { -void SearchCallbackImpl::OnAbilityConnectDone(const AppExecFwk::ElementName &element, - const sptr &remoteObject, int resultCode) -{ - std::unique_lock lock(connectingMtx_); - isConnectdone = true; - remoteObject_ = remoteObject; - connectCondition_.notify_all(); -} - -void SearchCallbackImpl::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) -{ - remoteObject_ = nullptr; -} - -sptr SearchCallbackImpl::GetRemoteObject() const { - return remoteObject_; -} -} \ No newline at end of file +/* + * 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 "ability_connect_callback_stub.h" +#include "block_data.h" +#include "iremote_object.h" +#include "datashare_log.h" +#include "search_callback_impl.h" + +namespace OHOS::DataShare { +void SearchCallbackImpl::OnAbilityConnectDone(const AppExecFwk::ElementName &element, + const sptr &remoteObject, int resultCode) +{ + std::unique_lock lock(connectingMtx_); + isConnectdone = true; + remoteObject_ = remoteObject; + connectCondition_.notify_all(); +} + +void SearchCallbackImpl::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) +{ + remoteObject_ = nullptr; +} + +sptr SearchCallbackImpl::GetRemoteObject() const { + return remoteObject_; +} +} diff --git a/services/distributeddataservice/service/data_share/common/search_connect_adaptor.cpp b/services/distributeddataservice/service/data_share/common/search_connect_adaptor.cpp index d49984915..65d28386a 100644 --- a/services/distributeddataservice/service/data_share/common/search_connect_adaptor.cpp +++ b/services/distributeddataservice/service/data_share/common/search_connect_adaptor.cpp @@ -1,110 +1,111 @@ -/* - * 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. - */ -#define LOG_TAG "SearchConnectAdaptor" -#include "search_connect_adaptor.h" -#include -#include "app_connect_manager.h" -#include "search_callback_impl.h" -#include "extension_ability_manager.h" -#include "extension_ability_info.h" -#include "extension_mgr_proxy.h" -#include "log_print.h" -#include "uri_utils.h" -#include "itypes_util.h" - -namespace OHOS::DataShare { - -const std::u16string INTERFACE_TOKEN = u"OHOS.DataShare.IDataShare"; -static constexpr int REQUEST_CODE = 13; -constexpr int64_t WAITTIME = 1; - -sptr SearchConnectAdaptor::callback_ = nullptr; -SearchConnectAdaptor::SearchConnectAdaptor() -{ - callback_ = new (std::nothrow) SearchCallbackImpl(); -} - -bool SearchConnectAdaptor::DoConnect(const std::string &uri, const std::string &bundleName, - AAFwk::WantParams &wantParams) -{ - if (callback_ == nullptr) { - return false; - } - ErrCode ret = ExtensionMgrProxy::GetInstance()->Connect(uri, callback_->AsObject(), nullptr, wantParams); - if (ret != ERR_OK) { - ZLOGE("connect ability failed, ret = %{public}d, uri: %{public}s", ret, - URIUtils::Anonymous(uri).c_str()); - return false; - } - ZLOGI("Do connect, uri: %{public}s", URIUtils::Anonymous(uri).c_str()); - return true; -} - -std::pair SearchConnectAdaptor::Connect(const std::string &uri, const StoreMetaData &meta, - AAFwk::WantParams &wantParams, int maxWaitTime) -{ - bool isConnectdone; - { - std::unique_lock lock(callback_->connectingMtx_); - isConnectdone = callback_->isConnectdone; - } - if (isConnectdone) { - std::unique_lock lock(callback_->connectingMtx_); - callback_->isConnectdone = false; - } else { - std::unique_lock lock(connectMutex); - callback_->connectCV.wait_for(lock, std::chrono::seconds(WAITTIME)); - std::unique_lock lck(callback_->connectingMtx_); - callback_->isConnectdone = false; - } - - SearchConnectAdaptor strategy; - auto bundleName = meta.bundleName; - auto errCode = AppConnectManager::Wait( - bundleName, maxWaitTime, - [&uri, &bundleName, &strategy, &wantParams]() { - auto connectCode = strategy.DoConnect(uri, bundleName, wantParams); - return connectCode; - }, - [&strategy]() {}); - - MessageParcel reply; - MessageOption option; - MessageParcel data; - data.WriteInterfaceToken(INTERFACE_TOKEN); - Uri dataDir(meta.dataDir); - sptr remote = callback_->GetRemoteObject(); - if (remote == nullptr) { - ZLOGE("remote is null"); - return std::make_pair(false, ""); - } - Uri result(""); - int32_t err = remote->SendRequest(static_cast(REQUEST_CODE), data, reply, option); - if (err != ERR_NONE) { - ZLOGE("NormalizeUri fail to SendRequest. err: %{public}d", err); - return std::make_pair(false, ""); - } - Disconnect(); - return std::make_pair(true, result.ToString()); -} - -void SearchConnectAdaptor::Disconnect() -{ - if (callback_ == nullptr) { - return; - } - callback_ = nullptr; -} -} // namespace OHOS::DataShare \ No newline at end of file +/* + * 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. + */ +#define LOG_TAG "SearchConnectAdaptor" +#include "search_connect_adaptor.h" +#include +#include "app_connect_manager.h" +#include "search_callback_impl.h" +#include "extension_ability_manager.h" +#include "extension_ability_info.h" +#include "extension_mgr_proxy.h" +#include "log_print.h" +#include "uri_utils.h" +#include "itypes_util.h" + +namespace OHOS::DataShare { + +const std::u16string INTERFACE_TOKEN = u"OHOS.DataShare.IDataShare"; +static constexpr int REQUEST_CODE = 13; +constexpr int64_t WAITTIME = 1; + +sptr SearchConnectAdaptor::callback_ = nullptr; +SearchConnectAdaptor::SearchConnectAdaptor() +{ + callback_ = new (std::nothrow) SearchCallbackImpl(); +} + +bool SearchConnectAdaptor::DoConnect(const std::string &uri, const std::string &bundleName, + AAFwk::WantParams &wantParams) +{ + if (callback_ == nullptr) { + ZLOGE("The callback_ is nullptr"); + return false; + } + ErrCode ret = ExtensionMgrProxy::GetInstance()->Connect(uri, callback_->AsObject(), nullptr, wantParams); + if (ret != ERR_OK) { + ZLOGE("connect ability failed, ret = %{public}d, uri: %{public}s", ret, + URIUtils::Anonymous(uri).c_str()); + return false; + } + ZLOGI("Do connect, uri: %{public}s", URIUtils::Anonymous(uri).c_str()); + return true; +} + +std::pair SearchConnectAdaptor::Connect(const std::string &uri, const StoreMetaData &meta, + AAFwk::WantParams &wantParams, int maxWaitTime) +{ + bool isConnectdone; + { + std::unique_lock lock(callback_->connectingMtx_); + isConnectdone = callback_->isConnectdone; + } + if (isConnectdone) { + std::unique_lock lock(callback_->connectingMtx_); + callback_->isConnectdone = false; + } else { + std::unique_lock lock(connectMutex); + callback_->connectCV.wait_for(lock, std::chrono::seconds(WAITTIME)); + std::unique_lock lck(callback_->connectingMtx_); + callback_->isConnectdone = false; + } + + SearchConnectAdaptor strategy; + auto bundleName = meta.bundleName; + auto errCode = AppConnectManager::Wait( + bundleName, maxWaitTime, + [&uri, &bundleName, &strategy, &wantParams]() { + auto connectCode = strategy.DoConnect(uri, bundleName, wantParams); + return connectCode; + }, + [&strategy]() {}); + + MessageParcel reply; + MessageOption option; + MessageParcel data; + data.WriteInterfaceToken(INTERFACE_TOKEN); + Uri dataDir(meta.dataDir); + sptr remote = callback_->GetRemoteObject(); + if (remote == nullptr) { + ZLOGE("remote is null"); + return std::make_pair(false, ""); + } + Uri result(""); + int32_t err = remote->SendRequest(static_cast(REQUEST_CODE), data, reply, option); + if (err != ERR_NONE) { + ZLOGE("NormalizeUri fail to SendRequest. err: %{public}d", err); + return std::make_pair(false, ""); + } + Disconnect(); + return std::make_pair(true, result.ToString()); +} + +void SearchConnectAdaptor::Disconnect() +{ + if (callback_ == nullptr) { + return; + } + callback_ = nullptr; +} +} // namespace OHOS::DataShare diff --git a/services/distributeddataservice/service/data_share/common/search_connect_adaptor.h b/services/distributeddataservice/service/data_share/common/search_connect_adaptor.h index bf9e7286d..cfda89c56 100644 --- a/services/distributeddataservice/service/data_share/common/search_connect_adaptor.h +++ b/services/distributeddataservice/service/data_share/common/search_connect_adaptor.h @@ -26,7 +26,7 @@ class SearchConnectAdaptor { public: using StoreMetaData = DistributedData::StoreMetaData; static std::pair Connect(const std::string &uri, const StoreMetaData &meta, - AAFwk::WantParams &wantParams, int maxWaitTime = 2); + AAFwk::WantParams &wantParams, int maxWaitTime = 1); SearchConnectAdaptor(); private: void Disconnect(); 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 ec39fe4b8..35408ae9d 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -655,6 +655,29 @@ void DataShareServiceImpl::SubscribeChange() }); } +void DataShareServiceImpl::SubScribeSearch() +{ + EventCenter::GetInstance().Subscribe(PullExtensionEvent::EVT_SEARCH, [this](const Event &event) { + auto &evt = static_cast(event); + auto eventInfo = evt.GetInfo(); + PullExtension(eventInfo); + }); +} + +void DataShareServiceImpl::PullService(PullExtensionEvent::EventInfo &eventInfo) +{ + const StoreMetaData &meta = eventInfo.meta; + const std::string &uri = eventInfo.Uri; + AAFwk::WantParams wantParams; + SearchConnectAdaptor adaptor; + auto[errcode, reply] = adaptor.Connect(uri, meta, wantParams); + BackSearchEvent::BackInfo backInfo; + backInfo.meta = meta; + backInfo.account = apply; + auto evt = std::make_unique(std::move(backInfo), Event::EVT_BACKSEARCH); + EventCenter::GetInstance().PostEvent(std::move(evt)); +} + void DataShareServiceImpl::SaveLaunchInfo(const std::string &bundleName, const std::string &userId, const std::string &deviceId) { -- Gitee From b5a1827060700bd06a5f0655b0c44209404ce7bd Mon Sep 17 00:00:00 2001 From: wangyingli Date: Sun, 6 Jul 2025 17:22:01 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E7=9B=91=E5=90=AC=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E6=8B=89=E8=B5=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangyingli --- .../service/data_share/data_share_service_impl.cpp | 4 ++++ 1 file changed, 4 insertions(+) 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 35408ae9d..22d9f1810 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -671,6 +671,10 @@ void DataShareServiceImpl::PullService(PullExtensionEvent::EventInfo &eventInfo) AAFwk::WantParams wantParams; SearchConnectAdaptor adaptor; auto[errcode, reply] = adaptor.Connect(uri, meta, wantParams); + if (!errcode) { + ZLOGE("Pull failed, bundlename:%{public}s", meta.bundlename.c_str()); + return; + } BackSearchEvent::BackInfo backInfo; backInfo.meta = meta; backInfo.account = apply; -- Gitee From 3e4c79b914e6c9a724ba173d793c7eee141096cd Mon Sep 17 00:00:00 2001 From: wangyingli Date: Sun, 6 Jul 2025 18:00:34 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E7=9B=91=E5=90=AC=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E6=8B=89=E8=B5=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangyingli --- services/distributeddataservice/service/test/BUILD.gn | 2 ++ .../service/test/fuzztest/datashareserviceimpl_fuzzer/BUILD.gn | 2 ++ .../service/test/fuzztest/datashareservicestub_fuzzer/BUILD.gn | 2 ++ 3 files changed, 6 insertions(+) diff --git a/services/distributeddataservice/service/test/BUILD.gn b/services/distributeddataservice/service/test/BUILD.gn index 756f5fa4d..00744a02d 100644 --- a/services/distributeddataservice/service/test/BUILD.gn +++ b/services/distributeddataservice/service/test/BUILD.gn @@ -1031,6 +1031,8 @@ ohos_unittest("DataShareServiceImplTest") { "${data_service_path}/service/data_share/common/div_strategy.cpp", "${data_service_path}/service/data_share/common/extension_ability_manager.cpp", "${data_service_path}/service/data_share/common/extension_connect_adaptor.cpp", + "${data_service_path}/service/data_share/common/search_connect_adaptor.cpp", + "${data_service_path}/service/data_share/common/search_callback_impl.cpp", "${data_service_path}/service/data_share/common/extension_mgr_proxy.cpp", "${data_service_path}/service/data_share/common/proxy_data_manager.cpp", "${data_service_path}/service/data_share/common/kv_delegate.cpp", diff --git a/services/distributeddataservice/service/test/fuzztest/datashareserviceimpl_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/datashareserviceimpl_fuzzer/BUILD.gn index 17ca41e7e..331198a12 100644 --- a/services/distributeddataservice/service/test/fuzztest/datashareserviceimpl_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/datashareserviceimpl_fuzzer/BUILD.gn @@ -58,6 +58,8 @@ ohos_fuzztest("DataShareServiceImplFuzzTest") { "${data_service_path}/service/data_share/common/div_strategy.cpp", "${data_service_path}/service/data_share/common/extension_ability_manager.cpp", "${data_service_path}/service/data_share/common/extension_connect_adaptor.cpp", + "${data_service_path}/service/data_share/common/search_connect_adaptor.cpp", + "${data_service_path}/service/data_share/common/search_callback_impl.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/proxy_data_manager.cpp", diff --git a/services/distributeddataservice/service/test/fuzztest/datashareservicestub_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/datashareservicestub_fuzzer/BUILD.gn index 8f2dc9fbb..6387ddc04 100644 --- a/services/distributeddataservice/service/test/fuzztest/datashareservicestub_fuzzer/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/datashareservicestub_fuzzer/BUILD.gn @@ -56,6 +56,8 @@ ohos_fuzztest("DataShareServiceStubFuzzTest") { "${data_service_path}/service/data_share/common/div_strategy.cpp", "${data_service_path}/service/data_share/common/extension_ability_manager.cpp", "${data_service_path}/service/data_share/common/extension_connect_adaptor.cpp", + "${data_service_path}/service/data_share/common/search_connect_adaptor.cpp", + "${data_service_path}/service/data_share/common/search_callback_impl.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/proxy_data_manager.cpp", -- Gitee From bebbdb116a491b209ccd43c2383015533db5728c Mon Sep 17 00:00:00 2001 From: wangyingli Date: Sun, 6 Jul 2025 18:16:55 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E7=9B=91=E5=90=AC=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E6=8B=89=E8=B5=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangyingli --- .../service/data_share/common/search_callback_impl.cpp | 3 ++- .../service/data_share/common/search_callback_impl.h | 4 ++-- .../service/data_share/common/search_connect_adaptor.cpp | 6 ++++-- .../service/data_share/common/search_connect_adaptor.h | 2 +- .../service/test/data_share_common_test.cpp | 3 ++- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/services/distributeddataservice/service/data_share/common/search_callback_impl.cpp b/services/distributeddataservice/service/data_share/common/search_callback_impl.cpp index f82d61010..59cc81d7e 100644 --- a/services/distributeddataservice/service/data_share/common/search_callback_impl.cpp +++ b/services/distributeddataservice/service/data_share/common/search_callback_impl.cpp @@ -33,7 +33,8 @@ void SearchCallbackImpl::OnAbilityDisconnectDone(const AppExecFwk::ElementName & remoteObject_ = nullptr; } -sptr SearchCallbackImpl::GetRemoteObject() const { +sptr SearchCallbackImpl::GetRemoteObject() const +{ return remoteObject_; } } diff --git a/services/distributeddataservice/service/data_share/common/search_callback_impl.h b/services/distributeddataservice/service/data_share/common/search_callback_impl.h index 887fc3b23..00c4abb25 100644 --- a/services/distributeddataservice/service/data_share/common/search_callback_impl.h +++ b/services/distributeddataservice/service/data_share/common/search_callback_impl.h @@ -22,13 +22,13 @@ namespace OHOS::DataShare { class SearchCallbackImpl : public AAFwk::AbilityConnectionStub { public: - explicit SearchCallbackImpl() : remoteObject_(nullptr){} + explicit SearchCallbackImpl() : remoteObject_(nullptr) {} void OnAbilityConnectDone( const AppExecFwk::ElementName &element, const sptr &remoteObject, int resultCode) override; void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override; sptr GetRemoteObject() const; - std::mutex connectingMtx_; + std::mutex connectingMtx_; std::condition_variable connectCondition_; bool isConnectdone = true; private: diff --git a/services/distributeddataservice/service/data_share/common/search_connect_adaptor.cpp b/services/distributeddataservice/service/data_share/common/search_connect_adaptor.cpp index 65d28386a..e4d396a62 100644 --- a/services/distributeddataservice/service/data_share/common/search_connect_adaptor.cpp +++ b/services/distributeddataservice/service/data_share/common/search_connect_adaptor.cpp @@ -66,7 +66,7 @@ std::pair SearchConnectAdaptor::Connect(const std::string &ur callback_->isConnectdone = false; } else { std::unique_lock lock(connectMutex); - callback_->connectCV.wait_for(lock, std::chrono::seconds(WAITTIME)); + callback_->connectCondition_.wait_for(lock, std::chrono::seconds(WAITTIME)); std::unique_lock lck(callback_->connectingMtx_); callback_->isConnectdone = false; } @@ -80,7 +80,9 @@ std::pair SearchConnectAdaptor::Connect(const std::string &ur return connectCode; }, [&strategy]() {}); - + if (!errCode) { + ZLOGE("The wait errcode is:%{public}d, bundlename:%{public}s", errCode, meta.bundleName.c_str()); + } MessageParcel reply; MessageOption option; MessageParcel data; diff --git a/services/distributeddataservice/service/data_share/common/search_connect_adaptor.h b/services/distributeddataservice/service/data_share/common/search_connect_adaptor.h index cfda89c56..0192f1c41 100644 --- a/services/distributeddataservice/service/data_share/common/search_connect_adaptor.h +++ b/services/distributeddataservice/service/data_share/common/search_connect_adaptor.h @@ -25,7 +25,7 @@ namespace OHOS::DataShare { class SearchConnectAdaptor { public: using StoreMetaData = DistributedData::StoreMetaData; - static std::pair Connect(const std::string &uri, const StoreMetaData &meta, + std::pair Connect(const std::string &uri, const StoreMetaData &meta, AAFwk::WantParams &wantParams, int maxWaitTime = 1); SearchConnectAdaptor(); private: diff --git a/services/distributeddataservice/service/test/data_share_common_test.cpp b/services/distributeddataservice/service/test/data_share_common_test.cpp index 613b05b7c..cb43ea5d2 100644 --- a/services/distributeddataservice/service/test/data_share_common_test.cpp +++ b/services/distributeddataservice/service/test/data_share_common_test.cpp @@ -44,7 +44,8 @@ public: static constexpr int64_t USER_TEST = 100; static void SetUpTestCase(void){}; static void TearDownTestCase(void){}; - void SetUp() override { + void SetUp() override + { adaptor_ = std::make_shared(); callback_ = new MockCallback(); adaptor_->callback_ = callback_; -- Gitee From 5798205cf5498cfbb104fc1c79d2be56f1831614 Mon Sep 17 00:00:00 2001 From: wangyingli Date: Sun, 6 Jul 2025 18:55:25 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E7=9B=91=E5=90=AC=E4=BA=8B=E4=BB=B6?= =?UTF-8?q?=E6=8B=89=E8=B5=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangyingli --- .../service/data_share/data_share_service_impl.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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 22d9f1810..8fd19e298 100644 --- a/services/distributeddataservice/service/data_share/data_share_service_impl.cpp +++ b/services/distributeddataservice/service/data_share/data_share_service_impl.cpp @@ -655,12 +655,12 @@ void DataShareServiceImpl::SubscribeChange() }); } -void DataShareServiceImpl::SubScribeSearch() +void DataShareServiceImpl::SubscribeSearch() { EventCenter::GetInstance().Subscribe(PullExtensionEvent::EVT_SEARCH, [this](const Event &event) { auto &evt = static_cast(event); auto eventInfo = evt.GetInfo(); - PullExtension(eventInfo); + PullService(eventInfo); }); } @@ -672,12 +672,12 @@ void DataShareServiceImpl::PullService(PullExtensionEvent::EventInfo &eventInfo) SearchConnectAdaptor adaptor; auto[errcode, reply] = adaptor.Connect(uri, meta, wantParams); if (!errcode) { - ZLOGE("Pull failed, bundlename:%{public}s", meta.bundlename.c_str()); + ZLOGE("Pull failed, bundlename:%{public}s", meta.bundleName.c_str()); return; } BackSearchEvent::BackInfo backInfo; backInfo.meta = meta; - backInfo.account = apply; + backInfo.account = reply; auto evt = std::make_unique(std::move(backInfo), Event::EVT_BACKSEARCH); EventCenter::GetInstance().PostEvent(std::move(evt)); } -- Gitee