The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 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.am; |
| 18 | |
Dianne Hackborn | 2aec55a | 2018-06-26 10:35:35 -0700 | [diff] [blame] | 19 | import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM; |
| 20 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.app.IServiceConnection; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 22 | import android.app.PendingIntent; |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Dianne Hackborn | 2aec55a | 2018-06-26 10:35:35 -0700 | [diff] [blame] | 24 | import android.util.Slog; |
Yi Jin | 6b51414 | 2017-10-30 14:54:12 -0700 | [diff] [blame] | 25 | import android.util.proto.ProtoOutputStream; |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 26 | import android.util.proto.ProtoUtils; |
Yi Jin | 6b51414 | 2017-10-30 14:54:12 -0700 | [diff] [blame] | 27 | |
Dianne Hackborn | 2aec55a | 2018-06-26 10:35:35 -0700 | [diff] [blame] | 28 | import com.android.internal.app.procstats.AssociationState; |
| 29 | import com.android.internal.app.procstats.ProcessStats; |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 30 | import com.android.server.wm.ActivityServiceConnectionsHolder; |
Dianne Hackborn | 2aec55a | 2018-06-26 10:35:35 -0700 | [diff] [blame] | 31 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import java.io.PrintWriter; |
| 33 | |
| 34 | /** |
| 35 | * Description of a single binding to a service. |
| 36 | */ |
Dianne Hackborn | be4e6aa | 2013-06-07 13:25:29 -0700 | [diff] [blame] | 37 | final class ConnectionRecord { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | final AppBindRecord binding; // The application/service binding. |
Wale Ogunwale | c4e63a4 | 2018-10-02 13:19:54 -0700 | [diff] [blame] | 39 | final ActivityServiceConnectionsHolder<ConnectionRecord> activity; // If non-null, the owning activity. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | final IServiceConnection conn; // The client connection. |
| 41 | final int flags; // Binding options. |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 42 | final int clientLabel; // String resource labeling this client. |
| 43 | final PendingIntent clientIntent; // How to launch the client. |
Dianne Hackborn | 2aec55a | 2018-06-26 10:35:35 -0700 | [diff] [blame] | 44 | final int clientUid; // The identity of this connection's client |
| 45 | final String clientProcessName; // The source process of this connection's client |
Dianne Hackborn | 24bbe58 | 2018-12-17 11:58:31 -0800 | [diff] [blame] | 46 | final String clientPackageName; // The source package of this connection's client |
Dianne Hackborn | 2aec55a | 2018-06-26 10:35:35 -0700 | [diff] [blame] | 47 | public AssociationState.SourceState association; // Association tracking |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 48 | String stringName; // Caching of toString. |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 49 | boolean serviceDead; // Well is it? |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 50 | |
| 51 | // Please keep the following two enum list synced. |
Dianne Hackborn | a631d56 | 2018-11-20 15:58:15 -0800 | [diff] [blame] | 52 | private static final int[] BIND_ORIG_ENUMS = new int[] { |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 53 | Context.BIND_AUTO_CREATE, |
| 54 | Context.BIND_DEBUG_UNBIND, |
| 55 | Context.BIND_NOT_FOREGROUND, |
| 56 | Context.BIND_IMPORTANT_BACKGROUND, |
| 57 | Context.BIND_ABOVE_CLIENT, |
| 58 | Context.BIND_ALLOW_OOM_MANAGEMENT, |
| 59 | Context.BIND_WAIVE_PRIORITY, |
| 60 | Context.BIND_IMPORTANT, |
| 61 | Context.BIND_ADJUST_WITH_ACTIVITY, |
| 62 | Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE, |
| 63 | Context.BIND_FOREGROUND_SERVICE, |
| 64 | Context.BIND_TREAT_LIKE_ACTIVITY, |
| 65 | Context.BIND_VISIBLE, |
| 66 | Context.BIND_SHOWING_UI, |
| 67 | Context.BIND_NOT_VISIBLE, |
| 68 | }; |
Dianne Hackborn | a631d56 | 2018-11-20 15:58:15 -0800 | [diff] [blame] | 69 | private static final int[] BIND_PROTO_ENUMS = new int[] { |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 70 | ConnectionRecordProto.AUTO_CREATE, |
| 71 | ConnectionRecordProto.DEBUG_UNBIND, |
| 72 | ConnectionRecordProto.NOT_FG, |
| 73 | ConnectionRecordProto.IMPORTANT_BG, |
| 74 | ConnectionRecordProto.ABOVE_CLIENT, |
| 75 | ConnectionRecordProto.ALLOW_OOM_MANAGEMENT, |
| 76 | ConnectionRecordProto.WAIVE_PRIORITY, |
| 77 | ConnectionRecordProto.IMPORTANT, |
| 78 | ConnectionRecordProto.ADJUST_WITH_ACTIVITY, |
| 79 | ConnectionRecordProto.FG_SERVICE_WHILE_AWAKE, |
| 80 | ConnectionRecordProto.FG_SERVICE, |
| 81 | ConnectionRecordProto.TREAT_LIKE_ACTIVITY, |
| 82 | ConnectionRecordProto.VISIBLE, |
| 83 | ConnectionRecordProto.SHOWING_UI, |
| 84 | ConnectionRecordProto.NOT_VISIBLE, |
| 85 | }; |
| 86 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | void dump(PrintWriter pw, String prefix) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | pw.println(prefix + "binding=" + binding); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 89 | if (activity != null) { |
Wale Ogunwale | c4e63a4 | 2018-10-02 13:19:54 -0700 | [diff] [blame] | 90 | activity.dump(pw, prefix); |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 91 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | pw.println(prefix + "conn=" + conn.asBinder() |
| 93 | + " flags=0x" + Integer.toHexString(flags)); |
| 94 | } |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 95 | |
Wale Ogunwale | c4e63a4 | 2018-10-02 13:19:54 -0700 | [diff] [blame] | 96 | ConnectionRecord(AppBindRecord _binding, |
| 97 | ActivityServiceConnectionsHolder<ConnectionRecord> _activity, |
Dianne Hackborn | 2aec55a | 2018-06-26 10:35:35 -0700 | [diff] [blame] | 98 | IServiceConnection _conn, int _flags, |
| 99 | int _clientLabel, PendingIntent _clientIntent, |
Dianne Hackborn | 24bbe58 | 2018-12-17 11:58:31 -0800 | [diff] [blame] | 100 | int _clientUid, String _clientProcessName, String _clientPackageName) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | binding = _binding; |
| 102 | activity = _activity; |
| 103 | conn = _conn; |
| 104 | flags = _flags; |
Dianne Hackborn | dd9b82c | 2009-09-03 00:18:47 -0700 | [diff] [blame] | 105 | clientLabel = _clientLabel; |
| 106 | clientIntent = _clientIntent; |
Dianne Hackborn | 2aec55a | 2018-06-26 10:35:35 -0700 | [diff] [blame] | 107 | clientUid = _clientUid; |
| 108 | clientProcessName = _clientProcessName; |
Dianne Hackborn | 24bbe58 | 2018-12-17 11:58:31 -0800 | [diff] [blame] | 109 | clientPackageName = _clientPackageName; |
Dianne Hackborn | 2aec55a | 2018-06-26 10:35:35 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | public void startAssociationIfNeeded() { |
Dianne Hackborn | 95031ef | 2018-07-09 09:09:05 -0700 | [diff] [blame] | 113 | // If we don't already have an active association, create one... but only if this |
| 114 | // is an association between two different processes. |
Dianne Hackborn | 2fd8ce4 | 2018-07-12 14:51:54 -0700 | [diff] [blame] | 115 | if (ActivityManagerService.TRACK_PROCSTATS_ASSOCIATIONS |
| 116 | && association == null && binding.service.app != null |
| 117 | && (binding.service.appInfo.uid != clientUid |
| 118 | || !binding.service.processName.equals(clientProcessName))) { |
| 119 | ProcessStats.ProcessStateHolder holder = binding.service.app.pkgList.get( |
Dianne Hackborn | 27b4d94 | 2018-11-12 15:01:40 -0800 | [diff] [blame] | 120 | binding.service.instanceName.getPackageName()); |
Dianne Hackborn | 2aec55a | 2018-06-26 10:35:35 -0700 | [diff] [blame] | 121 | if (holder == null) { |
| 122 | Slog.wtf(TAG_AM, "No package in referenced service " |
Dianne Hackborn | 27b4d94 | 2018-11-12 15:01:40 -0800 | [diff] [blame] | 123 | + binding.service.shortInstanceName + ": proc=" + binding.service.app); |
Dianne Hackborn | 2aec55a | 2018-06-26 10:35:35 -0700 | [diff] [blame] | 124 | } else if (holder.pkg == null) { |
| 125 | Slog.wtf(TAG_AM, "Inactive holder in referenced service " |
Dianne Hackborn | 27b4d94 | 2018-11-12 15:01:40 -0800 | [diff] [blame] | 126 | + binding.service.shortInstanceName + ": proc=" + binding.service.app); |
Dianne Hackborn | 2aec55a | 2018-06-26 10:35:35 -0700 | [diff] [blame] | 127 | } else { |
Dianne Hackborn | 95031ef | 2018-07-09 09:09:05 -0700 | [diff] [blame] | 128 | association = holder.pkg.getAssociationStateLocked(holder.state, |
Dianne Hackborn | 27b4d94 | 2018-11-12 15:01:40 -0800 | [diff] [blame] | 129 | binding.service.instanceName.getClassName()).startSource(clientUid, |
Dianne Hackborn | 24bbe58 | 2018-12-17 11:58:31 -0800 | [diff] [blame] | 130 | clientProcessName, clientPackageName); |
Dianne Hackborn | 2aec55a | 2018-06-26 10:35:35 -0700 | [diff] [blame] | 131 | |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
Dianne Hackborn | 95031ef | 2018-07-09 09:09:05 -0700 | [diff] [blame] | 136 | public void trackProcState(int procState, int seq, long now) { |
| 137 | if (association != null) { |
| 138 | association.trackProcState(procState, seq, now); |
| 139 | } |
| 140 | } |
| 141 | |
Dianne Hackborn | 2aec55a | 2018-06-26 10:35:35 -0700 | [diff] [blame] | 142 | public void stopAssociation() { |
| 143 | if (association != null) { |
| 144 | association.stop(); |
| 145 | association = null; |
| 146 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | public String toString() { |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 150 | if (stringName != null) { |
| 151 | return stringName; |
| 152 | } |
| 153 | StringBuilder sb = new StringBuilder(128); |
| 154 | sb.append("ConnectionRecord{"); |
| 155 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 156 | sb.append(" u"); |
| 157 | sb.append(binding.client.userId); |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 158 | sb.append(' '); |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 159 | if ((flags&Context.BIND_AUTO_CREATE) != 0) { |
| 160 | sb.append("CR "); |
| 161 | } |
| 162 | if ((flags&Context.BIND_DEBUG_UNBIND) != 0) { |
| 163 | sb.append("DBG "); |
| 164 | } |
| 165 | if ((flags&Context.BIND_NOT_FOREGROUND) != 0) { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 166 | sb.append("!FG "); |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 167 | } |
Dianne Hackborn | 83b40f6 | 2017-04-26 13:59:47 -0700 | [diff] [blame] | 168 | if ((flags&Context.BIND_IMPORTANT_BACKGROUND) != 0) { |
| 169 | sb.append("IMPB "); |
| 170 | } |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 171 | if ((flags&Context.BIND_ABOVE_CLIENT) != 0) { |
| 172 | sb.append("ABCLT "); |
| 173 | } |
| 174 | if ((flags&Context.BIND_ALLOW_OOM_MANAGEMENT) != 0) { |
| 175 | sb.append("OOM "); |
| 176 | } |
| 177 | if ((flags&Context.BIND_WAIVE_PRIORITY) != 0) { |
| 178 | sb.append("WPRI "); |
| 179 | } |
| 180 | if ((flags&Context.BIND_IMPORTANT) != 0) { |
| 181 | sb.append("IMP "); |
| 182 | } |
| 183 | if ((flags&Context.BIND_ADJUST_WITH_ACTIVITY) != 0) { |
Dianne Hackborn | d69e4c1 | 2015-04-24 09:54:54 -0700 | [diff] [blame] | 184 | sb.append("WACT "); |
| 185 | } |
| 186 | if ((flags&Context.BIND_FOREGROUND_SERVICE_WHILE_AWAKE) != 0) { |
| 187 | sb.append("FGSA "); |
| 188 | } |
| 189 | if ((flags&Context.BIND_FOREGROUND_SERVICE) != 0) { |
| 190 | sb.append("FGS "); |
| 191 | } |
| 192 | if ((flags&Context.BIND_TREAT_LIKE_ACTIVITY) != 0) { |
| 193 | sb.append("LACT "); |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 194 | } |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 195 | if ((flags&Context.BIND_VISIBLE) != 0) { |
| 196 | sb.append("VIS "); |
Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 197 | } |
Dianne Hackborn | 8585d68 | 2013-07-18 15:39:51 -0700 | [diff] [blame] | 198 | if ((flags&Context.BIND_SHOWING_UI) != 0) { |
| 199 | sb.append("UI "); |
| 200 | } |
| 201 | if ((flags&Context.BIND_NOT_VISIBLE) != 0) { |
| 202 | sb.append("!VIS "); |
| 203 | } |
Dianne Hackborn | 130b0d2 | 2011-07-26 22:07:48 -0700 | [diff] [blame] | 204 | if (serviceDead) { |
| 205 | sb.append("DEAD "); |
| 206 | } |
Dianne Hackborn | 27b4d94 | 2018-11-12 15:01:40 -0800 | [diff] [blame] | 207 | sb.append(binding.service.shortInstanceName); |
Dianne Hackborn | f210d6b | 2009-04-13 18:42:49 -0700 | [diff] [blame] | 208 | sb.append(":@"); |
| 209 | sb.append(Integer.toHexString(System.identityHashCode(conn.asBinder()))); |
| 210 | sb.append('}'); |
| 211 | return stringName = sb.toString(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | } |
Yi Jin | 6b51414 | 2017-10-30 14:54:12 -0700 | [diff] [blame] | 213 | |
| 214 | public void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 215 | if (binding == null) return; // if binding is null, don't write data, something is wrong. |
| 216 | long token = proto.start(fieldId); |
| 217 | proto.write(ConnectionRecordProto.HEX_HASH, |
| 218 | Integer.toHexString(System.identityHashCode(this))); |
| 219 | if (binding.client != null) { |
| 220 | proto.write(ConnectionRecordProto.USER_ID, binding.client.userId); |
| 221 | } |
Yi Jin | 148d7f4 | 2017-11-28 14:23:56 -0800 | [diff] [blame] | 222 | ProtoUtils.writeBitWiseFlagsToProtoEnum(proto, ConnectionRecordProto.FLAGS, |
| 223 | flags, BIND_ORIG_ENUMS, BIND_PROTO_ENUMS); |
Yi Jin | 6b51414 | 2017-10-30 14:54:12 -0700 | [diff] [blame] | 224 | if (serviceDead) { |
| 225 | proto.write(ConnectionRecordProto.FLAGS, ConnectionRecordProto.DEAD); |
| 226 | } |
| 227 | if (binding.service != null) { |
Dianne Hackborn | 27b4d94 | 2018-11-12 15:01:40 -0800 | [diff] [blame] | 228 | proto.write(ConnectionRecordProto.SERVICE_NAME, binding.service.shortInstanceName); |
Yi Jin | 6b51414 | 2017-10-30 14:54:12 -0700 | [diff] [blame] | 229 | } |
Yi Jin | 6b51414 | 2017-10-30 14:54:12 -0700 | [diff] [blame] | 230 | proto.end(token); |
| 231 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | } |