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 3 // found in the LICENSE file. 4 5 package org.chromium.content.browser; 6 7 /** 8 * Manages oom bindings used to bound child services. "Oom binding" is a binding that raises the 9 * process oom priority so that it shouldn't be killed by the OS out-of-memory killer under 10 * normal conditions (it can still be killed under drastic memory pressure). ChildProcessConnections 11 * have two oom bindings: initial binding and strong binding. 12 * 13 * BindingManager receives calls that signal visibility of each service (setInForeground()) and the 14 * entire embedding application (onSentToBackground(), onBroughtToForeground()) and manipulates 15 * child process bindings accordingly. 16 * 17 * In particular, BindingManager is responsible for: 18 * - removing the initial binding of a service when its visibility is determined for the first time 19 * - addition and (possibly delayed) removal of a strong binding when service visibility changes 20 * - dropping the current oom bindings when a new connection is started on a low-memory device 21 * - keeping a strong binding on the foreground service while the entire application is in 22 * background 23 * 24 * Thread-safety: most of the methods will be called only on the main thread, exceptions are 25 * explicitly noted. 26 */ 27 public interface BindingManager { 28 /** 29 * Registers a freshly started child process. On low-memory devices this will also drop the 30 * oom bindings of the last process that was oom-bound. We can do that, because every time a 31 * connection is created on the low-end, it is used in foreground (no prerendering, no 32 * loading of tabs opened in background). This can be called on any thread. 33 * @param pid handle of the service process 34 */ addNewConnection(int pid, ChildProcessConnection connection)35 void addNewConnection(int pid, ChildProcessConnection connection); 36 37 /** 38 * Called when the service visibility changes or is determined for the first time. 39 * @param pid handle of the service process 40 * @param inForeground true iff the service is visibile to the user 41 */ setInForeground(int pid, boolean inForeground)42 void setInForeground(int pid, boolean inForeground); 43 44 /** 45 * Called when the embedding application is sent to background. We want to maintain a strong 46 * binding on the most recently used renderer while the embedder is in background, to indicate 47 * the relative importance of the renderer to system oom killer. 48 * 49 * The embedder needs to ensure that: 50 * - every onBroughtToForeground() is followed by onSentToBackground() 51 * - pairs of consecutive onBroughtToForeground() / onSentToBackground() calls do not overlap 52 */ onSentToBackground()53 void onSentToBackground(); 54 55 /** 56 * Called when the embedding application is brought to foreground. This will drop the strong 57 * binding kept on the main renderer during the background period, so the embedder should make 58 * sure that this is called after the regular strong binding is attached for the foreground 59 * session. 60 */ onBroughtToForeground()61 void onBroughtToForeground(); 62 63 /** 64 * @return True iff the given service process is protected from the out-of-memory killing, or it 65 * was protected when it died unexpectedly. This can be used to decide if a disconnection of a 66 * renderer was a crash or a probable out-of-memory kill. This can be called on any thread. 67 */ isOomProtected(int pid)68 boolean isOomProtected(int pid); 69 70 /** 71 * Should be called when the connection to the child process goes away (either after a clean 72 * exit or an unexpected crash). At this point we let go of the reference to the 73 * ChildProcessConnection. This can be called on any thread. 74 */ clearConnection(int pid)75 void clearConnection(int pid); 76 } 77