• 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 "data_share_helper_manager.h"
17 #include "calendar_log.h"
18 #include "calendar_env.h"
19 #include "napi_env.h"
20 #include "accesstoken_kit.h"
21 #include "ipc_skeleton.h"
22 #include <thread>
23 #include <chrono>
24 
25 namespace {
26 const std::string CALENDAR_DATA_URI = "datashare:///calendardata";
27 const std::string CALENDAR_DATA_WHOLE_URI = "datashare:///calendardata_whole";
28 const std::string PERMISSION_NAME = "ohos.permission.READ_WHOLE_CALENDAR";
29 const int DESTROY_DATASHARE_DELAY = 2 * 60 * 1000;
30 const int CHECK_INTERVAL_DIVIDER = 4;
31 const uint32_t MAX_RETRY_ATTEMPTS = 3;
32 }  // namespace
33 
34 using namespace OHOS::DataShare;
35 using namespace std::chrono;
36 
37 namespace OHOS::CalendarApi {
SetDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> helper)38 void DataShareHelperManager::SetDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> helper)
39 {
40     m_dataShareHelper = helper;
41 }
42 
CreateDataShareHelper()43 std::shared_ptr<DataShareHelper> DataShareHelperManager::CreateDataShareHelper()
44 {
45     std::lock_guard<std::recursive_mutex> lock(dataShareLock);
46     DataShareHelperManager::SetDataShareHelperTimer(DESTROY_DATASHARE_DELAY);
47     if (m_dataShareHelper) {
48         return m_dataShareHelper;
49     }
50     uint32_t retryCount = 0;
51     do {
52         int32_t ret =
53             Security::AccessToken::AccessTokenKit::VerifyAccessToken(IPCSkeleton::GetCallingTokenID(), PERMISSION_NAME);
54         LOG_INFO("CreateDataShareHelper verify access result=%{public}d", ret);
55         if (ret == Security::AccessToken::PERMISSION_GRANTED) {
56             if (!CalendarEnvNapi::GetInstance().getContext()) {
57                 LOG_INFO("CalendarEnvNapi::GetInstance().getContext() is null");
58                 break;
59             }
60             m_dataShareHelper = DataShareHelper::Creator(
61                 CalendarEnvNapi::GetInstance().getContext()->GetToken(), CALENDAR_DATA_WHOLE_URI);
62             LOG_INFO("CreateDataShareHelper dataShareHelper create with whole authority result=%{public}d",
63                 m_dataShareHelper != nullptr);
64         } else {
65             if (!CalendarEnvNapi::GetInstance().getContext()) {
66                 LOG_INFO("CalendarEnvNapi::GetInstance().getContext() is null");
67                 break;
68             }
69             m_dataShareHelper =
70                 DataShareHelper::Creator(CalendarEnvNapi::GetInstance().getContext()->GetToken(), CALENDAR_DATA_URI);
71             LOG_INFO("CreateDataShareHelper dataShareHelper create with low authority result=%{public}d",
72                 m_dataShareHelper != nullptr);
73         }
74         if (m_dataShareHelper) {
75             break;
76         }
77         LOG_WARN("CreateDataShareHelper failed %{public}d times retired", retryCount);
78         retryCount = retryCount + 1;
79     } while (retryCount < MAX_RETRY_ATTEMPTS);
80     if (!m_dataShareHelper) {
81         LOG_ERROR("create dataShareHelper failed");
82         return nullptr;
83     }
84     return m_dataShareHelper;
85 }
86 
DestroyDataShareHelper()87 bool DataShareHelperManager::DestroyDataShareHelper()
88 {
89     std::lock_guard<std::recursive_mutex> lock(dataShareLock);
90     int64_t now = duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
91     if (useCount.load() > 0) {
92         LOG_INFO("DestroyDataShareHelper dataShareHelper in use count: %{public}d", useCount.load());
93         return false;
94     }
95     if (now >= expire) {
96         if (m_dataShareHelper) {
97             LOG_INFO("DestroyDataShareHelper dataShareHelper releasing");
98             m_dataShareHelper->Release();
99             m_dataShareHelper = nullptr;
100         } else {
101             LOG_INFO("DestroyDataShareHelper dataShareHelper is null");
102         }
103         return true;
104     }
105     LOG_INFO("DestroyDataShareHelper dataShareHelper not expired %{public}lld remaining", expire - now);
106     return false;
107 }
108 
SetDataShareHelperTimer(int milliseconds)109 void DataShareHelperManager::SetDataShareHelperTimer(int milliseconds)
110 {
111     int64_t curtime = duration_cast<std::chrono::milliseconds>(system_clock::now().time_since_epoch()).count();
112     expire.store(curtime + milliseconds, std::memory_order_seq_cst);
113     if (m_dataShareHelper) {
114         return;
115     }
116     LOG_INFO("SetDataShareHelperTimer expireTime=%{public}lld", expire.load());
117     std::thread th([&]() {
118         do {
119             std::this_thread::sleep_for(std::chrono::milliseconds(DESTROY_DATASHARE_DELAY / CHECK_INTERVAL_DIVIDER));
120         } while (!DestroyDataShareHelper());
121     });
122     th.detach();
123 }
124 
GetDataShareHelper()125 std::shared_ptr<DataShare::DataShareHelper> DataShareHelperManager::GetDataShareHelper()
126 {
127     return CreateDataShareHelper();
128 }
129 
Insert(const Uri & uri,const DataShareValuesBucket & value)130 int DataShareHelperManager::Insert(const Uri &uri, const DataShareValuesBucket &value)
131 {
132     auto dataShareHelper = CreateDataShareHelper();
133     if (!dataShareHelper) {
134         LOG_ERROR("Insert dataShareHelper is nullptr");
135         return -1;
136     }
137     useCount.fetch_add(1, std::memory_order_seq_cst);
138     auto res = dataShareHelper->Insert(const_cast<Uri &>(uri), value);
139     useCount.fetch_sub(1, std::memory_order_seq_cst);
140     return res;
141 }
142 
BatchInsert(const Uri & uri,const std::vector<DataShare::DataShareValuesBucket> & values)143 int DataShareHelperManager::BatchInsert(const Uri &uri, const std::vector<DataShare::DataShareValuesBucket> &values)
144 {
145     auto dataShareHelper = CreateDataShareHelper();
146     if (!dataShareHelper) {
147         LOG_ERROR("BatchInsert dataShareHelper is nullptr");
148         return -1;
149     }
150     useCount.fetch_add(1, std::memory_order_seq_cst);
151     auto res = dataShareHelper->BatchInsert(const_cast<Uri &>(uri), values);
152     useCount.fetch_sub(1, std::memory_order_seq_cst);
153     return res;
154 }
155 
Update(const Uri & uri,const DataSharePredicates & predicates,const DataShareValuesBucket & value)156 int DataShareHelperManager::Update(
157     const Uri &uri, const DataSharePredicates &predicates, const DataShareValuesBucket &value)
158 {
159     auto dataShareHelper = CreateDataShareHelper();
160     if (!dataShareHelper) {
161         LOG_ERROR("Update dataShareHelper is nullptr");
162         return -1;
163     }
164     useCount.fetch_add(1, std::memory_order_seq_cst);
165     auto res = dataShareHelper->Update(const_cast<Uri &>(uri), predicates, value);
166     useCount.fetch_sub(1, std::memory_order_seq_cst);
167     return res;
168 }
169 
Delete(const Uri & uri,const DataSharePredicates & predicates)170 int DataShareHelperManager::Delete(const Uri &uri, const DataSharePredicates &predicates)
171 {
172     auto dataShareHelper = CreateDataShareHelper();
173     if (!dataShareHelper) {
174         LOG_ERROR("Delete dataShareHelper is nullptr");
175         return -1;
176     }
177     useCount.fetch_add(1, std::memory_order_seq_cst);
178     auto res = dataShareHelper->Delete(const_cast<Uri &>(uri), predicates);
179     useCount.fetch_sub(1, std::memory_order_seq_cst);
180     return res;
181 }
182 
Query(const Uri & uri,const DataSharePredicates & predicates,std::vector<std::string> & columns,DatashareBusinessError * businessError)183 std::shared_ptr<DataShareResultSet> DataShareHelperManager::Query(const Uri &uri, const DataSharePredicates &predicates,
184     std::vector<std::string> &columns, DatashareBusinessError *businessError)
185 {
186     auto dataShareHelper = CreateDataShareHelper();
187     if (!dataShareHelper) {
188         LOG_ERROR("Query dataShareHelper is nullptr");
189         return nullptr;
190     }
191     useCount.fetch_add(1, std::memory_order_seq_cst);
192     auto res = dataShareHelper->Query(const_cast<Uri &>(uri), predicates, columns, businessError);
193     useCount.fetch_sub(1, std::memory_order_seq_cst);
194     return res;
195 }
196 }  // namespace OHOS::CalendarApi