Chromium Code Reviews| Index: chrome/browser/ui/cocoa/apps/quit_with_apps_controller_mac.cc |
| diff --git a/chrome/browser/ui/cocoa/apps/quit_with_apps_controller_mac.cc b/chrome/browser/ui/cocoa/apps/quit_with_apps_controller_mac.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..87f3db7c182eba6c96b70d3341d866b628dcd9a5 |
| --- /dev/null |
| +++ b/chrome/browser/ui/cocoa/apps/quit_with_apps_controller_mac.cc |
| @@ -0,0 +1,166 @@ |
| +// Copyright 2014 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 "chrome/browser/ui/cocoa/apps/quit_with_apps_controller_mac.h" |
| + |
| +#include "apps/app_window.h" |
| +#include "apps/app_window_registry.h" |
| +#include "apps/ui/native_app_window.h" |
| +#include "base/command_line.h" |
| +#include "base/i18n/number_formatting.h" |
| +#include "base/prefs/pref_registry_simple.h" |
| +#include "base/prefs/pref_service.h" |
| +#include "base/strings/sys_string_conversions.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/notifications/notification.h" |
| +#include "chrome/browser/notifications/notification_ui_manager.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| +#include "chrome/browser/ui/browser_iterator.h" |
| +#include "chrome/common/chrome_switches.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "extensions/common/extension.h" |
| +#include "grit/chromium_strings.h" |
| +#include "grit/generated_resources.h" |
| +#include "grit/google_chrome_strings.h" |
| +#include "grit/theme_resources.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| +#include "ui/base/l10n/l10n_util_mac.h" |
| +#include "ui/base/resource/resource_bundle.h" |
| + |
| +namespace { |
| + |
| +const char kQuitWithAppsOriginUrl[] = "chrome://quit-with-apps"; |
| +const int kQuitAllAppsButtonIndex = 0; |
| +const int kDontShowAgainButtonIndex = 1; |
| + |
| +} // namespace |
| + |
| +const char QuitWithAppsController::kQuitWithAppsNotificationID[] = |
| + "quit-with-apps"; |
| + |
| +QuitWithAppsController::QuitWithAppsController() |
| + : suppress_for_session_(false) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + |
| + // There is only ever one notification to replace, so use the same replace_id |
| + // each time. |
| + base::string16 replace_id = base::UTF8ToUTF16(id()); |
| + |
| + message_center::ButtonInfo quit_apps_button_info( |
| + l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_QUIT_LABEL)); |
| + message_center::ButtonInfo suppression_button_info( |
| + l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_SUPPRESSION_LABEL)); |
| + message_center::RichNotificationData rich_notification_data; |
| + rich_notification_data.buttons.push_back(quit_apps_button_info); |
| + rich_notification_data.buttons.push_back(suppression_button_info); |
| + |
| + notification_.reset(new Notification( |
| + message_center::NOTIFICATION_TYPE_SIMPLE, |
| + GURL(kQuitWithAppsOriginUrl), |
| + l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_TITLE), |
| + l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_EXPLANATION), |
| + ui::ResourceBundle::GetSharedInstance().GetImageNamed( |
| + IDR_APP_DEFAULT_ICON), |
| + blink::WebTextDirectionDefault, |
| + message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, |
| + kQuitWithAppsNotificationID), |
| + l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_NOTIFICATION_DISPLAY_SOURCE), |
| + replace_id, |
| + rich_notification_data, |
| + this)); |
| +} |
| + |
| +QuitWithAppsController::~QuitWithAppsController() {} |
| + |
| +void QuitWithAppsController::Display() {} |
| + |
| +void QuitWithAppsController::Error() { |
| + LOG(ERROR) << "Error displaying notification while quitting with apps open."; |
| +} |
| + |
| +void QuitWithAppsController::Close(bool by_user) { |
| + if (by_user) |
| + suppress_for_session_ = true; |
| +} |
| + |
| +void QuitWithAppsController::Click() { |
| + g_browser_process->notification_ui_manager()->CancelById(id()); |
| +} |
| + |
| +void QuitWithAppsController::ButtonClick(int button_index) { |
| + typedef apps::AppWindowRegistry::AppWindowList AppWindowList; |
| + |
| + g_browser_process->notification_ui_manager()->CancelById(id()); |
| + if (button_index == kQuitAllAppsButtonIndex) { |
| + ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| + if (!profile_manager) |
| + return; |
| + |
| + std::vector<Profile*> profiles(profile_manager->GetLoadedProfiles()); |
| + for (size_t i = 0; i < profiles.size(); ++i) { |
| + const AppWindowList& windows = |
| + apps::AppWindowRegistry::Get(profiles[i])->app_windows(); |
| + while (windows.size()) |
|
tapted
2014/04/03 01:45:38
(once moved): !windows.empty() -- AppWindowList is
jackhou1
2014/04/03 03:07:07
Done.
|
| + windows.front()->GetBaseWindow()->Close(); |
| + } |
| + } else if (button_index == kDontShowAgainButtonIndex) { |
| + g_browser_process->local_state()->SetBoolean( |
| + prefs::kNotifyWhenAppsKeepChromeAlive, false); |
| + } |
| +} |
| + |
| +content::RenderViewHost* QuitWithAppsController::GetRenderViewHost() const { |
| + return NULL; |
| +} |
| + |
| +std::string QuitWithAppsController::id() const { |
| + return kQuitWithAppsNotificationID; |
| +} |
| + |
| +bool QuitWithAppsController::ShouldQuit() { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + |
| + if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| + switches::kAppsKeepChromeAlive)) { |
| + g_browser_process->local_state()->ClearPref( |
| + prefs::kNotifyWhenAppsKeepChromeAlive); |
| + return true; |
| + } |
| + |
| + // Quit immediately if there are no windows or the confirmation has been |
| + // suppressed. |
| + if (!apps::AppWindowRegistry::IsAppWindowRegisteredInAnyProfile(0)) |
| + return true; |
| + |
| + // If there are browser windows, and this notification has been suppressed for |
| + // this session or permanently, then just return false to prevent Chrome from |
| + // quitting. If there are no browser windows, always show the notification. |
| + bool suppress_always = !g_browser_process->local_state()->GetBoolean( |
| + prefs::kNotifyWhenAppsKeepChromeAlive); |
| + if (!chrome::BrowserIterator().done() && |
| + (suppress_for_session_ || suppress_always)) { |
| + return false; |
| + } |
| + |
| + ProfileManager* profile_manager = g_browser_process->profile_manager(); |
| + if (!profile_manager) |
|
tapted
2014/04/03 01:45:38
nit: this can maybe be a DCHECK too (for same reas
jackhou1
2014/04/03 03:07:07
Done.
|
| + return true; |
| + |
| + std::vector<Profile*> profiles(profile_manager->GetLoadedProfiles()); |
| + DCHECK(profiles.size()); |
| + |
| + // Delete any existing notification to ensure this one is shown. |
| + g_browser_process->notification_ui_manager()->CancelById(id()); |
| + g_browser_process->notification_ui_manager()->Add(*notification_, |
| + profiles[0]); |
| + |
| + // Always return false, the notification UI can be used to quit all apps which |
| + // will cause Chrome to quit. |
| + return false; |
| +} |
| + |
| +void QuitWithAppsController::RegisterPrefs(PrefRegistrySimple* registry) { |
|
tapted
2014/04/03 01:45:38
nit: // static
jackhou1
2014/04/03 03:07:07
Done.
|
| + registry->RegisterBooleanPref(prefs::kNotifyWhenAppsKeepChromeAlive, true); |
| +} |