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 "iam_callback_interface.h" 36 #include "nocopyable.h" 37 #include "widget_json.h" 38 #include "widget_schedule_node.h" 39 #include "ui_extension_ability_connection.h" 40 41 namespace OHOS { 42 namespace UserIam { 43 namespace UserAuth { 44 using namespace OHOS::AppExecFwk; 45 class WidgetContext : public WidgetScheduleNodeCallback, 46 public Context, 47 public std::enable_shared_from_this<WidgetContext>, 48 public ContextDeathRecipientManager, 49 public ContextAppStateObserverManager, 50 public NoCopyable { 51 public: 52 WidgetContext(uint64_t contextId, const ContextFactory::AuthWidgetContextPara ¶, 53 std::shared_ptr<ContextCallback> callback); 54 ~WidgetContext() override; 55 56 // Context API 57 bool Start() override; 58 bool Stop() override; 59 uint64_t GetContextId() const override; 60 ContextType GetContextType() const override; 61 std::shared_ptr<ScheduleNode> GetScheduleNode(uint64_t scheduleId) const override; 62 uint32_t GetTokenId() const override; 63 int32_t GetLatestError() const override; 64 int32_t GetUserId() const override; 65 int32_t GetAuthType() const override; 66 std::string GetCallerName() const override; 67 68 // WidgetScheduleNodeCallback API 69 bool LaunchWidget() override; 70 void ExecuteAuthList(const std::set<AuthType> &authTypeList, bool endAfterFirstFail, 71 AuthIntent authIntent) override; 72 void EndAuthAsCancel() override; 73 void EndAuthAsNaviPin() override; 74 void EndAuthAsWidgetParaInvalid() override; 75 void StopAuthList(const std::vector<AuthType> &authTypeList) override; 76 void SuccessAuth(AuthType authType) override; 77 bool AuthWidgetReload(uint32_t orientation, uint32_t needRotate, uint32_t alreadyLoad, 78 AuthType &rotateAuthType) override; 79 void AuthWidgetReloadInit() override; 80 81 void AuthResult(int32_t resultCode, int32_t authType, const Attributes &finalResult); 82 void AuthTipInfo(int32_t tipInfo, int32_t authType, const Attributes &extraInfo); 83 protected: 84 virtual bool OnStart(); 85 virtual void OnResult(int32_t resultCode, const std::shared_ptr<Attributes> &scheduleResultAttr); 86 virtual bool OnStop(); 87 88 private: 89 struct WidgetRotatePara { 90 bool isReload {false}; 91 uint32_t orientation {0}; 92 uint32_t needRotate {0}; 93 uint32_t alreadyLoad {0}; 94 AuthType rotateAuthType {0}; 95 }; 96 void SetLatestError(int32_t error) override; 97 std::shared_ptr<Context> BuildTask(const std::vector<uint8_t> &challenge, 98 AuthType authType, AuthTrustLevel authTrustLevel, bool endAfterFirstFail, AuthIntent authIntent); 99 bool BuildSchedule(); 100 bool ConnectExtension(const WidgetRotatePara &widgetRotatePara); 101 int32_t ConnectExtensionAbility(const AAFwk::Want &want, const std::string commandStr); 102 bool DisconnectExtension(); 103 void End(const ResultCode &resultCode); 104 std::shared_ptr<ContextCallback> GetAuthContextCallback(AuthType authType, AuthTrustLevel authTrustLevel, 105 sptr<IamCallbackInterface> &callback); 106 void StopAllRunTask(const ResultCode &resultCode); 107 std::string BuildStartCommand(const WidgetRotatePara &widgetRotatePara); 108 void BuildStartPinSubType(WidgetCmdParameters &widgetCmdParameters); 109 void ProcessRotatePara(WidgetCmdParameters &widgetCmdParameters, const WidgetRotatePara &widgetRotatePara); 110 bool isValidRotate(const WidgetRotatePara &widgetRotatePara); 111 std::string GetCallingBundleName(); 112 113 private: 114 struct TaskInfo { 115 AuthType authType {0}; 116 std::shared_ptr<Context> task {nullptr}; 117 }; 118 119 struct WidgetAuthResultInfo { 120 std::vector<uint8_t> token {}; 121 AuthType authType {0}; 122 uint64_t credentialDigest; 123 uint16_t credentialCount; 124 int64_t pinExpiredInfo; 125 }; 126 127 uint64_t contextId_ {0}; 128 std::string description_ {""}; 129 std::shared_ptr<ContextCallback> callerCallback_ {nullptr}; 130 bool hasStarted_ {false}; 131 132 int32_t latestError_ {ResultCode::GENERAL_ERROR}; 133 ContextFactory::AuthWidgetContextPara para_ {}; 134 std::shared_ptr<WidgetScheduleNode> schedule_ {nullptr}; 135 std::recursive_mutex mutex_; 136 std::list<TaskInfo> runTaskInfoList_; 137 sptr<UIExtensionAbilityConnection> connection_ {nullptr}; 138 WidgetAuthResultInfo authResultInfo_ {}; 139 int32_t faceReload_ {0}; 140 uint32_t widgetRotateOrientation_ {0}; 141 uint32_t widgetAlreadyLoad_ {0}; 142 }; 143 } // namespace UserAuth 144 } // namespace UserIam 145 } // namespace OHOS 146 #endif // IAM_WIDGET_CONTEXT_H 147