1 /*
2  * Copyright (c) 2024 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 CODE_SIGN_UNLOCK_EVENT_HELPER_H
17 #define CODE_SIGN_UNLOCK_EVENT_HELPER_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <mutex>
22 
23 #include "common_event_manager.h"
24 #include "common_event_support.h"
25 #include "iservice_registry.h"
26 
27 #ifndef LOG_RUST
28 #define LOG_RUST
29 #endif
30 
31 namespace OHOS {
32 namespace Security {
33 namespace CodeSign {
34 class UnlockEventHelper {
35 public:
36     static UnlockEventHelper &GetInstance();
37 
38     bool StartWaitingUnlock();
39     void FinishWaiting();
40     bool CheckUserUnlockByScreenLockManager();
41 
42 private:
43     class UnlockEventSubscriber : public OHOS::EventFwk::CommonEventSubscriber {
44     public:
UnlockEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo & info)45         UnlockEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo& info) : CommonEventSubscriber(info) {}
46         ~UnlockEventSubscriber() override = default;
47         void OnReceiveEvent(const OHOS::EventFwk::CommonEventData& event) override;
48     };
49 
UnlockEventHelper()50     UnlockEventHelper() {};
51     ~UnlockEventHelper() = default;
52     void InitUnlockEventSubscriber();
53     bool RegisterEvent();
54     void UnregisterEvent();
55     bool WaitForCommonEventManager();
56 
57     bool hasRegistered_ = false;
58     bool hasInited_ = false;
59     bool hasUnLocked_ = false;
60     std::mutex unlockMutex_;
61     std::condition_variable unlockConVar_;
62     std::shared_ptr<UnlockEventSubscriber> unlockEventSubscriber_;
63 };
64 }
65 }
66 }
67 #endif