• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2024 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 "DataShareDbConfig"
16 
17 #include "data_share_db_config.h"
18 
19 #include <tuple>
20 #include <utility>
21 
22 #include "datashare_errno.h"
23 #include "datashare_radar_reporter.h"
24 #include "device_manager_adapter.h"
25 #include "extension_connect_adaptor.h"
26 #include "log_print.h"
27 #include "metadata/meta_data_manager.h"
28 #include "uri_utils.h"
29 #include "utils/anonymous.h"
30 
31 namespace OHOS::DataShare {
QueryMetaData(const std::string & bundleName,const std::string & storeName,int32_t userId)32 std::pair<bool, DistributedData::StoreMetaData> DataShareDbConfig::QueryMetaData(
33     const std::string &bundleName, const std::string &storeName, int32_t userId)
34 {
35     DistributedData::StoreMetaData meta;
36     meta.deviceId = DistributedData::DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid;
37     meta.user = std::to_string(userId);
38     meta.bundleName = bundleName;
39     meta.storeId = storeName;
40     DistributedData::StoreMetaData metaData;
41     bool isCreated = DistributedData::MetaDataManager::GetInstance().LoadMeta(meta.GetKey(), metaData, true);
42     return std::make_pair(isCreated, metaData);
43 }
44 
GetMetaData(const std::string & uri,const std::string & bundleName,const std::string & storeName,int32_t userId,bool hasExtension)45 std::pair<int, DistributedData::StoreMetaData> DataShareDbConfig::GetMetaData(const std::string &uri,
46     const std::string &bundleName, const std::string &storeName, int32_t userId, bool hasExtension)
47 {
48     auto [success, metaData] = QueryMetaData(bundleName, storeName, userId);
49     if (!success) {
50         if (!hasExtension) {
51             return std::pair(NativeRdb::E_DB_NOT_EXIST, metaData);
52         }
53         AAFwk::WantParams wantParams;
54         ExtensionConnectAdaptor::TryAndWait(uri, bundleName, wantParams);
55         auto [succ, meta] = QueryMetaData(bundleName, storeName, userId);
56         if (!succ) {
57             return std::pair(NativeRdb::E_DB_NOT_EXIST, meta);
58         }
59         metaData = std::move(meta);
60     }
61     return std::pair(E_OK, metaData);
62 }
63 
GetDbConfig(DbConfig & dbConfig)64 std::tuple<int, DistributedData::StoreMetaData, std::shared_ptr<DBDelegate>> DataShareDbConfig::GetDbConfig(
65     DbConfig &dbConfig)
66 {
67     auto [errCode, metaData] = GetMetaData(dbConfig.uri, dbConfig.bundleName, dbConfig.storeName,
68         dbConfig.userId, dbConfig.hasExtension);
69     if (errCode != E_OK) {
70         ZLOGE("DB not exist,bundleName:%{public}s,storeName:%{public}s,user:%{public}d,err:%{public}d,uri:%{public}s",
71             dbConfig.bundleName.c_str(), dbConfig.storeName.c_str(), dbConfig.userId, errCode,
72             URIUtils::Anonymous(dbConfig.uri).c_str());
73         RADAR_REPORT(__FUNCTION__, RadarReporter::SILENT_ACCESS, RadarReporter::PROXY_MATEDATA_EXISTS,
74             RadarReporter::FAILED, RadarReporter::ERROR_CODE, RadarReporter::META_DATA_NOT_EXISTS);
75         return std::make_tuple(errCode, metaData, nullptr);
76     }
77     auto dbDelegate = DBDelegate::Create(metaData, dbConfig.extUri, dbConfig.backup);
78     if (dbDelegate == nullptr) {
79         ZLOGE("Create delegate fail, bundleName:%{public}s, userId:%{public}d, uri:%{public}s",
80             dbConfig.bundleName.c_str(), dbConfig.userId, URIUtils::Anonymous(dbConfig.uri).c_str());
81         return std::make_tuple(E_ERROR, metaData, nullptr);
82     }
83     return std::make_tuple(E_OK, metaData, dbDelegate);
84 }
85 } // namespace OHOS::DataShare
86