Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
| 19 | import android.os.Binder; |
Dianne Hackborn | d9137ca | 2012-05-30 15:29:36 -0700 | [diff] [blame] | 20 | import android.os.SystemClock; |
Dianne Hackborn | 2aec55a | 2018-06-26 10:35:35 -0700 | [diff] [blame] | 21 | import android.util.Slog; |
Dianne Hackborn | d9137ca | 2012-05-30 15:29:36 -0700 | [diff] [blame] | 22 | import android.util.TimeUtils; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 23 | |
Dianne Hackborn | 2aec55a | 2018-06-26 10:35:35 -0700 | [diff] [blame] | 24 | import com.android.internal.app.procstats.AssociationState; |
| 25 | import com.android.internal.app.procstats.ProcessStats; |
| 26 | |
| 27 | import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM; |
| 28 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 29 | /** |
| 30 | * Represents a link between a content provider and client. |
| 31 | */ |
Dianne Hackborn | be4e6aa | 2013-06-07 13:25:29 -0700 | [diff] [blame] | 32 | public final class ContentProviderConnection extends Binder { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 33 | public final ContentProviderRecord provider; |
| 34 | public final ProcessRecord client; |
Dianne Hackborn | 24bbe58 | 2018-12-17 11:58:31 -0800 | [diff] [blame] | 35 | public final String clientPackage; |
Dianne Hackborn | 2aec55a | 2018-06-26 10:35:35 -0700 | [diff] [blame] | 36 | public AssociationState.SourceState association; |
Dianne Hackborn | d9137ca | 2012-05-30 15:29:36 -0700 | [diff] [blame] | 37 | public final long createTime; |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 38 | public int stableCount; |
| 39 | public int unstableCount; |
| 40 | // The client of this connection is currently waiting for the provider to appear. |
| 41 | // Protected by the provider lock. |
| 42 | public boolean waiting; |
| 43 | // The provider of this connection is now dead. |
| 44 | public boolean dead; |
| 45 | |
| 46 | // For debugging. |
| 47 | public int numStableIncs; |
| 48 | public int numUnstableIncs; |
| 49 | |
Dianne Hackborn | 24bbe58 | 2018-12-17 11:58:31 -0800 | [diff] [blame] | 50 | public ContentProviderConnection(ContentProviderRecord _provider, ProcessRecord _client, |
| 51 | String _clientPackage) { |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 52 | provider = _provider; |
| 53 | client = _client; |
Dianne Hackborn | 24bbe58 | 2018-12-17 11:58:31 -0800 | [diff] [blame] | 54 | clientPackage = _clientPackage; |
Dianne Hackborn | d9137ca | 2012-05-30 15:29:36 -0700 | [diff] [blame] | 55 | createTime = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Dianne Hackborn | 2aec55a | 2018-06-26 10:35:35 -0700 | [diff] [blame] | 58 | public void startAssociationIfNeeded() { |
Dianne Hackborn | 95031ef | 2018-07-09 09:09:05 -0700 | [diff] [blame] | 59 | // If we don't already have an active association, create one... but only if this |
| 60 | // is an association between two different processes. |
Dianne Hackborn | 2fd8ce4 | 2018-07-12 14:51:54 -0700 | [diff] [blame] | 61 | if (ActivityManagerService.TRACK_PROCSTATS_ASSOCIATIONS |
| 62 | && association == null && provider.proc != null |
| 63 | && (provider.appInfo.uid != client.uid |
| 64 | || !provider.info.processName.equals(client.processName))) { |
| 65 | ProcessStats.ProcessStateHolder holder = provider.proc.pkgList.get( |
| 66 | provider.name.getPackageName()); |
Dianne Hackborn | 2aec55a | 2018-06-26 10:35:35 -0700 | [diff] [blame] | 67 | if (holder == null) { |
| 68 | Slog.wtf(TAG_AM, "No package in referenced provider " |
| 69 | + provider.name.toShortString() + ": proc=" + provider.proc); |
| 70 | } else if (holder.pkg == null) { |
| 71 | Slog.wtf(TAG_AM, "Inactive holder in referenced provider " |
| 72 | + provider.name.toShortString() + ": proc=" + provider.proc); |
| 73 | } else { |
Dianne Hackborn | 95031ef | 2018-07-09 09:09:05 -0700 | [diff] [blame] | 74 | association = holder.pkg.getAssociationStateLocked(holder.state, |
Dianne Hackborn | 24bbe58 | 2018-12-17 11:58:31 -0800 | [diff] [blame] | 75 | provider.name.getClassName()).startSource(client.uid, client.processName, |
| 76 | clientPackage); |
Dianne Hackborn | 2aec55a | 2018-06-26 10:35:35 -0700 | [diff] [blame] | 77 | |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
Dianne Hackborn | 95031ef | 2018-07-09 09:09:05 -0700 | [diff] [blame] | 82 | public void trackProcState(int procState, int seq, long now) { |
| 83 | if (association != null) { |
| 84 | association.trackProcState(procState, seq, now); |
| 85 | } |
| 86 | } |
| 87 | |
Dianne Hackborn | 2aec55a | 2018-06-26 10:35:35 -0700 | [diff] [blame] | 88 | public void stopAssociation() { |
| 89 | if (association != null) { |
| 90 | association.stop(); |
| 91 | association = null; |
| 92 | } |
| 93 | } |
| 94 | |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 95 | public String toString() { |
| 96 | StringBuilder sb = new StringBuilder(128); |
| 97 | sb.append("ContentProviderConnection{"); |
| 98 | toShortString(sb); |
| 99 | sb.append('}'); |
| 100 | return sb.toString(); |
| 101 | } |
| 102 | |
| 103 | public String toShortString() { |
| 104 | StringBuilder sb = new StringBuilder(128); |
| 105 | toShortString(sb); |
| 106 | return sb.toString(); |
| 107 | } |
| 108 | |
| 109 | public String toClientString() { |
| 110 | StringBuilder sb = new StringBuilder(128); |
| 111 | toClientString(sb); |
| 112 | return sb.toString(); |
| 113 | } |
| 114 | |
| 115 | public void toShortString(StringBuilder sb) { |
| 116 | sb.append(provider.toShortString()); |
| 117 | sb.append("->"); |
| 118 | toClientString(sb); |
| 119 | } |
| 120 | |
| 121 | public void toClientString(StringBuilder sb) { |
| 122 | sb.append(client.toShortString()); |
| 123 | sb.append(" s"); |
| 124 | sb.append(stableCount); |
| 125 | sb.append("/"); |
| 126 | sb.append(numStableIncs); |
| 127 | sb.append(" u"); |
| 128 | sb.append(unstableCount); |
| 129 | sb.append("/"); |
| 130 | sb.append(numUnstableIncs); |
| 131 | if (waiting) { |
| 132 | sb.append(" WAITING"); |
| 133 | } |
| 134 | if (dead) { |
| 135 | sb.append(" DEAD"); |
| 136 | } |
Dianne Hackborn | d9137ca | 2012-05-30 15:29:36 -0700 | [diff] [blame] | 137 | long nowReal = SystemClock.elapsedRealtime(); |
| 138 | sb.append(" "); |
| 139 | TimeUtils.formatDuration(nowReal-createTime, sb); |
Dianne Hackborn | 6ae8d18 | 2012-05-23 13:12:42 -0700 | [diff] [blame] | 140 | } |
| 141 | } |