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 
16 #ifndef OHOS_ABILITY_RUNTIME_DIALOG_SESSION_MANAGEER_H
17 #define OHOS_ABILITY_RUNTIME_DIALOG_SESSION_MANAGEER_H
18 #include <list>
19 #include <unordered_map>
20 #include <string>
21 #include "ability_record.h"
22 #include "cpp/mutex.h"
23 #include "dialog_session_info.h"
24 #include "json_serializer.h"
25 #include "nocopyable.h"
26 #include "parcel.h"
27 #include "refbase.h"
28 #include "system_dialog_scheduler.h"
29 #include "want.h"
30 
31 namespace OHOS {
32 namespace AAFwk {
33 struct DialogCallerInfo {
34     int32_t userId = -1;
35     int requestCode = -1;
36     sptr<IRemoteObject> callerToken;
37     Want targetWant;
38     bool isSelector = false;
39 };
40 
41 enum class SelectorType {
42     IMPLICIT_START_SELECTOR = 0,
43     APP_CLONR_SELECTOR = 1
44 };
45 
46 struct StartupSessionInfo {
47     AbilityRequest abilityRequest;
48 };
49 
50 class DialogSessionManager {
51 public:
52     static DialogSessionManager &GetInstance();
53     ~DialogSessionManager() = default;
54 
55     sptr<DialogSessionInfo> GetDialogSessionInfo(const std::string &dialogSessionId) const;
56 
57     std::shared_ptr<DialogCallerInfo> GetDialogCallerInfo(const std::string &dialogSessionId) const;
58 
59     std::shared_ptr<StartupSessionInfo> GetStartupSessionInfo(const std::string &dialogSessionId) const;
60 
61     int SendDialogResult(const Want &want, const std::string &dialogSessionId, bool isAllowed);
62 
63     int CreateJumpModalDialog(AbilityRequest &abilityRequest, int32_t userId, const Want &replaceWant);
64 
65     int CreateImplicitSelectorModalDialog(AbilityRequest &abilityRequest, const Want &want, int32_t userId,
66         std::vector<DialogAppInfo> &dialogAppInfos);
67 
68     int CreateCloneSelectorModalDialog(AbilityRequest &abilityRequest, const Want &want, int32_t userId,
69         std::vector<DialogAppInfo> &dialogAppInfos, const std::string &replaceWant);
70 
71     int HandleErmsResult(AbilityRequest &abilityRequest, int32_t userId, const Want &replaceWant);
72 
73     int32_t HandleErmsResultBySCB(AbilityRequest &abilityRequest, const Want &replaceWant);
74 
75     bool IsCreateCloneSelectorDialog(const std::string &bundleName, int32_t userId);
76 
77 private:
78     DialogSessionManager() = default;
79     std::string GenerateDialogSessionId();
80 
81     void SetDialogSessionInfo(const std::string &dialogSessionId, sptr<DialogSessionInfo> &dilogSessionInfo,
82         std::shared_ptr<DialogCallerInfo> &dialogCallerInfo);
83 
84     void SetStartupSessionInfo(const std::string &dialogSessionId, const AbilityRequest &abilityRequest);
85 
86     int32_t NotifySCBToRecoveryAfterInterception(const std::string &dialogSessionId,
87         const AbilityRequest &abilityRequest);
88 
89     void ClearDialogContext(const std::string &dialogSessionId);
90 
91     void ClearAllDialogContexts();
92 
93     std::string GenerateDialogSessionRecordCommon(AbilityRequest &abilityRequest, int32_t userId,
94         const AAFwk::WantParams &parameters, std::vector<DialogAppInfo> &dialogAppInfos, bool isSelector);
95 
96     void GenerateCallerAbilityInfo(AbilityRequest &abilityRequest, DialogAbilityInfo &callerAbilityInfo);
97 
98     void GenerateSelectorTargetAbilityInfos(std::vector<DialogAppInfo> &dialogAppInfos,
99         std::vector<DialogAbilityInfo> &targetAbilityInfos);
100 
101     void GenerateJumpTargetAbilityInfos(AbilityRequest &abilityRequest,
102         std::vector<DialogAbilityInfo> &targetAbilityInfos);
103 
104     void GenerateDialogCallerInfo(AbilityRequest &abilityRequest, int32_t userId,
105         std::shared_ptr<DialogCallerInfo> dialogCallerInfo, bool isSelector);
106 
107     int CreateModalDialogCommon(const Want &replaceWant, sptr<IRemoteObject> callerToken,
108         const std::string &dialogSessionId);
109 
110     mutable ffrt::mutex dialogSessionRecordLock_;
111     std::unordered_map<std::string, sptr<DialogSessionInfo>> dialogSessionInfoMap_;
112     std::unordered_map<std::string, std::shared_ptr<DialogCallerInfo>> dialogCallerInfoMap_;
113     std::unordered_map<std::string, std::shared_ptr<StartupSessionInfo>> startupSessionInfoMap_;
114 
115     DISALLOW_COPY_AND_MOVE(DialogSessionManager);
116 };
117 }  // namespace AAFwk
118 }  // namespace OHOS
119 #endif // OHOS_ABILITY_RUNTIME_DIALOG_SESSION_MANAGEER_H
120