Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/ui/cocoa/apps/quit_with_apps_controller_mac.h" | |
| 6 | |
| 7 #include "apps/app_window.h" | |
| 8 #include "apps/app_window_registry.h" | |
| 9 #include "apps/ui/native_app_window.h" | |
| 10 #include "base/command_line.h" | |
| 11 #include "base/i18n/number_formatting.h" | |
| 12 #include "base/prefs/pref_registry_simple.h" | |
| 13 #include "base/prefs/pref_service.h" | |
| 14 #include "base/strings/sys_string_conversions.h" | |
| 15 #include "base/strings/utf_string_conversions.h" | |
| 16 #include "chrome/browser/browser_process.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/common/chrome_switches.h" | |
| 22 #include "chrome/common/pref_names.h" | |
| 23 #include "extensions/common/extension.h" | |
| 24 #include "grit/chromium_strings.h" | |
| 25 #include "grit/generated_resources.h" | |
| 26 #include "grit/google_chrome_strings.h" | |
| 27 #include "grit/theme_resources.h" | |
| 28 #include "ui/base/l10n/l10n_util.h" | |
| 29 #include "ui/base/l10n/l10n_util_mac.h" | |
| 30 #include "ui/base/resource/resource_bundle.h" | |
| 31 | |
| 32 namespace { | |
|
Nico
2014/04/22 22:32:02
const has implicit internal linkage, no need for n
jackhou1
2014/04/24 04:29:22
Done.
| |
| 33 | |
| 34 const char kQuitWithAppsOriginUrl[] = "chrome://quit-with-apps"; | |
| 35 const int kQuitAllAppsButtonIndex = 0; | |
| 36 const int kDontShowAgainButtonIndex = 1; | |
| 37 | |
| 38 } // namespace | |
| 39 | |
| 40 const char QuitWithAppsController::kQuitWithAppsNotificationID[] = | |
| 41 "quit-with-apps"; | |
| 42 | |
| 43 QuitWithAppsController::QuitWithAppsController() | |
| 44 : suppress_for_session_(false) { | |
| 45 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 46 | |
| 47 // There is only ever one notification to replace, so use the same replace_id | |
| 48 // each time. | |
| 49 base::string16 replace_id = base::UTF8ToUTF16(id()); | |
| 50 | |
| 51 message_center::ButtonInfo quit_apps_button_info( | |
| 52 l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_QUIT_LABEL)); | |
| 53 message_center::ButtonInfo suppression_button_info( | |
| 54 l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_SUPPRESSION_LABEL)); | |
| 55 message_center::RichNotificationData rich_notification_data; | |
| 56 rich_notification_data.buttons.push_back(quit_apps_button_info); | |
| 57 rich_notification_data.buttons.push_back(suppression_button_info); | |
| 58 | |
| 59 notification_.reset(new Notification( | |
| 60 message_center::NOTIFICATION_TYPE_SIMPLE, | |
| 61 GURL(kQuitWithAppsOriginUrl), | |
| 62 l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_TITLE), | |
| 63 l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_EXPLANATION), | |
| 64 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 65 IDR_APP_DEFAULT_ICON), | |
| 66 blink::WebTextDirectionDefault, | |
| 67 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, | |
| 68 kQuitWithAppsNotificationID), | |
| 69 l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_NOTIFICATION_DISPLAY_SOURCE), | |
| 70 replace_id, | |
| 71 rich_notification_data, | |
| 72 this)); | |
| 73 } | |
| 74 | |
| 75 QuitWithAppsController::~QuitWithAppsController() {} | |
| 76 | |
| 77 void QuitWithAppsController::Display() {} | |
| 78 | |
| 79 void QuitWithAppsController::Error() { | |
| 80 LOG(ERROR) << "Error displaying notification while quitting with apps open."; | |
|
Nico
2014/04/22 22:32:02
Is this a useful log message? Try to minimize logs
jackhou1
2014/04/24 04:29:22
Done.
| |
| 81 } | |
| 82 | |
| 83 void QuitWithAppsController::Close(bool by_user) { | |
| 84 if (by_user) | |
| 85 suppress_for_session_ = true; | |
| 86 } | |
| 87 | |
| 88 void QuitWithAppsController::Click() { | |
| 89 g_browser_process->notification_ui_manager()->CancelById(id()); | |
| 90 } | |
| 91 | |
| 92 void QuitWithAppsController::ButtonClick(int button_index) { | |
| 93 typedef apps::AppWindowRegistry::AppWindowList AppWindowList; | |
| 94 | |
| 95 g_browser_process->notification_ui_manager()->CancelById(id()); | |
| 96 if (button_index == kQuitAllAppsButtonIndex) { | |
| 97 apps::AppWindowRegistry::CloseAllAppWindows(); | |
| 98 } else if (button_index == kDontShowAgainButtonIndex) { | |
| 99 g_browser_process->local_state()->SetBoolean( | |
| 100 prefs::kNotifyWhenAppsKeepChromeAlive, false); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 content::RenderViewHost* QuitWithAppsController::GetRenderViewHost() const { | |
| 105 return NULL; | |
|
Nico
2014/04/22 22:32:02
Huh, is this allowed?
jackhou1
2014/04/24 04:29:22
Yeah, the comment in c/b/notifications/notificatio
| |
| 106 } | |
| 107 | |
| 108 std::string QuitWithAppsController::id() const { | |
| 109 return kQuitWithAppsNotificationID; | |
| 110 } | |
| 111 | |
| 112 bool QuitWithAppsController::ShouldQuit() { | |
| 113 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 114 | |
| 115 if (!CommandLine::ForCurrentProcess()->HasSwitch( | |
| 116 switches::kAppsKeepChromeAlive)) { | |
| 117 g_browser_process->local_state()->ClearPref( | |
| 118 prefs::kNotifyWhenAppsKeepChromeAlive); | |
| 119 return true; | |
| 120 } | |
| 121 | |
| 122 // Quit immediately if there are no windows or the confirmation has been | |
| 123 // suppressed. | |
| 124 if (!apps::AppWindowRegistry::IsAppWindowRegisteredInAnyProfile(0)) | |
| 125 return true; | |
| 126 | |
| 127 // If there are browser windows, and this notification has been suppressed for | |
| 128 // this session or permanently, then just return false to prevent Chrome from | |
| 129 // quitting. If there are no browser windows, always show the notification. | |
| 130 bool suppress_always = !g_browser_process->local_state()->GetBoolean( | |
| 131 prefs::kNotifyWhenAppsKeepChromeAlive); | |
| 132 if (!chrome::BrowserIterator().done() && | |
| 133 (suppress_for_session_ || suppress_always)) { | |
| 134 return false; | |
| 135 } | |
| 136 | |
| 137 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 138 DCHECK(profile_manager); | |
| 139 | |
| 140 std::vector<Profile*> profiles(profile_manager->GetLoadedProfiles()); | |
| 141 DCHECK(profiles.size()); | |
| 142 | |
| 143 // Delete any existing notification to ensure this one is shown. | |
|
Nico
2014/04/22 22:32:02
This only cancels previous versions of the same no
jackhou1
2014/04/24 04:29:22
Yup that's correct.
| |
| 144 g_browser_process->notification_ui_manager()->CancelById(id()); | |
| 145 g_browser_process->notification_ui_manager()->Add(*notification_, | |
| 146 profiles[0]); | |
| 147 | |
| 148 // Always return false, the notification UI can be used to quit all apps which | |
| 149 // will cause Chrome to quit. | |
| 150 return false; | |
| 151 } | |
| 152 | |
| 153 // static | |
| 154 void QuitWithAppsController::RegisterPrefs(PrefRegistrySimple* registry) { | |
| 155 registry->RegisterBooleanPref(prefs::kNotifyWhenAppsKeepChromeAlive, true); | |
| 156 } | |
| OLD | NEW |