1 /* 2 * Copyright (c) 2023-2024 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 IAM_WIDGET_CONTEXT_H 17 #define IAM_WIDGET_CONTEXT_H 18 19 #include <cstdint> 20 #include <map> 21 #include <memory> 22 #include <mutex> 23 #include <list> 24 #include <vector> 25 26 #include "auth_common.h" 27 #include "extension_manager_client.h" 28 #include "authentication_impl.h" 29 #include "base_context.h" 30 #include "context.h" 31 #include "context_appstate_observer.h" 32 #include "context_death_recipient.h" 33 #include "context_factory.h" 34 #include "in_process_call_wrapper.h" 35 #include "iiam_callback.h" 36 #include "imodal_callback.h" 37 #include "nocopyable.h" 38 #include "widget_json.h" 39 #include "widget_schedule_node.h" 40 #include "ui_extension_ability_connection.h" 41 42 namespace OHOS { 43 namespace UserIam { 44 namespace UserAuth { 45 using namespace OHOS::AppExecFwk; 46 class WidgetContext : public WidgetScheduleNodeCallback, 47 public Context, 48 public std::enable_shared_from_this<WidgetContext>, 49 public ContextDeathRecipientManager, 50 public ContextAppStateObserverManager, 51 public NoCopyable { 52 public: 53 WidgetContext(uint64_t contextId, const ContextFactory::AuthWidgetContextPara ¶, 54 std::shared_ptr<ContextCallback> callback, const sptr<IModalCallback> &modalCallback); 55 ~WidgetContext() override; 56 57 // Context API 58 bool Start() override; 59 bool Stop() override; 60 uint64_t GetContextId() const override; 61 ContextType GetContextType() const override; 62 std::shared_ptr<ScheduleNode> GetScheduleNode(uint64_t scheduleId) const override; 63 uint32_t GetTokenId() const override; 64 int32_t GetLatestError() const override; 65 int32_t GetUserId() const override; 66 int32_t GetAuthType() const override; 67 std::string GetCallerName() const override; 68 69 // WidgetScheduleNodeCallback API 70 bool LaunchWidget() override; 71 void ExecuteAuthList(const std::set<AuthType> &authTypeList, bool endAfterFirstFail, 72 AuthIntent authIntent) override; 73 void EndAuthAsCancel() override; 74 void EndAuthAsNaviPin() override; 75 void EndAuthAsWidgetParaInvalid() override; 76 void StopAuthList(const std::vector<AuthType> &authTypeList) override; 77 void SuccessAuth(AuthType authType) override; 78 void FailAuth(AuthType authType) override; 79 bool AuthWidgetReload(uint32_t orientation, uint32_t needRotate, uint32_t alreadyLoad, 80 AuthType &rotateAuthType) override; 81 void AuthWidgetReloadInit() override; 82 83 void AuthResult(int32_t resultCode, int32_t authType, const Attributes &finalResult); 84 void AuthTipInfo(int32_t tipInfo, int32_t authType, const Attributes &extraInfo); 85 void ClearSchedule() override; 86 void SendAuthTipInfo(int32_t authType, int32_t tipInfo) override; 87 88 protected: 89 virtual bool OnStart(); 90 virtual void OnResult(int32_t resultCode, const std::shared_ptr<Attributes> &scheduleResultAttr); 91 virtual bool OnStop(); 92 93 private: 94 struct WidgetRotatePara { 95 bool isReload {false}; 96 uint32_t orientation {0}; 97 uint32_t needRotate {0}; 98 uint32_t alreadyLoad {0}; 99 AuthType rotateAuthType {0}; 100 }; 101 void SetLatestError(int32_t error) override; 102 std::shared_ptr<Context> BuildTask(const std::vector<uint8_t> &challenge, 103 AuthType authType, AuthTrustLevel authTrustLevel, bool endAfterFirstFail, AuthIntent authIntent); 104 bool BuildSchedule(); 105 bool ConnectExtension(const WidgetRotatePara &widgetRotatePara); 106 int32_t ConnectExtensionAbility(const AAFwk::Want &want, const std::string commandStr); 107 bool DisconnectExtension(); 108 void End(const ResultCode &resultCode); 109 std::shared_ptr<ContextCallback> GetAuthContextCallback(AuthType authType, AuthTrustLevel authTrustLevel, 110 sptr<IIamCallback> &callback); 111 void StopAllRunTask(const ResultCode &resultCode); 112 std::string BuildStartCommand(const WidgetRotatePara &widgetRotatePara); 113 void BuildStartPinSubType(WidgetCmdParameters &widgetCmdParameters); 114 void ProcessRotatePara(WidgetCmdParameters &widgetCmdParameters, const WidgetRotatePara &widgetRotatePara); 115 bool IsValidRotate(const WidgetRotatePara &widgetRotatePara); 116 std::string GetCallingBundleName(); 117 bool IsSupportFollowCallerUi(); 118 bool IsInFollowCallerList(); 119 void SetSysDialogZOrder(WidgetCmdParameters &widgetCmdParameters); 120 bool IsSingleFaceOrFingerPrintAuth(); 121 bool IsNavigationAuth(); 122 UserAuthTipCode GetAuthTipCode(int32_t authResult, int32_t freezingTime); 123 void ProcAuthResult(int32_t resultCode, AuthType authType, int32_t freezingTime, 124 const Attributes &finalResult); 125 void ProcAuthTipInfo(int32_t tip, AuthType authType, const std::vector<uint8_t> &extraInfo); 126 127 private: 128 struct TaskInfo { 129 AuthType authType {0}; 130 std::shared_ptr<Context> task {nullptr}; 131 }; 132 133 struct WidgetAuthResultInfo { 134 std::vector<uint8_t> token {}; 135 AuthType authType {0}; 136 uint64_t credentialDigest; 137 uint16_t credentialCount; 138 int64_t pinExpiredInfo; 139 }; 140 141 struct ResultInfo { 142 int32_t resultCode; 143 AuthType authType; 144 int32_t freezingTime; 145 }; 146 147 uint64_t contextId_ {0}; 148 std::string description_ {""}; 149 std::shared_ptr<ContextCallback> callerCallback_ {nullptr}; 150 bool hasStarted_ {false}; 151 bool useModalApplication_ {false}; 152 153 int32_t latestError_ {ResultCode::GENERAL_ERROR}; 154 ContextFactory::AuthWidgetContextPara para_ {}; 155 std::shared_ptr<WidgetScheduleNode> schedule_ {nullptr}; 156 sptr<IModalCallback> modalCallback_ {nullptr}; 157 std::recursive_mutex mutex_; 158 std::list<TaskInfo> runTaskInfoList_; 159 sptr<UIExtensionAbilityConnection> connection_ {nullptr}; 160 WidgetAuthResultInfo authResultInfo_ {}; 161 int32_t faceReload_ {0}; 162 uint32_t widgetAlreadyLoad_ {0}; 163 nlohmann::json jsonBuf_ = {}; 164 }; 165 } // namespace UserAuth 166 } // namespace UserIam 167 } // namespace OHOS 168 #endif // IAM_WIDGET_CONTEXT_H 169