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