Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.pm; |
| 18 | |
Todd Kennedy | 31b95e0 | 2016-08-04 14:27:15 -0700 | [diff] [blame] | 19 | import android.app.EphemeralResolverService; |
| 20 | import android.app.IEphemeralResolver; |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 21 | import android.content.ComponentName; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.content.ServiceConnection; |
Todd Kennedy | 7440f17 | 2015-12-09 14:31:22 -0800 | [diff] [blame] | 25 | import android.content.pm.EphemeralResolveInfo; |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 26 | import android.os.Build; |
| 27 | import android.os.Bundle; |
| 28 | import android.os.IBinder; |
| 29 | import android.os.IRemoteCallback; |
| 30 | import android.os.RemoteException; |
| 31 | import android.os.SystemClock; |
| 32 | import android.os.UserHandle; |
| 33 | import android.util.TimedRemoteCaller; |
| 34 | |
Jeff Sharkey | c06f184 | 2016-11-09 12:25:44 -0700 | [diff] [blame] | 35 | import com.android.internal.os.TransferPipe; |
| 36 | |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 37 | import java.io.FileDescriptor; |
Jeff Sharkey | c06f184 | 2016-11-09 12:25:44 -0700 | [diff] [blame] | 38 | import java.io.IOException; |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 39 | import java.io.PrintWriter; |
| 40 | import java.util.ArrayList; |
| 41 | import java.util.List; |
| 42 | import 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 | */ |
| 49 | final 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 Sharkey | 7765d73 | 2016-10-26 16:58:49 -0600 | [diff] [blame] | 62 | private volatile boolean mBindRequested; |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 63 | private IEphemeralResolver mRemoteInstance; |
| 64 | |
| 65 | public EphemeralResolverConnection(Context context, ComponentName componentName) { |
| 66 | mContext = context; |
Todd Kennedy | 7d59cb8 | 2016-09-08 13:49:05 -0700 | [diff] [blame] | 67 | mIntent = new Intent(Intent.ACTION_RESOLVE_EPHEMERAL_PACKAGE).setComponent(componentName); |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Todd Kennedy | a97045b | 2016-06-10 16:42:00 -0700 | [diff] [blame] | 70 | public final List<EphemeralResolveInfo> getEphemeralResolveInfoList( |
| 71 | int hashPrefix[], int prefixMask) { |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 72 | throwIfCalledOnMainThread(); |
| 73 | try { |
| 74 | return mGetEphemeralResolveInfoCaller.getEphemeralResolveInfoList( |
Todd Kennedy | a97045b | 2016-06-10 16:42:00 -0700 | [diff] [blame] | 75 | getRemoteInstanceLazy(), hashPrefix, prefixMask); |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 76 | } 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 Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 92 | try { |
Jeff Sharkey | c06f184 | 2016-11-09 12:25:44 -0700 | [diff] [blame] | 93 | 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 Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 97 | } |
| 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 Sharkey | 7765d73 | 2016-10-26 16:58:49 -0600 | [diff] [blame] | 116 | if (!mBindRequested) { |
| 117 | mBindRequested = true; |
| 118 | mContext.bindServiceAsUser(mIntent, mServiceConnection, |
| 119 | Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE, UserHandle.SYSTEM); |
| 120 | } |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 121 | |
| 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 Kennedy | a97045b | 2016-06-10 16:42:00 -0700 | [diff] [blame] | 185 | IEphemeralResolver target, int hashPrefix[], int prefixMask) |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 186 | throws RemoteException, TimeoutException { |
| 187 | final int sequence = onBeforeRemoteCall(); |
Todd Kennedy | a97045b | 2016-06-10 16:42:00 -0700 | [diff] [blame] | 188 | target.getEphemeralResolveInfoList(mCallback, hashPrefix, prefixMask, sequence); |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 189 | return getResultTimed(sequence); |
| 190 | } |
| 191 | } |
| 192 | } |