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 #ifndef RESOURCE_SCHEDULE_SERVICE_RESSCHED_COMMON_OOBE_DATASHARE_UTILS_H
17 #define RESOURCE_SCHEDULE_SERVICE_RESSCHED_COMMON_OOBE_DATASHARE_UTILS_H
18 
19 #include "datashare_helper.h"
20 #include "errors.h"
21 #include "ipc_skeleton.h"
22 #include "res_sched_log.h"
23 #include <map>
24 #include "mutex"
25 #include "ffrt.h"
26 
27 namespace OHOS {
28 namespace ResourceSchedule {
29 class DataShareUtils {
30 public:
31     ~DataShareUtils ();
32     static DataShareUtils& GetInstance();
33     template <typename T>
34     ErrCode GetValue(const std::string& key, T& value);
35     Uri AssembleUri(const std::string& key);
36     bool ReleaseDataShareHelper(std::shared_ptr<DataShare::DataShareHelper>& helper);
37     std::shared_ptr<DataShare::DataShareHelper> CreateDataShareHelper();
38     bool IsConnectDataShareSucc();
39     bool GetDataShareReadyFlag();
40     void SetDataShareReadyFlag(bool flag);
41 
42 private:
43     DataShareUtils ();
44     static constexpr int32_t PARAM_NUM_TEN = 10;
45     static sptr<IRemoteObject> remoteObj_;
46     static ffrt::mutex mutex_;
47     ErrCode GetStringValue(const std::string& key, std::string& value);
48     void InitSystemAbilityManager();
49     bool isDataShareReady_ = false;
50     bool isConnectDataShareSucc = false;
51 };
52 
53 template <typename T>
GetValue(const std::string & key,T & value)54 ErrCode DataShareUtils::GetValue(const std::string& key, T& value)
55 {
56     std::string result;
57     std::string callingIdentity = IPCSkeleton::ResetCallingIdentity();
58     int32_t ret = GetStringValue(key, result);
59     IPCSkeleton::SetCallingIdentity(callingIdentity);
60     if (ret != ERR_OK) {
61         RESSCHED_LOGW("resultSet->GetStringValue return not ok, ret=%{public}d", ret);
62         return ret;
63     }
64     using ValueType = std::remove_cv_t<std::remove_reference_t<T>>;
65     if constexpr (std::is_same_v<std::string, ValueType>) {
66         value = result;
67     } else if constexpr (std::is_same_v<int64_t, ValueType>) {
68         value = static_cast<int64_t>(strtoll(result.c_str(), nullptr, PARAM_NUM_TEN));
69     } else if constexpr (std::is_same_v<int32_t, ValueType>) {
70         value = static_cast<int32_t>(strtoll(result.c_str(), nullptr, PARAM_NUM_TEN));
71     } else {
72         RESSCHED_LOGE("GetValue: invalid operation!");
73         return ERR_INVALID_OPERATION;
74     }
75     return ERR_OK;
76 }
77 } // namespace ResourceSchedule
78 } // namespace OHOS
79 #endif // RESOURCE_SCHEDULE_SERVICE_RESSCHED_COMMON_OOBE_DATASHARE_UTILS_H
80