• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
26 #include "context.h"
27 #include "context_callback.h"
28 
29 namespace OHOS {
30 namespace UserIam {
31 namespace UserAuth {
32 class BaseContext : public ScheduleNodeCallback,
33                     public Context,
34                     public std::enable_shared_from_this<BaseContext>,
35                     public NoCopyable {
36 public:
37     BaseContext(const std::string &type, uint64_t contextId, std::shared_ptr<ContextCallback> callback);
38     ~BaseContext() override = default;
39 
40     uint64_t GetContextId() const override;
41     std::shared_ptr<ScheduleNode> GetScheduleNode(uint64_t scheduleId) const override;
42 
43     void OnScheduleStarted() override;
44     void OnScheduleProcessed(ExecutorRole src, int32_t moduleType, const std::vector<uint8_t> &acquireMsg) override;
45     void OnScheduleStoped(int32_t resultCode, const std::shared_ptr<Attributes> &finalResult) override;
46     int32_t GetLatestError() const override;
47 
48 protected:
49     void SetLatestError(int32_t error) override;
50     const char *GetDescription() const;
51     virtual bool OnStart() = 0;
52     virtual void OnResult(int32_t resultCode, const std::shared_ptr<Attributes> &scheduleResultAttr) = 0;
53     virtual bool OnStop() = 0;
54     std::shared_ptr<ContextCallback> callback_ = nullptr;
55     std::vector<std::shared_ptr<ScheduleNode>> scheduleList_;
56 
57 private:
58     bool Start() final;
59     bool Stop() final;
60     uint64_t contextId_;
61     std::string description_;
62     bool hasStarted_ = false;
63     std::mutex mutex_;
64     int32_t latestError_ = ResultCode::GENERAL_ERROR;
65 };
66 } // namespace UserAuth
67 } // namespace UserIam
68 } // namespace OHOS
69 #endif // IAM_BASE_CONTEXT_H