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