1 /* 2 * Copyright (c) 2022 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_BASE_CONTEXT_H 17 #define IAM_BASE_CONTEXT_H 18 19 #include <cstdint> 20 #include <memory> 21 #include <mutex> 22 #include <string> 23 24 #include "nocopyable.h" 25 #include "context.h" 26 #include "context_appstate_observer.h" 27 #include "context_callback.h" 28 #include "context_death_recipient.h" 29 30 namespace OHOS { 31 namespace UserIam { 32 namespace UserAuth { 33 using namespace OHOS::AppExecFwk; 34 class BaseContext : public ScheduleNodeCallback, 35 public Context, 36 public std::enable_shared_from_this<BaseContext>, 37 public ContextDeathRecipientManager, 38 public ContextAppStateObserverManager, 39 public NoCopyable { 40 public: 41 BaseContext(const std::string &type, uint64_t contextId, std::shared_ptr<ContextCallback> callback, 42 bool needSubscribeAppState); 43 ~BaseContext() override; 44 45 uint64_t GetContextId() const override; 46 std::shared_ptr<ScheduleNode> GetScheduleNode(uint64_t scheduleId) const override; 47 int32_t GetUserId() const override; 48 int32_t GetAuthType() const override; 49 std::string GetCallerName() const override; 50 51 void OnScheduleStarted() override; 52 void OnScheduleProcessed(ExecutorRole src, int32_t moduleType, const std::vector<uint8_t> &acquireMsg) override; 53 void OnScheduleStoped(int32_t resultCode, const std::shared_ptr<Attributes> &finalResult) override; 54 int32_t GetLatestError() const override; 55 56 protected: 57 void SetLatestError(int32_t error) override; 58 const char *GetDescription() const; 59 virtual bool OnStart() = 0; 60 virtual void OnResult(int32_t resultCode, const std::shared_ptr<Attributes> &scheduleResultAttr) = 0; 61 virtual bool OnStop() = 0; 62 std::shared_ptr<ContextCallback> callback_ = nullptr; 63 std::vector<std::shared_ptr<ScheduleNode>> scheduleList_; 64 65 private: 66 bool Start() final; 67 bool Stop() final; 68 uint64_t contextId_ = INVALID_CONTEXT_ID; 69 std::string description_; 70 bool hasStarted_ = false; 71 std::mutex mutex_; 72 int32_t latestError_ = ResultCode::GENERAL_ERROR; 73 }; 74 } // namespace UserAuth 75 } // namespace UserIam 76 } // namespace OHOS 77 #endif // IAM_BASE_CONTEXT_H