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