Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(703)

Side by Side Diff: chrome/browser/background_mode_manager.cc

Issue 3198007: BackgroudModeManager now listens for APP_TERMINATING and shuts down background (Closed)
Patch Set: Created 10 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 #include "app/l10n_util.h" 5 #include "app/l10n_util.h"
6 #include "app/resource_bundle.h" 6 #include "app/resource_bundle.h"
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "chrome/app/chrome_dll_resource.h" 8 #include "chrome/app/chrome_dll_resource.h"
9 #include "chrome/browser/background_mode_manager.h" 9 #include "chrome/browser/background_mode_manager.h"
10 #include "chrome/browser/browser_list.h" 10 #include "chrome/browser/browser_list.h"
(...skipping 17 matching lines...) Expand all
28 background_app_count_(0), 28 background_app_count_(0),
29 status_tray_(NULL), 29 status_tray_(NULL),
30 status_icon_(NULL) { 30 status_icon_(NULL) {
31 // If background mode is disabled for unittests, just exit - don't listen for 31 // If background mode is disabled for unittests, just exit - don't listen for
32 // any notifications. 32 // any notifications.
33 if (CommandLine::ForCurrentProcess()->HasSwitch( 33 if (CommandLine::ForCurrentProcess()->HasSwitch(
34 switches::kDisableBackgroundMode)) 34 switches::kDisableBackgroundMode))
35 return; 35 return;
36 36
37 // If the -keep-alive-for-test flag is passed, then always keep chrome running 37 // If the -keep-alive-for-test flag is passed, then always keep chrome running
38 // in the background until the user explicitly terminates it. 38 // in the background until the user explicitly terminates it, by acting as if
39 if (CommandLine::ForCurrentProcess()->HasSwitch( 39 // we loaded a background app.
40 switches::kKeepAliveForTest)) { 40 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kKeepAliveForTest))
41 StartBackgroundMode(); 41 OnBackgroundAppLoaded();
42 registrar_.Add(this, NotificationType::APP_TERMINATING,
43 NotificationService::AllSources());
44 }
45 42
46 // When an extension is installed, make sure launch on startup is properly 43 // When an extension is installed, make sure launch on startup is properly
47 // set if appropriate. Likewise, turn off launch on startup when the last 44 // set if appropriate. Likewise, turn off launch on startup when the last
48 // background app is uninstalled. 45 // background app is uninstalled.
49 registrar_.Add(this, NotificationType::EXTENSION_INSTALLED, 46 registrar_.Add(this, NotificationType::EXTENSION_INSTALLED,
50 Source<Profile>(profile)); 47 Source<Profile>(profile));
51 registrar_.Add(this, NotificationType::EXTENSION_UNINSTALLED, 48 registrar_.Add(this, NotificationType::EXTENSION_UNINSTALLED,
52 Source<Profile>(profile)); 49 Source<Profile>(profile));
53 // Listen for when extensions are loaded/unloaded so we can track the 50 // Listen for when extensions are loaded/unloaded so we can track the
54 // number of background apps. 51 // number of background apps.
55 registrar_.Add(this, NotificationType::EXTENSION_LOADED, 52 registrar_.Add(this, NotificationType::EXTENSION_LOADED,
56 Source<Profile>(profile)); 53 Source<Profile>(profile));
57 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, 54 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED,
58 Source<Profile>(profile)); 55 Source<Profile>(profile));
59 56
60 // Check for the presence of background apps after all extensions have been 57 // Check for the presence of background apps after all extensions have been
61 // loaded, to handle the case where an extension has been manually removed 58 // loaded, to handle the case where an extension has been manually removed
62 // while Chrome was not running. 59 // while Chrome was not running.
63 registrar_.Add(this, NotificationType::EXTENSIONS_READY, 60 registrar_.Add(this, NotificationType::EXTENSIONS_READY,
64 Source<Profile>(profile)); 61 Source<Profile>(profile));
65 62
63 // Listen for the application shutting down so we can decrement our KeepAlive
64 // count.
65 registrar_.Add(this, NotificationType::APP_TERMINATING,
66 NotificationService::AllSources());
67
68
66 } 69 }
67 70
68 BackgroundModeManager::~BackgroundModeManager() { 71 BackgroundModeManager::~BackgroundModeManager() {
69 // If we're going away, remove our status tray icon so we don't get any events 72 // If we're going away, remove our status tray icon so we don't get any events
70 // from it. 73 // from it.
71 RemoveStatusTrayIcon(); 74 RemoveStatusTrayIcon();
72 } 75 }
73 76
74 bool BackgroundModeManager::IsBackgroundModeEnabled() { 77 bool BackgroundModeManager::IsBackgroundModeEnabled() {
75 return profile_->GetPrefs()->GetBoolean(prefs::kBackgroundModeEnabled); 78 return profile_->GetPrefs()->GetBoolean(prefs::kBackgroundModeEnabled);
(...skipping 17 matching lines...) Expand all
93 break; 96 break;
94 case NotificationType::EXTENSION_INSTALLED: 97 case NotificationType::EXTENSION_INSTALLED:
95 if (IsBackgroundApp(Details<Extension>(details).ptr())) 98 if (IsBackgroundApp(Details<Extension>(details).ptr()))
96 OnBackgroundAppInstalled(); 99 OnBackgroundAppInstalled();
97 break; 100 break;
98 case NotificationType::EXTENSION_UNINSTALLED: 101 case NotificationType::EXTENSION_UNINSTALLED:
99 if (IsBackgroundApp(Details<Extension>(details).ptr())) 102 if (IsBackgroundApp(Details<Extension>(details).ptr()))
100 OnBackgroundAppUninstalled(); 103 OnBackgroundAppUninstalled();
101 break; 104 break;
102 case NotificationType::APP_TERMINATING: 105 case NotificationType::APP_TERMINATING:
103 // Performing an explicit shutdown, so allow the browser process to exit. 106 // Performing an explicit shutdown, so exit background mode if we were in
104 // (we only listen for this notification if the keep-alive-for-test flag 107 // background mode.
105 // is passed). 108 if (background_app_count_ > 0 && IsBackgroundModeEnabled())
106 DCHECK(CommandLine::ForCurrentProcess()->HasSwitch( 109 EndBackgroundMode();
107 switches::kKeepAliveForTest)); 110 // Shutting down, so don't listen for any more notifications so we don't
108 EndBackgroundMode(); 111 // try to re-enter/exit background mode again.
112 registrar_.RemoveAll();
109 break; 113 break;
110 default: 114 default:
111 NOTREACHED(); 115 NOTREACHED();
112 break; 116 break;
113 } 117 }
114 } 118 }
115 119
116 bool BackgroundModeManager::IsBackgroundApp(Extension* extension) { 120 bool BackgroundModeManager::IsBackgroundApp(Extension* extension) {
117 return extension->HasApiPermission(Extension::kBackgroundPermission); 121 return extension->HasApiPermission(Extension::kBackgroundPermission);
118 } 122 }
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
240 default: 244 default:
241 NOTREACHED(); 245 NOTREACHED();
242 break; 246 break;
243 } 247 }
244 } 248 }
245 249
246 // static 250 // static
247 void BackgroundModeManager::RegisterUserPrefs(PrefService* prefs) { 251 void BackgroundModeManager::RegisterUserPrefs(PrefService* prefs) {
248 prefs->RegisterBooleanPref(prefs::kBackgroundModeEnabled, true); 252 prefs->RegisterBooleanPref(prefs::kBackgroundModeEnabled, true);
249 } 253 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698