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 "RdbSchemaConfig"
16
17 #include "rdb_schema_config.h"
18 #include <fstream>
19 #include <sstream>
20 #include "if_system_ability_manager.h"
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23 #include "metadata/meta_data_manager.h"
24 #include "iremote_broker.h"
25 #include "iremote_object.h"
26 #include "refbase.h"
27 #include "resource_manager.h"
28 #include "log_print.h"
29
30 namespace OHOS::DistributedRdb {
31 using namespace OHOS::Global::Resource;
32 using namespace OHOS::DistributedData;
33 using Serializable = DistributedData::Serializable;
34 constexpr const char *SCHEMA_PATH = "arkdata/schema/sync_schema.json";
35
GetDistributedSchema(const StoreMetaData & meta,Database & database)36 bool RdbSchemaConfig::GetDistributedSchema(const StoreMetaData &meta, Database &database)
37 {
38 OHOS::AppExecFwk::BundleInfo bundleInfo;
39 if (!InitBundleInfo(meta.bundleName, std::atoi(meta.user.c_str()), bundleInfo)) {
40 return false;
41 }
42 auto ret = GetSchemaFromHap(bundleInfo, meta.storeId, database);
43 if (ret) {
44 database.user = meta.user;
45 database.deviceId = meta.deviceId;
46 return true;
47 }
48 return false;
49 }
50
InitBundleInfo(const std::string & bundleName,int32_t userId,OHOS::AppExecFwk::BundleInfo & bundleInfo)51 bool RdbSchemaConfig::InitBundleInfo(
52 const std::string &bundleName, int32_t userId, OHOS::AppExecFwk::BundleInfo &bundleInfo)
53 {
54 OHOS::sptr<OHOS::ISystemAbilityManager> systemAbilityManager =
55 OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
56 if (systemAbilityManager == nullptr) {
57 ZLOGE("Fail to get ststem ability mgr, bundleName: %{public}s.", bundleName.c_str());
58 return false;
59 }
60
61 OHOS::sptr<OHOS::IRemoteObject> remoteObject =
62 systemAbilityManager->GetSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
63 if (remoteObject == nullptr) {
64 ZLOGE("Fail to get bundle manager proxy, bundleName: %{public}s.", bundleName.c_str());
65 return false;
66 }
67
68 auto bundleMgrProxy = iface_cast<OHOS::AppExecFwk::BundleMgrProxy>(remoteObject);
69 if (bundleMgrProxy == nullptr) {
70 ZLOGE("Fail to cast proxy, bundleName: %{public}s.", bundleName.c_str());
71 return false;
72 }
73 bool ret = bundleMgrProxy->GetBundleInfo(
74 bundleName, OHOS::AppExecFwk::BundleFlag::GET_BUNDLE_WITH_EXTENSION_INFO, bundleInfo, userId);
75 if (!ret || bundleInfo.moduleDirs.size() == 0) {
76 ZLOGE("Get bundle info failed, bundleName: %{public}s.", bundleName.c_str());
77 return false;
78 }
79 return true;
80 }
81
GetSchemaFromHap(const OHOS::AppExecFwk::BundleInfo & bundleInfo,const std::string & storeName,Database & database)82 bool RdbSchemaConfig::GetSchemaFromHap(
83 const OHOS::AppExecFwk::BundleInfo &bundleInfo, const std::string &storeName, Database &database)
84 {
85 for (auto &hapInfo : bundleInfo.hapModuleInfos) {
86 std::shared_ptr<ResourceManager> resMgr(CreateResourceManager());
87 if (resMgr == nullptr) {
88 ZLOGE("Create resourceManager failed");
89 return {};
90 }
91 resMgr->AddResource(hapInfo.hapPath.c_str());
92 size_t length = 0;
93 std::unique_ptr<uint8_t[]> fileContent;
94 int err = resMgr->GetRawFileFromHap(SCHEMA_PATH, length, fileContent);
95 if (err != 0) {
96 continue;
97 }
98 std::string jsonData(fileContent.get(), fileContent.get() + length);
99 DbSchema databases;
100 databases.Unmarshall(jsonData);
101 for (const auto &schema : databases.databases) {
102 if (schema.name == storeName) {
103 database = schema;
104 return true;
105 }
106 }
107 }
108 return false;
109 }
110 } // namespace OHOS::DistributedRdb