1 /*
2  * Copyright (c) 2024-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 FOUNDATION_BUNDLE_FRAMEWORK_AOT_AOT_SIGN_DATA_CACHE_MGR
17 #define FOUNDATION_BUNDLE_FRAMEWORK_AOT_AOT_SIGN_DATA_CACHE_MGR
18 
19 #include <mutex>
20 #include <optional>
21 #include <string>
22 #include <unordered_map>
23 
24 #include "aot/aot_args.h"
25 #include "bundle_mgr_service.h"
26 #include "common_event_manager.h"
27 #include "common_event_support.h"
28 #include "inner_bundle_info.h"
29 #include "iservice_registry.h"
30 #include "nocopyable.h"
31 
32 namespace OHOS {
33 namespace AppExecFwk {
34 class AOTSignDataCacheMgr final {
35 public:
36     static AOTSignDataCacheMgr& GetInstance();
37     void RegisterScreenUnlockListener();
38     void AddPendSignData(const AOTArgs &aotArgs, const uint32_t versionCode,
39         const std::vector<uint8_t> &pendSignData, const ErrCode ret);
40 private:
41     AOTSignDataCacheMgr() = default;
42     ~AOTSignDataCacheMgr() = default;
43     DISALLOW_COPY_AND_MOVE(AOTSignDataCacheMgr);
44 
45     bool RegisterScreenUnlockEvent();
46     void UnregisterScreenUnlockEvent();
47     void HandleUnlockEvent();
48     ErrCode ExecutePendSign();
49     class UnlockEventSubscriber : public OHOS::EventFwk::CommonEventSubscriber {
50     public:
UnlockEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo & info)51         UnlockEventSubscriber(const OHOS::EventFwk::CommonEventSubscribeInfo &info) : CommonEventSubscriber(info) {}
52         ~UnlockEventSubscriber() override = default;
53         void OnReceiveEvent(const OHOS::EventFwk::CommonEventData &event) override;
54     };
55 private:
56     struct PendingData {
57         uint32_t versionCode {0};
58         std::vector<uint8_t> signData;
59     };
60     mutable std::mutex mutex_;
61     std::atomic<bool> isLocked_ { true };
62     std::shared_ptr<UnlockEventSubscriber> unlockEventSubscriber_;
63     std::unordered_map<std::string, std::unordered_map<std::string, PendingData>> pendingSignData_;
64 };
65 }  // namespace AppExecFwk
66 }  // namespace OHOS
67 #endif  // FOUNDATION_BUNDLE_FRAMEWORK_AOT_AOT_SIGN_DATA_CACHE_MGR
68