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 #ifndef SECURITY_COMPONENT_CLIENT_H
17 #define SECURITY_COMPONENT_CLIENT_H
18 
19 #include <condition_variable>
20 #include <mutex>
21 #include <string>
22 #include "i_sec_comp_service.h"
23 #include "sec_comp_death_recipient.h"
24 #include "sec_comp_err.h"
25 
26 namespace OHOS {
27 namespace Security {
28 namespace SecurityComponent {
29 class SecCompClient final {
30 public:
31     static SecCompClient& GetInstance();
32 
33     int32_t RegisterSecurityComponent(SecCompType type, const std::string& componentInfo, int32_t& scId);
34     int32_t UpdateSecurityComponent(int32_t scId, const std::string& componentInfo);
35     int32_t UnregisterSecurityComponent(int32_t scId);
36     int32_t ReportSecurityComponentClickEvent(int32_t scId,
37         const std::string& componentInfo, const SecCompClickEvent& clickInfo,
38         sptr<IRemoteObject> callerToken, sptr<IRemoteObject> dialogCallback);
39     bool VerifySavePermission(AccessToken::AccessTokenID tokenId);
40     sptr<IRemoteObject> GetEnhanceRemoteObject(bool doLoadSa);
41     int32_t PreRegisterSecCompProcess();
42     bool IsServiceExist();
43     bool LoadService();
44     bool IsSystemAppCalling();
45 
46     void FinishStartSASuccess(const sptr<IRemoteObject>& remoteObject);
47     void FinishStartSAFail();
48     void OnRemoteDiedHandle();
49 
50 private:
51     SecCompClient();
52     virtual ~SecCompClient();
53     DISALLOW_COPY_AND_MOVE(SecCompClient);
54 
55     bool TryToGetSecCompSa();
56     bool StartLoadSecCompSa();
57     void WaitForSecCompSa();
58     void GetSecCompSa();
59     void LoadSecCompSa();
60     sptr<ISecCompService> GetProxy(bool doLoadSa);
61     void GetProxyFromRemoteObject(const sptr<IRemoteObject>& remoteObject);
62 
63     std::mutex cvLock_;
64     bool readyFlag_ = false;
65     std::condition_variable secComCon_;
66     std::mutex proxyMutex_;
67     sptr<ISecCompService> proxy_ = nullptr;
68     sptr<SecCompDeathRecipient> serviceDeathObserver_ = nullptr;
69 };
70 }  // namespace SecurityComponent
71 }  // namespace Security
72 }  // namespace OHOS
73 #endif  // SECURITY_COMPONENT_CLIENT_H
74