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
16 #include "app_config_data_manager.h"
17
18 #include <unistd.h>
19
20 #include "errors.h"
21 #include "hilog_tag_wrapper.h"
22
23 namespace OHOS {
24 namespace AbilityRuntime {
25 namespace {
26 constexpr int32_t CHECK_INTERVAL = 100000; // 100ms
27 constexpr int32_t MAX_TIMES = 5;
28 constexpr const char *APP_CONFIG_STORAGE_DIR = "/data/service/el1/public/database/app_config_data";
29 const std::string KEY_WAITING_DEBUG_INFO = "WaitingDebugInfo";
30 } // namespace
31 const DistributedKv::AppId AppConfigDataManager::APP_ID = { "app_config_data_storage" };
32 const DistributedKv::StoreId AppConfigDataManager::STORE_ID = { "app_config_data_infos" };
AppConfigDataManager()33 AppConfigDataManager::AppConfigDataManager() {}
34
~AppConfigDataManager()35 AppConfigDataManager::~AppConfigDataManager()
36 {
37 if (kvStorePtr_ != nullptr) {
38 dataManager_.CloseKvStore(APP_ID, kvStorePtr_);
39 }
40 }
41
GetKvStore()42 DistributedKv::Status AppConfigDataManager::GetKvStore()
43 {
44 DistributedKv::Options options = {
45 .createIfMissing = true,
46 .encrypt = false,
47 .autoSync = false,
48 .syncable = false,
49 .securityLevel = DistributedKv::SecurityLevel::S2,
50 .area = DistributedKv::EL1,
51 .kvStoreType = DistributedKv::KvStoreType::SINGLE_VERSION,
52 .baseDir = APP_CONFIG_STORAGE_DIR
53 };
54
55 DistributedKv::Status status = dataManager_.GetSingleKvStore(options, APP_ID, STORE_ID, kvStorePtr_);
56 if (status != DistributedKv::Status::SUCCESS) {
57 TAG_LOGE(AAFwkTag::APPMGR, "error is %{public}d", status);
58 return status;
59 }
60
61 return status;
62 }
63
CheckKvStore()64 bool AppConfigDataManager::CheckKvStore()
65 {
66 if (kvStorePtr_ != nullptr) {
67 return true;
68 }
69
70 int32_t tryTimes = MAX_TIMES;
71 while (tryTimes > 0) {
72 DistributedKv::Status status = GetKvStore();
73 if (status == DistributedKv::Status::SUCCESS && kvStorePtr_ != nullptr) {
74 return true;
75 }
76 usleep(CHECK_INTERVAL);
77 tryTimes--;
78 }
79 return kvStorePtr_ != nullptr;
80 }
81
SetAppWaitingDebugInfo(const std::string & bundleName)82 int32_t AppConfigDataManager::SetAppWaitingDebugInfo(const std::string &bundleName)
83 {
84 TAG_LOGD(AAFwkTag::APPMGR, "Called, bundle name is %{public}s.", bundleName.c_str());
85 if (bundleName.empty()) {
86 TAG_LOGE(AAFwkTag::APPMGR, "invalid value");
87 return ERR_INVALID_VALUE;
88 }
89
90 {
91 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
92 if (!CheckKvStore()) {
93 TAG_LOGE(AAFwkTag::APPMGR, "null kvStore");
94 return ERR_NO_INIT;
95 }
96 }
97
98 DistributedKv::Key key(KEY_WAITING_DEBUG_INFO);
99 DistributedKv::Value value(bundleName);
100 DistributedKv::Status status;
101 {
102 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
103 if (kvStorePtr_ == nullptr) {
104 TAG_LOGE(AAFwkTag::APPMGR, "kvStorePtr_ null");
105 return ERR_INVALID_OPERATION;
106 }
107 status = kvStorePtr_->Put(key, value);
108 }
109
110 if (status != DistributedKv::Status::SUCCESS) {
111 TAG_LOGE(AAFwkTag::APPMGR, "error is %{public}d", status);
112 return ERR_INVALID_OPERATION;
113 }
114 return ERR_OK;
115 }
116
ClearAppWaitingDebugInfo()117 int32_t AppConfigDataManager::ClearAppWaitingDebugInfo()
118 {
119 TAG_LOGD(AAFwkTag::APPMGR, "called");
120 {
121 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
122 if (!CheckKvStore()) {
123 TAG_LOGE(AAFwkTag::APPMGR, "null kvStore");
124 return ERR_NO_INIT;
125 }
126 }
127
128 DistributedKv::Key key(KEY_WAITING_DEBUG_INFO);
129 DistributedKv::Status status;
130 {
131 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
132 if (kvStorePtr_ == nullptr) {
133 TAG_LOGE(AAFwkTag::APPMGR, "kvStorePtr_ null");
134 return ERR_INVALID_OPERATION;
135 }
136 status = kvStorePtr_->Delete(key);
137 }
138
139 if (status != DistributedKv::Status::SUCCESS) {
140 TAG_LOGE(AAFwkTag::APPMGR, "error is %{public}d", status);
141 return ERR_INVALID_OPERATION;
142 }
143 return ERR_OK;
144 }
145
GetAppWaitingDebugList(std::vector<std::string> & bundleNameList)146 int32_t AppConfigDataManager::GetAppWaitingDebugList(std::vector<std::string> &bundleNameList)
147 {
148 TAG_LOGD(AAFwkTag::APPMGR, "called");
149 {
150 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
151 if (!CheckKvStore()) {
152 TAG_LOGE(AAFwkTag::APPMGR, "null kvStore");
153 return ERR_NO_INIT;
154 }
155 }
156
157 DistributedKv::Status status;
158 std::vector<DistributedKv::Entry> allEntries;
159 {
160 std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
161 if (kvStorePtr_ == nullptr) {
162 TAG_LOGE(AAFwkTag::APPMGR, "kvStorePtr_ null");
163 return ERR_INVALID_OPERATION;
164 }
165 status = kvStorePtr_->GetEntries(nullptr, allEntries);
166 }
167
168 if (status != DistributedKv::Status::SUCCESS) {
169 TAG_LOGE(AAFwkTag::APPMGR, "error is %{public}d", status);
170 return ERR_INVALID_OPERATION;
171 }
172
173 for (const auto &item : allEntries) {
174 if (item.key.ToString() == KEY_WAITING_DEBUG_INFO) {
175 bundleNameList.emplace_back(item.value.ToString());
176 }
177 }
178 TAG_LOGD(AAFwkTag::APPMGR, "The bundle name list size is %{public}zu.", bundleNameList.size());
179 return ERR_OK;
180 }
181 } // namespace AbilityRuntime
182 } // namespace OHOS
183