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