1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <atomic> 20 #include <chrono> 21 #include <memory> 22 #include <string> 23 24 #include <teeui/msg_formatting.h> 25 26 #include "common/libs/confui/confui.h" 27 #include "host/libs/confui/cbor.h" 28 #include "host/libs/confui/host_mode_ctrl.h" 29 #include "host/libs/confui/host_renderer.h" 30 #include "host/libs/confui/server_common.h" 31 #include "host/libs/confui/sign.h" 32 33 namespace cuttlefish { 34 namespace confui { 35 36 /** 37 * Confirmation UI Session 38 * 39 * E.g. Two guest apps could drive confirmation UI respectively, 40 * and both are alive at the moment. Each needs one session 41 * 42 */ 43 class Session { 44 public: 45 Session(const std::string& session_name, const std::uint32_t display_num, 46 ConfUiRenderer& host_renderer, HostModeCtrl& host_mode_ctrl, 47 const std::string& locale = "en"); 48 GetId()49 std::string GetId() { return session_id_; } 50 GetState()51 MainLoopState GetState() { return state_; } 52 53 MainLoopState Transition(SharedFD& hal_cli, const FsmInput fsm_input, 54 const ConfUiMessage& conf_ui_message); 55 56 /** 57 * this make a transition from kWaitStop or kInSession to kSuspend 58 */ 59 bool Suspend(SharedFD hal_cli); 60 61 /** 62 * this make a transition from kRestore to the saved state 63 */ 64 bool Restore(SharedFD hal_cli); 65 66 // abort session 67 void Abort(); 68 69 // client on the host wants to abort 70 // should let the guest know it 71 void UserAbort(SharedFD hal_cli); 72 73 bool IsSuspended() const; 74 void CleanUp(); 75 IsConfirm(const int x,const int y)76 bool IsConfirm(const int x, const int y) { 77 return renderer_.IsInConfirm(x, y); 78 } 79 IsCancel(const int x,const int y)80 bool IsCancel(const int x, const int y) { return renderer_.IsInCancel(x, y); } 81 82 // tell if grace period has passed 83 bool IsReadyForUserInput() const; 84 85 private: IsUserInput(const FsmInput fsm_input)86 bool IsUserInput(const FsmInput fsm_input) { 87 return fsm_input == FsmInput::kUserEvent; 88 } 89 90 /** create a frame, and render it on the webRTC client 91 * 92 * note that this does not check host_ctrl_mode_ 93 */ 94 bool RenderDialog(); 95 96 // transition actions on each state per input 97 // the new state will be save to the state_ at the end of each call 98 // 99 // when false is returned, the FSM must terminate 100 // and, no need to let the guest know 101 bool HandleInit(SharedFD hal_cli, const FsmInput fsm_input, 102 const ConfUiMessage& conf_ui_msg); 103 104 bool HandleWaitStop(SharedFD hal_cli, const FsmInput fsm_input); 105 106 bool HandleInSession(SharedFD hal_cli, const FsmInput fsm_input, 107 const ConfUiMessage& conf_ui_msg); 108 109 // report with an error ack to HAL, and reset the FSM 110 bool ReportErrorToHal(SharedFD hal_cli, const std::string& msg); 111 112 void ScheduleToTerminate(); 113 114 const std::string session_id_; 115 const std::uint32_t display_num_; 116 ConfUiRenderer& renderer_; 117 HostModeCtrl& host_mode_ctrl_; 118 119 // only context to save 120 std::string prompt_text_; 121 std::string locale_; 122 std::vector<teeui::UIOption> ui_options_; 123 std::vector<std::uint8_t> extra_data_; 124 // the second argument for resultCB of promptUserConfirmation 125 std::vector<std::uint8_t> signed_confirmation_; 126 std::vector<std::uint8_t> message_; 127 128 std::unique_ptr<Cbor> cbor_; 129 130 // effectively, this variables are shared with webRTC thread 131 // the input demuxer will check the confirmation UI mode based on this 132 std::atomic<MainLoopState> state_; 133 MainLoopState saved_state_; // for restore/suspend 134 using Clock = std::chrono::steady_clock; 135 using TimePoint = std::chrono::time_point<Clock>; 136 std::unique_ptr<TimePoint> start_time_; 137 }; 138 } // end of namespace confui 139 } // end of namespace cuttlefish 140