23b0a7ce123c1304f852576ba5f791afd603e38d
[platform/framework/web/crosswalk.git] / src / chrome / browser / signin / signin_error_notifier_ash.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/signin/signin_error_notifier_ash.h"
6
7 #include "ash/shell.h"
8 #include "ash/shell_delegate.h"
9 #include "ash/system/system_notifier.h"
10 #include "base/logging.h"
11 #include "base/strings/string16.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/lifetime/application_lifetime.h"
15 #include "chrome/browser/notifications/notification.h"
16 #include "chrome/browser/notifications/notification_delegate.h"
17 #include "chrome/browser/notifications/notification_ui_manager.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
20 #include "chrome/browser/ui/browser_tabstrip.h"
21 #include "chrome/browser/ui/browser_window.h"
22 #include "chrome/browser/ui/chrome_pages.h"
23 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
24 #include "chrome/browser/ui/webui/signin/login_ui_service.h"
25 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
26 #include "chrome/common/url_constants.h"
27 #include "grit/chromium_strings.h"
28 #include "grit/generated_resources.h"
29 #include "grit/theme_resources.h"
30 #include "third_party/WebKit/public/web/WebTextDirection.h"
31 #include "ui/base/l10n/l10n_util.h"
32 #include "ui/base/resource/resource_bundle.h"
33 #include "ui/message_center/notification.h"
34 #include "ui/message_center/notification_delegate.h"
35
36 namespace {
37
38 const char kProfileSigninNotificationId[] = "chrome://settings/signin/";
39
40 // A notification delegate for the sign-out button.
41 class SigninNotificationDelegate : public NotificationDelegate {
42  public:
43   SigninNotificationDelegate(const std::string& id,
44                              Profile* profile);
45
46   // NotificationDelegate:
47   virtual void Display() OVERRIDE;
48   virtual void Error() OVERRIDE;
49   virtual void Close(bool by_user) OVERRIDE;
50   virtual bool HasClickedListener() OVERRIDE;
51   virtual void Click() OVERRIDE;
52   virtual void ButtonClick(int button_index) OVERRIDE;
53   virtual std::string id() const OVERRIDE;
54   virtual content::RenderViewHost* GetRenderViewHost() const OVERRIDE;
55
56  protected:
57   virtual ~SigninNotificationDelegate();
58
59  private:
60   // Unique id of the notification.
61   const std::string id_;
62
63   Profile* profile_;
64
65   DISALLOW_COPY_AND_ASSIGN(SigninNotificationDelegate);
66 };
67
68 SigninNotificationDelegate::SigninNotificationDelegate(
69     const std::string& id,
70     Profile* profile)
71     : id_(id),
72       profile_(profile) {
73 }
74
75 SigninNotificationDelegate::~SigninNotificationDelegate() {
76 }
77
78 void SigninNotificationDelegate::Display() {
79 }
80
81 void SigninNotificationDelegate::Error() {
82 }
83
84 void SigninNotificationDelegate::Close(bool by_user) {
85 }
86
87 bool SigninNotificationDelegate::HasClickedListener() {
88   return false;
89 }
90
91 void SigninNotificationDelegate::Click() {
92 }
93
94 void SigninNotificationDelegate::ButtonClick(int button_index) {
95 #if defined(OS_CHROMEOS)
96   chrome::AttemptUserExit();
97 #else
98   LoginUIService* login_ui = LoginUIServiceFactory::GetForProfile(profile_);
99   if (login_ui->current_login_ui()) {
100     login_ui->current_login_ui()->FocusUI();
101     return;
102   }
103
104   // Find a browser instance or create one.
105   chrome::ScopedTabbedBrowserDisplayer browser_displayer(
106       profile_, chrome::HOST_DESKTOP_TYPE_ASH);
107
108   // Navigate to the sync setup subpage, which will launch a login page.
109   chrome::ShowSettingsSubPage(browser_displayer.browser(),
110                               chrome::kSyncSetupSubPage);
111 #endif
112 }
113
114 std::string SigninNotificationDelegate::id() const {
115   return id_;
116 }
117
118 content::RenderViewHost* SigninNotificationDelegate::GetRenderViewHost() const {
119   return NULL;
120 }
121
122 }  // namespace
123
124 SigninErrorNotifier::SigninErrorNotifier(SigninErrorController* controller,
125                                          Profile* profile)
126     : error_controller_(controller),
127       profile_(profile) {
128   // Create a unique notification ID for this profile.
129   notification_id_ = kProfileSigninNotificationId + profile->GetProfileName();
130
131   error_controller_->AddObserver(this);
132   OnErrorChanged();
133 }
134
135 SigninErrorNotifier::~SigninErrorNotifier() {
136   DCHECK(!error_controller_)
137       << "SigninErrorNotifier::Shutdown() was not called";
138 }
139
140 void SigninErrorNotifier::Shutdown() {
141   error_controller_->RemoveObserver(this);
142   error_controller_ = NULL;
143 }
144
145 void SigninErrorNotifier::OnErrorChanged() {
146   NotificationUIManager* notification_ui_manager =
147       g_browser_process->notification_ui_manager();
148
149   // notification_ui_manager() may return NULL when shutting down.
150   if (!notification_ui_manager)
151     return;
152
153   if (!error_controller_->HasError()) {
154     g_browser_process->notification_ui_manager()->CancelById(notification_id_);
155     return;
156   }
157
158   // Add an accept button to sign the user out.
159   message_center::RichNotificationData data;
160   data.buttons.push_back(message_center::ButtonInfo(
161       l10n_util::GetStringUTF16(IDS_SYNC_RELOGIN_LINK_LABEL)));
162
163   // Set the delegate for the notification's sign-out button.
164   SigninNotificationDelegate* delegate =
165       new SigninNotificationDelegate(notification_id_, profile_);
166
167   message_center::NotifierId notifier_id(
168       message_center::NotifierId::SYSTEM_COMPONENT,
169       kProfileSigninNotificationId);
170
171   // Set |profile_id| for multi-user notification blocker.
172   notifier_id.profile_id = multi_user_util::GetUserIDFromProfile(profile_);
173
174   Notification notification(
175       message_center::NOTIFICATION_TYPE_SIMPLE,
176       GURL(notification_id_),
177       l10n_util::GetStringUTF16(IDS_SIGNIN_ERROR_BUBBLE_VIEW_TITLE),
178       GetMessageBody(),
179       ui::ResourceBundle::GetSharedInstance().GetImageNamed(
180           IDR_NOTIFICATION_ALERT),
181       blink::WebTextDirectionDefault,
182       notifier_id,
183       base::string16(),  // display_source
184       base::ASCIIToUTF16(notification_id_),
185       data,
186       delegate);
187
188   // Update or add the notification.
189   if (notification_ui_manager->FindById(notification_id_))
190     notification_ui_manager->Update(notification, profile_);
191   else
192     notification_ui_manager->Add(notification, profile_);
193 }
194
195 base::string16 SigninErrorNotifier::GetMessageBody() const {
196   switch (error_controller_->auth_error().state()) {
197     // TODO(rogerta): use account id in error messages.
198
199     // User credentials are invalid (bad acct, etc).
200     case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS:
201     case GoogleServiceAuthError::SERVICE_ERROR:
202     case GoogleServiceAuthError::ACCOUNT_DELETED:
203     case GoogleServiceAuthError::ACCOUNT_DISABLED:
204       return l10n_util::GetStringUTF16(
205           IDS_SYNC_SIGN_IN_ERROR_BUBBLE_VIEW_MESSAGE);
206       break;
207
208     // Sync service is not available for this account's domain.
209     case GoogleServiceAuthError::SERVICE_UNAVAILABLE:
210       return l10n_util::GetStringUTF16(
211           IDS_SYNC_UNAVAILABLE_ERROR_BUBBLE_VIEW_MESSAGE);
212       break;
213
214     // Generic message for "other" errors.
215     default:
216       return l10n_util::GetStringUTF16(
217           IDS_SYNC_OTHER_SIGN_IN_ERROR_BUBBLE_VIEW_MESSAGE);
218   }
219 }