Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 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 | package com.android.server.storage; |
| 18 | |
| 19 | import static android.service.storage.ExternalStorageService.EXTRA_ERROR; |
| 20 | import static android.service.storage.ExternalStorageService.FLAG_SESSION_ATTRIBUTE_INDEXABLE; |
| 21 | import static android.service.storage.ExternalStorageService.FLAG_SESSION_TYPE_FUSE; |
| 22 | |
| 23 | import static com.android.server.storage.StorageSessionController.ExternalStorageServiceException; |
| 24 | |
| 25 | import android.annotation.MainThread; |
| 26 | import android.annotation.Nullable; |
| 27 | import android.content.ComponentName; |
| 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
| 30 | import android.content.ServiceConnection; |
| 31 | import android.os.Bundle; |
| 32 | import android.os.IBinder; |
| 33 | import android.os.ParcelFileDescriptor; |
| 34 | import android.os.ParcelableException; |
| 35 | import android.os.RemoteCallback; |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 36 | import android.os.UserHandle; |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 37 | import android.service.storage.ExternalStorageService; |
| 38 | import android.service.storage.IExternalStorageService; |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 39 | import android.text.TextUtils; |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 40 | import android.util.Slog; |
| 41 | |
| 42 | import com.android.internal.annotations.GuardedBy; |
| 43 | import com.android.internal.util.Preconditions; |
| 44 | |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 45 | import java.io.IOException; |
| 46 | import java.util.HashMap; |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 47 | import java.util.HashSet; |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 48 | import java.util.Map; |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 49 | import java.util.Set; |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 50 | import java.util.concurrent.CountDownLatch; |
| 51 | import java.util.concurrent.TimeUnit; |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 52 | import java.util.concurrent.TimeoutException; |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Controls the lifecycle of the {@link ActiveConnection} to an {@link ExternalStorageService} |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 56 | * for a user and manages storage sessions associated with mounted volumes. |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 57 | */ |
| 58 | public final class StorageUserConnection { |
| 59 | private static final String TAG = "StorageUserConnection"; |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 60 | private static final int REMOTE_TIMEOUT_SECONDS = 15; |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 61 | |
| 62 | private final Object mLock = new Object(); |
| 63 | private final Context mContext; |
| 64 | private final int mUserId; |
| 65 | private final StorageSessionController mSessionController; |
| 66 | private final ActiveConnection mActiveConnection = new ActiveConnection(); |
| 67 | @GuardedBy("mLock") private final Map<String, Session> mSessions = new HashMap<>(); |
| 68 | |
| 69 | public StorageUserConnection(Context context, int userId, StorageSessionController controller) { |
| 70 | mContext = Preconditions.checkNotNull(context); |
| 71 | mUserId = Preconditions.checkArgumentNonnegative(userId); |
| 72 | mSessionController = controller; |
| 73 | } |
| 74 | |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 75 | /** |
| 76 | * Creates and stores a storage {@link Session}. |
| 77 | * |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 78 | * They must also be cleaned up with {@link #removeSession}. |
| 79 | * |
| 80 | * @throws IllegalArgumentException if a {@code Session} with {@code sessionId} already exists |
| 81 | */ |
Zim | 95eca1d | 2019-11-15 18:03:00 +0000 | [diff] [blame] | 82 | public void createSession(String sessionId, ParcelFileDescriptor pfd, String upperPath, |
| 83 | String lowerPath) { |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 84 | Preconditions.checkNotNull(sessionId); |
| 85 | Preconditions.checkNotNull(pfd); |
Zim | 95eca1d | 2019-11-15 18:03:00 +0000 | [diff] [blame] | 86 | Preconditions.checkNotNull(upperPath); |
| 87 | Preconditions.checkNotNull(lowerPath); |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 88 | |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 89 | synchronized (mLock) { |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 90 | Preconditions.checkArgument(!mSessions.containsKey(sessionId)); |
Zim | 95eca1d | 2019-11-15 18:03:00 +0000 | [diff] [blame] | 91 | mSessions.put(sessionId, new Session(sessionId, pfd, upperPath, lowerPath)); |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Starts an already created storage {@link Session} for {@code sessionId}. |
| 97 | * |
| 98 | * It is safe to call this multiple times, however if the session is already started, |
| 99 | * subsequent calls will be ignored. |
| 100 | * |
| 101 | * @throws ExternalStorageServiceException if the session failed to start |
| 102 | **/ |
| 103 | public void startSession(String sessionId) throws ExternalStorageServiceException { |
| 104 | Session session; |
| 105 | synchronized (mLock) { |
| 106 | session = mSessions.get(sessionId); |
| 107 | } |
| 108 | |
| 109 | prepareRemote(); |
| 110 | synchronized (mLock) { |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 111 | mActiveConnection.startSessionLocked(session); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | /** |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 116 | * Removes a session without ending it or waiting for exit. |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 117 | * |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 118 | * This should only be used if the session has certainly been ended because the volume was |
| 119 | * unmounted or the user running the session has been stopped. Otherwise, wait for session |
| 120 | * with {@link #waitForExit}. |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 121 | **/ |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 122 | public Session removeSession(String sessionId) { |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 123 | synchronized (mLock) { |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 124 | Session session = mSessions.remove(sessionId); |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 125 | if (session != null) { |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 126 | session.close(); |
| 127 | return session; |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 128 | } |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 129 | return null; |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 133 | |
| 134 | /** |
| 135 | * Removes a session and waits for exit |
| 136 | * |
| 137 | * @throws ExternalStorageServiceException if the session may not have exited |
| 138 | **/ |
| 139 | public void removeSessionAndWait(String sessionId) throws ExternalStorageServiceException { |
| 140 | Session session = removeSession(sessionId); |
| 141 | if (session == null) { |
| 142 | Slog.i(TAG, "No session found for id: " + sessionId); |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | Slog.i(TAG, "Waiting for session end " + session + " ..."); |
| 147 | prepareRemote(); |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 148 | synchronized (mLock) { |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 149 | mActiveConnection.endSessionLocked(session); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /** Starts all available sessions for a user without blocking. Any failures will be ignored. */ |
| 154 | public void startAllSessions() { |
| 155 | try { |
| 156 | prepareRemote(); |
| 157 | } catch (ExternalStorageServiceException e) { |
| 158 | Slog.e(TAG, "Failed to start all sessions for user: " + mUserId, e); |
| 159 | return; |
| 160 | } |
| 161 | |
| 162 | synchronized (mLock) { |
| 163 | Slog.i(TAG, "Starting " + mSessions.size() + " sessions for user: " + mUserId + "..."); |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 164 | for (Session session : mSessions.values()) { |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 165 | try { |
| 166 | mActiveConnection.startSessionLocked(session); |
| 167 | } catch (IllegalStateException | ExternalStorageServiceException e) { |
| 168 | // TODO: Don't crash process? We could get into process crash loop |
| 169 | Slog.e(TAG, "Failed to start " + session, e); |
| 170 | } |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 175 | /** |
| 176 | * Closes the connection to the {@link ExternalStorageService}. The connection will typically |
| 177 | * be restarted after close. |
| 178 | */ |
| 179 | public void close() { |
| 180 | mActiveConnection.close(); |
| 181 | } |
| 182 | |
| 183 | /** Throws an {@link IllegalArgumentException} if {@code path} is not ready for access */ |
| 184 | public void checkPathReady(String path) { |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 185 | synchronized (mLock) { |
| 186 | for (Session session : mSessions.values()) { |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 187 | if (session.upperPath != null && path.startsWith(session.upperPath)) { |
| 188 | if (mActiveConnection.isActiveLocked(session)) { |
| 189 | return; |
| 190 | } |
| 191 | } |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 192 | } |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 193 | throw new IllegalStateException("Path not ready " + path); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /** Returns all created sessions. */ |
| 198 | public Set<String> getAllSessionIds() { |
| 199 | synchronized (mLock) { |
| 200 | return new HashSet<>(mSessions.keySet()); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | private void prepareRemote() throws ExternalStorageServiceException { |
| 205 | try { |
| 206 | waitForLatch(mActiveConnection.bind(), "remote_prepare_user " + mUserId); |
| 207 | } catch (IllegalStateException | TimeoutException e) { |
| 208 | throw new ExternalStorageServiceException("Failed to prepare remote", e); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | private void waitForLatch(CountDownLatch latch, String reason) throws TimeoutException { |
| 213 | try { |
| 214 | if (!latch.await(REMOTE_TIMEOUT_SECONDS, TimeUnit.SECONDS)) { |
| 215 | // TODO(b/140025078): Call ActivityManager ANR API? |
| 216 | throw new TimeoutException("Latch wait for " + reason + " elapsed"); |
| 217 | } |
| 218 | } catch (InterruptedException e) { |
| 219 | Thread.currentThread().interrupt(); |
| 220 | throw new IllegalStateException("Latch wait for " + reason + " interrupted"); |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
| 224 | private final class ActiveConnection implements AutoCloseable { |
| 225 | // Lifecycle connection to the external storage service, needed to unbind. |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 226 | @GuardedBy("mLock") @Nullable private ServiceConnection mServiceConnection; |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 227 | // True if we are connecting, either bound or binding |
| 228 | // False && mRemote != null means we are connected |
| 229 | // False && mRemote == null means we are neither connecting nor connected |
| 230 | @GuardedBy("mLock") @Nullable private boolean mIsConnecting; |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 231 | // Binder object representing the external storage service. |
| 232 | // Non-null indicates we are connected |
| 233 | @GuardedBy("mLock") @Nullable private IExternalStorageService mRemote; |
| 234 | // Exception, if any, thrown from #startSessionLocked or #endSessionLocked |
| 235 | // Local variables cannot be referenced from a lambda expression :( so we |
| 236 | // save the exception received in the callback here. Since we guard access |
| 237 | // (and clear the exception state) with the same lock which we hold during |
| 238 | // the entire transaction, there is no risk of race. |
| 239 | @GuardedBy("mLock") @Nullable private ParcelableException mLastException; |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 240 | // Not guarded by any lock intentionally and non final because we cannot |
| 241 | // reset latches so need to create a new one after one use |
| 242 | private CountDownLatch mLatch; |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 243 | |
| 244 | @Override |
| 245 | public void close() { |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 246 | ServiceConnection oldConnection = null; |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 247 | synchronized (mLock) { |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 248 | Slog.i(TAG, "Closing connection for user " + mUserId); |
| 249 | mIsConnecting = false; |
| 250 | oldConnection = mServiceConnection; |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 251 | mServiceConnection = null; |
| 252 | mRemote = null; |
| 253 | } |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 254 | |
| 255 | if (oldConnection != null) { |
| 256 | mContext.unbindService(oldConnection); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | public boolean isActiveLocked(Session session) { |
| 261 | if (!session.isInitialisedLocked()) { |
| 262 | Slog.i(TAG, "Session not initialised " + session); |
| 263 | return false; |
| 264 | } |
| 265 | |
| 266 | if (mRemote == null) { |
| 267 | throw new IllegalStateException("Valid session with inactive connection"); |
| 268 | } |
| 269 | return true; |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | public void startSessionLocked(Session session) throws ExternalStorageServiceException { |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 273 | if (!isActiveLocked(session)) { |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 274 | return; |
| 275 | } |
| 276 | |
| 277 | CountDownLatch latch = new CountDownLatch(1); |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 278 | try (ParcelFileDescriptor dupedPfd = session.pfd.dup()) { |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 279 | mRemote.startSession(session.sessionId, |
| 280 | FLAG_SESSION_TYPE_FUSE | FLAG_SESSION_ATTRIBUTE_INDEXABLE, |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 281 | dupedPfd, session.upperPath, session.lowerPath, new RemoteCallback(result -> |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 282 | setResultLocked(latch, result))); |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 283 | waitForLatch(latch, "start_session " + session); |
| 284 | maybeThrowExceptionLocked(); |
| 285 | } catch (Exception e) { |
| 286 | throw new ExternalStorageServiceException("Failed to start session: " + session, e); |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 287 | } |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | public void endSessionLocked(Session session) throws ExternalStorageServiceException { |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 291 | session.close(); |
| 292 | if (!isActiveLocked(session)) { |
| 293 | // Nothing to end, not started yet |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 294 | return; |
| 295 | } |
| 296 | |
| 297 | CountDownLatch latch = new CountDownLatch(1); |
| 298 | try { |
| 299 | mRemote.endSession(session.sessionId, new RemoteCallback(result -> |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 300 | setResultLocked(latch, result))); |
| 301 | waitForLatch(latch, "end_session " + session); |
| 302 | maybeThrowExceptionLocked(); |
| 303 | } catch (Exception e) { |
| 304 | throw new ExternalStorageServiceException("Failed to end session: " + session, e); |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 305 | } |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | private void setResultLocked(CountDownLatch latch, Bundle result) { |
| 309 | mLastException = result.getParcelable(EXTRA_ERROR); |
| 310 | latch.countDown(); |
| 311 | } |
| 312 | |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 313 | private void maybeThrowExceptionLocked() throws IOException { |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 314 | if (mLastException != null) { |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 315 | ParcelableException lastException = mLastException; |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 316 | mLastException = null; |
| 317 | try { |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 318 | lastException.maybeRethrow(IOException.class); |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 319 | } catch (IOException e) { |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 320 | throw e; |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 321 | } |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 322 | throw new RuntimeException(lastException); |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 323 | } |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 324 | } |
| 325 | |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 326 | public CountDownLatch bind() throws ExternalStorageServiceException { |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 327 | ComponentName name = mSessionController.getExternalStorageServiceComponentName(); |
| 328 | if (name == null) { |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 329 | // Not ready to bind |
| 330 | throw new ExternalStorageServiceException( |
| 331 | "Not ready to bind to the ExternalStorageService for user " + mUserId); |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 332 | } |
| 333 | |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 334 | synchronized (mLock) { |
| 335 | if (mRemote != null || mIsConnecting) { |
| 336 | // Connected or connecting (bound or binding) |
| 337 | // Will wait on a latch that will countdown when we connect, unless we are |
| 338 | // connected and the latch has already countdown, yay! |
| 339 | return mLatch; |
| 340 | } // else neither connected nor connecting |
| 341 | |
| 342 | mLatch = new CountDownLatch(1); |
| 343 | mIsConnecting = true; |
| 344 | mServiceConnection = new ServiceConnection() { |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 345 | @Override |
| 346 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 347 | Slog.i(TAG, "Service: [" + name + "] connected. User [" + mUserId + "]"); |
| 348 | handleConnection(service); |
| 349 | } |
| 350 | |
| 351 | @Override |
| 352 | @MainThread |
| 353 | public void onServiceDisconnected(ComponentName name) { |
| 354 | // Service crashed or process was killed, #onServiceConnected will be called |
| 355 | // Don't need to re-bind. |
| 356 | Slog.i(TAG, "Service: [" + name + "] disconnected. User [" + mUserId + "]"); |
| 357 | handleDisconnection(); |
| 358 | } |
| 359 | |
| 360 | @Override |
| 361 | public void onBindingDied(ComponentName name) { |
| 362 | // Application hosting service probably got updated |
| 363 | // Need to re-bind. |
| 364 | Slog.i(TAG, "Service: [" + name + "] died. User [" + mUserId + "]"); |
| 365 | handleDisconnection(); |
| 366 | } |
| 367 | |
| 368 | @Override |
| 369 | public void onNullBinding(ComponentName name) { |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 370 | Slog.wtf(TAG, "Service: [" + name + "] is null. User [" + mUserId + "]"); |
| 371 | } |
| 372 | |
| 373 | private void handleConnection(IBinder service) { |
| 374 | synchronized (mLock) { |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 375 | if (mIsConnecting) { |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 376 | mRemote = IExternalStorageService.Stub.asInterface(service); |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 377 | mIsConnecting = false; |
| 378 | mLatch.countDown(); |
| 379 | // Separate thread so we don't block the main thead |
| 380 | return; |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 381 | } |
| 382 | } |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 383 | Slog.wtf(TAG, "Connection closed to the ExternalStorageService for user " |
| 384 | + mUserId); |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | private void handleDisconnection() { |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 388 | // Clear all sessions because we will need a new device fd since |
| 389 | // StorageManagerService will reset the device mount state and #startSession |
| 390 | // will be called for any required mounts. |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 391 | // Notify StorageManagerService so it can restart all necessary sessions |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 392 | close(); |
| 393 | new Thread(StorageUserConnection.this::startAllSessions).start(); |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 394 | } |
| 395 | }; |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 396 | } |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 397 | |
| 398 | Slog.i(TAG, "Binding to the ExternalStorageService for user " + mUserId); |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 399 | if (mContext.bindServiceAsUser(new Intent().setComponent(name), mServiceConnection, |
| 400 | Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT, |
| 401 | UserHandle.of(mUserId))) { |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 402 | Slog.i(TAG, "Bound to the ExternalStorageService for user " + mUserId); |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 403 | return mLatch; |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 404 | } else { |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 405 | synchronized (mLock) { |
| 406 | mIsConnecting = false; |
| 407 | } |
| 408 | throw new ExternalStorageServiceException( |
| 409 | "Failed to bind to the ExternalStorageService for user " + mUserId); |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 414 | private static final class Session implements AutoCloseable { |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 415 | public final String sessionId; |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 416 | public final ParcelFileDescriptor pfd; |
Zim | 95eca1d | 2019-11-15 18:03:00 +0000 | [diff] [blame] | 417 | public final String lowerPath; |
| 418 | public final String upperPath; |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 419 | |
Zim | 95eca1d | 2019-11-15 18:03:00 +0000 | [diff] [blame] | 420 | Session(String sessionId, ParcelFileDescriptor pfd, String upperPath, String lowerPath) { |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 421 | this.sessionId = sessionId; |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 422 | this.pfd = pfd; |
Zim | 95eca1d | 2019-11-15 18:03:00 +0000 | [diff] [blame] | 423 | this.upperPath = upperPath; |
| 424 | this.lowerPath = lowerPath; |
Zim | 17be6f9 | 2019-09-25 14:37:55 +0100 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | @Override |
| 428 | public void close() { |
| 429 | try { |
| 430 | pfd.close(); |
| 431 | } catch (IOException e) { |
| 432 | Slog.i(TAG, "Failed to close session: " + this); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | @Override |
| 437 | public String toString() { |
| 438 | return "[SessionId: " + sessionId + ". UpperPath: " + upperPath + ". LowerPath: " |
| 439 | + lowerPath + "]"; |
| 440 | } |
| 441 | |
| 442 | @GuardedBy("mLock") |
| 443 | public boolean isInitialisedLocked() { |
| 444 | return !TextUtils.isEmpty(upperPath) && !TextUtils.isEmpty(lowerPath); |
Zim | 42f1e9f | 2019-08-15 17:35:00 +0100 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | } |