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