blob: 27e2a6e03ae5c0f7b9830946fbb4fe35319efe5f [file] [log] [blame]
Todd Kennedyb8a279e2015-11-18 09:59:47 -08001/*
2 * Copyright (C) 2015 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
17package com.android.server.pm;
18
Todd Kennedy31b95e02016-08-04 14:27:15 -070019import android.app.EphemeralResolverService;
20import android.app.IEphemeralResolver;
Todd Kennedyb8a279e2015-11-18 09:59:47 -080021import android.content.ComponentName;
22import android.content.Context;
23import android.content.Intent;
24import android.content.ServiceConnection;
Todd Kennedy7440f172015-12-09 14:31:22 -080025import android.content.pm.EphemeralResolveInfo;
Todd Kennedyb8a279e2015-11-18 09:59:47 -080026import android.os.Build;
27import android.os.Bundle;
28import android.os.IBinder;
29import android.os.IRemoteCallback;
30import android.os.RemoteException;
31import android.os.SystemClock;
32import android.os.UserHandle;
33import android.util.TimedRemoteCaller;
34
Jeff Sharkeyc06f1842016-11-09 12:25:44 -070035import com.android.internal.os.TransferPipe;
36
Todd Kennedyb8a279e2015-11-18 09:59:47 -080037import java.io.FileDescriptor;
Jeff Sharkeyc06f1842016-11-09 12:25:44 -070038import java.io.IOException;
Todd Kennedyb8a279e2015-11-18 09:59:47 -080039import java.io.PrintWriter;
40import java.util.ArrayList;
41import java.util.List;
42import java.util.concurrent.TimeoutException;
43
44/**
45 * Represents a remote ephemeral resolver. It is responsible for binding to the remote
46 * service and handling all interactions in a timely manner.
47 * @hide
48 */
49final class EphemeralResolverConnection {
50 // This is running in a critical section and the timeout must be sufficiently low
51 private static final long BIND_SERVICE_TIMEOUT_MS =
52 ("eng".equals(Build.TYPE)) ? 300 : 200;
53
54 private final Object mLock = new Object();
55 private final GetEphemeralResolveInfoCaller mGetEphemeralResolveInfoCaller =
56 new GetEphemeralResolveInfoCaller();
57 private final ServiceConnection mServiceConnection = new MyServiceConnection();
58 private final Context mContext;
59 /** Intent used to bind to the service */
60 private final Intent mIntent;
61
Jeff Sharkey7765d732016-10-26 16:58:49 -060062 private volatile boolean mBindRequested;
Todd Kennedyb8a279e2015-11-18 09:59:47 -080063 private IEphemeralResolver mRemoteInstance;
64
65 public EphemeralResolverConnection(Context context, ComponentName componentName) {
66 mContext = context;
Todd Kennedy7d59cb82016-09-08 13:49:05 -070067 mIntent = new Intent(Intent.ACTION_RESOLVE_EPHEMERAL_PACKAGE).setComponent(componentName);
Todd Kennedyb8a279e2015-11-18 09:59:47 -080068 }
69
Todd Kennedya97045b2016-06-10 16:42:00 -070070 public final List<EphemeralResolveInfo> getEphemeralResolveInfoList(
71 int hashPrefix[], int prefixMask) {
Todd Kennedyb8a279e2015-11-18 09:59:47 -080072 throwIfCalledOnMainThread();
73 try {
74 return mGetEphemeralResolveInfoCaller.getEphemeralResolveInfoList(
Todd Kennedya97045b2016-06-10 16:42:00 -070075 getRemoteInstanceLazy(), hashPrefix, prefixMask);
Todd Kennedyb8a279e2015-11-18 09:59:47 -080076 } catch (RemoteException re) {
77 } catch (TimeoutException te) {
78 } finally {
79 synchronized (mLock) {
80 mLock.notifyAll();
81 }
82 }
83 return null;
84 }
85
86 public void dump(FileDescriptor fd, PrintWriter pw, String prefix) {
87 synchronized (mLock) {
88 pw.append(prefix).append("bound=")
89 .append((mRemoteInstance != null) ? "true" : "false").println();
90
91 pw.flush();
Todd Kennedyb8a279e2015-11-18 09:59:47 -080092 try {
Jeff Sharkeyc06f1842016-11-09 12:25:44 -070093 TransferPipe.dumpAsync(getRemoteInstanceLazy().asBinder(), fd,
94 new String[] { prefix });
95 } catch (IOException | TimeoutException | RemoteException e) {
96 pw.println("Failed to dump remote instance: " + e);
Todd Kennedyb8a279e2015-11-18 09:59:47 -080097 }
98 }
99 }
100
101 private IEphemeralResolver getRemoteInstanceLazy() throws TimeoutException {
102 synchronized (mLock) {
103 if (mRemoteInstance != null) {
104 return mRemoteInstance;
105 }
106 bindLocked();
107 return mRemoteInstance;
108 }
109 }
110
111 private void bindLocked() throws TimeoutException {
112 if (mRemoteInstance != null) {
113 return;
114 }
115
Jeff Sharkey7765d732016-10-26 16:58:49 -0600116 if (!mBindRequested) {
117 mBindRequested = true;
118 mContext.bindServiceAsUser(mIntent, mServiceConnection,
119 Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE, UserHandle.SYSTEM);
120 }
Todd Kennedyb8a279e2015-11-18 09:59:47 -0800121
122 final long startMillis = SystemClock.uptimeMillis();
123 while (true) {
124 if (mRemoteInstance != null) {
125 break;
126 }
127 final long elapsedMillis = SystemClock.uptimeMillis() - startMillis;
128 final long remainingMillis = BIND_SERVICE_TIMEOUT_MS - elapsedMillis;
129 if (remainingMillis <= 0) {
130 throw new TimeoutException("Didn't bind to resolver in time.");
131 }
132 try {
133 mLock.wait(remainingMillis);
134 } catch (InterruptedException ie) {
135 /* ignore */
136 }
137 }
138
139 mLock.notifyAll();
140 }
141
142 private void throwIfCalledOnMainThread() {
143 if (Thread.currentThread() == mContext.getMainLooper().getThread()) {
144 throw new RuntimeException("Cannot invoke on the main thread");
145 }
146 }
147
148 private final class MyServiceConnection implements ServiceConnection {
149 @Override
150 public void onServiceConnected(ComponentName name, IBinder service) {
151 synchronized (mLock) {
152 mRemoteInstance = IEphemeralResolver.Stub.asInterface(service);
153 mLock.notifyAll();
154 }
155 }
156
157 @Override
158 public void onServiceDisconnected(ComponentName name) {
159 synchronized (mLock) {
160 mRemoteInstance = null;
161 }
162 }
163 }
164
165 private static final class GetEphemeralResolveInfoCaller
166 extends TimedRemoteCaller<List<EphemeralResolveInfo>> {
167 private final IRemoteCallback mCallback;
168
169 public GetEphemeralResolveInfoCaller() {
170 super(TimedRemoteCaller.DEFAULT_CALL_TIMEOUT_MILLIS);
171 mCallback = new IRemoteCallback.Stub() {
172 @Override
173 public void sendResult(Bundle data) throws RemoteException {
174 final ArrayList<EphemeralResolveInfo> resolveList =
175 data.getParcelableArrayList(
176 EphemeralResolverService.EXTRA_RESOLVE_INFO);
177 int sequence =
178 data.getInt(EphemeralResolverService.EXTRA_SEQUENCE, -1);
179 onRemoteMethodResult(resolveList, sequence);
180 }
181 };
182 }
183
184 public List<EphemeralResolveInfo> getEphemeralResolveInfoList(
Todd Kennedya97045b2016-06-10 16:42:00 -0700185 IEphemeralResolver target, int hashPrefix[], int prefixMask)
Todd Kennedyb8a279e2015-11-18 09:59:47 -0800186 throws RemoteException, TimeoutException {
187 final int sequence = onBeforeRemoteCall();
Todd Kennedya97045b2016-06-10 16:42:00 -0700188 target.getEphemeralResolveInfoList(mCallback, hashPrefix, prefixMask, sequence);
Todd Kennedyb8a279e2015-11-18 09:59:47 -0800189 return getResultTimed(sequence);
190 }
191 }
192}