• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "host/libs/screen_connector/screen_connector.h"
33 
34 namespace cuttlefish {
35 namespace confui {
36 
37 /**
38  * Confirmation UI Session
39  *
40  * E.g. Two guest apps could drive confirmation UI respectively,
41  * and both are alive at the moment. Each needs one session
42  *
43  */
44 class Session {
45  public:
46   Session(const std::string& session_name, const std::uint32_t display_num,
47           HostModeCtrl& host_mode_ctrl,
48           ScreenConnectorFrameRenderer& screen_connector,
49           const std::string& locale = "en");
50 
51   bool IsConfUiActive() const;
52 
GetId()53   std::string GetId() { return session_id_; }
54 
GetState()55   MainLoopState GetState() { return state_; }
56 
57   MainLoopState Transition(SharedFD& hal_cli, const FsmInput fsm_input,
58                            const ConfUiMessage& conf_ui_message);
59 
60   /**
61    * this make a transition from kWaitStop or kInSession to kSuspend
62    */
63   bool Suspend(SharedFD hal_cli);
64 
65   /**
66    * this make a transition from kRestore to the saved state
67    */
68   bool Restore(SharedFD hal_cli);
69 
70   // abort session
71   void Abort();
72 
73   // client on the host wants to abort
74   // should let the guest know it
75   void UserAbort(SharedFD hal_cli);
76 
77   bool IsSuspended() const;
78   void CleanUp();
79 
IsConfirm(const int x,const int y)80   bool IsConfirm(const int x, const int y) {
81     return renderer_->IsInConfirm(x, y);
82   }
83 
IsCancel(const int x,const int y)84   bool IsCancel(const int x, const int y) {
85     return renderer_->IsInCancel(x, y);
86   }
87 
88   // tell if grace period has passed
89   bool IsReadyForUserInput() const;
90 
91  private:
IsUserInput(const FsmInput fsm_input)92   bool IsUserInput(const FsmInput fsm_input) {
93     return fsm_input == FsmInput::kUserEvent;
94   }
95 
96   /** create a frame, and render it on the webRTC client
97    *
98    * note that this does not check host_ctrl_mode_
99    */
100   bool RenderDialog();
101 
102   // transition actions on each state per input
103   // the new state will be save to the state_ at the end of each call
104   //
105   // when false is returned, the FSM must terminate
106   // and, no need to let the guest know
107   bool HandleInit(SharedFD hal_cli, const FsmInput fsm_input,
108                   const ConfUiMessage& conf_ui_msg);
109 
110   bool HandleWaitStop(SharedFD hal_cli, const FsmInput fsm_input);
111 
112   bool HandleInSession(SharedFD hal_cli, const FsmInput fsm_input,
113                        const ConfUiMessage& conf_ui_msg);
114 
115   // report with an error ack to HAL, and reset the FSM
116   bool ReportErrorToHal(SharedFD hal_cli, const std::string& msg);
117 
118   void ScheduleToTerminate();
119 
120   bool IsInverted() const;
121   bool IsMagnified() const;
122 
123   const std::string session_id_;
124   const std::uint32_t display_num_;
125   std::unique_ptr<ConfUiRenderer> renderer_;
126   HostModeCtrl& host_mode_ctrl_;
127   ScreenConnectorFrameRenderer& screen_connector_;
128 
129   // only context to save
130   std::string prompt_text_;
131   std::string locale_;
132   std::vector<teeui::UIOption> ui_options_;
133   std::vector<std::uint8_t> extra_data_;
134   // the second argument for resultCB of promptUserConfirmation
135   std::vector<std::uint8_t> signed_confirmation_;
136   std::vector<std::uint8_t> message_;
137 
138   std::unique_ptr<Cbor> cbor_;
139 
140   // effectively, this variables are shared with webRTC thread
141   // the input demuxer will check the confirmation UI mode based on this
142   std::atomic<MainLoopState> state_;
143   MainLoopState saved_state_;  // for restore/suspend
144   using Clock = std::chrono::steady_clock;
145   using TimePoint = std::chrono::time_point<Clock>;
146   std::unique_ptr<TimePoint> start_time_;
147 };
148 }  // end of namespace confui
149 }  // end of namespace cuttlefish
150