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,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::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, true);
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(
56         context->calledBundleName, context->calledStoreName, metaData, context->currentUserId, context->appIndex)) {
57         // connect extension and retry
58         AAFwk::WantParams wantParams;
59         ExtensionConnectAdaptor::TryAndWait(context->uri, context->calledBundleName, wantParams);
60         if (!QueryMetaData(
61             context->calledBundleName, context->calledStoreName, metaData, context->currentUserId, context->appIndex)) {
62             ZLOGE("QueryMetaData fail, %{public}s", DistributedData::Anonymous::Change(context->uri).c_str());
63             context->errCode = NativeRdb::E_DB_NOT_EXIST;
64             return false;
65         }
66     }
67     context->calledSourceDir = metaData.dataDir;
68     context->isEncryptDb = metaData.isEncrypt;
69     if (context->isEncryptDb) {
70         context->secretMetaKey = metaData.GetSecretKey();
71     }
72     return true;
73 }
74 
operator ()(std::shared_ptr<Context> context)75 bool LoadConfigSingleDataInfoStrategy::operator()(std::shared_ptr<Context> context)
76 {
77     DistributedData::StoreMetaData metaData;
78     if (!QueryMetaData(context->calledBundleName, context->calledStoreName, metaData, 0, context->appIndex)) {
79         // connect extension and retry
80         AAFwk::WantParams wantParams;
81         ExtensionConnectAdaptor::TryAndWait(context->uri, context->calledBundleName, wantParams);
82         if (!QueryMetaData(context->calledBundleName, context->calledStoreName, metaData, 0, context->appIndex)) {
83             ZLOGE("QueryMetaData fail, %{public}s", DistributedData::Anonymous::Change(context->uri).c_str());
84             context->errCode = NativeRdb::E_DB_NOT_EXIST;
85             return false;
86         }
87     }
88     context->calledSourceDir = metaData.dataDir;
89     return true;
90 }
91 } // namespace OHOS::DataShare