Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / cocoa / apps / quit_with_apps_controller_mac.cc
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 "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/notifications/notification.h"
16 #include "chrome/browser/notifications/notification_ui_manager.h"
17 #include "chrome/browser/profiles/profile_manager.h"
18 #include "chrome/browser/ui/browser_iterator.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/pref_names.h"
21 #include "chrome/grit/chromium_strings.h"
22 #include "chrome/grit/generated_resources.h"
23 #include "chrome/grit/google_chrome_strings.h"
24 #include "extensions/browser/app_window/app_window.h"
25 #include "extensions/browser/app_window/native_app_window.h"
26 #include "extensions/common/extension.h"
27 #include "grit/chrome_unscaled_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 const char kQuitWithAppsOriginUrl[] = "chrome://quit-with-apps";
33 const int kQuitAllAppsButtonIndex = 0;
34 const int kDontShowAgainButtonIndex = 1;
35
36 const char QuitWithAppsController::kQuitWithAppsNotificationID[] =
37     "quit-with-apps";
38
39 QuitWithAppsController::QuitWithAppsController()
40     : notification_profile_(NULL), suppress_for_session_(false) {
41   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
42
43   // There is only ever one notification to replace, so use the same replace_id
44   // each time.
45   base::string16 replace_id = base::UTF8ToUTF16(id());
46
47   message_center::ButtonInfo quit_apps_button_info(
48       l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_QUIT_LABEL));
49   message_center::ButtonInfo suppression_button_info(
50       l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_SUPPRESSION_LABEL));
51   message_center::RichNotificationData rich_notification_data;
52   rich_notification_data.buttons.push_back(quit_apps_button_info);
53   rich_notification_data.buttons.push_back(suppression_button_info);
54
55   notification_.reset(new Notification(
56       message_center::NOTIFICATION_TYPE_SIMPLE,
57       GURL(kQuitWithAppsOriginUrl),
58       l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_TITLE),
59       l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_EXPLANATION),
60       ui::ResourceBundle::GetSharedInstance().GetImageNamed(
61           IDR_PRODUCT_LOGO_128),
62       blink::WebTextDirectionDefault,
63       message_center::NotifierId(message_center::NotifierId::SYSTEM_COMPONENT,
64                                  kQuitWithAppsNotificationID),
65       l10n_util::GetStringUTF16(IDS_QUIT_WITH_APPS_NOTIFICATION_DISPLAY_SOURCE),
66       replace_id,
67       rich_notification_data,
68       this));
69 }
70
71 QuitWithAppsController::~QuitWithAppsController() {}
72
73 void QuitWithAppsController::Display() {}
74
75 void QuitWithAppsController::Close(bool by_user) {
76   if (by_user)
77     suppress_for_session_ = true;
78 }
79
80 void QuitWithAppsController::Click() {
81   g_browser_process->notification_ui_manager()->CancelById(
82       id(), NotificationUIManager::GetProfileID(notification_profile_));
83 }
84
85 void QuitWithAppsController::ButtonClick(int button_index) {
86   g_browser_process->notification_ui_manager()->CancelById(
87       id(), NotificationUIManager::GetProfileID(notification_profile_));
88   if (button_index == kQuitAllAppsButtonIndex) {
89     AppWindowRegistryUtil::CloseAllAppWindows();
90   } else if (button_index == kDontShowAgainButtonIndex) {
91     g_browser_process->local_state()->SetBoolean(
92         prefs::kNotifyWhenAppsKeepChromeAlive, false);
93   }
94 }
95
96 std::string QuitWithAppsController::id() const {
97   return kQuitWithAppsNotificationID;
98 }
99
100 bool QuitWithAppsController::ShouldQuit() {
101   DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
102
103   // Quit immediately if this is a test.
104   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType) &&
105       !CommandLine::ForCurrentProcess()->HasSwitch(
106           switches::kAppsKeepChromeAliveInTests)) {
107     return true;
108   }
109
110   // Quit immediately if there are no windows or the confirmation has been
111   // suppressed.
112   if (!AppWindowRegistryUtil::IsAppWindowRegisteredInAnyProfile(0))
113     return true;
114
115   // If there are browser windows, and this notification has been suppressed for
116   // this session or permanently, then just return false to prevent Chrome from
117   // quitting. If there are no browser windows, always show the notification.
118   bool suppress_always = !g_browser_process->local_state()->GetBoolean(
119       prefs::kNotifyWhenAppsKeepChromeAlive);
120   if (!chrome::BrowserIterator().done() &&
121       (suppress_for_session_ || suppress_always)) {
122     return false;
123   }
124
125   ProfileManager* profile_manager = g_browser_process->profile_manager();
126   DCHECK(profile_manager);
127
128   std::vector<Profile*> profiles(profile_manager->GetLoadedProfiles());
129   DCHECK(profiles.size());
130
131   // Delete any existing notification to ensure this one is shown. If
132   // notification_profile_ is NULL then it must be that no notification has been
133   // added by this class yet.
134   if (notification_profile_) {
135     g_browser_process->notification_ui_manager()->CancelById(
136         id(), NotificationUIManager::GetProfileID(notification_profile_));
137   }
138   notification_profile_ = profiles[0];
139   g_browser_process->notification_ui_manager()->Add(*notification_,
140                                                     notification_profile_);
141
142   // Always return false, the notification UI can be used to quit all apps which
143   // will cause Chrome to quit.
144   return false;
145 }
146
147 // static
148 void QuitWithAppsController::RegisterPrefs(PrefRegistrySimple* registry) {
149   registry->RegisterBooleanPref(prefs::kNotifyWhenAppsKeepChromeAlive, true);
150 }