Home | History | Annotate | Download | only in signin
      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 "chrome/grit/chromium_strings.h"
     28 #include "chrome/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 #if defined(OS_CHROMEOS)
     37 #include "chrome/browser/chromeos/login/user_flow.h"
     38 #include "chrome/browser/chromeos/login/users/chrome_user_manager.h"
     39 #include "components/user_manager/user_manager.h"
     40 #endif
     41 
     42 namespace {
     43 
     44 const char kProfileSigninNotificationId[] = "chrome://settings/signin/";
     45 
     46 // A notification delegate for the sign-out button.
     47 class SigninNotificationDelegate : public NotificationDelegate {
     48  public:
     49   SigninNotificationDelegate(const std::string& id,
     50                              Profile* profile);
     51 
     52   // NotificationDelegate:
     53   virtual void Display() OVERRIDE;
     54   virtual void Error() OVERRIDE;
     55   virtual void Close(bool by_user) OVERRIDE;
     56   virtual bool HasClickedListener() OVERRIDE;
     57   virtual void Click() OVERRIDE;
     58   virtual void ButtonClick(int button_index) OVERRIDE;
     59   virtual std::string id() const OVERRIDE;
     60   virtual content::WebContents* GetWebContents() const OVERRIDE;
     61 
     62  protected:
     63   virtual ~SigninNotificationDelegate();
     64 
     65  private:
     66   void FixSignIn();
     67 
     68   // Unique id of the notification.
     69   const std::string id_;
     70 
     71   Profile* profile_;
     72 
     73   DISALLOW_COPY_AND_ASSIGN(SigninNotificationDelegate);
     74 };
     75 
     76 SigninNotificationDelegate::SigninNotificationDelegate(
     77     const std::string& id,
     78     Profile* profile)
     79     : id_(id),
     80       profile_(profile) {
     81 }
     82 
     83 SigninNotificationDelegate::~SigninNotificationDelegate() {
     84 }
     85 
     86 void SigninNotificationDelegate::Display() {
     87 }
     88 
     89 void SigninNotificationDelegate::Error() {
     90 }
     91 
     92 void SigninNotificationDelegate::Close(bool by_user) {
     93 }
     94 
     95 bool SigninNotificationDelegate::HasClickedListener() {
     96   return false;
     97 }
     98 
     99 void SigninNotificationDelegate::Click() {
    100   FixSignIn();
    101 }
    102 
    103 void SigninNotificationDelegate::ButtonClick(int button_index) {
    104   FixSignIn();
    105 }
    106 
    107 std::string SigninNotificationDelegate::id() const {
    108   return id_;
    109 }
    110 
    111 content::WebContents* SigninNotificationDelegate::GetWebContents() const {
    112   return NULL;
    113 }
    114 
    115 void SigninNotificationDelegate::FixSignIn() {
    116 #if defined(OS_CHROMEOS)
    117   chrome::AttemptUserExit();
    118 #else
    119   LoginUIService* login_ui = LoginUIServiceFactory::GetForProfile(profile_);
    120   if (login_ui->current_login_ui()) {
    121     login_ui->current_login_ui()->FocusUI();
    122     return;
    123   }
    124 
    125   // Find a browser instance or create one.
    126   chrome::ScopedTabbedBrowserDisplayer browser_displayer(
    127       profile_, chrome::HOST_DESKTOP_TYPE_ASH);
    128 
    129   // Navigate to the sync setup subpage, which will launch a login page.
    130   chrome::ShowSettingsSubPage(browser_displayer.browser(),
    131                               chrome::kSyncSetupSubPage);
    132 #endif
    133 }
    134 
    135 }  // namespace
    136 
    137 SigninErrorNotifier::SigninErrorNotifier(SigninErrorController* controller,
    138                                          Profile* profile)
    139     : error_controller_(controller),
    140       profile_(profile) {
    141   // Create a unique notification ID for this profile.
    142   notification_id_ = kProfileSigninNotificationId + profile->GetProfileName();
    143 
    144   error_controller_->AddObserver(this);
    145   OnErrorChanged();
    146 }
    147 
    148 SigninErrorNotifier::~SigninErrorNotifier() {
    149   DCHECK(!error_controller_)
    150       << "SigninErrorNotifier::Shutdown() was not called";
    151 }
    152 
    153 void SigninErrorNotifier::Shutdown() {
    154   error_controller_->RemoveObserver(this);
    155   error_controller_ = NULL;
    156 }
    157 
    158 void SigninErrorNotifier::OnErrorChanged() {
    159   NotificationUIManager* notification_ui_manager =
    160       g_browser_process->notification_ui_manager();
    161 
    162   // notification_ui_manager() may return NULL when shutting down.
    163   if (!notification_ui_manager)
    164     return;
    165 
    166   if (!error_controller_->HasError()) {
    167     g_browser_process->notification_ui_manager()->CancelById(notification_id_);
    168     return;
    169   }
    170 
    171 #if defined(OS_CHROMEOS)
    172   if (user_manager::UserManager::IsInitialized()) {
    173     chromeos::UserFlow* user_flow =
    174         chromeos::ChromeUserManager::Get()->GetCurrentUserFlow();
    175 
    176     // Check whether Chrome OS user flow allows launching browser.
    177     // Example: Supervised user creation flow which handles token invalidation
    178     // itself and notifications should be suppressed. http://crbug.com/359045
    179     if (!user_flow->ShouldLaunchBrowser())
    180       return;
    181   }
    182 #endif
    183 
    184   // Add an accept button to sign the user out.
    185   message_center::RichNotificationData data;
    186   data.buttons.push_back(message_center::ButtonInfo(
    187       l10n_util::GetStringUTF16(IDS_SYNC_RELOGIN_LINK_LABEL)));
    188 
    189   // Set the delegate for the notification's sign-out button.
    190   SigninNotificationDelegate* delegate =
    191       new SigninNotificationDelegate(notification_id_, profile_);
    192 
    193   message_center::NotifierId notifier_id(
    194       message_center::NotifierId::SYSTEM_COMPONENT,
    195       kProfileSigninNotificationId);
    196 
    197   // Set |profile_id| for multi-user notification blocker.
    198   notifier_id.profile_id = multi_user_util::GetUserIDFromProfile(profile_);
    199 
    200   Notification notification(
    201       message_center::NOTIFICATION_TYPE_SIMPLE,
    202       GURL(notification_id_),
    203       l10n_util::GetStringUTF16(IDS_SIGNIN_ERROR_BUBBLE_VIEW_TITLE),
    204       GetMessageBody(),
    205       ui::ResourceBundle::GetSharedInstance().GetImageNamed(
    206           IDR_NOTIFICATION_ALERT),
    207       blink::WebTextDirectionDefault,
    208       notifier_id,
    209       base::string16(),  // display_source
    210       base::ASCIIToUTF16(notification_id_),
    211       data,
    212       delegate);
    213 
    214   // Update or add the notification.
    215   if (notification_ui_manager->FindById(notification_id_))
    216     notification_ui_manager->Update(notification, profile_);
    217   else
    218     notification_ui_manager->Add(notification, profile_);
    219 }
    220 
    221 base::string16 SigninErrorNotifier::GetMessageBody() const {
    222   switch (error_controller_->auth_error().state()) {
    223     // TODO(rogerta): use account id in error messages.
    224 
    225     // User credentials are invalid (bad acct, etc).
    226     case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS:
    227     case GoogleServiceAuthError::SERVICE_ERROR:
    228     case GoogleServiceAuthError::ACCOUNT_DELETED:
    229     case GoogleServiceAuthError::ACCOUNT_DISABLED:
    230       return l10n_util::GetStringUTF16(
    231           IDS_SYNC_SIGN_IN_ERROR_BUBBLE_VIEW_MESSAGE);
    232       break;
    233 
    234     // Sync service is not available for this account's domain.
    235     case GoogleServiceAuthError::SERVICE_UNAVAILABLE:
    236       return l10n_util::GetStringUTF16(
    237           IDS_SYNC_UNAVAILABLE_ERROR_BUBBLE_VIEW_MESSAGE);
    238       break;
    239 
    240     // Generic message for "other" errors.
    241     default:
    242       return l10n_util::GetStringUTF16(
    243           IDS_SYNC_OTHER_SIGN_IN_ERROR_BUBBLE_VIEW_MESSAGE);
    244   }
    245 }
    246