1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "app/l10n_util.h"
#include "app/resource_bundle.h"
#include "base/command_line.h"
#include "chrome/browser/background_mode_manager.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/metrics/user_metrics.h"
#include "chrome/browser/pref_service.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/status_icons/status_icon.h"
#include "chrome/browser/status_icons/status_tray.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/notification_type.h"
#include "chrome/common/pref_names.h"
#include "grit/browser_resources.h"
#include "grit/chromium_strings.h"
#include "grit/theme_resources.h"
BackgroundModeManager::BackgroundModeManager(Profile* profile)
: profile_(profile),
background_app_count_(0),
status_tray_(NULL) {
// If background mode is disabled for unittests, just exit - don't listen for
// any notifications.
if (CommandLine::ForCurrentProcess()->HasSwitch(
switches::kDisableBackgroundMode))
return;
// Only need status icons on windows/linux. ChromeOS doesn't allow exiting
// Chrome and Mac can use the dock icon instead.
#if !defined(OS_MACOSX) && !defined(OS_CHROMEOS)
status_tray_ = profile->GetStatusTray();
#endif
// If the -keep-alive-for-test flag is passed, then always keep chrome running
// in the background until the user explicitly terminates it.
if (CommandLine::ForCurrentProcess()->HasSwitch(
switches::kKeepAliveForTest)) {
StartBackgroundMode();
registrar_.Add(this, NotificationType::APP_TERMINATING,
NotificationService::AllSources());
}
// When an extension is installed, make sure launch on startup is properly
// set if appropriate. Likewise, turn off launch on startup when the last
// background app is uninstalled.
registrar_.Add(this, NotificationType::EXTENSION_INSTALLED,
Source<Profile>(profile));
registrar_.Add(this, NotificationType::EXTENSION_UNINSTALLED,
Source<Profile>(profile));
// Listen for when extensions are loaded/unloaded so we can track the
// number of background apps.
registrar_.Add(this, NotificationType::EXTENSION_LOADED,
Source<Profile>(profile));
registrar_.Add(this, NotificationType::EXTENSION_UNLOADED,
Source<Profile>(profile));
// Check for the presence of background apps after all extensions have been
// loaded, to handle the case where an extension has been manually removed
// while Chrome was not running.
registrar_.Add(this, NotificationType::EXTENSIONS_READY,
Source<Profile>(profile));
}
BackgroundModeManager::~BackgroundModeManager() {
}
bool BackgroundModeManager::IsBackgroundModeEnabled() {
return profile_->GetPrefs()->GetBoolean(prefs::kBackgroundModeEnabled);
}
void BackgroundModeManager::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
switch (type.value) {
case NotificationType::EXTENSIONS_READY:
EnableLaunchOnStartup(IsBackgroundModeEnabled() &&
background_app_count_ > 0);
break;
case NotificationType::EXTENSION_LOADED:
if (IsBackgroundApp(Details<Extension>(details).ptr()))
OnBackgroundAppLoaded();
break;
case NotificationType::EXTENSION_UNLOADED:
if (IsBackgroundApp(Details<Extension>(details).ptr()))
OnBackgroundAppUnloaded();
break;
case NotificationType::EXTENSION_INSTALLED:
if (IsBackgroundApp(Details<Extension>(details).ptr()))
OnBackgroundAppInstalled();
break;
case NotificationType::EXTENSION_UNINSTALLED:
if (IsBackgroundApp(Details<Extension>(details).ptr()))
OnBackgroundAppUninstalled();
break;
case NotificationType::APP_TERMINATING:
// Performing an explicit shutdown, so allow the browser process to exit.
// (we only listen for this notification if the keep-alive-for-test flag
// is passed).
DCHECK(CommandLine::ForCurrentProcess()->HasSwitch(
switches::kKeepAliveForTest));
EndBackgroundMode();
break;
default:
NOTREACHED();
break;
}
}
bool BackgroundModeManager::IsBackgroundApp(Extension* extension) {
return extension->HasApiPermission(Extension::kBackgroundPermission);
}
void BackgroundModeManager::OnBackgroundAppLoaded() {
// When a background app loads, increment our count and also enable
// KeepAlive mode if the preference is set.
background_app_count_++;
if (background_app_count_ == 1 && IsBackgroundModeEnabled())
StartBackgroundMode();
}
void BackgroundModeManager::StartBackgroundMode() {
// Put ourselves in KeepAlive mode and create a status tray icon.
BrowserList::StartKeepAlive();
// Display a status icon to exit Chrome.
CreateStatusTrayIcon();
}
void BackgroundModeManager::OnBackgroundAppUnloaded() {
// When a background app unloads, decrement our count and also end
// KeepAlive mode if appropriate.
background_app_count_--;
DCHECK(background_app_count_ == 0);
if (background_app_count_ == 0 && IsBackgroundModeEnabled())
EndBackgroundMode();
}
void BackgroundModeManager::EndBackgroundMode() {
// End KeepAlive mode and blow away our status tray icon.
BrowserList::EndKeepAlive();
RemoveStatusTrayIcon();
}
void BackgroundModeManager::OnBackgroundAppInstalled() {
// We're installing a background app. If this is the first background app
// being installed, make sure we are set to launch on startup.
if (IsBackgroundModeEnabled() && background_app_count_ == 0)
EnableLaunchOnStartup(true);
}
void BackgroundModeManager::OnBackgroundAppUninstalled() {
// When uninstalling a background app, disable launch on startup if it's the
// last one.
if (IsBackgroundModeEnabled() && background_app_count_ == 1)
EnableLaunchOnStartup(false);
}
void BackgroundModeManager::EnableLaunchOnStartup(bool should_launch) {
// TODO(atwilson): Add platform-specific code to enable/disable launch on
// startup.
}
void BackgroundModeManager::CreateStatusTrayIcon() {
// If the platform doesn't support status icons, or we've already created
// our status icon, just return.
if (!status_tray_ || status_icon_)
return;
status_icon_ = status_tray_->CreateStatusIcon();
if (!status_icon_)
return;
// Set the image and add ourselves as a click observer on it
SkBitmap* bitmap = ResourceBundle::GetSharedInstance().GetBitmapNamed(
IDR_STATUS_TRAY_ICON);
status_icon_->SetImage(*bitmap);
status_icon_->SetToolTip(l10n_util::GetStringUTF16(IDS_PRODUCT_NAME));
status_icon_->AddObserver(this);
}
void BackgroundModeManager::RemoveStatusTrayIcon() {
if (status_icon_)
status_tray_->RemoveStatusIcon(status_icon_);
status_icon_ = NULL;
}
void BackgroundModeManager::OnClicked() {
UserMetrics::RecordAction(UserMetricsAction("Exit"), profile_);
BrowserList::CloseAllBrowsersAndExit();
}
// static
void BackgroundModeManager::RegisterUserPrefs(PrefService* prefs) {
prefs->RegisterBooleanPref(prefs::kBackgroundModeEnabled, true);
}
|