Home | History | Annotate | Download | only in browser
      1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_BACKGROUND_MODE_MANAGER_H_
      6 #define CHROME_BROWSER_BACKGROUND_MODE_MANAGER_H_
      7 #pragma once
      8 
      9 #include "base/gtest_prod_util.h"
     10 #include "chrome/browser/background_application_list_model.h"
     11 #include "chrome/browser/prefs/pref_change_registrar.h"
     12 #include "chrome/browser/profiles/profile_keyed_service.h"
     13 #include "chrome/browser/status_icons/status_icon.h"
     14 #include "content/common/notification_observer.h"
     15 #include "content/common/notification_registrar.h"
     16 #include "ui/base/models/simple_menu_model.h"
     17 
     18 class Browser;
     19 class CommandLine;
     20 class Extension;
     21 class PrefService;
     22 class Profile;
     23 class StatusIcon;
     24 class StatusTray;
     25 
     26 // BackgroundModeManager is responsible for switching Chrome into and out of
     27 // "background mode" and for providing UI for the user to exit Chrome when there
     28 // are no open browser windows.
     29 //
     30 // Chrome enters background mode whenever there is an application with the
     31 // "background" permission installed. This class monitors the set of
     32 // installed/loaded extensions to ensure that Chrome enters/exits background
     33 // mode at the appropriate time.
     34 //
     35 // When Chrome is in background mode, it will continue running even after the
     36 // last browser window is closed, until the user explicitly exits the app.
     37 // Additionally, when in background mode, Chrome will launch on OS login with
     38 // no open windows to allow apps with the "background" permission to run in the
     39 // background.
     40 class BackgroundModeManager
     41     : public NotificationObserver,
     42       public ui::SimpleMenuModel::Delegate,
     43       public BackgroundApplicationListModel::Observer,
     44       public ProfileKeyedService {
     45  public:
     46   BackgroundModeManager(Profile* profile, CommandLine* command_line);
     47   virtual ~BackgroundModeManager();
     48 
     49   static bool IsBackgroundModeEnabled(const CommandLine* command_line);
     50   static void RegisterPrefs(PrefService* prefs);
     51 
     52  private:
     53   friend class TestBackgroundModeManager;
     54   friend class BackgroundModeManagerTest;
     55   FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest,
     56                            BackgroundAppLoadUnload);
     57   FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest,
     58                            BackgroundAppInstallUninstall);
     59   FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest,
     60                            BackgroundPrefDisabled);
     61   FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest,
     62                            BackgroundPrefDynamicDisable);
     63   FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest,
     64                            BackgroundPrefDynamicEnable);
     65 
     66   // NotificationObserver implementation.
     67   virtual void Observe(NotificationType type,
     68                        const NotificationSource& source,
     69                        const NotificationDetails& details);
     70 
     71   // SimpleMenuModel::Delegate implementation.
     72   virtual bool IsCommandIdChecked(int command_id) const;
     73   virtual bool IsCommandIdEnabled(int command_id) const;
     74   virtual bool GetAcceleratorForCommandId(int command_id,
     75                                           ui::Accelerator* accelerator);
     76   virtual void ExecuteCommand(int command_id);
     77 
     78   // Open an application in a new tab, opening a new window if needed.
     79   virtual void ExecuteApplication(int application_id);
     80 
     81   // BackgroundApplicationListModel::Observer implementation.
     82   virtual void OnApplicationDataChanged(const Extension* extension);
     83   virtual void OnApplicationListChanged();
     84 
     85   // Called when an extension is loaded to manage count of background apps.
     86   void OnBackgroundAppLoaded();
     87 
     88   // Called when an extension is unloaded to manage count of background apps.
     89   void OnBackgroundAppUnloaded();
     90 
     91   // Invoked when an extension is installed so we can ensure that
     92   // launch-on-startup is enabled if appropriate. |extension| can be NULL when
     93   // called from unit tests.
     94   void OnBackgroundAppInstalled(const Extension* extension);
     95 
     96   // Invoked when an extension is uninstalled so we can ensure that
     97   // launch-on-startup is disabled if appropriate.
     98   void OnBackgroundAppUninstalled();
     99 
    100   // Called to make sure that our launch-on-startup mode is properly set.
    101   // (virtual so we can override for tests).
    102   virtual void EnableLaunchOnStartup(bool should_launch);
    103 
    104   // Invoked when a background app is installed so we can display a
    105   // platform-specific notification to the user.
    106   void DisplayAppInstalledNotification(const Extension* extension);
    107 
    108   // Invoked to put Chrome in KeepAlive mode - chrome runs in the background
    109   // and has a status bar icon.
    110   void StartBackgroundMode();
    111 
    112   // Invoked to take Chrome out of KeepAlive mode - chrome stops running in
    113   // the background and removes its status bar icon.
    114   void EndBackgroundMode();
    115 
    116   // If --no-startup-window is passed, BackgroundModeManager will manually keep
    117   // chrome running while waiting for apps to load. This is called when we no
    118   // longer need to do this (either because the user has chosen to exit chrome
    119   // manually, or all apps have been loaded).
    120   void EndKeepAliveForStartup();
    121 
    122   // Return an appropriate name for a Preferences menu entry.  Preferences is
    123   // sometimes called Options or Settings.
    124   string16 GetPreferencesMenuLabel();
    125 
    126   // Create a status tray icon to allow the user to shutdown Chrome when running
    127   // in background mode. Virtual to enable testing.
    128   virtual void CreateStatusTrayIcon();
    129 
    130   // Removes the status tray icon because we are exiting background mode.
    131   // Virtual to enable testing.
    132   virtual void RemoveStatusTrayIcon();
    133 
    134   // Updates the status icon's context menu entry corresponding to |extension|
    135   // to use the icon associated with |extension| in the
    136   // BackgroundApplicationListModel.
    137   void UpdateContextMenuEntryIcon(const Extension* extension);
    138 
    139   // Create a context menu, or replace/update an existing context menu, for the
    140   // status tray icon which, among other things, allows the user to shutdown
    141   // Chrome when running in background mode.
    142   virtual void UpdateStatusTrayIconContextMenu();
    143 
    144   // Returns a browser window, or creates one if none are open. Used by
    145   // operations (like displaying the preferences dialog) that require a Browser
    146   // window.
    147   Browser* GetBrowserWindow();
    148 
    149   NotificationRegistrar registrar_;
    150 
    151   // The parent profile for this object.
    152   Profile* profile_;
    153 
    154   // The cached list of BackgroundApplications.
    155   BackgroundApplicationListModel applications_;
    156 
    157   // The number of background apps currently loaded.
    158   int background_app_count_;
    159 
    160   // Reference to our status icon's context menu (if any) - owned by the
    161   // status_icon_
    162   ui::SimpleMenuModel* context_menu_;
    163 
    164   // Set to the position of the first application entry in the status icon's
    165   // context menu.
    166   int context_menu_application_offset_;
    167 
    168   // Set to true when we are running in background mode. Allows us to track our
    169   // current background state so we can take the appropriate action when the
    170   // user disables/enables background mode via preferences.
    171   bool in_background_mode_;
    172 
    173   // Set when we are keeping chrome running during the startup process - this
    174   // is required when running with the --no-startup-window flag, as otherwise
    175   // chrome would immediately exit due to having no open windows.
    176   bool keep_alive_for_startup_;
    177 
    178   // Reference to our status tray (owned by our parent profile). If null, the
    179   // platform doesn't support status icons.
    180   StatusTray* status_tray_;
    181 
    182   // Reference to our status icon (if any) - owned by the StatusTray.
    183   StatusIcon* status_icon_;
    184 
    185   DISALLOW_COPY_AND_ASSIGN(BackgroundModeManager);
    186 };
    187 
    188 #endif  // CHROME_BROWSER_BACKGROUND_MODE_MANAGER_H_
    189