Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(211)

Side by Side Diff: base/android/java/src/org/chromium/base/process_launcher/ManagedChildProcessConnection.java

Issue 2845243002: Moving BindingManager and ChildProcessConnection to base/.
Patch Set: Moving BindingManager and ChildProcessConnection to base/. Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.base.process_launcher;
6 6
7 import android.content.Context; 7 import android.content.Context;
8 import android.os.Bundle; 8 import android.os.Bundle;
9 import android.os.Handler;
9 10
10 import org.chromium.base.Log; 11 import org.chromium.base.Log;
11 import org.chromium.base.VisibleForTesting; 12 import org.chromium.base.VisibleForTesting;
12 import org.chromium.base.process_launcher.ChildProcessCreationParams;
13 13
14 /** 14 /**
15 * ManagedChildProcessConnection is a connection to a child service that can hol d several bindings 15 * ManagedChildProcessConnection is a connection to a child service that can hol d several bindings
16 * to the service so it can be more or less agressively protected against OOM. 16 * to the service so it can be more or less agressively protected against OOM.
17 * Accessed from the launcher thread. (but for isOomProtectedOrWasWhenDied()). 17 * Accessed exclusively from the connection thread (the one specified in the con structor), but for
18 * isOomProtectedOrWasWhenDied().
18 */ 19 */
19 public class ManagedChildProcessConnection extends BaseChildProcessConnection { 20 public class ManagedChildProcessConnection extends BaseChildProcessConnection {
20 private static final String TAG = "ManChildProcessConn"; 21 private static final String TAG = "ManChildProcessConn";
21 22
22 public static final Factory FACTORY = new BaseChildProcessConnection.Factory () { 23 public static final Factory FACTORY = new BaseChildProcessConnection.Factory () {
23 @Override 24 @Override
24 public BaseChildProcessConnection create(Context context, int number, bo olean sandboxed, 25 public BaseChildProcessConnection create(Handler handler, Context contex t, int number,
25 DeathCallback deathCallback, String serviceClassName, 26 boolean sandboxed, DeathCallback deathCallback, String serviceCl assName,
26 Bundle childProcessCommonParameters, ChildProcessCreationParams creationParams) { 27 Bundle childProcessCommonParameters, ChildProcessCreationParams creationParams) {
27 assert LauncherThread.runningOnLauncherThread(); 28 return new ManagedChildProcessConnection(handler, context, number, s andboxed,
28 return new ManagedChildProcessConnection(context, number, sandboxed, deathCallback, 29 deathCallback, serviceClassName, childProcessCommonParameter s, creationParams);
29 serviceClassName, childProcessCommonParameters, creationPara ms);
30 } 30 }
31 }; 31 };
32 32
33 // Initial binding protects the newly spawned process from being killed befo re it is put to use, 33 // Initial binding protects the newly spawned process from being killed befo re it is put to use,
34 // it is maintained between calls to start() and removeInitialBinding(). 34 // it is maintained between calls to start() and removeInitialBinding().
35 private final ChildServiceConnection mInitialBinding; 35 private final ChildServiceConnection mInitialBinding;
36 36
37 // Strong binding will make the service priority equal to the priority of th e activity. We want 37 // Strong binding will make the service priority equal to the priority of th e activity. We want
38 // the OS to be able to kill background renderers as it kills other backgrou nd apps, so strong 38 // the OS to be able to kill background renderers as it kills other backgrou nd apps, so strong
39 // bindings are maintained only for services that are active at the moment ( between 39 // bindings are maintained only for services that are active at the moment ( between
(...skipping 13 matching lines...) Expand all
53 private final ChildServiceConnection mModerateBinding; 53 private final ChildServiceConnection mModerateBinding;
54 54
55 // Indicates whether the connection is OOM protected (if the connection is u nbound, it contains 55 // Indicates whether the connection is OOM protected (if the connection is u nbound, it contains
56 // the state at time of unbinding). 56 // the state at time of unbinding).
57 private boolean mOomProtected; 57 private boolean mOomProtected;
58 58
59 // Set to true once unbind() was called. 59 // Set to true once unbind() was called.
60 private boolean mUnbound; 60 private boolean mUnbound;
61 61
62 @VisibleForTesting 62 @VisibleForTesting
63 ManagedChildProcessConnection(Context context, int number, boolean sandboxed , 63 ManagedChildProcessConnection(Handler handler, Context context, int number, boolean sandboxed,
64 DeathCallback deathCallback, String serviceClassName, 64 DeathCallback deathCallback, String serviceClassName,
65 Bundle childProcessCommonParameters, ChildProcessCreationParams crea tionParams) { 65 Bundle childProcessCommonParameters, ChildProcessCreationParams crea tionParams) {
66 super(context, number, sandboxed, deathCallback, serviceClassName, 66 super(handler, context, number, sandboxed, deathCallback, serviceClassNa me,
67 childProcessCommonParameters, creationParams); 67 childProcessCommonParameters, creationParams);
68 68
69 int initialFlags = Context.BIND_AUTO_CREATE; 69 int initialFlags = Context.BIND_AUTO_CREATE;
70 int extraBindFlags = shouldBindAsExportedService() ? Context.BIND_EXTERN AL_SERVICE : 0; 70 int extraBindFlags = shouldBindAsExportedService() ? Context.BIND_EXTERN AL_SERVICE : 0;
71 mInitialBinding = createServiceConnection(initialFlags | extraBindFlags) ; 71 mInitialBinding = createServiceConnection(initialFlags | extraBindFlags) ;
72 mStrongBinding = createServiceConnection( 72 mStrongBinding = createServiceConnection(
73 Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT | extraBindFla gs); 73 Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT | extraBindFla gs);
74 mWaivedBinding = createServiceConnection( 74 mWaivedBinding = createServiceConnection(
75 Context.BIND_AUTO_CREATE | Context.BIND_WAIVE_PRIORITY | extraBi ndFlags); 75 Context.BIND_AUTO_CREATE | Context.BIND_WAIVE_PRIORITY | extraBi ndFlags);
76 mModerateBinding = createServiceConnection(Context.BIND_AUTO_CREATE | ex traBindFlags); 76 mModerateBinding = createServiceConnection(Context.BIND_AUTO_CREATE | ex traBindFlags);
77 } 77 }
78 78
79 @Override 79 @Override
80 protected boolean bind() { 80 protected boolean bind() {
81 assert LauncherThread.runningOnLauncherThread(); 81 checkOnValidThread();
82 assert !mUnbound; 82 assert !mUnbound;
83 if (!mInitialBinding.bind()) { 83 if (!mInitialBinding.bind()) {
84 return false; 84 return false;
85 } 85 }
86 updateOomProtectedState(); 86 updateOomProtectedState();
87 mWaivedBinding.bind(); 87 mWaivedBinding.bind();
88 return true; 88 return true;
89 } 89 }
90 90
91 @Override 91 @Override
92 public void unbind() { 92 public void unbind() {
93 assert LauncherThread.runningOnLauncherThread(); 93 checkOnValidThread();
94 mUnbound = true; 94 mUnbound = true;
95 mInitialBinding.unbind(); 95 mInitialBinding.unbind();
96 mStrongBinding.unbind(); 96 mStrongBinding.unbind();
97 // Note that we don't update the OOM state here as to preserve the last OOM state. 97 // Note that we don't update the OOM state here as to preserve the last OOM state.
98 mWaivedBinding.unbind(); 98 mWaivedBinding.unbind();
99 mModerateBinding.unbind(); 99 mModerateBinding.unbind();
100 mStrongBindingCount = 0; 100 mStrongBindingCount = 0;
101 } 101 }
102 102
103 public boolean isInitialBindingBound() { 103 public boolean isInitialBindingBound() {
104 assert LauncherThread.runningOnLauncherThread(); 104 checkOnValidThread();
105 return mInitialBinding.isBound(); 105 return mInitialBinding.isBound();
106 } 106 }
107 107
108 public boolean isStrongBindingBound() { 108 public boolean isStrongBindingBound() {
109 assert LauncherThread.runningOnLauncherThread(); 109 checkOnValidThread();
110 return mStrongBinding.isBound(); 110 return mStrongBinding.isBound();
111 } 111 }
112 112
113 public void removeInitialBinding() { 113 public void removeInitialBinding() {
114 assert LauncherThread.runningOnLauncherThread(); 114 checkOnValidThread();
115 mInitialBinding.unbind(); 115 mInitialBinding.unbind();
116 updateOomProtectedState(); 116 updateOomProtectedState();
117 } 117 }
118 118
119 /** 119 /**
120 * @return true if the connection is bound and OOM protected or was OOM prot ected when unbound. 120 * @return true if the connection is bound and OOM protected or was OOM prot ected when unbound.
121 */ 121 */
122 public boolean isOomProtectedOrWasWhenDied() { 122 public boolean isOomProtectedOrWasWhenDied() {
123 // WARNING: this method can be called from a thread other than the launc her thread. 123 // WARNING: this method can be called from a thread other than the conne ction's thread.
124 // Note that it returns the current OOM protected state and is racy. Thi s not really 124 // Note that it returns the current OOM protected state and is racy. Thi s not really
125 // preventable without changing the caller's API, short of blocking. 125 // preventable as the renderer currently requires an API that returns a value and is accesed
126 // from a different thread (short of blocking, which is not acceptable e ither).
126 return mOomProtected; 127 return mOomProtected;
127 } 128 }
128 129
129 public void dropOomBindings() { 130 public void dropOomBindings() {
130 assert LauncherThread.runningOnLauncherThread(); 131 checkOnValidThread();
131 mInitialBinding.unbind(); 132 mInitialBinding.unbind();
132 133
133 mStrongBindingCount = 0; 134 mStrongBindingCount = 0;
134 mStrongBinding.unbind(); 135 mStrongBinding.unbind();
135 updateOomProtectedState(); 136 updateOomProtectedState();
136 137
137 mModerateBinding.unbind(); 138 mModerateBinding.unbind();
138 } 139 }
139 140
140 public void addStrongBinding() { 141 public void addStrongBinding() {
141 assert LauncherThread.runningOnLauncherThread(); 142 checkOnValidThread();
142 if (!isConnected()) { 143 if (!isConnected()) {
143 Log.w(TAG, "The connection is not bound for %d", getPid()); 144 Log.w(TAG, "The connection is not bound for %d", getPid());
144 return; 145 return;
145 } 146 }
146 if (mStrongBindingCount == 0) { 147 if (mStrongBindingCount == 0) {
147 mStrongBinding.bind(); 148 mStrongBinding.bind();
148 updateOomProtectedState(); 149 updateOomProtectedState();
149 } 150 }
150 mStrongBindingCount++; 151 mStrongBindingCount++;
151 } 152 }
152 153
153 public void removeStrongBinding() { 154 public void removeStrongBinding() {
154 assert LauncherThread.runningOnLauncherThread(); 155 checkOnValidThread();
155 if (!isConnected()) { 156 if (!isConnected()) {
156 Log.w(TAG, "The connection is not bound for %d", getPid()); 157 Log.w(TAG, "The connection is not bound for %d", getPid());
157 return; 158 return;
158 } 159 }
159 assert mStrongBindingCount > 0; 160 assert mStrongBindingCount > 0;
160 mStrongBindingCount--; 161 mStrongBindingCount--;
161 if (mStrongBindingCount == 0) { 162 if (mStrongBindingCount == 0) {
162 mStrongBinding.unbind(); 163 mStrongBinding.unbind();
163 updateOomProtectedState(); 164 updateOomProtectedState();
164 } 165 }
165 updateOomProtectedState(); 166 updateOomProtectedState();
166 } 167 }
167 168
168 public boolean isModerateBindingBound() { 169 public boolean isModerateBindingBound() {
169 assert LauncherThread.runningOnLauncherThread(); 170 checkOnValidThread();
170 return mModerateBinding.isBound(); 171 return mModerateBinding.isBound();
171 } 172 }
172 173
173 public void addModerateBinding() { 174 public void addModerateBinding() {
174 assert LauncherThread.runningOnLauncherThread(); 175 checkOnValidThread();
175 if (!isConnected()) { 176 if (!isConnected()) {
176 Log.w(TAG, "The connection is not bound for %d", getPid()); 177 Log.w(TAG, "The connection is not bound for %d", getPid());
177 return; 178 return;
178 } 179 }
179 mModerateBinding.bind(); 180 mModerateBinding.bind();
180 } 181 }
181 182
182 public void removeModerateBinding() { 183 public void removeModerateBinding() {
183 assert LauncherThread.runningOnLauncherThread(); 184 checkOnValidThread();
184 if (!isConnected()) { 185 if (!isConnected()) {
185 Log.w(TAG, "The connection is not bound for %d", getPid()); 186 Log.w(TAG, "The connection is not bound for %d", getPid());
186 return; 187 return;
187 } 188 }
188 mModerateBinding.unbind(); 189 mModerateBinding.unbind();
189 } 190 }
190 191
191 // Should be called every time the mInitialBinding or mStrongBinding are bou nd/unbound. 192 // Should be called every time the mInitialBinding or mStrongBinding are bou nd/unbound.
192 private void updateOomProtectedState() { 193 private void updateOomProtectedState() {
193 if (!mUnbound) { 194 if (!mUnbound) {
194 mOomProtected = mInitialBinding.isBound() || mStrongBinding.isBound( ); 195 mOomProtected = mInitialBinding.isBound() || mStrongBinding.isBound( );
195 } 196 }
196 } 197 }
197 } 198 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698