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/pref_names.h" | |
| 10 #include "apps/ui/native_app_window.h" | |
| 11 #include "base/command_line.h" | |
| 12 #include "base/i18n/number_formatting.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 "extensions/common/extension.h" | |
| 23 #include "grit/chromium_strings.h" | |
| 24 #include "grit/generated_resources.h" | |
| 25 #include "grit/google_chrome_strings.h" | |
| 26 #include "grit/theme_resources.h" | |
| 27 #include "ui/base/l10n/l10n_util.h" | |
| 28 #include "ui/base/l10n/l10n_util_mac.h" | |
| 29 #include "ui/base/resource/resource_bundle.h" | |
| 30 | |
| 31 const char QuitWithAppsController::kQuitWithApps[] = "quit-with-apps"; | |
| 32 | |
| 33 QuitWithAppsController::QuitWithAppsController() | |
| 34 : suppress_for_session_(false) { | |
| 35 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
|
tapted
2014/04/01 08:09:45
nit: there's a shiny new DCHECK_CURRENTLY_ON macro
jackhou1
2014/04/02 06:38:38
Done.
| |
| 36 | |
| 37 message_center::ButtonInfo quit_apps_button_info( | |
| 38 l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_QUIT_LABEL)); | |
| 39 message_center::ButtonInfo suppression_button_info( | |
| 40 l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_SUPPRESSION_LABEL)); | |
| 41 message_center::RichNotificationData rich_notification_data; | |
| 42 rich_notification_data.buttons.push_back(quit_apps_button_info); | |
| 43 rich_notification_data.buttons.push_back(suppression_button_info); | |
| 44 | |
| 45 notification_.reset(new Notification( | |
| 46 message_center::NOTIFICATION_TYPE_SIMPLE, | |
| 47 GURL("chrome://quit-with-apps"), | |
|
tapted
2014/04/01 08:09:45
nit: poking around other notifications, it seems c
jackhou1
2014/04/02 06:38:38
Done.
| |
| 48 l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_TITLE), | |
| 49 l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_EXPLANATION), | |
| 50 ui::ResourceBundle::GetSharedInstance().GetImageNamed( | |
| 51 IDR_APP_DEFAULT_ICON), | |
| 52 blink::WebTextDirectionDefault, | |
| 53 message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT, | |
| 54 kQuitWithApps), | |
| 55 base::UTF8ToUTF16(kQuitWithApps), | |
|
tapted
2014/04/01 08:09:45
should this be localized?
actually this and the n
jackhou1
2014/04/02 06:38:38
Done.
| |
| 56 base::UTF8ToUTF16(id()), | |
| 57 rich_notification_data, | |
| 58 this)); | |
| 59 } | |
| 60 | |
| 61 QuitWithAppsController::~QuitWithAppsController() {} | |
| 62 | |
| 63 void QuitWithAppsController::Display() {} | |
| 64 | |
| 65 void QuitWithAppsController::Error() { | |
| 66 LOG(ERROR) << "Error displaying notification while quitting with apps open."; | |
| 67 } | |
| 68 | |
| 69 void QuitWithAppsController::Close(bool by_user) { | |
| 70 if (by_user) | |
| 71 suppress_for_session_ = true; | |
| 72 } | |
| 73 | |
| 74 void QuitWithAppsController::Click() { | |
| 75 g_browser_process->notification_ui_manager()->CancelById(id()); | |
| 76 } | |
| 77 | |
| 78 void QuitWithAppsController::ButtonClick(int button_index) { | |
| 79 typedef apps::AppWindowRegistry::AppWindowList AppWindowList; | |
| 80 | |
| 81 g_browser_process->notification_ui_manager()->CancelById(id()); | |
| 82 if (button_index == 0) { | |
| 83 // Quit all apps by closing all windows. | |
| 84 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 85 if (!profile_manager) | |
| 86 return; | |
| 87 | |
| 88 std::vector<Profile*> profiles(profile_manager->GetLoadedProfiles()); | |
| 89 for (size_t i = 0; i < profiles.size(); ++i) { | |
| 90 AppWindowList windows = | |
| 91 apps::AppWindowRegistry::Get(profiles[i])->app_windows(); | |
| 92 for (AppWindowList::const_iterator it = windows.begin(); | |
| 93 it != windows.end(); | |
| 94 ++it) { | |
| 95 (*it)->GetBaseWindow()->Close(); | |
|
tapted
2014/04/01 08:09:45
are we sure this is safe? (e.g. did you have any i
jackhou1
2014/04/02 06:38:38
I don't have a fix/proof, but this code can be wri
tapted
2014/04/03 01:45:38
Yeah, I think a method in a method in AppWindowReg
jackhou1
2014/04/03 03:07:07
Done.
| |
| 96 } | |
| 97 } | |
| 98 } else if (button_index == 1) { | |
| 99 // Don't show this again. | |
|
tapted
2014/04/01 08:09:45
nit: maybe (in anon namespace)
const int kQuitAll
jackhou1
2014/04/02 06:38:38
Done.
| |
| 100 g_browser_process->local_state()->SetBoolean( | |
| 101 apps::prefs::kNotifyWhenAppsKeepChromeAlive, false); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 content::RenderViewHost* QuitWithAppsController::GetRenderViewHost() const { | |
| 106 return NULL; | |
| 107 } | |
| 108 | |
| 109 std::string QuitWithAppsController::id() const { return kQuitWithApps; } | |
| 110 | |
| 111 bool QuitWithAppsController::ShouldQuit() { | |
| 112 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
|
tapted
2014/04/01 08:09:45
DCHECK_CURRENTLY_ON
jackhou1
2014/04/02 06:38:38
Done.
| |
| 113 | |
| 114 if (!CommandLine::ForCurrentProcess()->HasSwitch( | |
| 115 switches::kAppsKeepChromeAlive)) { | |
| 116 g_browser_process->local_state()->ClearPref( | |
| 117 apps::prefs::kNotifyWhenAppsKeepChromeAlive); | |
| 118 return true; | |
| 119 } | |
| 120 | |
| 121 // Quit immediately if there are no windows or the confirmation has been | |
| 122 // suppressed. | |
| 123 if (!apps::AppWindowRegistry::IsAppWindowRegisteredInAnyProfile(0)) | |
| 124 return true; | |
| 125 | |
| 126 // If there are browser windows, and this notification has been suppressed for | |
| 127 // this session or permanently, then just return false to prevent Chrome from | |
| 128 // quitting. | |
| 129 bool suppress_always = !g_browser_process->local_state()->GetBoolean( | |
| 130 apps::prefs::kNotifyWhenAppsKeepChromeAlive); | |
| 131 if (!chrome::BrowserIterator().done() && | |
|
tapted
2014/04/01 08:09:45
Does this mean choosing 'Quit' from the Chrome doc
jackhou1
2014/04/02 06:38:38
Done.
| |
| 132 (suppress_for_session_ || suppress_always)) | |
|
tapted
2014/04/01 08:09:45
nit: curlies for multi-line if condition
jackhou1
2014/04/02 06:38:38
Done.
| |
| 133 return false; | |
| 134 | |
| 135 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 136 if (!profile_manager) | |
| 137 return true; | |
| 138 | |
| 139 std::vector<Profile*> profiles(profile_manager->GetLoadedProfiles()); | |
| 140 if (!profiles.size()) | |
|
tapted
2014/04/01 08:09:45
nit: if (profiles.empty()). But also shouldn't thi
jackhou1
2014/04/02 06:38:38
Done.
| |
| 141 return true; | |
| 142 | |
| 143 g_browser_process->notification_ui_manager()->CancelById(id()); | |
|
tapted
2014/04/01 08:09:45
what happens without this line? (e.g. is it necess
jackhou1
2014/04/02 06:38:38
Yeah, Add() will replace an existing one, which mi
| |
| 144 g_browser_process->notification_ui_manager()->Add(*notification_, | |
| 145 profiles[0]); | |
| 146 | |
| 147 // Always return false, "Quit All Apps" will cause Chrome to quit. | |
|
tapted
2014/04/01 08:09:45
this makes it sound like returning false will caus
jackhou1
2014/04/02 06:38:38
Done.
| |
| 148 return false; | |
| 149 } | |
| OLD | NEW |