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