1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/cocoa/apps/quit_with_apps_controller_mac.h"
7 #include "base/command_line.h"
8 #include "base/i18n/number_formatting.h"
9 #include "base/prefs/pref_registry_simple.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/strings/sys_string_conversions.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/apps/app_window_registry_util.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/lifetime/application_lifetime.h"
17 #include "chrome/browser/notifications/notification.h"
18 #include "chrome/browser/notifications/notification_ui_manager.h"
19 #include "chrome/browser/profiles/profile_manager.h"
20 #include "chrome/browser/ui/browser_iterator.h"
21 #include "chrome/browser/ui/browser_window.h"
22 #include "chrome/browser/web_applications/web_app.h"
23 #include "chrome/common/chrome_switches.h"
24 #include "chrome/common/pref_names.h"
25 #include "chrome/grit/chromium_strings.h"
26 #include "chrome/grit/generated_resources.h"
27 #include "content/public/browser/notification_service.h"
28 #include "extensions/browser/app_window/app_window.h"
29 #include "extensions/browser/app_window/native_app_window.h"
30 #include "extensions/browser/extension_registry.h"
31 #include "extensions/common/extension.h"
32 #include "grit/chrome_unscaled_resources.h"
33 #include "ui/base/l10n/l10n_util.h"
34 #include "ui/base/l10n/l10n_util_mac.h"
35 #include "ui/base/resource/resource_bundle.h"
37 using extensions::ExtensionRegistry
;
39 const char kQuitWithAppsOriginUrl
[] = "chrome://quit-with-apps";
40 const int kQuitAllAppsButtonIndex
= 0;
41 const int kDontShowAgainButtonIndex
= 1;
43 const char QuitWithAppsController::kQuitWithAppsNotificationID
[] =
46 QuitWithAppsController::QuitWithAppsController()
47 : notification_profile_(NULL
), suppress_for_session_(false) {
48 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
50 hosted_app_quit_notification_
=
51 base::CommandLine::ForCurrentProcess()->HasSwitch(
52 switches::kHostedAppQuitNotification
);
54 // There is only ever one notification to replace, so use the same tag
56 std::string tag
= id();
58 message_center::ButtonInfo
quit_apps_button_info(
59 l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_QUIT_LABEL
));
60 message_center::RichNotificationData rich_notification_data
;
61 rich_notification_data
.buttons
.push_back(quit_apps_button_info
);
62 if (!hosted_app_quit_notification_
) {
63 message_center::ButtonInfo
suppression_button_info(
64 l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_SUPPRESSION_LABEL
));
65 rich_notification_data
.buttons
.push_back(suppression_button_info
);
68 notification_
.reset(new Notification(
69 message_center::NOTIFICATION_TYPE_SIMPLE
,
70 l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_TITLE
),
71 l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_EXPLANATION
),
72 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
73 IDR_PRODUCT_LOGO_128
),
74 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT
,
75 kQuitWithAppsNotificationID
),
76 l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_NOTIFICATION_DISPLAY_SOURCE
),
77 GURL(kQuitWithAppsOriginUrl
), tag
, rich_notification_data
, this));
80 QuitWithAppsController::~QuitWithAppsController() {}
82 void QuitWithAppsController::Display() {}
84 void QuitWithAppsController::Close(bool by_user
) {
86 suppress_for_session_
= hosted_app_quit_notification_
? false : true;
90 void QuitWithAppsController::Click() {
91 g_browser_process
->notification_ui_manager()->CancelById(
92 id(), NotificationUIManager::GetProfileID(notification_profile_
));
95 void QuitWithAppsController::ButtonClick(int button_index
) {
96 g_browser_process
->notification_ui_manager()->CancelById(
97 id(), NotificationUIManager::GetProfileID(notification_profile_
));
99 if (button_index
== kQuitAllAppsButtonIndex
) {
100 if (hosted_app_quit_notification_
) {
101 content::NotificationService::current()->Notify(
102 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST
,
103 content::NotificationService::AllSources(),
104 content::NotificationService::NoDetails());
105 chrome::CloseAllBrowsers();
107 AppWindowRegistryUtil::CloseAllAppWindows();
108 } else if (button_index
== kDontShowAgainButtonIndex
&&
109 !hosted_app_quit_notification_
) {
110 g_browser_process
->local_state()->SetBoolean(
111 prefs::kNotifyWhenAppsKeepChromeAlive
, false);
115 std::string
QuitWithAppsController::id() const {
116 return kQuitWithAppsNotificationID
;
119 bool QuitWithAppsController::ShouldQuit() {
120 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
122 // Quit immediately if this is a test.
123 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType
) &&
124 !base::CommandLine::ForCurrentProcess()->HasSwitch(
125 switches::kAppsKeepChromeAliveInTests
)) {
129 // Quit immediately if Chrome is restarting.
130 if (g_browser_process
->local_state()->GetBoolean(
131 prefs::kRestartLastSessionOnShutdown
)) {
135 if (hosted_app_quit_notification_
) {
136 bool hosted_apps_open
= false;
137 const BrowserList
* browser_list
=
138 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE
);
139 for (Browser
* browser
: *browser_list
) {
140 if (!browser
->is_app())
143 ExtensionRegistry
* registry
= ExtensionRegistry::Get(browser
->profile());
144 const extensions::Extension
* extension
= registry
->GetExtensionById(
145 web_app::GetExtensionIdFromApplicationName(browser
->app_name()),
146 ExtensionRegistry::ENABLED
);
147 if (extension
->is_hosted_app()) {
148 hosted_apps_open
= true;
153 // Quit immediately if there are no packaged app windows or hosted apps open
154 // or the confirmation has been suppressed. Ignore panels.
155 if (!AppWindowRegistryUtil::IsAppWindowVisibleInAnyProfile(
156 extensions::AppWindow::WINDOW_TYPE_DEFAULT
) &&
161 // Quit immediately if there are no windows or the confirmation has been
163 if (!AppWindowRegistryUtil::IsAppWindowVisibleInAnyProfile(
164 extensions::AppWindow::WINDOW_TYPE_DEFAULT
))
168 // If there are browser windows, and this notification has been suppressed for
169 // this session or permanently, then just return false to prevent Chrome from
170 // quitting. If there are no browser windows, always show the notification.
171 bool suppress_always
= !g_browser_process
->local_state()->GetBoolean(
172 prefs::kNotifyWhenAppsKeepChromeAlive
);
173 if (!chrome::BrowserIterator().done() &&
174 (suppress_for_session_
|| suppress_always
)) {
178 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
179 DCHECK(profile_manager
);
181 std::vector
<Profile
*> profiles(profile_manager
->GetLoadedProfiles());
182 DCHECK(profiles
.size());
184 // Delete any existing notification to ensure this one is shown. If
185 // notification_profile_ is NULL then it must be that no notification has been
186 // added by this class yet.
187 if (notification_profile_
) {
188 g_browser_process
->notification_ui_manager()->CancelById(
189 id(), NotificationUIManager::GetProfileID(notification_profile_
));
191 notification_profile_
= profiles
[0];
192 g_browser_process
->notification_ui_manager()->Add(*notification_
,
193 notification_profile_
);
195 // Always return false, the notification UI can be used to quit all apps which
196 // will cause Chrome to quit.
201 void QuitWithAppsController::RegisterPrefs(PrefRegistrySimple
* registry
) {
202 registry
->RegisterBooleanPref(prefs::kNotifyWhenAppsKeepChromeAlive
, true);