1a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Copyright 2012 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)package org.chromium.chrome.browser;
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)import org.chromium.base.CalledByNative;
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)import org.chromium.base.ObserverList;
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)/**
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) * Watches for when Chrome is told to restart itself.
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) */
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)public class ApplicationLifetime {
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    /**
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)     * Interface to be implemented to be notified of application termination.
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)     */
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    public interface Observer {
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        /**
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)         * Called when the application should be terminated.
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)         * @param restart Whether or not to restart Chrome.
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)         */
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        void onTerminate(boolean restart);
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    private static ObserverList<Observer> sObservers = new ObserverList<Observer>();
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /**
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)     * Adds an observer to watch for application termination.
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)     * @param observer The observer to add.
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     */
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    public static void addObserver(Observer observer) {
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        sObservers.addObserver(observer);
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    /**
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)     * Removes an observer from watching for application termination.
37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)     * @oparam observer The observer to remove.
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)     */
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    public static void removeObserver(Observer observer) {
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        sObservers.removeObserver(observer);
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    @CalledByNative
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    public static void terminate(boolean restart) {
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        for (Observer observer : sObservers) {
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            observer.onTerminate(restart);
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        }
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
50