Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / signin / signin_global_error.cc
1 // Copyright (c) 2013 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_global_error.h"
6
7 #include "base/logging.h"
8 #include "chrome/app/chrome_command_ids.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/signin/signin_manager_factory.h"
11 #include "chrome/browser/signin/signin_promo.h"
12 #include "chrome/browser/ui/browser_commands.h"
13 #include "chrome/browser/ui/chrome_pages.h"
14 #include "chrome/browser/ui/global_error/global_error_service.h"
15 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
16 #include "chrome/browser/ui/singleton_tabs.h"
17 #include "chrome/browser/ui/webui/signin/login_ui_service.h"
18 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
19 #include "chrome/common/url_constants.h"
20 #include "components/signin/core/browser/signin_manager.h"
21 #include "grit/chromium_strings.h"
22 #include "grit/generated_resources.h"
23 #include "net/base/url_util.h"
24 #include "ui/base/l10n/l10n_util.h"
25
26 SigninGlobalError::SigninGlobalError(
27     SigninErrorController* error_controller,
28     Profile* profile)
29     : profile_(profile),
30       error_controller_(error_controller) {
31   error_controller_->AddObserver(this);
32   GlobalErrorServiceFactory::GetForProfile(profile_)->AddGlobalError(this);
33 }
34
35 SigninGlobalError::~SigninGlobalError() {
36   DCHECK(!error_controller_)
37       << "SigninGlobalError::Shutdown() was not called";
38 }
39
40 void SigninGlobalError::Shutdown() {
41   GlobalErrorServiceFactory::GetForProfile(profile_)->RemoveGlobalError(this);
42   error_controller_->RemoveObserver(this);
43   error_controller_ = NULL;
44 }
45
46 bool SigninGlobalError::HasMenuItem() {
47   return error_controller_->HasError();
48 }
49
50 int SigninGlobalError::MenuItemCommandID() {
51   return IDC_SHOW_SIGNIN_ERROR;
52 }
53
54 base::string16 SigninGlobalError::MenuItemLabel() {
55   // Notify the user if there's an auth error the user should know about.
56   if (error_controller_->HasError())
57     return l10n_util::GetStringUTF16(IDS_SYNC_SIGN_IN_ERROR_WRENCH_MENU_ITEM);
58   return base::string16();
59 }
60
61 void SigninGlobalError::ExecuteMenuItem(Browser* browser) {
62 #if defined(OS_CHROMEOS)
63   if (error_controller_->auth_error().state() !=
64       GoogleServiceAuthError::NONE) {
65     DVLOG(1) << "Signing out the user to fix a sync error.";
66     // TODO(beng): seems like this could just call chrome::AttemptUserExit().
67     chrome::ExecuteCommand(browser, IDC_EXIT);
68     return;
69   }
70 #endif
71
72   // Global errors don't show up in the wrench menu on android.
73 #if !defined(OS_ANDROID)
74   LoginUIService* login_ui = LoginUIServiceFactory::GetForProfile(profile_);
75   if (login_ui->current_login_ui()) {
76     login_ui->current_login_ui()->FocusUI();
77     return;
78   }
79
80   chrome::ShowSingletonTab(
81       browser,
82       signin::GetReauthURL(profile_, error_controller_->error_account_id()));
83 #endif
84 }
85
86 bool SigninGlobalError::HasBubbleView() {
87   return !GetBubbleViewMessages().empty();
88 }
89
90 base::string16 SigninGlobalError::GetBubbleViewTitle() {
91   return l10n_util::GetStringUTF16(IDS_SIGNIN_ERROR_BUBBLE_VIEW_TITLE);
92 }
93
94 std::vector<base::string16> SigninGlobalError::GetBubbleViewMessages() {
95   std::vector<base::string16> messages;
96
97   // If the user isn't signed in, no need to display an error bubble.
98   SigninManagerBase* signin_manager =
99       SigninManagerFactory::GetForProfileIfExists(profile_);
100   if (signin_manager) {
101     std::string username = signin_manager->GetAuthenticatedUsername();
102     if (username.empty())
103       return messages;
104   }
105
106   if (!error_controller_->HasError())
107     return messages;
108
109   switch (error_controller_->auth_error().state()) {
110     // TODO(rogerta): use account id in error messages.
111
112     // User credentials are invalid (bad acct, etc).
113     case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS:
114     case GoogleServiceAuthError::SERVICE_ERROR:
115     case GoogleServiceAuthError::ACCOUNT_DELETED:
116     case GoogleServiceAuthError::ACCOUNT_DISABLED:
117       messages.push_back(l10n_util::GetStringUTF16(
118           IDS_SYNC_SIGN_IN_ERROR_BUBBLE_VIEW_MESSAGE));
119       break;
120
121     // Sync service is not available for this account's domain.
122     case GoogleServiceAuthError::SERVICE_UNAVAILABLE:
123       messages.push_back(l10n_util::GetStringUTF16(
124           IDS_SYNC_UNAVAILABLE_ERROR_BUBBLE_VIEW_MESSAGE));
125       break;
126
127     // Generic message for "other" errors.
128     default:
129       messages.push_back(l10n_util::GetStringUTF16(
130           IDS_SYNC_OTHER_SIGN_IN_ERROR_BUBBLE_VIEW_MESSAGE));
131   }
132   return messages;
133 }
134
135 base::string16 SigninGlobalError::GetBubbleViewAcceptButtonLabel() {
136   // If the auth service is unavailable, don't give the user the option to try
137   // signing in again.
138   if (error_controller_->auth_error().state() ==
139       GoogleServiceAuthError::SERVICE_UNAVAILABLE) {
140     return l10n_util::GetStringUTF16(
141         IDS_SYNC_UNAVAILABLE_ERROR_BUBBLE_VIEW_ACCEPT);
142   } else {
143     return l10n_util::GetStringUTF16(IDS_SYNC_SIGN_IN_ERROR_BUBBLE_VIEW_ACCEPT);
144   }
145 }
146
147 base::string16 SigninGlobalError::GetBubbleViewCancelButtonLabel() {
148   return base::string16();
149 }
150
151 void SigninGlobalError::OnBubbleViewDidClose(Browser* browser) {
152 }
153
154 void SigninGlobalError::BubbleViewAcceptButtonPressed(Browser* browser) {
155   ExecuteMenuItem(browser);
156 }
157
158 void SigninGlobalError::BubbleViewCancelButtonPressed(Browser* browser) {
159   NOTREACHED();
160 }
161
162 void SigninGlobalError::OnErrorChanged() {
163   GlobalErrorServiceFactory::GetForProfile(profile_)->NotifyErrorsChanged(this);
164 }