• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #include "client/storage_manager_client.h"
16 
17 #include "iremote_object.h"
18 #include "iremote_proxy.h"
19 #include "iservice_registry.h"
20 #include "storage_service_log.h"
21 #include "storage_service_errno.h"
22 #include "system_ability_definition.h"
23 #include "hitrace_meter.h"
24 
25 namespace OHOS {
26 namespace StorageManager {
GetStorageManagerProxy(void)27 sptr<IStorageManager> StorageManagerClient::GetStorageManagerProxy(void)
28 {
29     auto samgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
30     if (samgr == nullptr) {
31         LOGE("samgr empty error");
32         return nullptr;
33     }
34 
35     sptr<IRemoteObject> object = samgr->GetSystemAbility(OHOS::STORAGE_MANAGER_MANAGER_ID);
36     if (object == nullptr) {
37         LOGE("storage manager client samgr ablity empty error");
38         return nullptr;
39     }
40 
41     return iface_cast<IStorageManager>(object);
42 }
43 
PrepareAddUser(uint32_t userId,uint32_t flags)44 int32_t StorageManagerClient::PrepareAddUser(uint32_t userId, uint32_t flags)
45 {
46     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
47     sptr<IStorageManager> client = GetStorageManagerProxy();
48     if (client == nullptr) {
49         LOGE("get storage manager service failed");
50         return E_SA_IS_NULLPTR;
51     }
52 
53     return client->PrepareAddUser(userId, flags);
54 }
55 
RemoveUser(uint32_t userId,uint32_t flags)56 int32_t StorageManagerClient::RemoveUser(uint32_t userId, uint32_t flags)
57 {
58     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
59     sptr<IStorageManager> client = GetStorageManagerProxy();
60     if (client == nullptr) {
61         LOGE("get storage manager service failed");
62         return E_SA_IS_NULLPTR;
63     }
64 
65     return client->RemoveUser(userId, flags);
66 }
67 
GenerateUserKeys(uint32_t userId,uint32_t flags)68 int32_t StorageManagerClient::GenerateUserKeys(uint32_t userId, uint32_t flags)
69 {
70     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
71     sptr<IStorageManager> client = GetStorageManagerProxy();
72     if (client == nullptr) {
73         LOGE("get storage manager service failed");
74         return E_SA_IS_NULLPTR;
75     }
76 
77     return client->GenerateUserKeys(userId, flags);
78 }
79 
DeleteUserKeys(uint32_t userId)80 int32_t StorageManagerClient::DeleteUserKeys(uint32_t userId)
81 {
82     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
83     sptr<IStorageManager> client = GetStorageManagerProxy();
84     if (client == nullptr) {
85         LOGE("get storage manager service failed");
86         return E_SA_IS_NULLPTR;
87     }
88 
89     return client->DeleteUserKeys(userId);
90 }
91 
UpdateUserAuth(uint32_t userId,uint64_t secureUid,const std::vector<uint8_t> & token,const std::vector<uint8_t> & oldSecret,const std::vector<uint8_t> & newSecret)92 int32_t StorageManagerClient::UpdateUserAuth(uint32_t userId, uint64_t secureUid,
93                                              const std::vector<uint8_t> &token,
94                                              const std::vector<uint8_t> &oldSecret,
95                                              const std::vector<uint8_t> &newSecret)
96 {
97     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
98     sptr<IStorageManager> client = GetStorageManagerProxy();
99     if (client == nullptr) {
100         LOGE("get storage manager service failed");
101         return E_SA_IS_NULLPTR;
102     }
103 
104     return client->UpdateUserAuth(userId, secureUid, token, oldSecret, newSecret);
105 }
106 
UpdateUseAuthWithRecoveryKey(const std::vector<uint8_t> & authToken,const std::vector<uint8_t> & newSecret,uint64_t secureUid,uint32_t userId,const std::vector<std::vector<uint8_t>> & plainText)107 int32_t StorageManagerClient::UpdateUseAuthWithRecoveryKey(const std::vector<uint8_t> &authToken,
108                                                            const std::vector<uint8_t> &newSecret,
109                                                            uint64_t secureUid,
110                                                            uint32_t userId,
111                                                            const std::vector<std::vector<uint8_t>> &plainText)
112 {
113     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
114     sptr<IStorageManager> client = GetStorageManagerProxy();
115     if (client == nullptr) {
116         LOGE("get storage manager service failed");
117         return E_SA_IS_NULLPTR;
118     }
119 
120     return client->UpdateUseAuthWithRecoveryKey(authToken, newSecret, secureUid, userId, plainText);
121 }
122 
ActiveUserKey(uint32_t userId,const std::vector<uint8_t> & token,const std::vector<uint8_t> & secret)123 int32_t StorageManagerClient::ActiveUserKey(uint32_t userId,
124                                             const std::vector<uint8_t> &token,
125                                             const std::vector<uint8_t> &secret)
126 {
127     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
128     sptr<IStorageManager> client = GetStorageManagerProxy();
129     if (client == nullptr) {
130         LOGE("get storage manager service failed");
131         return E_SA_IS_NULLPTR;
132     }
133 
134     return client->ActiveUserKey(userId, token, secret);
135 }
136 
InactiveUserKey(uint32_t userId)137 int32_t StorageManagerClient::InactiveUserKey(uint32_t userId)
138 {
139     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
140     sptr<IStorageManager> client = GetStorageManagerProxy();
141     if (client == nullptr) {
142         LOGE("get storage manager service failed");
143         return E_SA_IS_NULLPTR;
144     }
145 
146     return client->InactiveUserKey(userId);
147 }
148 
UpdateKeyContext(uint32_t userId,bool needRemoveTmpKey)149 int32_t StorageManagerClient::UpdateKeyContext(uint32_t userId, bool needRemoveTmpKey)
150 {
151     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
152     sptr<IStorageManager> client = GetStorageManagerProxy();
153     if (client == nullptr) {
154         LOGE("get storage manager service failed");
155         return E_SA_IS_NULLPTR;
156     }
157 
158     return client->UpdateKeyContext(userId, needRemoveTmpKey);
159 }
160 
LockUserScreen(uint32_t userId)161 int32_t StorageManagerClient::LockUserScreen(uint32_t userId)
162 {
163     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
164     sptr<IStorageManager> client = GetStorageManagerProxy();
165     if (client == nullptr) {
166         LOGE("get storage manager service failed");
167         return E_SA_IS_NULLPTR;
168     }
169 
170     return client->LockUserScreen(userId);
171 }
172 
UnlockUserScreen(uint32_t userId,const std::vector<uint8_t> & token,const std::vector<uint8_t> & secret)173 int32_t StorageManagerClient::UnlockUserScreen(uint32_t userId,
174                                                const std::vector<uint8_t> &token,
175                                                const std::vector<uint8_t> &secret)
176 {
177     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
178     sptr<IStorageManager> client = GetStorageManagerProxy();
179     if (client == nullptr) {
180         LOGE("get storage manager service failed");
181         return E_SA_IS_NULLPTR;
182     }
183 
184     return client->UnlockUserScreen(userId, token, secret);
185 }
186 
GetFileEncryptStatus(uint32_t userId,bool & isEncrypted,bool needCheckDirMount)187 int32_t StorageManagerClient::GetFileEncryptStatus(uint32_t userId, bool &isEncrypted, bool needCheckDirMount)
188 {
189     sptr<IStorageManager> client = GetStorageManagerProxy();
190     if (client == nullptr) {
191         LOGE("get storage manager service failed");
192         return E_SA_IS_NULLPTR;
193     }
194 
195     return client->GetFileEncryptStatus(userId, isEncrypted, needCheckDirMount);
196 }
197 
GetUserNeedActiveStatus(uint32_t userId,bool & needActive)198 int32_t StorageManagerClient::GetUserNeedActiveStatus(uint32_t userId, bool &needActive)
199 {
200     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
201     sptr<IStorageManager> client = GetStorageManagerProxy();
202     if (client == nullptr) {
203         LOGE("get storage manager service failed");
204         return E_SA_IS_NULLPTR;
205     }
206 
207     return client->GetUserNeedActiveStatus(userId, needActive);
208 }
209 
GetLockScreenStatus(uint32_t userId,bool & lockScreenStatus)210 int32_t StorageManagerClient::GetLockScreenStatus(uint32_t userId, bool &lockScreenStatus)
211 {
212     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
213     sptr<IStorageManager> client = GetStorageManagerProxy();
214     if (client == nullptr) {
215         LOGE("get storage manager service failed");
216         return E_SA_IS_NULLPTR;
217     }
218 
219     return client->GetLockScreenStatus(userId, lockScreenStatus);
220 }
221 
MountDfsDocs(int32_t userId,const std::string & relativePath,const std::string & networkId,const std::string & deviceId)222 int32_t StorageManagerClient::MountDfsDocs(int32_t userId, const std::string &relativePath,
223     const std::string &networkId, const std::string &deviceId)
224 {
225     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
226     sptr<IStorageManager> client = GetStorageManagerProxy();
227     if (client == nullptr) {
228         LOGE("get storage manager service failed");
229         return E_SA_IS_NULLPTR;
230     }
231 
232     return client->MountDfsDocs(userId, relativePath, networkId, deviceId);
233 }
234 
UMountDfsDocs(int32_t userId,const std::string & relativePath,const std::string & networkId,const std::string & deviceId)235 int32_t StorageManagerClient::UMountDfsDocs(int32_t userId, const std::string &relativePath,
236     const std::string &networkId, const std::string &deviceId)
237 {
238     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
239     sptr<IStorageManager> client = GetStorageManagerProxy();
240     if (client == nullptr) {
241         LOGE("get storage manager service failed");
242         return E_SA_IS_NULLPTR;
243     }
244 
245     return client->UMountDfsDocs(userId, relativePath, networkId, deviceId);
246 }
247 
RegisterUeceActivationCallback(const sptr<StorageManager::IUeceActivationCallback> & ueceCallback)248 int32_t StorageManagerClient::RegisterUeceActivationCallback(
249     const sptr<StorageManager::IUeceActivationCallback> &ueceCallback)
250 {
251     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
252     sptr<IStorageManager> client = GetStorageManagerProxy();
253     if (client == nullptr) {
254         LOGE("get storage manager service failed");
255         return E_SA_IS_NULLPTR;
256     }
257 
258     return client->RegisterUeceActivationCallback(ueceCallback);
259 }
260 
UnregisterUeceActivationCallback()261 int32_t StorageManagerClient::UnregisterUeceActivationCallback()
262 {
263     HITRACE_METER_NAME(HITRACE_TAG_FILEMANAGEMENT, __PRETTY_FUNCTION__);
264     sptr<IStorageManager> client = GetStorageManagerProxy();
265     if (client == nullptr) {
266         LOGE("get storage manager service failed");
267         return E_SA_IS_NULLPTR;
268     }
269     return client->UnregisterUeceActivationCallback();
270 }
271 }
272 }
273