| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.content.browser; | 5 package org.chromium.content.browser; |
| 6 | 6 |
| 7 import android.content.ComponentName; | 7 import android.content.ComponentName; |
| 8 import android.content.Context; | 8 import android.content.Context; |
| 9 import android.content.Intent; | 9 import android.content.Intent; |
| 10 import android.content.ServiceConnection; | 10 import android.content.ServiceConnection; |
| 11 import android.content.pm.PackageManager; | 11 import android.content.pm.PackageManager; |
| 12 import android.content.pm.ServiceInfo; | 12 import android.content.pm.ServiceInfo; |
| 13 import android.os.Build; | 13 import android.os.Build; |
| 14 import android.os.Bundle; | 14 import android.os.Bundle; |
| 15 import android.os.DeadObjectException; | 15 import android.os.DeadObjectException; |
| 16 import android.os.IBinder; | 16 import android.os.IBinder; |
| 17 import android.os.RemoteException; | 17 import android.os.RemoteException; |
| 18 | 18 |
| 19 import org.chromium.base.Log; | 19 import org.chromium.base.Log; |
| 20 import org.chromium.base.ThreadUtils; | 20 import org.chromium.base.ThreadUtils; |
| 21 import org.chromium.base.TraceEvent; | 21 import org.chromium.base.TraceEvent; |
| 22 import org.chromium.base.VisibleForTesting; | 22 import org.chromium.base.VisibleForTesting; |
| 23 import org.chromium.content.app.ChromiumLinkerParams; | |
| 24 import org.chromium.content.common.FileDescriptorInfo; | 23 import org.chromium.content.common.FileDescriptorInfo; |
| 25 import org.chromium.content.common.IChildProcessCallback; | 24 import org.chromium.content.common.IChildProcessCallback; |
| 26 import org.chromium.content.common.IChildProcessService; | 25 import org.chromium.content.common.IChildProcessService; |
| 27 | 26 |
| 28 import java.io.IOException; | 27 import java.io.IOException; |
| 29 | 28 |
| 30 /** | 29 /** |
| 31 * Manages a connection between the browser activity and a child service. | 30 * Manages a connection between the browser activity and a child service. |
| 32 */ | 31 */ |
| 33 public class ChildProcessConnectionImpl implements ChildProcessConnection { | 32 public class ChildProcessConnectionImpl implements ChildProcessConnection { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 // Low priority binding maintained in the entire lifetime of the connection,
i.e. between calls | 64 // Low priority binding maintained in the entire lifetime of the connection,
i.e. between calls |
| 66 // to start() and stop(). | 65 // to start() and stop(). |
| 67 private ChildServiceConnection mWaivedBinding; | 66 private ChildServiceConnection mWaivedBinding; |
| 68 // Incremented on addStrongBinding(), decremented on removeStrongBinding(). | 67 // Incremented on addStrongBinding(), decremented on removeStrongBinding(). |
| 69 private int mStrongBindingCount; | 68 private int mStrongBindingCount; |
| 70 // Moderate binding will make the service priority equal to the priority of
a visible process | 69 // Moderate binding will make the service priority equal to the priority of
a visible process |
| 71 // while the app is in the foreground. It will stay bound only while the app
is in the | 70 // while the app is in the foreground. It will stay bound only while the app
is in the |
| 72 // foreground to protect a background process from the system out-of-memory
killer. | 71 // foreground to protect a background process from the system out-of-memory
killer. |
| 73 private ChildServiceConnection mModerateBinding; | 72 private ChildServiceConnection mModerateBinding; |
| 74 | 73 |
| 75 // Linker-related parameters. | 74 // Parameters passed to the child process through the service binding intent
. |
| 76 private ChromiumLinkerParams mLinkerParams; | 75 // If the service gets recreated by the framework the intent will be reused,
so these parameters |
| 76 // should be common to all processes of that type. |
| 77 private final Bundle mChildProcessCommonParameters; |
| 77 | 78 |
| 78 private final boolean mAlwaysInForeground; | 79 private final boolean mAlwaysInForeground; |
| 79 private final ChildProcessCreationParams mCreationParams; | 80 private final ChildProcessCreationParams mCreationParams; |
| 80 | 81 |
| 81 // Caches whether non-sandboxed and sandboxed services require an extra | 82 // Caches whether non-sandboxed and sandboxed services require an extra |
| 82 // binding flag provided via ChildProcessCreationParams. | 83 // binding flag provided via ChildProcessCreationParams. |
| 83 // TODO(mnaganov): Get rid of it after the release of the next Android SDK. | 84 // TODO(mnaganov): Get rid of it after the release of the next Android SDK. |
| 84 private static Boolean sNeedsExtrabindFlags[] = new Boolean[2]; | 85 private static Boolean sNeedsExtrabindFlags[] = new Boolean[2]; |
| 85 | 86 |
| 86 private static final String TAG = "ChildProcessConnect"; | 87 private static final String TAG = "ChildProcessConnect"; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 } | 129 } |
| 129 | 130 |
| 130 public ChildServiceConnection(int bindFlags) { | 131 public ChildServiceConnection(int bindFlags) { |
| 131 mBindFlags = bindFlags; | 132 mBindFlags = bindFlags; |
| 132 } | 133 } |
| 133 | 134 |
| 134 boolean bind() { | 135 boolean bind() { |
| 135 if (!mBound) { | 136 if (!mBound) { |
| 136 try { | 137 try { |
| 137 TraceEvent.begin("ChildProcessConnectionImpl.ChildServiceCon
nection.bind"); | 138 TraceEvent.begin("ChildProcessConnectionImpl.ChildServiceCon
nection.bind"); |
| 138 final Intent intent = createServiceBindIntent(); | 139 Intent intent = createServiceBindIntent(); |
| 139 // Note, the intent may be saved and re-used by Android for
re-launching the | 140 if (mChildProcessCommonParameters != null) { |
| 140 // child service. Do not pass data that is different for eac
h child; command | 141 intent.putExtras(mChildProcessCommonParameters); |
| 141 // line arguments for example. | |
| 142 if (mLinkerParams != null) { | |
| 143 mLinkerParams.addIntentExtras(intent); | |
| 144 } | 142 } |
| 145 mBound = mContext.bindService(intent, this, mBindFlags); | 143 mBound = mContext.bindService(intent, this, mBindFlags); |
| 146 } finally { | 144 } finally { |
| 147 TraceEvent.end("ChildProcessConnectionImpl.ChildServiceConne
ction.bind"); | 145 TraceEvent.end("ChildProcessConnectionImpl.ChildServiceConne
ction.bind"); |
| 148 } | 146 } |
| 149 } | 147 } |
| 150 return mBound; | 148 return mBound; |
| 151 } | 149 } |
| 152 | 150 |
| 153 void unbind() { | 151 void unbind() { |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 // the caller. | 232 // the caller. |
| 235 if (mConnectionCallback != null) { | 233 if (mConnectionCallback != null) { |
| 236 mConnectionCallback.onConnected(0); | 234 mConnectionCallback.onConnected(0); |
| 237 } | 235 } |
| 238 mConnectionCallback = null; | 236 mConnectionCallback = null; |
| 239 } | 237 } |
| 240 } | 238 } |
| 241 } | 239 } |
| 242 | 240 |
| 243 ChildProcessConnectionImpl(Context context, int number, boolean inSandbox, | 241 ChildProcessConnectionImpl(Context context, int number, boolean inSandbox, |
| 244 ChildProcessConnection.DeathCallback deathCallback, | 242 ChildProcessConnection.DeathCallback deathCallback, String serviceCl
assName, |
| 245 String serviceClassName, | 243 Bundle childProcessCommonParameters, boolean alwaysInForeground, |
| 246 ChromiumLinkerParams chromiumLinkerParams, | |
| 247 boolean alwaysInForeground, | |
| 248 ChildProcessCreationParams creationParams) { | 244 ChildProcessCreationParams creationParams) { |
| 249 mContext = context; | 245 mContext = context; |
| 250 mServiceNumber = number; | 246 mServiceNumber = number; |
| 251 mInSandbox = inSandbox; | 247 mInSandbox = inSandbox; |
| 252 mDeathCallback = deathCallback; | 248 mDeathCallback = deathCallback; |
| 253 String packageName = | 249 String packageName = |
| 254 creationParams != null ? creationParams.getPackageName() : conte
xt.getPackageName(); | 250 creationParams != null ? creationParams.getPackageName() : conte
xt.getPackageName(); |
| 255 mServiceName = new ComponentName(packageName, serviceClassName + mServic
eNumber); | 251 mServiceName = new ComponentName(packageName, serviceClassName + mServic
eNumber); |
| 256 mLinkerParams = chromiumLinkerParams; | 252 mChildProcessCommonParameters = childProcessCommonParameters; |
| 257 mAlwaysInForeground = alwaysInForeground; | 253 mAlwaysInForeground = alwaysInForeground; |
| 258 mCreationParams = creationParams; | 254 mCreationParams = creationParams; |
| 259 int initialFlags = Context.BIND_AUTO_CREATE; | 255 int initialFlags = Context.BIND_AUTO_CREATE; |
| 260 if (mAlwaysInForeground) initialFlags |= Context.BIND_IMPORTANT; | 256 if (mAlwaysInForeground) initialFlags |= Context.BIND_IMPORTANT; |
| 261 int extraBindFlags = 0; | 257 int extraBindFlags = 0; |
| 262 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && mCreationParams !=
null | 258 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && mCreationParams !=
null |
| 263 && mCreationParams.getIsExternalService() | 259 && mCreationParams.getIsExternalService() |
| 264 && isExportedService(inSandbox, mContext, mServiceName)) { | 260 && isExportedService(inSandbox, mContext, mServiceName)) { |
| 265 extraBindFlags = Context.BIND_EXTERNAL_SERVICE; | 261 extraBindFlags = Context.BIND_EXTERNAL_SERVICE; |
| 266 } | 262 } |
| (...skipping 289 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 556 return true; | 552 return true; |
| 557 } | 553 } |
| 558 return false; | 554 return false; |
| 559 } | 555 } |
| 560 | 556 |
| 561 @VisibleForTesting | 557 @VisibleForTesting |
| 562 public boolean isConnected() { | 558 public boolean isConnected() { |
| 563 return mService != null; | 559 return mService != null; |
| 564 } | 560 } |
| 565 } | 561 } |
| OLD | NEW |