Merge "[incfs] Cleanup data loader connection and fds in Java" into rvc-dev

This commit is contained in:
Yurii Zubrytskyi
2020-04-22 22:36:30 +00:00
committed by Android (Google) Code Review

View File

@@ -38,6 +38,8 @@ import android.util.SparseArray;
import com.android.internal.annotations.GuardedBy; import com.android.internal.annotations.GuardedBy;
import com.android.server.SystemService; import com.android.server.SystemService;
import libcore.io.IoUtils;
import java.util.List; import java.util.List;
/** /**
@@ -64,31 +66,52 @@ public class DataLoaderManagerService extends SystemService {
publishBinderService(Context.DATA_LOADER_MANAGER_SERVICE, mBinderService); publishBinderService(Context.DATA_LOADER_MANAGER_SERVICE, mBinderService);
} }
private static void closeQuietly(FileSystemControlParcel control) {
if (control == null || control.incremental == null) {
return;
}
IoUtils.closeQuietly(control.incremental.cmd);
IoUtils.closeQuietly(control.incremental.pendingReads);
IoUtils.closeQuietly(control.incremental.log);
}
final class DataLoaderManagerBinderService extends IDataLoaderManager.Stub { final class DataLoaderManagerBinderService extends IDataLoaderManager.Stub {
@Override @Override
public boolean initializeDataLoader(int dataLoaderId, DataLoaderParamsParcel params, public boolean initializeDataLoader(int dataLoaderId, DataLoaderParamsParcel params,
FileSystemControlParcel control, IDataLoaderStatusListener listener) { FileSystemControlParcel control, IDataLoaderStatusListener listener) {
synchronized (mLock) { DataLoaderServiceConnection connection = null;
if (mServiceConnections.get(dataLoaderId) != null) { try {
Slog.e(TAG, "Data loader of ID=" + dataLoaderId + " already exists."); synchronized (mLock) {
if (mServiceConnections.get(dataLoaderId) != null) {
Slog.e(TAG, "Data loader of ID=" + dataLoaderId + " already exists.");
return false;
}
}
ComponentName componentName =
new ComponentName(params.packageName, params.className);
ComponentName dataLoaderComponent = resolveDataLoaderComponentName(componentName);
if (dataLoaderComponent == null) {
return false; return false;
} }
} // Binds to the specific data loader service
ComponentName componentName = new ComponentName(params.packageName, params.className); connection =
ComponentName dataLoaderComponent = resolveDataLoaderComponentName(componentName); new DataLoaderServiceConnection(dataLoaderId, params,
if (dataLoaderComponent == null) { control, listener);
return false; control = null; // now connection manages it
} Intent intent = new Intent();
// Binds to the specific data loader service intent.setComponent(dataLoaderComponent);
DataLoaderServiceConnection connection = if (!mContext.bindServiceAsUser(intent, connection, Context.BIND_AUTO_CREATE,
new DataLoaderServiceConnection(dataLoaderId, params, control, listener); UserHandle.of(UserHandle.getCallingUserId()))) {
Intent intent = new Intent(); Slog.e(TAG, "Failed to bind to data loader binder service.");
intent.setComponent(dataLoaderComponent); mContext.unbindService(connection);
if (!mContext.bindServiceAsUser(intent, connection, Context.BIND_AUTO_CREATE, return false;
UserHandle.of(UserHandle.getCallingUserId()))) { }
Slog.e(TAG, "Failed to bind to data loader binder service."); connection = null;
mContext.unbindService(connection); } finally {
return false; DataLoaderManagerService.closeQuietly(control);
if (connection != null) {
connection.close();
}
} }
return true; return true;
} }
@@ -173,7 +196,7 @@ public class DataLoaderManagerService extends SystemService {
} }
} }
class DataLoaderServiceConnection implements ServiceConnection { class DataLoaderServiceConnection implements ServiceConnection, AutoCloseable {
final int mId; final int mId;
final DataLoaderParamsParcel mParams; final DataLoaderParamsParcel mParams;
final FileSystemControlParcel mControl; final FileSystemControlParcel mControl;
@@ -204,15 +227,34 @@ public class DataLoaderManagerService extends SystemService {
@Override @Override
public void onServiceDisconnected(ComponentName arg0) { public void onServiceDisconnected(ComponentName arg0) {
if (mListener != null) { Slog.i(TAG, "DataLoader " + mId + " disconnected, but will try to recover");
try { callListener(IDataLoaderStatusListener.DATA_LOADER_DESTROYED);
mListener.onStatusChanged(mId, IDataLoaderStatusListener.DATA_LOADER_DESTROYED);
} catch (RemoteException ignored) {
}
}
remove(); remove();
} }
@Override
public void onBindingDied(ComponentName name) {
Slog.i(TAG, "DataLoader " + mId + " died");
callListener(IDataLoaderStatusListener.DATA_LOADER_DESTROYED);
mContext.unbindService(this);
close();
remove();
}
@Override
public void onNullBinding(ComponentName name) {
Slog.i(TAG, "DataLoader " + mId + " failed to start");
callListener(IDataLoaderStatusListener.DATA_LOADER_DESTROYED);
mContext.unbindService(this);
close();
remove();
}
@Override
public void close() {
DataLoaderManagerService.closeQuietly(mControl);
}
IDataLoader getDataLoader() { IDataLoader getDataLoader() {
return mDataLoader; return mDataLoader;
} }
@@ -223,6 +265,8 @@ public class DataLoaderManagerService extends SystemService {
} catch (RemoteException ignored) { } catch (RemoteException ignored) {
} }
mContext.unbindService(this); mContext.unbindService(this);
close();
remove();
} }
private void remove() { private void remove() {
@@ -230,5 +274,14 @@ public class DataLoaderManagerService extends SystemService {
mServiceConnections.remove(mId); mServiceConnections.remove(mId);
} }
} }
private void callListener(int status) {
if (mListener != null) {
try {
mListener.onStatusChanged(mId, status);
} catch (RemoteException ignored) {
}
}
}
} }
} }