• 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 
16 #include "static_subscriber_data_manager.h"
17 
18 #include <unistd.h>
19 
20 #include "ces_inner_error_code.h"
21 #include "event_log_wrapper.h"
22 
23 namespace OHOS {
24 namespace EventFwk {
25 namespace {
26 constexpr int32_t CHECK_INTERVAL = 100000; // 100ms
27 constexpr int32_t MAX_TIMES = 5;           // 5 * 100ms = 500ms
28 constexpr const char *STATIC_SUBSCRIBER_STORAGE_DIR = "/data/service/el1/public/database/common_event_service";
29 const std::string STATIC_SUBSCRIBER_VALUE_DEFAULT = "0";
30 } // namespace
StaticSubscriberDataManager()31 StaticSubscriberDataManager::StaticSubscriberDataManager() {}
32 
~StaticSubscriberDataManager()33 StaticSubscriberDataManager::~StaticSubscriberDataManager()
34 {
35     if (kvStorePtr_ != nullptr) {
36         dataManager_.CloseKvStore(appId_, kvStorePtr_);
37     }
38 }
39 
GetKvStore()40 DistributedKv::Status StaticSubscriberDataManager::GetKvStore()
41 {
42     DistributedKv::Options options = {
43         .createIfMissing = true,
44         .encrypt = false,
45         .autoSync = false,
46         .syncable = false,
47         .securityLevel = DistributedKv::SecurityLevel::S1,
48         .area = DistributedKv::EL1,
49         .kvStoreType = DistributedKv::KvStoreType::SINGLE_VERSION,
50         .baseDir = STATIC_SUBSCRIBER_STORAGE_DIR
51     };
52 
53     DistributedKv::Status status = dataManager_.GetSingleKvStore(options, appId_, storeId_, kvStorePtr_);
54     if (status != DistributedKv::Status::SUCCESS) {
55         EVENT_LOGE("return error: %{public}d", status);
56     }
57     return status;
58 }
59 
CheckKvStore()60 bool StaticSubscriberDataManager::CheckKvStore()
61 {
62     if (kvStorePtr_ != nullptr) {
63         return true;
64     }
65     int32_t tryTimes = MAX_TIMES;
66     while (tryTimes > 0) {
67         DistributedKv::Status status = GetKvStore();
68         if (status == DistributedKv::Status::SUCCESS && kvStorePtr_ != nullptr) {
69             return true;
70         }
71         EVENT_LOGI("try times: %{public}d", tryTimes);
72         usleep(CHECK_INTERVAL);
73         tryTimes--;
74     }
75     return kvStorePtr_ != nullptr;
76 }
77 
InsertDisableStaticSubscribeData(const std::string & bundleName)78 int32_t StaticSubscriberDataManager::InsertDisableStaticSubscribeData(const std::string &bundleName)
79 {
80     if (bundleName.empty()) {
81         EVENT_LOGW("invalid value!");
82         return ERR_INVALID_VALUE;
83     }
84     DistributedKv::Status status;
85     EVENT_LOGD("bundleName = %{public}s", bundleName.c_str());
86     {
87         std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
88         if (!CheckKvStore()) {
89             EVENT_LOGE("kvStore is null");
90             return ERR_NO_INIT;
91         }
92         DistributedKv::Key key(bundleName);
93         DistributedKv::Value value(STATIC_SUBSCRIBER_VALUE_DEFAULT);
94         status = kvStorePtr_->Put(key, value);
95     }
96     int32_t result = ERR_OK;
97     if (status != DistributedKv::Status::SUCCESS) {
98         EVENT_LOGE("insert data [%{public}s] to kvStore failed. error code: %{public}d", bundleName.c_str(), status);
99         result = ERR_INVALID_OPERATION;
100     }
101 
102     dataManager_.CloseKvStore(appId_, kvStorePtr_);
103     kvStorePtr_ = nullptr;
104 
105     return result;
106 }
107 
DeleteDisableStaticSubscribeData(const std::string & bundleName)108 int32_t StaticSubscriberDataManager::DeleteDisableStaticSubscribeData(const std::string &bundleName)
109 {
110     if (bundleName.empty()) {
111         EVENT_LOGW("invalid value!");
112         return ERR_INVALID_VALUE;
113     }
114     DistributedKv::Status status;
115     EVENT_LOGD("bundleName: %{public}s", bundleName.c_str());
116     {
117         std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
118         if (!CheckKvStore()) {
119             EVENT_LOGE("kvStore is nullptr");
120             return ERR_NO_INIT;
121         }
122         DistributedKv::Key key(bundleName);
123         status = kvStorePtr_->Delete(key);
124     }
125     int32_t result = ERR_OK;
126     if (status != DistributedKv::Status::SUCCESS) {
127         EVENT_LOGE("delete data [%{public}s] from kvStore failed. error code: %{public}d", bundleName.c_str(), status);
128         result = ERR_INVALID_OPERATION;
129     }
130 
131     dataManager_.CloseKvStore(appId_, kvStorePtr_);
132     kvStorePtr_ = nullptr;
133 
134     return result;
135 }
136 
QueryDisableStaticSubscribeAllData(std::set<std::string> & disableStaticSubscribeAllData)137 int32_t StaticSubscriberDataManager::QueryDisableStaticSubscribeAllData(
138     std::set<std::string> &disableStaticSubscribeAllData)
139 {
140     {
141         std::lock_guard<std::mutex> lock(kvStorePtrMutex_);
142         if (!CheckKvStore()) {
143             EVENT_LOGE("kvStore is nullptr");
144             return ERR_NO_INIT;
145         }
146     }
147 
148     std::vector<DistributedKv::Entry> allEntries;
149     DistributedKv::Status status = kvStorePtr_->GetEntries(nullptr, allEntries);
150     int32_t result = ERR_OK;
151     if (status != DistributedKv::Status::SUCCESS) {
152         EVENT_LOGE("get entries failed, error code: %{public}d", status);
153         result = ERR_INVALID_OPERATION;
154     }
155 
156     if (!allEntries.empty()) {
157         for (const auto &item : allEntries) {
158             disableStaticSubscribeAllData.emplace(item.key.ToString());
159             EVENT_LOGI("current disable static subscriber bundle name: %{public}s", item.key.ToString().c_str());
160         }
161     }
162 
163     dataManager_.CloseKvStore(appId_, kvStorePtr_);
164     kvStorePtr_ = nullptr;
165 
166     return result;
167 }
168 } // namespace EventFwk
169 } // namespace OHOS
170