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