• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "collector_service_loader.h"
17 
18 #include "if_system_ability_manager.h"
19 #include "isystem_ability_load_callback.h"
20 #include "iservice_registry.h"
21 
22 #include "i_security_collector_manager.h"
23 #include "security_collector_log.h"
24 
25 
26 namespace OHOS::Security::SecurityCollector {
LoadCollectorService()27 sptr<IRemoteObject> CollectorServiceLoader::LoadCollectorService()
28 {
29     auto registry = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
30     if (registry == nullptr) {
31         LOGE("GetSystemAbilityManager error.");
32         return nullptr;
33     }
34     auto object = registry->CheckSystemAbility(SECURITY_COLLECTOR_MANAGER_SA_ID);
35     if (object != nullptr) {
36         return object;
37     }
38 
39     sptr<LoadCallback> callback = new (std::nothrow) LoadCallback();
40     if (callback == nullptr) {
41         LOGE("LoadCallback new error.");
42         return nullptr;
43     }
44     int32_t result = registry->LoadSystemAbility(SECURITY_COLLECTOR_MANAGER_SA_ID, callback);
45     if (result != ERR_OK) {
46         LOGE("LoadSystemAbility error.");
47         return nullptr;
48     }
49     return callback->Promise();
50 }
51 
OnLoadSystemAbilitySuccess(int32_t sid,const sptr<IRemoteObject> & object)52 void CollectorServiceLoader::LoadCallback::OnLoadSystemAbilitySuccess(int32_t sid, const sptr<IRemoteObject> &object)
53 {
54     LOGI("OnLoadSystemAbilitySuccess = %{public}d.", sid);
55     promise_.set_value(object);
56 }
57 
OnLoadSystemAbilityFail(int32_t sid)58 void CollectorServiceLoader::LoadCallback::OnLoadSystemAbilityFail(int32_t sid)
59 {
60     LOGI("OnLoadSystemAbilityFail = %{public}d.", sid);
61 }
62 
Promise()63 sptr<IRemoteObject> CollectorServiceLoader::LoadCallback::Promise()
64 {
65     constexpr int32_t TimeoutInterval = 1;
66     auto future = promise_.get_future();
67     auto span = std::chrono::seconds(TimeoutInterval);
68     if (future.wait_for(span) == std::future_status::timeout) {
69         LOGE("LoadCallback Get Promise Timeout");
70         return nullptr;
71     } else {
72         return future.get();
73     }
74 }
75 } // namespace OHOS::Security::SecurityAudit