1 /* 2 * Copyright (C) 2019 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 #ifndef ANDROID_APEXD_APEXD_SESSION_H_ 18 #define ANDROID_APEXD_APEXD_SESSION_H_ 19 20 #include <android-base/result.h> 21 22 #include "apex_constants.h" 23 24 #include "session_state.pb.h" 25 26 #include <optional> 27 28 namespace android { 29 namespace apex { 30 31 class ApexSession { 32 public: 33 // Returns top-level directory to store sessions metadata in. 34 // If device has /metadata partition, this will return 35 // /metadata/apex/sessions, on all other devices it will return 36 // /data/apex/sessions. 37 static std::string GetSessionsDir(); 38 // Migrates content of /data/apex/sessions to /metadata/apex/sessions. 39 // If device doesn't have /metadata partition this call will be a no-op. 40 // If /data/apex/sessions this call will also be a no-op. 41 static android::base::Result<void> MigrateToMetadataSessionsDir(); 42 43 static android::base::Result<ApexSession> CreateSession(int session_id); 44 static android::base::Result<ApexSession> GetSession(int session_id); 45 static std::vector<ApexSession> GetSessions(); 46 static std::vector<ApexSession> GetSessionsInState( 47 ::apex::proto::SessionState::State state); 48 static android::base::Result<std::optional<ApexSession>> GetActiveSession(); 49 static std::vector<ApexSession> GetActiveSessions(); 50 ApexSession() = delete; 51 52 const google::protobuf::RepeatedField<int> GetChildSessionIds() const; 53 ::apex::proto::SessionState::State GetState() const; 54 int GetId() const; 55 const std::string& GetBuildFingerprint() const; 56 const std::string& GetCrashingNativeProcess() const; 57 const std::string& GetErrorMessage() const; 58 bool IsFinalized() const; 59 bool HasRollbackEnabled() const; 60 bool IsRollback() const; 61 int GetRollbackId() const; 62 const google::protobuf::RepeatedPtrField<std::string> GetApexNames() const; 63 64 void SetChildSessionIds(const std::vector<int>& child_session_ids); 65 void SetBuildFingerprint(const std::string& fingerprint); 66 void SetHasRollbackEnabled(const bool enabled); 67 void SetIsRollback(const bool is_rollback); 68 void SetRollbackId(const int rollback_id); 69 void SetCrashingNativeProcess(const std::string& crashing_process); 70 void SetErrorMessage(const std::string& error_message); 71 void AddApexName(const std::string& apex_name); 72 73 android::base::Result<void> UpdateStateAndCommit( 74 const ::apex::proto::SessionState::State& state); 75 76 android::base::Result<void> DeleteSession() const; 77 static void DeleteFinalizedSessions(); 78 79 private: 80 explicit ApexSession(::apex::proto::SessionState state); 81 ::apex::proto::SessionState state_; 82 83 static android::base::Result<ApexSession> GetSessionFromFile( 84 const std::string& path); 85 }; 86 87 std::ostream& operator<<(std::ostream& out, const ApexSession& session); 88 89 } // namespace apex 90 } // namespace android 91 92 #endif // ANDROID_APEXD_APEXD_SESSION_H 93