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