1 /*
2 * Copyright (c) 2022 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 "device_security_level_loader.h"
17
18 #include <future>
19 #include <type_traits>
20
21 #include "hilog/log_cpp.h"
22 #include "if_system_ability_manager.h"
23 #include "isystem_ability_load_callback.h"
24 #include "iservice_registry.h"
25
26 #include "device_security_level_defines.h"
27 #include "idevice_security_level.h"
28
29 namespace OHOS {
30 namespace Security {
31 namespace DeviceSecurityLevel {
32 using namespace OHOS::HiviewDFX;
33
LoadDslmService()34 sptr<IRemoteObject> DeviceSecurityLevelLoader::LoadDslmService()
35 {
36 auto registry = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
37 if (registry == nullptr) {
38 HILOG_ERROR(LOG_CORE, "GetDeviceSecurityInfo get registry error.");
39 return {};
40 }
41 auto object = registry->CheckSystemAbility(DEVICE_SECURITY_LEVEL_MANAGER_SA_ID);
42 if (object != nullptr) {
43 return object;
44 }
45
46 sptr<LoadCallback> callback = new (std::nothrow) LoadCallback();
47 if (callback == nullptr) {
48 HILOG_ERROR(LOG_CORE, "LoadCallback new fail.");
49 return {};
50 }
51 int32_t result = registry->LoadSystemAbility(DEVICE_SECURITY_LEVEL_MANAGER_SA_ID, callback);
52 if (result != ERR_OK) {
53 HILOG_ERROR(LOG_CORE, "GetDeviceSecurityInfo LoadSystemAbility error.");
54 return {};
55 }
56 return callback->Promise();
57 }
58
OnLoadSystemAbilitySuccess(int32_t sid,const sptr<IRemoteObject> & object)59 void DeviceSecurityLevelLoader::LoadCallback::OnLoadSystemAbilitySuccess(int32_t sid, const sptr<IRemoteObject> &object)
60 {
61 HILOG_INFO(LOG_CORE, "OnLoadSystemAbilitySuccess = %{public}d.", sid);
62 promise_.set_value(object);
63 }
64
OnLoadSystemAbilityFail(int32_t sid)65 void DeviceSecurityLevelLoader::LoadCallback::OnLoadSystemAbilityFail(int32_t sid)
66 {
67 HILOG_ERROR(LOG_CORE, "OnLoadSystemAbilityFail = %{public}d.", sid);
68 promise_.set_value(nullptr);
69 }
70
Promise()71 sptr<IRemoteObject> DeviceSecurityLevelLoader::LoadCallback::Promise()
72 {
73 return promise_.get_future().get();
74 }
75 } // namespace DeviceSecurityLevel
76 } // namespace Security
77 } // namespace OHOS
78