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 std::string GetBuildFingerprint() const; 56 std::string GetCrashingNativeProcess() const; 57 bool IsFinalized() const; 58 bool HasRollbackEnabled() const; 59 bool IsRollback() const; 60 int GetRollbackId() const; 61 const google::protobuf::RepeatedPtrField<std::string> GetApexNames() const; 62 63 void SetChildSessionIds(const std::vector<int>& child_session_ids); 64 void SetBuildFingerprint(const std::string& fingerprint); 65 void SetHasRollbackEnabled(const bool enabled); 66 void SetIsRollback(const bool is_rollback); 67 void SetRollbackId(const int rollback_id); 68 void SetCrashingNativeProcess(const std::string& crashing_process); 69 void AddApexName(const std::string& apex_name); 70 71 android::base::Result<void> UpdateStateAndCommit( 72 const ::apex::proto::SessionState::State& state); 73 74 android::base::Result<void> DeleteSession() const; 75 76 private: 77 ApexSession(::apex::proto::SessionState state); 78 ::apex::proto::SessionState state_; 79 80 static android::base::Result<ApexSession> GetSessionFromFile( 81 const std::string& path); 82 }; 83 84 std::ostream& operator<<(std::ostream& out, const ApexSession& session); 85 86 } // namespace apex 87 } // namespace android 88 89 #endif // ANDROID_APEXD_APEXD_SESSION_H 90