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.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,const int32_t appIndex)33 static bool QueryMetaData(const std::string &bundleName, const std::string &storeName,
34 DistributedData::StoreMetaData &metaData, const int32_t userId, const int32_t appIndex)
35 {
36 DistributedData::StoreMetaMapping storeMetaMapping;
37 storeMetaMapping.deviceId = DistributedData::DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid;
38 storeMetaMapping.user = std::to_string(userId);
39 storeMetaMapping.bundleName = bundleName;
40 storeMetaMapping.storeId = storeName;
41 bool isCreated = DistributedData::MetaDataManager::GetInstance().LoadMeta(
42 storeMetaMapping.GetKey(), storeMetaMapping, true);
43 metaData = storeMetaMapping;
44 if (!isCreated) {
45 ZLOGE("DB not exist");
46 return false;
47 }
48 return true;
49 }
50
operator ()(std::shared_ptr<Context> context)51 bool LoadConfigNormalDataInfoStrategy::operator()(std::shared_ptr<Context> context)
52 {
53 if (context->type != "rdb") {
54 return true;
55 }
56 DistributedData::StoreMetaData metaData;
57 if (!QueryMetaData(
58 context->calledBundleName, context->calledStoreName, metaData, context->visitedUserId, context->appIndex)) {
59 // connect extension and retry
60 AAFwk::WantParams wantParams;
61 ExtensionConnectAdaptor::TryAndWait(context->uri, context->calledBundleName, wantParams);
62 if (!QueryMetaData(
63 context->calledBundleName, context->calledStoreName, metaData, context->visitedUserId, context->appIndex)) {
64 ZLOGE("QueryMetaData fail, %{public}s", URIUtils::Anonymous(context->uri).c_str());
65 context->errCode = NativeRdb::E_DB_NOT_EXIST;
66 return false;
67 }
68 }
69 context->calledSourceDir = metaData.dataDir;
70 context->isEncryptDb = metaData.isEncrypt;
71 context->calledTokenId = metaData.tokenId;
72 context->calledStoreName = metaData.storeId;
73 context->haMode = metaData.haMode;
74 if (context->isEncryptDb) {
75 context->secretMetaKey = metaData.GetSecretKey();
76 }
77 return true;
78 }
79
operator ()(std::shared_ptr<Context> context)80 bool LoadConfigSingleDataInfoStrategy::operator()(std::shared_ptr<Context> context)
81 {
82 DistributedData::StoreMetaData metaData;
83 if (!QueryMetaData(context->calledBundleName, context->calledStoreName, metaData, 0, context->appIndex)) {
84 // connect extension and retry
85 AAFwk::WantParams wantParams;
86 ExtensionConnectAdaptor::TryAndWait(context->uri, context->calledBundleName, wantParams);
87 if (!QueryMetaData(context->calledBundleName, context->calledStoreName, metaData, 0, context->appIndex)) {
88 ZLOGE("QueryMetaData fail, %{public}s", URIUtils::Anonymous(context->uri).c_str());
89 context->errCode = NativeRdb::E_DB_NOT_EXIST;
90 return false;
91 }
92 }
93 context->calledSourceDir = metaData.dataDir;
94 context->calledTokenId = metaData.tokenId;
95 context->calledStoreName = metaData.storeId;
96 context->haMode = metaData.haMode;
97 return true;
98 }
99 } // namespace OHOS::DataShare