// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.chrome.browser;

import org.chromium.base.CalledByNative;

/**
 * Watches for when Chrome is told to restart itself.
 */
public class ApplicationLifetime {
    public interface Observer {
        void onTerminate(boolean restart);
    }
    private static Observer sObserver = null;

    /**
     * Sets the observer that monitors for ApplicationLifecycle events.
     * We only allow one observer to be set to avoid race conditions for shutdown events.
     */
    public static void setObserver(Observer observer) {
        assert sObserver == null;
        sObserver = observer;
    }

    /**
     * Removes whatever observer is currently watching this class.
     */
    public static void removeObserver() {
        sObserver = null;
    }

    @CalledByNative
    public static void terminate(boolean restart) {
        if (sObserver != null)
            sObserver.onTerminate(restart);
    }
}

@@ -237,16 +237,55 @@
 yNative;
+%0Aimport org.chromium.base.ObserverList;
 %0A%0A/**%0A *
@@ -383,35 +383,274 @@
     
-public interface Observer %7B
+/**%0A     * Interface to be implemented to be notified of application termination.%0A     */%0A    public interface Observer %7B%0A        /**%0A         * Called when the application should be terminated.%0A         * @param restart Whether or not to restart Chrome.%0A         */
 %0A   
@@ -695,16 +695,17 @@
 ;%0A    %7D%0A
+%0A
     priv
@@ -723,16 +723,30 @@
 Observer
+List%3CObserver%3E
  sObserv
@@ -747,23 +747,48 @@
 Observer
+s
  = n
-ull
+ew ObserverList%3CObserver%3E()
 ;%0A%0A    /
@@ -801,16 +801,15 @@
   * 
-Sets the
+Adds an
  obs
@@ -819,26 +819,21 @@
 er t
-hat monitors
+o watch
  for 
-A
+a
 ppli
@@ -842,114 +842,64 @@
 tion
-Lifecycle events.%0A     * We only allow one observer to be set to avoid race conditions for shutdown events
+ termination.%0A     * @param observer The observer to add
 .%0A  
@@ -931,11 +931,11 @@
 oid 
-set
+add
 Obse
@@ -972,15 +972,8 @@
     
-assert 
 sObs
@@ -981,35 +981,21 @@
 rver
- == null;%0A        s
+s.add
 Observer
  = o
@@ -994,19 +994,17 @@
 rver
- = 
+(
 observer
 ;%0A  
@@ -999,16 +999,17 @@
 observer
+)
 ;%0A    %7D%0A
@@ -1036,16 +1036,10 @@
 ves 
-whatever
+an
  obs
@@ -1048,40 +1048,97 @@
 ver 
-is currently watching this class
+from watching for application termination.%0A     * @oparam observer The observer to remove
 .%0A  
@@ -1181,16 +1181,33 @@
 bserver(
+Observer observer
 ) %7B%0A    
@@ -1223,15 +1223,34 @@
 rver
- = null
+s.removeObserver(observer)
 ;%0A  
@@ -1338,30 +1338,46 @@
     
-if (sObserver != null)
+for (Observer observer : sObservers) %7B
 %0A   
@@ -1381,26 +1381,25 @@
             
-sO
+o
 bserver.onTe
@@ -1412,20 +1412,30 @@
 e(restart);%0A
+        %7D%0A
     %7D%0A%7D%0A

Make ApplicationLifetime support multiple observers