| OLD | NEW | 
|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef CHROME_BROWSER_BACKGROUND_MODE_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_BACKGROUND_MODE_MANAGER_H_ | 
| 6 #define CHROME_BROWSER_BACKGROUND_MODE_MANAGER_H_ | 6 #define CHROME_BROWSER_BACKGROUND_MODE_MANAGER_H_ | 
| 7 #pragma once | 7 #pragma once | 
| 8 | 8 | 
|  | 9 #include <map> | 
|  | 10 | 
| 9 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" | 
| 10 #include "chrome/browser/background_application_list_model.h" | 12 #include "chrome/browser/background_application_list_model.h" | 
| 11 #include "chrome/browser/prefs/pref_change_registrar.h" | 13 #include "chrome/browser/prefs/pref_change_registrar.h" | 
| 12 #include "chrome/browser/profiles/profile_keyed_service.h" | 14 #include "chrome/browser/profiles/profile_keyed_service.h" | 
| 13 #include "chrome/browser/status_icons/status_icon.h" | 15 #include "chrome/browser/status_icons/status_icon.h" | 
| 14 #include "content/common/notification_observer.h" | 16 #include "content/common/notification_observer.h" | 
| 15 #include "content/common/notification_registrar.h" | 17 #include "content/common/notification_registrar.h" | 
| 16 #include "ui/base/models/simple_menu_model.h" | 18 #include "ui/base/models/simple_menu_model.h" | 
| 17 | 19 | 
| 18 class Browser; | 20 class Browser; | 
| (...skipping 13 matching lines...) Expand all  Loading... | 
| 32 // installed/loaded extensions to ensure that Chrome enters/exits background | 34 // installed/loaded extensions to ensure that Chrome enters/exits background | 
| 33 // mode at the appropriate time. | 35 // mode at the appropriate time. | 
| 34 // | 36 // | 
| 35 // When Chrome is in background mode, it will continue running even after the | 37 // 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. | 38 // 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 | 39 // 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 | 40 // no open windows to allow apps with the "background" permission to run in the | 
| 39 // background. | 41 // background. | 
| 40 class BackgroundModeManager | 42 class BackgroundModeManager | 
| 41     : public NotificationObserver, | 43     : public NotificationObserver, | 
| 42       public ui::SimpleMenuModel::Delegate, |  | 
| 43       public BackgroundApplicationListModel::Observer, | 44       public BackgroundApplicationListModel::Observer, | 
| 44       public ProfileKeyedService { | 45       public ProfileKeyedService { | 
| 45  public: | 46  public: | 
| 46   BackgroundModeManager(Profile* profile, CommandLine* command_line); | 47   explicit BackgroundModeManager(CommandLine* command_line); | 
| 47   virtual ~BackgroundModeManager(); | 48   virtual ~BackgroundModeManager(); | 
| 48 | 49 | 
| 49   static void RegisterPrefs(PrefService* prefs); | 50   static void RegisterPrefs(PrefService* prefs); | 
| 50 | 51 | 
|  | 52   virtual void RegisterProfile(Profile* profile); | 
|  | 53 | 
| 51  private: | 54  private: | 
| 52   friend class TestBackgroundModeManager; | 55   friend class TestBackgroundModeManager; | 
| 53   friend class BackgroundModeManagerTest; | 56   friend class BackgroundModeManagerTest; | 
| 54   FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 57   FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 
| 55                            BackgroundAppLoadUnload); | 58                            BackgroundAppLoadUnload); | 
| 56   FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 59   FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 
| 57                            BackgroundAppInstallUninstall); | 60                            BackgroundAppInstallUninstall); | 
| 58   FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 61   FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 
| 59                            BackgroundAppInstallUninstallWhileDisabled); | 62                            BackgroundAppInstallUninstallWhileDisabled); | 
| 60   FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 63   FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 
| 61                            EnableAfterBackgroundAppInstall); | 64                            EnableAfterBackgroundAppInstall); | 
|  | 65   FRIEND_TEST_ALL_PREFIXES(BackgroundModeManagerTest, | 
|  | 66                            MultiProfile); | 
|  | 67 | 
|  | 68   class BackgroundModeData : public ui::SimpleMenuModel::Delegate { | 
|  | 69    public: | 
|  | 70     explicit BackgroundModeData( | 
|  | 71         Profile* profile, | 
|  | 72         BackgroundModeManager* background_mode_manager); | 
|  | 73     virtual ~BackgroundModeData(); | 
|  | 74 | 
|  | 75     // The cached list of BackgroundApplications. | 
|  | 76     scoped_ptr<BackgroundApplicationListModel> applications_; | 
|  | 77 | 
|  | 78     // Reference to our status icon (if any) - owned by the StatusTray. | 
|  | 79     StatusIcon* status_icon_; | 
|  | 80 | 
|  | 81     // Reference to our status icon's context menu (if any) - owned by the | 
|  | 82     // status_icon_ | 
|  | 83     ui::SimpleMenuModel* context_menu_; | 
|  | 84 | 
|  | 85     // Set to the position of the first application entry in the status icon's | 
|  | 86     // context menu. | 
|  | 87     int context_menu_application_offset_; | 
|  | 88 | 
|  | 89     // The profile associated with this background app data. | 
|  | 90     Profile* profile_; | 
|  | 91 | 
|  | 92     // The background mode manager which owns this BackgroundModeData | 
|  | 93     BackgroundModeManager* background_mode_manager_; | 
|  | 94 | 
|  | 95     // Overrides from SimpleMenuModel::Delegate implementation. | 
|  | 96     virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; | 
|  | 97     virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; | 
|  | 98     virtual bool GetAcceleratorForCommandId(int command_id, | 
|  | 99                                             ui::Accelerator* accelerator) | 
|  | 100                                             OVERRIDE; | 
|  | 101     virtual void ExecuteCommand(int command_id) OVERRIDE; | 
|  | 102 | 
|  | 103     // Returns a browser window, or creates one if none are open. Used by | 
|  | 104     // operations (like displaying the preferences dialog) that require a | 
|  | 105     // Browser window. | 
|  | 106     Browser* GetBrowserWindow(); | 
|  | 107 | 
|  | 108     // Open an application in a new tab, opening a new window if needed. | 
|  | 109     virtual void ExecuteApplication(int application_id); | 
|  | 110 | 
|  | 111     // Updates the status icon's context menu entry corresponding to | 
|  | 112     // |extension| to use the icon associated with |extension| in the | 
|  | 113     // BackgroundApplicationListModel. | 
|  | 114     // TODO(rlp): Remove after creating one status icon. | 
|  | 115     void UpdateContextMenuEntryIcon(const Extension* extension); | 
|  | 116 | 
|  | 117     // Returns whether any of the extensions are background apps. | 
|  | 118     bool HasBackgroundApp(); | 
|  | 119   }; | 
|  | 120 | 
|  | 121   // Ideally we would want our BackgroundModeData to be scoped_ptrs, | 
|  | 122   // but since maps copy their entries, we can't used scoped_ptrs. | 
|  | 123   // Similarly, we can't just have a map of BackgroundModeData objects, | 
|  | 124   // since BackgroundModeData contains a scoped_ptr which once again | 
|  | 125   // can't be copied. So rather than using BackgroundModeData* which | 
|  | 126   // we'd have to remember to delete, we use the ref-counted linked_ptr | 
|  | 127   // which is similar to a shared_ptr. | 
|  | 128   typedef linked_ptr<BackgroundModeData> BackgroundModeInfo; | 
| 62 | 129 | 
| 63   // NotificationObserver implementation. | 130   // NotificationObserver implementation. | 
| 64   virtual void Observe(NotificationType type, | 131   virtual void Observe(NotificationType type, | 
| 65                        const NotificationSource& source, | 132                        const NotificationSource& source, | 
| 66                        const NotificationDetails& details); | 133                        const NotificationDetails& details) OVERRIDE; | 
| 67 |  | 
| 68   // SimpleMenuModel::Delegate implementation. |  | 
| 69   virtual bool IsCommandIdChecked(int command_id) const; |  | 
| 70   virtual bool IsCommandIdEnabled(int command_id) const; |  | 
| 71   virtual bool GetAcceleratorForCommandId(int command_id, |  | 
| 72                                           ui::Accelerator* accelerator); |  | 
| 73   virtual void ExecuteCommand(int command_id); |  | 
| 74 |  | 
| 75   // Open an application in a new tab, opening a new window if needed. |  | 
| 76   virtual void ExecuteApplication(int application_id); |  | 
| 77 | 134 | 
| 78   // BackgroundApplicationListModel::Observer implementation. | 135   // BackgroundApplicationListModel::Observer implementation. | 
| 79   virtual void OnApplicationDataChanged(const Extension* extension); | 136   virtual void OnApplicationDataChanged(const Extension* extension, | 
| 80   virtual void OnApplicationListChanged(); | 137                                         Profile* profile) OVERRIDE; | 
|  | 138   virtual void OnApplicationListChanged(Profile* profile) OVERRIDE; | 
| 81 | 139 | 
| 82   // Called when an extension is loaded to manage count of background apps. | 140   // Called when an extension is loaded to manage count of background apps. | 
| 83   void OnBackgroundAppLoaded(); | 141   void OnBackgroundAppLoaded(); | 
| 84 | 142 | 
| 85   // Called when an extension is unloaded to manage count of background apps. | 143   // Called when an extension is unloaded to manage count of background apps. | 
| 86   void OnBackgroundAppUnloaded(); | 144   void OnBackgroundAppUnloaded(); | 
| 87 | 145 | 
| 88   // Invoked when an extension is installed so we can ensure that | 146   // Invoked when an extension is installed so we can ensure that | 
| 89   // launch-on-startup is enabled if appropriate. |extension| can be NULL when | 147   // launch-on-startup is enabled if appropriate. |extension| can be NULL when | 
| 90   // called from unit tests. | 148   // called from unit tests. | 
| 91   void OnBackgroundAppInstalled(const Extension* extension); | 149   void OnBackgroundAppInstalled(const Extension* extension, Profile* profile); | 
| 92 | 150 | 
| 93   // Invoked when an extension is uninstalled so we can ensure that | 151   // Invoked when an extension is uninstalled so we can ensure that | 
| 94   // launch-on-startup is disabled if appropriate. | 152   // launch-on-startup is disabled if appropriate. | 
| 95   void OnBackgroundAppUninstalled(); | 153   void OnBackgroundAppUninstalled(Profile* profile); | 
| 96 | 154 | 
| 97   // Called to make sure that our launch-on-startup mode is properly set. | 155   // Called to make sure that our launch-on-startup mode is properly set. | 
| 98   // (virtual so we can override for tests). | 156   // (virtual so we can override for tests). | 
| 99   virtual void EnableLaunchOnStartup(bool should_launch); | 157   virtual void EnableLaunchOnStartup(bool should_launch); | 
| 100 | 158 | 
| 101   // Invoked when a background app is installed so we can display a | 159   // Invoked when a background app is installed so we can display a | 
| 102   // platform-specific notification to the user. | 160   // platform-specific notification to the user. | 
| 103   void DisplayAppInstalledNotification(const Extension* extension); | 161   void DisplayAppInstalledNotification(const Extension* extension, | 
|  | 162                                        Profile* profile); | 
| 104 | 163 | 
| 105   // Invoked to put Chrome in KeepAlive mode - chrome runs in the background | 164   // Invoked to put Chrome in KeepAlive mode - chrome runs in the background | 
| 106   // and has a status bar icon. | 165   // and has a status bar icon. | 
| 107   void StartBackgroundMode(); | 166   void StartBackgroundMode(); | 
| 108 | 167 | 
|  | 168   // Invoked to create status icons for any profiles currently running | 
|  | 169   // background apps so that there is a way to exit Chrome. | 
|  | 170   void InitStatusTrayIcons(); | 
|  | 171 | 
| 109   // Invoked to take Chrome out of KeepAlive mode - chrome stops running in | 172   // Invoked to take Chrome out of KeepAlive mode - chrome stops running in | 
| 110   // the background and removes its status bar icon. | 173   // the background and removes its status bar icon. | 
| 111   void EndBackgroundMode(); | 174   void EndBackgroundMode(); | 
| 112 | 175 | 
| 113   // If --no-startup-window is passed, BackgroundModeManager will manually keep | 176   // If --no-startup-window is passed, BackgroundModeManager will manually keep | 
| 114   // chrome running while waiting for apps to load. This is called when we no | 177   // chrome running while waiting for apps to load. This is called when we no | 
| 115   // longer need to do this (either because the user has chosen to exit chrome | 178   // longer need to do this (either because the user has chosen to exit chrome | 
| 116   // manually, or all apps have been loaded). | 179   // manually, or all apps have been loaded). | 
| 117   void EndKeepAliveForStartup(); | 180   void EndKeepAliveForStartup(); | 
| 118 | 181 | 
| 119   // Return an appropriate name for a Preferences menu entry.  Preferences is | 182   // Return an appropriate name for a Preferences menu entry.  Preferences is | 
| 120   // sometimes called Options or Settings. | 183   // sometimes called Options or Settings. | 
| 121   string16 GetPreferencesMenuLabel(); | 184   string16 GetPreferencesMenuLabel(); | 
| 122 | 185 | 
| 123   // Create a status tray icon to allow the user to shutdown Chrome when running | 186   // Create a status tray icon to allow the user to shutdown Chrome when running | 
| 124   // in background mode. Virtual to enable testing. | 187   // in background mode. Virtual to enable testing. | 
| 125   virtual void CreateStatusTrayIcon(); | 188   virtual void CreateStatusTrayIcon(Profile* profile); | 
| 126 | 189 | 
| 127   // Removes the status tray icon because we are exiting background mode. | 190   // Removes the status tray icon because we are exiting background mode. | 
| 128   // Virtual to enable testing. | 191   // Virtual to enable testing. | 
| 129   virtual void RemoveStatusTrayIcon(); | 192   virtual void RemoveStatusTrayIcon(Profile* profile); | 
| 130 | 193 | 
| 131   // Updates the status icon's context menu entry corresponding to |extension| | 194   // Updates the status icon's context menu entry corresponding to |extension| | 
| 132   // to use the icon associated with |extension| in the | 195   // to use the icon associated with |extension| in the | 
| 133   // BackgroundApplicationListModel. | 196   // BackgroundApplicationListModel. | 
| 134   void UpdateContextMenuEntryIcon(const Extension* extension); | 197   void UpdateContextMenuEntryIcon(const Extension* extension, Profile* profile); | 
| 135 | 198 | 
| 136   // Create a context menu, or replace/update an existing context menu, for the | 199   // Create a context menu, or replace/update an existing context menu, for the | 
| 137   // status tray icon which, among other things, allows the user to shutdown | 200   // status tray icon which, among other things, allows the user to shutdown | 
| 138   // Chrome when running in background mode. | 201   // Chrome when running in background mode. | 
| 139   virtual void UpdateStatusTrayIconContextMenu(); | 202   virtual void UpdateStatusTrayIconContextMenu(Profile* profile); | 
| 140 | 203 | 
| 141   // Returns a browser window, or creates one if none are open. Used by | 204   // Returns the BackgroundModeInfo associated with this profile. If it does | 
| 142   // operations (like displaying the preferences dialog) that require a Browser | 205   // not exist, returns an empty BackgroundModeInfo. | 
| 143   // window. | 206   BackgroundModeManager::BackgroundModeInfo GetBackgroundModeInfo( | 
| 144   Browser* GetBrowserWindow(); | 207       Profile* profile); | 
| 145 | 208 | 
| 146   // Returns true if the "Let chrome run in the background" pref is checked. | 209   // Returns true if the "Let chrome run in the background" pref is checked. | 
| 147   // (virtual to allow overriding in tests). | 210   // (virtual to allow overriding in tests). | 
| 148   virtual bool IsBackgroundModePrefEnabled(); | 211   virtual bool IsBackgroundModePrefEnabled(); | 
| 149 | 212 | 
| 150   // Turns off background mode if it's currently enabled. | 213   // Turns off background mode if it's currently enabled. | 
| 151   void DisableBackgroundMode(); | 214   void DisableBackgroundMode(); | 
| 152 | 215 | 
| 153   // Turns on background mode if it's currently disabled. | 216   // Turns on background mode if it's currently disabled. | 
| 154   void EnableBackgroundMode(); | 217   void EnableBackgroundMode(); | 
| 155 | 218 | 
| 156   // Returns true if background mode is permanently disabled for this chrome | 219   // Returns true if background mode is permanently disabled for this chrome | 
| 157   // session. | 220   // session. | 
| 158   static bool IsBackgroundModePermanentlyDisabled( | 221   static bool IsBackgroundModePermanentlyDisabled( | 
| 159       const CommandLine* command_line); | 222       const CommandLine* command_line); | 
| 160 | 223 | 
| 161   // Registrars for managing our change observers. | 224   // Registrars for managing our change observers. | 
| 162   NotificationRegistrar registrar_; | 225   NotificationRegistrar registrar_; | 
| 163   PrefChangeRegistrar pref_registrar_; | 226   PrefChangeRegistrar pref_registrar_; | 
| 164 | 227 | 
| 165   // The parent profile for this object. | 228   // The profile-keyed data for this background mode manager. Keyed on profile. | 
| 166   Profile* profile_; | 229   std::map<Profile*, BackgroundModeInfo> background_mode_data_; | 
| 167 | 230 | 
| 168   // The cached list of BackgroundApplications. | 231   // Reference to our status tray. If null, the platform doesn't support status | 
| 169   BackgroundApplicationListModel applications_; | 232   // icons. | 
|  | 233   StatusTray* status_tray_; | 
| 170 | 234 | 
| 171   // The number of background apps currently loaded. | 235   // The number of background apps currently loaded. This is the total over | 
|  | 236   // all profiles. | 
| 172   int background_app_count_; | 237   int background_app_count_; | 
| 173 | 238 | 
| 174   // Reference to our status icon's context menu (if any) - owned by the |  | 
| 175   // status_icon_ |  | 
| 176   ui::SimpleMenuModel* context_menu_; |  | 
| 177 |  | 
| 178   // Set to the position of the first application entry in the status icon's |  | 
| 179   // context menu. |  | 
| 180   int context_menu_application_offset_; |  | 
| 181 |  | 
| 182   // Set to true when we are running in background mode. Allows us to track our | 239   // Set to true when we are running in background mode. Allows us to track our | 
| 183   // current background state so we can take the appropriate action when the | 240   // current background state so we can take the appropriate action when the | 
| 184   // user disables/enables background mode via preferences. | 241   // user disables/enables background mode via preferences. | 
| 185   bool in_background_mode_; | 242   bool in_background_mode_; | 
| 186 | 243 | 
| 187   // Set when we are keeping chrome running during the startup process - this | 244   // Set when we are keeping chrome running during the startup process - this | 
| 188   // is required when running with the --no-startup-window flag, as otherwise | 245   // is required when running with the --no-startup-window flag, as otherwise | 
| 189   // chrome would immediately exit due to having no open windows. | 246   // chrome would immediately exit due to having no open windows. | 
| 190   bool keep_alive_for_startup_; | 247   bool keep_alive_for_startup_; | 
| 191 | 248 | 
| 192   // Reference to our status tray (owned by our parent profile). If null, the |  | 
| 193   // platform doesn't support status icons. |  | 
| 194   StatusTray* status_tray_; |  | 
| 195 |  | 
| 196   // Reference to our status icon (if any) - owned by the StatusTray. |  | 
| 197   StatusIcon* status_icon_; |  | 
| 198 |  | 
| 199   DISALLOW_COPY_AND_ASSIGN(BackgroundModeManager); | 249   DISALLOW_COPY_AND_ASSIGN(BackgroundModeManager); | 
| 200 }; | 250 }; | 
| 201 | 251 | 
| 202 #endif  // CHROME_BROWSER_BACKGROUND_MODE_MANAGER_H_ | 252 #endif  // CHROME_BROWSER_BACKGROUND_MODE_MANAGER_H_ | 
| OLD | NEW | 
|---|