1/*
2 * Copyright (C) 2017 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 */
16package com.android.server.am;
17
18import android.annotation.NonNull;
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.Intent;
22import android.content.ServiceConnection;
23import android.os.Handler;
24import android.os.IBinder;
25import android.os.SystemClock;
26import android.os.UserHandle;
27import android.util.Slog;
28import android.util.TimeUtils;
29
30import com.android.internal.annotations.GuardedBy;
31import com.android.internal.annotations.VisibleForTesting;
32
33import java.io.PrintWriter;
34
35/**
36 * Connects to a given service component on a given user.
37 *
38 * - Call {@link #bind()} to create a connection.
39 * - Call {@link #unbind()} to disconnect.  Make sure to disconnect when the user stops.
40 *
41 * Add onConnected/onDisconnected callbacks as needed.
42 *
43 * When the target process gets killed (by OOM-killer, etc), then the activity manager will
44 * re-connect the connection automatically, in which case onServiceDisconnected() gets called
45 * and then onServiceConnected().
46 *
47 * However sometimes the activity manager just "kills" the connection -- like when the target
48 * package gets updated or the target process crashes multiple times in a row, in which case
49 * onBindingDied() gets called.  This class handles this case by re-connecting in the time
50 * {@link #mRebindBackoffMs}.  If this happens again, this class increases the back-off time
51 * by {@link #mRebindBackoffIncrease} and retry.  The back-off time is capped at
52 * {@link #mRebindMaxBackoffMs}.
53 *
54 * The back-off time will never be reset until {@link #unbind()} and {@link #bind()} are called
55 * explicitly.
56 *
57 * NOTE: This class does *not* handle package-updates -- i.e. even if the binding dies due to
58 * the target package being updated, this class won't reconnect.  This is because this class doesn't
59 * know what to do when the service component has gone missing, for example.  If the user of this
60 * class wants to restore the connection, then it should call {@link #unbind()} and {@link #bind}
61 * explicitly.
62 */
63public abstract class PersistentConnection<T> {
64    private final Object mLock = new Object();
65
66    private final static boolean DEBUG = false;
67
68    private final String mTag;
69    private final Context mContext;
70    private final Handler mHandler;
71    private final int mUserId;
72    private final ComponentName mComponentName;
73
74    private long mNextBackoffMs;
75
76    private final long mRebindBackoffMs;
77    private final double mRebindBackoffIncrease;
78    private final long mRebindMaxBackoffMs;
79
80    private long mReconnectTime;
81
82    // TODO too many booleans... Should clean up.
83
84    @GuardedBy("mLock")
85    private boolean mBound;
86
87    /**
88     * Whether {@link #bind()} has been called and {@link #unbind()} hasn't been yet; meaning this
89     * is the expected bind state from the caller's point of view.
90     */
91    @GuardedBy("mLock")
92    private boolean mShouldBeBound;
93
94    @GuardedBy("mLock")
95    private boolean mRebindScheduled;
96
97    @GuardedBy("mLock")
98    private boolean mIsConnected;
99
100    @GuardedBy("mLock")
101    private T mService;
102
103    private final ServiceConnection mServiceConnection = new ServiceConnection() {
104        @Override
105        public void onServiceConnected(ComponentName name, IBinder service) {
106            synchronized (mLock) {
107                if (!mBound) {
108                    // Callback came in after PersistentConnection.unbind() was called.
109                    // We just ignore this.
110                    // (We've already called unbindService() already in unbind)
111                    Slog.w(mTag, "Connected: " + mComponentName.flattenToShortString()
112                            + " u" + mUserId + " but not bound, ignore.");
113                    return;
114                }
115                Slog.i(mTag, "Connected: " + mComponentName.flattenToShortString()
116                        + " u" + mUserId);
117
118                mIsConnected = true;
119                mService = asInterface(service);
120            }
121        }
122
123        @Override
124        public void onServiceDisconnected(ComponentName name) {
125            synchronized (mLock) {
126                Slog.i(mTag, "Disconnected: " + mComponentName.flattenToShortString()
127                        + " u" + mUserId);
128
129                cleanUpConnectionLocked();
130            }
131        }
132
133        @Override
134        public void onBindingDied(ComponentName name) {
135            // Activity manager gave up; we'll schedule a re-connect by ourselves.
136            synchronized (mLock) {
137                if (!mBound) {
138                    // Callback came in late?
139                    Slog.w(mTag, "Binding died: " + mComponentName.flattenToShortString()
140                            + " u" + mUserId + " but not bound, ignore.");
141                    return;
142                }
143
144                Slog.w(mTag, "Binding died: " + mComponentName.flattenToShortString()
145                        + " u" + mUserId);
146                scheduleRebindLocked();
147            }
148        }
149    };
150
151    private final Runnable mBindForBackoffRunnable = () -> bindForBackoff();
152
153    public PersistentConnection(@NonNull String tag, @NonNull Context context,
154            @NonNull Handler handler, int userId, @NonNull ComponentName componentName,
155            long rebindBackoffSeconds, double rebindBackoffIncrease, long rebindMaxBackoffSeconds) {
156        mTag = tag;
157        mContext = context;
158        mHandler = handler;
159        mUserId = userId;
160        mComponentName = componentName;
161
162        mRebindBackoffMs = rebindBackoffSeconds * 1000;
163        mRebindBackoffIncrease = rebindBackoffIncrease;
164        mRebindMaxBackoffMs = rebindMaxBackoffSeconds * 1000;
165
166        mNextBackoffMs = mRebindBackoffMs;
167    }
168
169    public final ComponentName getComponentName() {
170        return mComponentName;
171    }
172
173    /**
174     * @return whether {@link #bind()} has been called and {@link #unbind()} hasn't.
175     *
176     * Note when the AM gives up on connection, this class detects it and un-bind automatically,
177     * and schedule rebind, and {@link #isBound} returns false when it's waiting for a retry.
178     */
179    public final boolean isBound() {
180        synchronized (mLock) {
181            return mBound;
182        }
183    }
184
185    /**
186     * @return whether re-bind is scheduled after the AM gives up on a connection.
187     */
188    public final boolean isRebindScheduled() {
189        synchronized (mLock) {
190            return mRebindScheduled;
191        }
192    }
193
194    /**
195     * @return whether connected.
196     */
197    public final boolean isConnected() {
198        synchronized (mLock) {
199            return mIsConnected;
200        }
201    }
202
203    /**
204     * @return the service binder interface.
205     */
206    public final T getServiceBinder() {
207        synchronized (mLock) {
208            return mService;
209        }
210    }
211
212    /**
213     * Connects to the service.
214     */
215    public final void bind() {
216        synchronized (mLock) {
217            mShouldBeBound = true;
218
219            bindInnerLocked(/* resetBackoff= */ true);
220        }
221    }
222
223    @GuardedBy("mLock")
224    public final void bindInnerLocked(boolean resetBackoff) {
225        unscheduleRebindLocked();
226
227        if (mBound) {
228            return;
229        }
230        mBound = true;
231
232        if (resetBackoff) {
233            // Note this is the only place we reset the backoff time.
234            mNextBackoffMs = mRebindBackoffMs;
235        }
236
237        final Intent service = new Intent().setComponent(mComponentName);
238
239        if (DEBUG) {
240            Slog.d(mTag, "Attempting to connect to " + mComponentName);
241        }
242
243        final boolean success = mContext.bindServiceAsUser(service, mServiceConnection,
244                Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE,
245                mHandler, UserHandle.of(mUserId));
246
247        if (!success) {
248            Slog.e(mTag, "Binding: " + service.getComponent() + " u" + mUserId
249                    + " failed.");
250        }
251    }
252
253    final void bindForBackoff() {
254        synchronized (mLock) {
255            if (!mShouldBeBound) {
256                // Race condition -- by the time we got here, unbind() has already been called.
257                return;
258            }
259
260            bindInnerLocked(/* resetBackoff= */ false);
261        }
262    }
263
264    @GuardedBy("mLock")
265    private void cleanUpConnectionLocked() {
266        mIsConnected = false;
267        mService = null;
268    }
269
270    /**
271     * Disconnect from the service.
272     */
273    public final void unbind() {
274        synchronized (mLock) {
275            mShouldBeBound = false;
276
277            unbindLocked();
278        }
279    }
280
281    @GuardedBy("mLock")
282    private final void unbindLocked() {
283        unscheduleRebindLocked();
284
285        if (!mBound) {
286            return;
287        }
288        Slog.i(mTag, "Stopping: " + mComponentName.flattenToShortString() + " u" + mUserId);
289        mBound = false;
290        mContext.unbindService(mServiceConnection);
291
292        cleanUpConnectionLocked();
293    }
294
295    @GuardedBy("mLock")
296    void unscheduleRebindLocked() {
297        injectRemoveCallbacks(mBindForBackoffRunnable);
298        mRebindScheduled = false;
299    }
300
301    @GuardedBy("mLock")
302    void scheduleRebindLocked() {
303        unbindLocked();
304
305        if (!mRebindScheduled) {
306            Slog.i(mTag, "Scheduling to reconnect in " + mNextBackoffMs + " ms (uptime)");
307
308            mReconnectTime = injectUptimeMillis() + mNextBackoffMs;
309
310            injectPostAtTime(mBindForBackoffRunnable, mReconnectTime);
311
312            mNextBackoffMs = Math.min(mRebindMaxBackoffMs,
313                    (long) (mNextBackoffMs * mRebindBackoffIncrease));
314
315            mRebindScheduled = true;
316        }
317    }
318
319    /** Must be implemented by a subclass to convert an {@link IBinder} to a stub. */
320    protected abstract T asInterface(IBinder binder);
321
322    public void dump(String prefix, PrintWriter pw) {
323        synchronized (mLock) {
324            pw.print(prefix);
325            pw.print(mComponentName.flattenToShortString());
326            pw.print(mBound ? "  [bound]" : "  [not bound]");
327            pw.print(mIsConnected ? "  [connected]" : "  [not connected]");
328            if (mRebindScheduled) {
329                pw.print("  reconnect in ");
330                TimeUtils.formatDuration((mReconnectTime - injectUptimeMillis()), pw);
331            }
332            pw.println();
333
334            pw.print(prefix);
335            pw.print("  Next backoff(sec): ");
336            pw.print(mNextBackoffMs / 1000);
337        }
338    }
339
340    @VisibleForTesting
341    void injectRemoveCallbacks(Runnable r) {
342        mHandler.removeCallbacks(r);
343    }
344
345    @VisibleForTesting
346    void injectPostAtTime(Runnable r, long uptimeMillis) {
347        mHandler.postAtTime(r, uptimeMillis);
348    }
349
350    @VisibleForTesting
351    long injectUptimeMillis() {
352        return SystemClock.uptimeMillis();
353    }
354
355    @VisibleForTesting
356    long getNextBackoffMsForTest() {
357        return mNextBackoffMs;
358    }
359
360    @VisibleForTesting
361    long getReconnectTimeForTest() {
362        return mReconnectTime;
363    }
364
365    @VisibleForTesting
366    ServiceConnection getServiceConnectionForTest() {
367        return mServiceConnection;
368    }
369
370    @VisibleForTesting
371    Runnable getBindForBackoffRunnableForTest() {
372        return mBindForBackoffRunnable;
373    }
374
375    @VisibleForTesting
376    boolean shouldBeBoundForTest() {
377        return mShouldBeBound;
378    }
379}
380