blob: 5f184c2dabeedbc4af900e792f9c30542bd466c6 [file] [log] [blame]
Dianne Hackborn6ae8d182012-05-23 13:12:42 -07001/*
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
17package com.android.server.am;
18
19import android.os.Binder;
Dianne Hackbornd9137ca2012-05-30 15:29:36 -070020import android.os.SystemClock;
Dianne Hackborn2aec55a2018-06-26 10:35:35 -070021import android.util.Slog;
Dianne Hackbornd9137ca2012-05-30 15:29:36 -070022import android.util.TimeUtils;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070023
Dianne Hackborn2aec55a2018-06-26 10:35:35 -070024import com.android.internal.app.procstats.AssociationState;
25import com.android.internal.app.procstats.ProcessStats;
26
27import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
28
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070029/**
30 * Represents a link between a content provider and client.
31 */
Dianne Hackbornbe4e6aa2013-06-07 13:25:29 -070032public final class ContentProviderConnection extends Binder {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070033 public final ContentProviderRecord provider;
34 public final ProcessRecord client;
Dianne Hackborn24bbe582018-12-17 11:58:31 -080035 public final String clientPackage;
Dianne Hackborn2aec55a2018-06-26 10:35:35 -070036 public AssociationState.SourceState association;
Dianne Hackbornd9137ca2012-05-30 15:29:36 -070037 public final long createTime;
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070038 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 Hackborn24bbe582018-12-17 11:58:31 -080050 public ContentProviderConnection(ContentProviderRecord _provider, ProcessRecord _client,
51 String _clientPackage) {
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070052 provider = _provider;
53 client = _client;
Dianne Hackborn24bbe582018-12-17 11:58:31 -080054 clientPackage = _clientPackage;
Dianne Hackbornd9137ca2012-05-30 15:29:36 -070055 createTime = SystemClock.elapsedRealtime();
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070056 }
57
Dianne Hackborn2aec55a2018-06-26 10:35:35 -070058 public void startAssociationIfNeeded() {
Dianne Hackborn95031ef2018-07-09 09:09:05 -070059 // If we don't already have an active association, create one... but only if this
60 // is an association between two different processes.
Dianne Hackborn2fd8ce42018-07-12 14:51:54 -070061 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 Hackborn2aec55a2018-06-26 10:35:35 -070067 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 Hackborn95031ef2018-07-09 09:09:05 -070074 association = holder.pkg.getAssociationStateLocked(holder.state,
Dianne Hackborn24bbe582018-12-17 11:58:31 -080075 provider.name.getClassName()).startSource(client.uid, client.processName,
76 clientPackage);
Dianne Hackborn2aec55a2018-06-26 10:35:35 -070077
78 }
79 }
80 }
81
Dianne Hackborn95031ef2018-07-09 09:09:05 -070082 public void trackProcState(int procState, int seq, long now) {
83 if (association != null) {
84 association.trackProcState(procState, seq, now);
85 }
86 }
87
Dianne Hackborn2aec55a2018-06-26 10:35:35 -070088 public void stopAssociation() {
89 if (association != null) {
90 association.stop();
91 association = null;
92 }
93 }
94
Dianne Hackborn6ae8d182012-05-23 13:12:42 -070095 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 Hackbornd9137ca2012-05-30 15:29:36 -0700137 long nowReal = SystemClock.elapsedRealtime();
138 sb.append(" ");
139 TimeUtils.formatDuration(nowReal-createTime, sb);
Dianne Hackborn6ae8d182012-05-23 13:12:42 -0700140 }
141}