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 "chrome/grit/google_chrome_strings.h"
28 #include "content/public/browser/notification_service.h"
29 #include "extensions/browser/app_window/app_window.h"
30 #include "extensions/browser/app_window/native_app_window.h"
31 #include "extensions/browser/extension_registry.h"
32 #include "extensions/common/extension.h"
33 #include "grit/chrome_unscaled_resources.h"
34 #include "ui/base/l10n/l10n_util.h"
35 #include "ui/base/l10n/l10n_util_mac.h"
36 #include "ui/base/resource/resource_bundle.h"
38 using extensions::ExtensionRegistry
;
40 const char kQuitWithAppsOriginUrl
[] = "chrome://quit-with-apps";
41 const int kQuitAllAppsButtonIndex
= 0;
42 const int kDontShowAgainButtonIndex
= 1;
44 const char QuitWithAppsController::kQuitWithAppsNotificationID
[] =
47 QuitWithAppsController::QuitWithAppsController()
48 : notification_profile_(NULL
), suppress_for_session_(false) {
49 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
51 hosted_app_quit_notification_
=
52 base::CommandLine::ForCurrentProcess()->HasSwitch(
53 switches::kHostedAppQuitNotification
);
55 // There is only ever one notification to replace, so use the same replace_id
57 base::string16 replace_id
= base::UTF8ToUTF16(id());
59 message_center::ButtonInfo
quit_apps_button_info(
60 l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_QUIT_LABEL
));
61 message_center::RichNotificationData rich_notification_data
;
62 rich_notification_data
.buttons
.push_back(quit_apps_button_info
);
63 if (!hosted_app_quit_notification_
) {
64 message_center::ButtonInfo
suppression_button_info(
65 l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_SUPPRESSION_LABEL
));
66 rich_notification_data
.buttons
.push_back(suppression_button_info
);
69 notification_
.reset(new Notification(
70 message_center::NOTIFICATION_TYPE_SIMPLE
,
71 GURL(kQuitWithAppsOriginUrl
),
72 l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_TITLE
),
73 l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_EXPLANATION
),
74 ui::ResourceBundle::GetSharedInstance().GetImageNamed(
75 IDR_PRODUCT_LOGO_128
),
76 blink::WebTextDirectionDefault
,
77 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT
,
78 kQuitWithAppsNotificationID
),
79 l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_NOTIFICATION_DISPLAY_SOURCE
),
81 rich_notification_data
,
85 QuitWithAppsController::~QuitWithAppsController() {}
87 void QuitWithAppsController::Display() {}
89 void QuitWithAppsController::Close(bool by_user
) {
91 suppress_for_session_
= hosted_app_quit_notification_
? false : true;
95 void QuitWithAppsController::Click() {
96 g_browser_process
->notification_ui_manager()->CancelById(
97 id(), NotificationUIManager::GetProfileID(notification_profile_
));
100 void QuitWithAppsController::ButtonClick(int button_index
) {
101 g_browser_process
->notification_ui_manager()->CancelById(
102 id(), NotificationUIManager::GetProfileID(notification_profile_
));
104 if (button_index
== kQuitAllAppsButtonIndex
) {
105 if (hosted_app_quit_notification_
) {
106 content::NotificationService::current()->Notify(
107 chrome::NOTIFICATION_CLOSE_ALL_BROWSERS_REQUEST
,
108 content::NotificationService::AllSources(),
109 content::NotificationService::NoDetails());
110 chrome::CloseAllBrowsers();
112 AppWindowRegistryUtil::CloseAllAppWindows();
113 } else if (button_index
== kDontShowAgainButtonIndex
&&
114 !hosted_app_quit_notification_
) {
115 g_browser_process
->local_state()->SetBoolean(
116 prefs::kNotifyWhenAppsKeepChromeAlive
, false);
120 std::string
QuitWithAppsController::id() const {
121 return kQuitWithAppsNotificationID
;
124 bool QuitWithAppsController::ShouldQuit() {
125 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
127 // Quit immediately if this is a test.
128 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType
) &&
129 !base::CommandLine::ForCurrentProcess()->HasSwitch(
130 switches::kAppsKeepChromeAliveInTests
)) {
134 if (hosted_app_quit_notification_
) {
135 bool hosted_apps_open
= false;
136 const BrowserList
* browser_list
=
137 BrowserList::GetInstance(chrome::HOST_DESKTOP_TYPE_NATIVE
);
138 for (Browser
* browser
: *browser_list
) {
139 if (!browser
->is_app())
142 ExtensionRegistry
* registry
= ExtensionRegistry::Get(browser
->profile());
143 const extensions::Extension
* extension
= registry
->GetExtensionById(
144 web_app::GetExtensionIdFromApplicationName(browser
->app_name()),
145 ExtensionRegistry::ENABLED
);
146 if (extension
->is_hosted_app()) {
147 hosted_apps_open
= true;
152 // Quit immediately if there are no packaged app windows or hosted apps open
153 // or the confirmation has been suppressed. Ignore panels.
154 if (!AppWindowRegistryUtil::IsAppWindowVisibleInAnyProfile(
155 extensions::AppWindow::WINDOW_TYPE_DEFAULT
) &&
160 // Quit immediately if there are no windows or the confirmation has been
162 if (!AppWindowRegistryUtil::IsAppWindowVisibleInAnyProfile(
163 extensions::AppWindow::WINDOW_TYPE_DEFAULT
))
167 // If there are browser windows, and this notification has been suppressed for
168 // this session or permanently, then just return false to prevent Chrome from
169 // quitting. If there are no browser windows, or Chrome is restarting, always
170 // show the notification.
171 bool is_restart
= g_browser_process
->local_state()->GetBoolean(
172 prefs::kRestartLastSessionOnShutdown
);
173 bool suppress_always
= !g_browser_process
->local_state()->GetBoolean(
174 prefs::kNotifyWhenAppsKeepChromeAlive
);
175 if (!chrome::BrowserIterator().done() &&
177 (suppress_for_session_
|| suppress_always
)) {
181 ProfileManager
* profile_manager
= g_browser_process
->profile_manager();
182 DCHECK(profile_manager
);
184 std::vector
<Profile
*> profiles(profile_manager
->GetLoadedProfiles());
185 DCHECK(profiles
.size());
187 // Delete any existing notification to ensure this one is shown. If
188 // notification_profile_ is NULL then it must be that no notification has been
189 // added by this class yet.
190 if (notification_profile_
) {
191 g_browser_process
->notification_ui_manager()->CancelById(
192 id(), NotificationUIManager::GetProfileID(notification_profile_
));
194 notification_profile_
= profiles
[0];
195 g_browser_process
->notification_ui_manager()->Add(*notification_
,
196 notification_profile_
);
198 // Always return false, the notification UI can be used to quit all apps which
199 // will cause Chrome to quit.
204 void QuitWithAppsController::RegisterPrefs(PrefRegistrySimple
* registry
) {
205 registry
->RegisterBooleanPref(prefs::kNotifyWhenAppsKeepChromeAlive
, true);