• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 #include "drm_helper.h"
17 #include "drm_log.h"
18 #include "parameters.h"
19 #include "iservice_registry.h"
20 #include "os_account_manager.h"
21 #include "uri.h"
22 
23 namespace OHOS {
24 namespace DrmStandard {
25 const int32_t MEDIA_KEY_SYSTEM_SERVICE_ID = 3012;
26 const std::string SECURE_TYPE = "secure";
27 const std::string KEY_WORD = "KEYWORD";
28 const std::string SETTING_USER_SECURE_URI_PROXY =
29     "datashare:///com.ohos.settingsdata/entry/settingsdata/USER_SETTINGSDATA_SECURE_";
30 const std::string SETTINGS_DATA_EXT_URI = "datashare:///com.ohos.settingsdata.DataAbility";
31 const std::string SETTING_USER_SECURE_URI_PROXY_SUFFIX = "?Proxy=true&key=";
32 
GetDeviceType()33 std::string DrmHelper::GetDeviceType()
34 {
35     std::string deviceType = OHOS::system::GetParameter("const.product.devicetype", "");
36     if (deviceType.empty()) {
37         deviceType = OHOS::system::GetParameter("const.build.characteristics", "");
38         DRM_INFO_LOG("DrmHostManager GetParameter characteristics is:%{public}s", deviceType.c_str());
39     }
40     return deviceType;
41 }
42 
GetSettingDataValue(const std::string & tableType,const std::string & key)43 std::string DrmHelper::GetSettingDataValue(const std::string &tableType, const std::string &key)
44 {
45     std::string value;
46     int32_t currentuserId = DrmHelper::GetCurrentUserId();
47     if (currentuserId < 0) {
48         DRM_ERR_LOG("DrmHostManager currentuserId is invalid");
49         return "";
50     }
51     auto dataShareHelper = DrmHelper::CreateDataShareHelperProxy(currentuserId, tableType);
52     if (dataShareHelper == nullptr) {
53         DRM_ERR_LOG("DrmHostManager dataShareHelper return nullptr");
54         return "";
55     }
56 
57     std::string SettingSystemUri = SETTING_USER_SECURE_URI_PROXY +
58         std::to_string(currentuserId) + SETTING_USER_SECURE_URI_PROXY_SUFFIX + key;
59     OHOS::Uri uri(SettingSystemUri);
60 
61     DataShare::DataSharePredicates predicates;
62     std::vector<std::string> columns;
63     predicates.EqualTo(KEY_WORD, key);
64     auto result = dataShareHelper->Query(uri, predicates, columns);
65     if (result == nullptr) {
66         DRM_WARNING_LOG("DrmHostManager query error, result is null");
67         dataShareHelper->Release();
68         return "";
69     }
70 
71     if (result->GoToFirstRow() != DataShare::E_OK) {
72         DRM_WARNING_LOG("DrmHostManager query error, go to first row error");
73         result->Close();
74         dataShareHelper->Release();
75         return "";
76     }
77 
78     int columnIndex{0};
79     result->GetColumnIndex("VALUE", columnIndex);
80     result->GetString(columnIndex, value);
81     result->Close();
82     dataShareHelper->Release();
83     DRM_INFO_LOG("DrmHostManager query success, value: %{public}s", value.c_str());
84     return value;
85 }
86 
CreateDataShareHelperProxy(int32_t userId,std::string tableType)87 std::shared_ptr<DataShare::DataShareHelper> DrmHelper::CreateDataShareHelperProxy(int32_t userId, std::string tableType)
88 {
89     auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
90     if (saManager == nullptr) {
91         DRM_ERR_LOG("DrmHostManager saManager return nullptr");
92         return nullptr;
93     }
94     auto remoteObj = saManager->GetSystemAbility(MEDIA_KEY_SYSTEM_SERVICE_ID);
95     if (remoteObj == nullptr) {
96         DRM_ERR_LOG("DrmHostManager saManager->GetSystemAbility return nullptr");
97         return nullptr;
98     }
99 
100     std::shared_ptr<DataShare::DataShareHelper> helper = nullptr;
101     std::string SettingSystemUri = "";
102 
103     if (userId > 0 && tableType == SECURE_TYPE) {
104         SettingSystemUri = SETTING_USER_SECURE_URI_PROXY + std::to_string(userId) + "?Proxy=true";
105         helper = DataShare::DataShareHelper::Creator(remoteObj, SettingSystemUri, SETTINGS_DATA_EXT_URI);
106     }
107 
108     if (helper == nullptr) {
109         DRM_WARNING_LOG("DrmHostManager helper is nullptr, uri=%{public}s", SettingSystemUri.c_str());
110         return nullptr;
111     }
112     return helper;
113 }
114 
GetCurrentUserId()115 int32_t DrmHelper::GetCurrentUserId()
116 {
117     std::vector<int32_t> ids;
118     int32_t currentuserId = -1;
119     ErrCode result;
120     int32_t retry = 5;
121     while (retry--) {
122         result = AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
123         if (result == ERR_OK && !ids.empty()) {
124             currentuserId = ids[0];
125             DRM_DEBUG_LOG("DrmHostManager current userId is :%{public}d", currentuserId);
126             break;
127         }
128         sleep(1);
129     }
130     if (result != ERR_OK || ids.empty()) {
131         DRM_WARNING_LOG("DrmHostManager current userId is empty");
132     }
133     return currentuserId;
134 }
135 } // namespace DrmStandard
136 } // namespace OHOS