1 /* 2 * Copyright (C) 2019 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.content.ComponentName; 20 21 /** 22 * This class describes various information required to start a process. 23 * 24 * The {@code mHostingType} field describes the reason why we started a process, and 25 * is only used for logging and stats. 26 * 27 * The {@code mHostingName} field describes the Component for which we are starting the 28 * process, and is only used for logging and stats. 29 * 30 * The {@code mHostingZygote} field describes from which Zygote the new process should be spawned. 31 * 32 * {@code mDefiningPackageName} contains the packageName of the package that defines the 33 * component we want to start; this can be different from the packageName and uid in the 34 * ApplicationInfo that we're creating the process with, in case the service is a 35 * {@link android.content.Context#BIND_EXTERNAL_SERVICE} service. In that case, the packageName 36 * and uid in the ApplicationInfo will be set to those of the caller, not of the defining package. 37 * 38 * {@code mDefiningUid} contains the uid of the application that defines the component we want to 39 * start; this can be different from the packageName and uid in the ApplicationInfo that we're 40 * creating the process with, in case the service is a 41 * {@link android.content.Context#BIND_EXTERNAL_SERVICE} service. In that case, the packageName 42 * and uid in the ApplicationInfo will be set to those of the caller, not of the defining package. 43 * 44 */ 45 46 public final class HostingRecord { 47 private static final int REGULAR_ZYGOTE = 0; 48 private static final int WEBVIEW_ZYGOTE = 1; 49 private static final int APP_ZYGOTE = 2; 50 51 private final String mHostingType; 52 private final String mHostingName; 53 private final int mHostingZygote; 54 private final String mDefiningPackageName; 55 private final int mDefiningUid; 56 HostingRecord(String hostingType)57 public HostingRecord(String hostingType) { 58 this(hostingType, null, REGULAR_ZYGOTE, null, -1); 59 } 60 HostingRecord(String hostingType, ComponentName hostingName)61 public HostingRecord(String hostingType, ComponentName hostingName) { 62 this(hostingType, hostingName, REGULAR_ZYGOTE); 63 } 64 HostingRecord(String hostingType, String hostingName)65 public HostingRecord(String hostingType, String hostingName) { 66 this(hostingType, hostingName, REGULAR_ZYGOTE); 67 } 68 HostingRecord(String hostingType, ComponentName hostingName, int hostingZygote)69 private HostingRecord(String hostingType, ComponentName hostingName, int hostingZygote) { 70 this(hostingType, hostingName.toShortString(), hostingZygote); 71 } 72 HostingRecord(String hostingType, String hostingName, int hostingZygote)73 private HostingRecord(String hostingType, String hostingName, int hostingZygote) { 74 this(hostingType, hostingName, hostingZygote, null, -1); 75 } 76 HostingRecord(String hostingType, String hostingName, int hostingZygote, String definingPackageName, int definingUid)77 private HostingRecord(String hostingType, String hostingName, int hostingZygote, 78 String definingPackageName, int definingUid) { 79 mHostingType = hostingType; 80 mHostingName = hostingName; 81 mHostingZygote = hostingZygote; 82 mDefiningPackageName = definingPackageName; 83 mDefiningUid = definingUid; 84 } 85 getType()86 public String getType() { 87 return mHostingType; 88 } 89 getName()90 public String getName() { 91 return mHostingName; 92 } 93 94 /** 95 * Returns the UID of the package defining the component we want to start. Only valid 96 * when {@link #usesAppZygote()} returns true. 97 * 98 * @return the UID of the hosting application 99 */ getDefiningUid()100 public int getDefiningUid() { 101 return mDefiningUid; 102 } 103 104 /** 105 * Returns the packageName of the package defining the component we want to start. Only valid 106 * when {@link #usesAppZygote()} returns true. 107 * 108 * @return the packageName of the hosting application 109 */ getDefiningPackageName()110 public String getDefiningPackageName() { 111 return mDefiningPackageName; 112 } 113 114 /** 115 * Creates a HostingRecord for a process that must spawn from the webview zygote 116 * @param hostingName name of the component to be hosted in this process 117 * @return The constructed HostingRecord 118 */ byWebviewZygote(ComponentName hostingName)119 public static HostingRecord byWebviewZygote(ComponentName hostingName) { 120 return new HostingRecord("", hostingName.toShortString(), WEBVIEW_ZYGOTE); 121 } 122 123 /** 124 * Creates a HostingRecord for a process that must spawn from the application zygote 125 * @param hostingName name of the component to be hosted in this process 126 * @param definingPackageName name of the package defining the service 127 * @param definingUid uid of the package defining the service 128 * @return The constructed HostingRecord 129 */ byAppZygote(ComponentName hostingName, String definingPackageName, int definingUid)130 public static HostingRecord byAppZygote(ComponentName hostingName, String definingPackageName, 131 int definingUid) { 132 return new HostingRecord("", hostingName.toShortString(), APP_ZYGOTE, 133 definingPackageName, definingUid); 134 } 135 136 /** 137 * @return whether the process should spawn from the application zygote 138 */ usesAppZygote()139 public boolean usesAppZygote() { 140 return mHostingZygote == APP_ZYGOTE; 141 } 142 143 /** 144 * @return whether the process should spawn from the webview zygote 145 */ usesWebviewZygote()146 public boolean usesWebviewZygote() { 147 return mHostingZygote == WEBVIEW_ZYGOTE; 148 } 149 } 150