1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #define LOG_TAG "LoadConfigDataInfoStrategy"
16 #include "load_config_data_info_strategy.h"
17
18 #include "check_is_single_app_strategy.h"
19 #include "device_manager_adapter.h"
20 #include "extension_connect_adaptor.h"
21 #include "log_print.h"
22 #include "metadata/meta_data_manager.h"
23 #include "metadata/store_meta_data.h"
24 #include "rdb_errno.h"
25 #include "utils/anonymous.h"
26
27 namespace OHOS::DataShare {
LoadConfigDataInfoStrategy()28 LoadConfigDataInfoStrategy::LoadConfigDataInfoStrategy()
29 : DivStrategy(std::make_shared<CheckIsSingleAppStrategy>(), std::make_shared<LoadConfigSingleDataInfoStrategy>(),
30 std::make_shared<LoadConfigNormalDataInfoStrategy>())
31 {
32 }
QueryMetaData(const std::string & bundleName,const std::string & storeName,DistributedData::StoreMetaData & metaData,const int32_t userId)33 static bool QueryMetaData(const std::string &bundleName, const std::string &storeName,
34 DistributedData::StoreMetaData &metaData, const int32_t userId)
35 {
36 DistributedData::StoreMetaData meta;
37 meta.deviceId = DistributedData::DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid;
38 meta.user = std::to_string(userId);
39 meta.bundleName = bundleName;
40 meta.storeId = storeName;
41 bool isCreated = DistributedData::MetaDataManager::GetInstance().LoadMeta(meta.GetKey(), metaData);
42 if (!isCreated) {
43 ZLOGE("DB not exist");
44 return false;
45 }
46 return true;
47 }
48
operator ()(std::shared_ptr<Context> context)49 bool LoadConfigNormalDataInfoStrategy::operator()(std::shared_ptr<Context> context)
50 {
51 if (context->type != "rdb") {
52 return true;
53 }
54 DistributedData::StoreMetaData metaData;
55 if (!QueryMetaData(context->calledBundleName, context->calledStoreName, metaData, context->currentUserId)) {
56 // connect extension and retry
57 ExtensionConnectAdaptor::TryAndWait(context);
58 if (!QueryMetaData(context->calledBundleName, context->calledStoreName, metaData, context->currentUserId)) {
59 ZLOGE("QueryMetaData fail, %{public}s", DistributedData::Anonymous::Change(context->uri).c_str());
60 context->errCode = NativeRdb::E_DB_NOT_EXIST;
61 return false;
62 }
63 }
64 context->calledSourceDir = metaData.dataDir;
65 return true;
66 }
67
operator ()(std::shared_ptr<Context> context)68 bool LoadConfigSingleDataInfoStrategy::operator()(std::shared_ptr<Context> context)
69 {
70 DistributedData::StoreMetaData metaData;
71 if (!QueryMetaData(context->calledBundleName, context->calledStoreName, metaData, 0)) {
72 // connect extension and retry
73 ExtensionConnectAdaptor::TryAndWait(context);
74 if (!QueryMetaData(context->calledBundleName, context->calledStoreName, metaData, 0)) {
75 ZLOGE("QueryMetaData fail, %{public}s", DistributedData::Anonymous::Change(context->uri).c_str());
76 context->errCode = NativeRdb::E_DB_NOT_EXIST;
77 return false;
78 }
79 }
80 context->calledSourceDir = metaData.dataDir;
81 return true;
82 }
83 } // namespace OHOS::DataShare