• 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 "metadata/store_meta_data.h"
29 #include "utils.h"
30 #include "utils/anonymous.h"
31 #include "ipc_skeleton.h"
32 
33 namespace OHOS::DataShare {
QueryMetaData(const std::string & bundleName,const std::string & storeName,int32_t userId,int32_t appIndex)34 std::pair<bool, DistributedData::StoreMetaData> DataShareDbConfig::QueryMetaData(
35     const std::string &bundleName, const std::string &storeName, int32_t userId, int32_t appIndex)
36 {
37     TimeoutReport timeoutReport({bundleName, "", storeName, __FUNCTION__, 0});
38     DistributedData::StoreMetaMapping storeMetaMapping;
39     storeMetaMapping.deviceId = DistributedData::DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid;
40     storeMetaMapping.user = std::to_string(userId);
41     storeMetaMapping.bundleName = bundleName;
42     storeMetaMapping.storeId = storeName;
43     storeMetaMapping.instanceId = appIndex;
44     bool isCreated =
45         DistributedData::MetaDataManager::GetInstance().LoadMeta(storeMetaMapping.GetKey(), storeMetaMapping, true);
46 
47     auto callingPid = IPCSkeleton::GetCallingPid();
48     timeoutReport.Report(std::to_string(userId), callingPid, appIndex);
49     return std::make_pair(isCreated, storeMetaMapping);
50 }
51 
GetMetaData(const DbConfig & dbConfig)52 std::pair<int, DistributedData::StoreMetaData> DataShareDbConfig::GetMetaData(const DbConfig &dbConfig)
53 {
54     auto [success, metaData] = QueryMetaData(
55         dbConfig.bundleName, dbConfig.storeName, dbConfig.userId, dbConfig.appIndex);
56     if (!success) {
57         if (!dbConfig.hasExtension) {
58             return std::pair(NativeRdb::E_DB_NOT_EXIST, metaData);
59         }
60         AAFwk::WantParams wantParams;
61         ExtensionConnectAdaptor::TryAndWait(dbConfig.uri, dbConfig.bundleName, wantParams);
62         auto [succ, meta] = QueryMetaData(dbConfig.bundleName, dbConfig.storeName, dbConfig.userId, dbConfig.appIndex);
63         if (!succ) {
64             return std::pair(NativeRdb::E_DB_NOT_EXIST, meta);
65         }
66         metaData = std::move(meta);
67     }
68     return std::pair(E_OK, metaData);
69 }
70 
GetDbConfig(DbConfig & dbConfig)71 std::tuple<int, DistributedData::StoreMetaData, std::shared_ptr<DBDelegate>> DataShareDbConfig::GetDbConfig(
72     DbConfig &dbConfig)
73 {
74     auto [errCode, metaData] = GetMetaData(dbConfig);
75     if (errCode != E_OK) {
76         ZLOGE("DB not exist,bundleName:%{public}s,storeName:%{public}s,user:%{public}d,err:%{public}d,uri:%{public}s",
77             dbConfig.bundleName.c_str(), StringUtils::GeneralAnonymous(dbConfig.storeName).c_str(), dbConfig.userId,
78             errCode, URIUtils::Anonymous(dbConfig.uri).c_str());
79         RADAR_REPORT(__FUNCTION__, RadarReporter::SILENT_ACCESS, RadarReporter::PROXY_MATEDATA_EXISTS,
80             RadarReporter::FAILED, RadarReporter::ERROR_CODE, RadarReporter::META_DATA_NOT_EXISTS);
81         return std::make_tuple(errCode, metaData, nullptr);
82     }
83     auto dbDelegate = DBDelegate::Create(metaData, dbConfig.extUri, dbConfig.backup);
84     if (dbDelegate == nullptr) {
85         ZLOGE("Create delegate fail, bundleName:%{public}s, userId:%{public}d, uri:%{public}s",
86             dbConfig.bundleName.c_str(), dbConfig.userId, URIUtils::Anonymous(dbConfig.uri).c_str());
87         return std::make_tuple(E_ERROR, metaData, nullptr);
88     }
89     return std::make_tuple(E_OK, metaData, dbDelegate);
90 }
91 } // namespace OHOS::DataShare
92