1 /*
2  * Copyright (C) 2023 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 package com.android.server.pm;
18 
19 import android.content.pm.PackageInstaller.PreapprovalDetails;
20 import android.content.pm.PackageInstaller.SessionInfo;
21 import android.content.pm.PackageInstaller.SessionParams;
22 import android.content.pm.verify.domain.DomainSet;
23 
24 import com.android.internal.util.IndentingPrintWriter;
25 
26 import java.io.CharArrayWriter;
27 import java.io.File;
28 
29 /**
30  * A historical session object that stores minimal session info.
31  */
32 public final class PackageInstallerHistoricalSession {
33     public final int sessionId;
34     public final int userId;
35     private final String mParams;
36     private final long mCreatedMillis;
37 
38     private final File mStageDir;
39     private final String mStageCid;
40 
41     private final long mUpdatedMillis;
42 
43     private final long mCommittedMillis;
44 
45     private final int mOriginalInstallerUid;
46 
47     private final String mOriginalInstallerPackageName;
48 
49     private final int mInstallerUid;
50 
51     private final InstallSource mInstallSource;
52 
53     private final float mClientProgress;
54 
55     private final float mProgress;
56     private final boolean mSealed;
57 
58     private final boolean mPreapprovalRequested;
59     private final boolean mCommitted;
60 
61     private final boolean mStageDirInUse;
62 
63     private final boolean mPermissionsManuallyAccepted;
64 
65     private final int mFinalStatus;
66     private final String mFinalMessage;
67 
68     private final int mFds;
69     private final int mBridges;
70 
71     private final String mPreapprovalDetails;
72     private final int mParentSessionId;
73     private final boolean mDestroyed;
74     private final int[] mChildSessionIds;
75     private final boolean mSessionApplied;
76     private final boolean mSessionReady;
77     private final boolean mSessionFailed;
78     private final int mSessionErrorCode;
79     private final String mSessionErrorMessage;
80     private final String mPreVerifiedDomains;
81     private final String mPackageName;
82 
PackageInstallerHistoricalSession(int sessionId, int userId, int originalInstallerUid, String originalInstallerPackageName, InstallSource installSource, int installerUid, long createdMillis, long updatedMillis, long committedMillis, File stageDir, String stageCid, float clientProgress, float progress, boolean committed, boolean preapprovalRequested, boolean sealed, boolean permissionsManuallyAccepted, boolean stageDirInUse, boolean destroyed, int fds, int bridges, int finalStatus, String finalMessage, SessionParams params, int parentSessionId, int[] childSessionIds, boolean sessionApplied, boolean sessionFailed, boolean sessionReady, int sessionErrorCode, String sessionErrorMessage, PreapprovalDetails preapprovalDetails, DomainSet preVerifiedDomains, String packageNameFromApk)83     PackageInstallerHistoricalSession(int sessionId, int userId, int originalInstallerUid,
84             String originalInstallerPackageName, InstallSource installSource, int installerUid,
85             long createdMillis, long updatedMillis, long committedMillis, File stageDir,
86             String stageCid, float clientProgress, float progress, boolean committed,
87             boolean preapprovalRequested, boolean sealed, boolean permissionsManuallyAccepted,
88             boolean stageDirInUse, boolean destroyed, int fds, int bridges, int finalStatus,
89             String finalMessage, SessionParams params, int parentSessionId,
90             int[] childSessionIds, boolean sessionApplied, boolean sessionFailed,
91             boolean sessionReady, int sessionErrorCode, String sessionErrorMessage,
92             PreapprovalDetails preapprovalDetails, DomainSet preVerifiedDomains,
93             String packageNameFromApk) {
94         this.sessionId = sessionId;
95         this.userId = userId;
96         this.mOriginalInstallerUid = originalInstallerUid;
97         this.mOriginalInstallerPackageName = originalInstallerPackageName;
98         this.mInstallSource = installSource;
99         this.mInstallerUid = installerUid;
100         this.mCreatedMillis = createdMillis;
101         this.mUpdatedMillis = updatedMillis;
102         this.mCommittedMillis = committedMillis;
103         this.mStageDir = stageDir;
104         this.mStageCid = stageCid;
105         this.mClientProgress = clientProgress;
106         this.mProgress = progress;
107         this.mCommitted = committed;
108         this.mPreapprovalRequested = preapprovalRequested;
109         this.mSealed = sealed;
110         this.mPermissionsManuallyAccepted = permissionsManuallyAccepted;
111         this.mStageDirInUse = stageDirInUse;
112         this.mDestroyed = destroyed;
113         this.mFds = fds;
114         this.mBridges = bridges;
115         this.mFinalStatus = finalStatus;
116         this.mFinalMessage = finalMessage;
117 
118         CharArrayWriter writer = new CharArrayWriter();
119         IndentingPrintWriter pw = new IndentingPrintWriter(writer, "    ");
120         params.dump(pw);
121         this.mParams = writer.toString();
122 
123         this.mParentSessionId = parentSessionId;
124         this.mChildSessionIds = childSessionIds;
125         this.mSessionApplied = sessionApplied;
126         this.mSessionFailed = sessionFailed;
127         this.mSessionReady = sessionReady;
128         this.mSessionErrorCode = sessionErrorCode;
129         this.mSessionErrorMessage = sessionErrorMessage;
130         if (preapprovalDetails != null) {
131             this.mPreapprovalDetails = preapprovalDetails.toString();
132         } else {
133             this.mPreapprovalDetails = null;
134         }
135         if (preVerifiedDomains != null) {
136             this.mPreVerifiedDomains = String.join(",", preVerifiedDomains.getDomains());
137         } else {
138             this.mPreVerifiedDomains = null;
139         }
140 
141         this.mPackageName = preapprovalDetails != null ? preapprovalDetails.getPackageName()
142                 : packageNameFromApk != null ? packageNameFromApk : params.appPackageName;
143     }
144 
dump(IndentingPrintWriter pw)145     void dump(IndentingPrintWriter pw) {
146         pw.println("Session " + sessionId + ":");
147         pw.increaseIndent();
148 
149         pw.printPair("userId", userId);
150         pw.printPair("mOriginalInstallerUid", mOriginalInstallerUid);
151         pw.printPair("mOriginalInstallerPackageName", mOriginalInstallerPackageName);
152         pw.printPair("installerPackageName", mInstallSource.mInstallerPackageName);
153         pw.printPair("installInitiatingPackageName", mInstallSource.mInitiatingPackageName);
154         pw.printPair("installOriginatingPackageName", mInstallSource.mOriginatingPackageName);
155         pw.printPair("mInstallerUid", mInstallerUid);
156         pw.printPair("createdMillis", mCreatedMillis);
157         pw.printPair("updatedMillis", mUpdatedMillis);
158         pw.printPair("committedMillis", mCommittedMillis);
159         pw.printPair("stageDir", mStageDir);
160         pw.printPair("stageCid", mStageCid);
161         pw.println();
162 
163         pw.print(mParams);
164 
165         pw.printPair("mClientProgress", mClientProgress);
166         pw.printPair("mProgress", mProgress);
167         pw.printPair("mCommitted", mCommitted);
168         pw.printPair("mPreapprovalRequested", mPreapprovalRequested);
169         pw.printPair("mSealed", mSealed);
170         pw.printPair("mPermissionsManuallyAccepted", mPermissionsManuallyAccepted);
171         pw.printPair("mStageDirInUse", mStageDirInUse);
172         pw.printPair("mDestroyed", mDestroyed);
173         pw.printPair("mFds", mFds);
174         pw.printPair("mBridges", mBridges);
175         pw.printPair("mFinalStatus", mFinalStatus);
176         pw.printPair("mFinalMessage", mFinalMessage);
177         pw.printPair("mParentSessionId", mParentSessionId);
178         pw.printPair("mChildSessionIds", mChildSessionIds);
179         pw.printPair("mSessionApplied", mSessionApplied);
180         pw.printPair("mSessionFailed", mSessionFailed);
181         pw.printPair("mSessionReady", mSessionReady);
182         pw.printPair("mSessionErrorCode", mSessionErrorCode);
183         pw.printPair("mSessionErrorMessage", mSessionErrorMessage);
184         pw.printPair("mPreapprovalDetails", mPreapprovalDetails);
185         pw.printPair("mPreVerifiedDomains", mPreVerifiedDomains);
186         pw.printPair("mAppPackageName", mPackageName);
187         pw.println();
188 
189         pw.decreaseIndent();
190     }
191 
192     /**
193      * Generates a {@link SessionInfo} object.
194      */
generateInfo()195     public SessionInfo generateInfo() {
196         final SessionInfo info = new SessionInfo();
197         info.sessionId = sessionId;
198         info.userId = userId;
199         info.installerPackageName = mInstallSource.mInstallerPackageName;
200         info.installerAttributionTag = mInstallSource.mInstallerAttributionTag;
201         info.progress = mProgress;
202         info.sealed = mSealed;
203         info.isCommitted = mCommitted;
204         info.isPreapprovalRequested = mPreapprovalRequested;
205 
206         info.parentSessionId = mParentSessionId;
207         info.childSessionIds = mChildSessionIds;
208         info.isSessionApplied = mSessionApplied;
209         info.isSessionReady = mSessionReady;
210         info.isSessionFailed = mSessionFailed;
211         info.setSessionErrorCode(mSessionErrorCode, mSessionErrorMessage);
212         info.createdMillis = mCreatedMillis;
213         info.updatedMillis = mUpdatedMillis;
214         info.installerUid = mInstallerUid;
215         info.appPackageName = mPackageName;
216         return info;
217     }
218 }
219