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 #ifndef FIRST_USE_DIALOG_H
16 #define FIRST_USE_DIALOG_H
17 
18 #include <map>
19 #include <memory>
20 #include <mutex>
21 #include <string>
22 #include "access_token.h"
23 #include "iremote_object.h"
24 #include "nlohmann/json.hpp"
25 #include "sec_comp_dialog_callback_stub.h"
26 #include "sec_comp_entity.h"
27 #include "sec_comp_err.h"
28 #include "sec_comp_info.h"
29 #include "sec_event_handler.h"
30 
31 namespace OHOS {
32 namespace Security {
33 namespace SecurityComponent {
34 class SecCompDialogSrvCallback : public SecCompDialogCallbackStub {
35 public:
SecCompDialogSrvCallback(int32_t scId,std::shared_ptr<SecCompEntity> sc,sptr<IRemoteObject> dialogCallback)36     explicit SecCompDialogSrvCallback(int32_t scId, std::shared_ptr<SecCompEntity> sc,
37         sptr<IRemoteObject> dialogCallback) : scId_(scId), sc_(sc), dialogCallback_(dialogCallback) {};
38 
~SecCompDialogSrvCallback()39     ~SecCompDialogSrvCallback() override
40     {
41         dialogCallback_ = nullptr;
42     };
43 
44     void OnDialogClosed(int32_t result) override;
45 private:
46     int32_t scId_;
47     std::shared_ptr<SecCompEntity> sc_;
48     sptr<IRemoteObject> dialogCallback_;
49 };
50 
51 class FirstUseDialog final {
52 public:
53     static FirstUseDialog& GetInstance();
54 
55     ~FirstUseDialog() = default;
56     int32_t NotifyFirstUseDialog(std::shared_ptr<SecCompEntity> entity,
57         sptr<IRemoteObject> callerToken, sptr<IRemoteObject> dialogCallback);
58     void Init(std::shared_ptr<SecEventHandler> secHandler);
59     int32_t GrantDialogWaitEntity(int32_t scId);
60     void RemoveDialogWaitEntitys(int32_t pid);
61     bool SetFirstUseMap(std::shared_ptr<SecCompEntity> entity);
62 
63 private:
FirstUseDialog()64     FirstUseDialog() {};
65     bool IsCfgDirExist(void);
66     bool IsCfgFileExist(void);
67     bool IsCfgFileValid(void);
68     bool ReadCfgContent(std::string& content);
69     void WriteCfgContent(const std::string& content);
70     bool ParseRecord(nlohmann::json& jsonRes,
71         AccessToken::AccessTokenID& id, uint64_t& type);
72     void ParseRecords(nlohmann::json& jsonRes);
73     void LoadFirstUseRecord(void);
74     void SaveFirstUseRecord(void);
75     void StartDialogAbility(std::shared_ptr<SecCompEntity> entity,
76         sptr<IRemoteObject> callerToken, sptr<IRemoteObject> dialogCallback);
77     void SendSaveEventHandler(void);
78 
79     std::mutex useMapMutex_;
80     std::unordered_map<AccessToken::AccessTokenID, uint64_t> firstUseMap_;
81     std::unordered_map<int32_t, std::shared_ptr<SecCompEntity>> dialogWaitMap_;
82     std::shared_ptr<SecEventHandler> secHandler_;
83 };
84 }  // namespace SecurityComponentEnhance
85 }  // namespace Security
86 }  // namespace OHOS
87 #endif  // FIRST_USE_DIALOG_H
88