- add sources.
[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 "base/prefs/pref_service.h"
9 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome/browser/profiles/profile.h"
11 #include "chrome/browser/signin/profile_oauth2_token_service.h"
12 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
13 #include "chrome/browser/signin/signin_manager.h"
14 #include "chrome/browser/signin/signin_manager_factory.h"
15 #include "chrome/browser/ui/browser_commands.h"
16 #include "chrome/browser/ui/chrome_pages.h"
17 #include "chrome/browser/ui/global_error/global_error_service.h"
18 #include "chrome/browser/ui/global_error/global_error_service_factory.h"
19 #include "chrome/browser/ui/webui/signin/login_ui_service.h"
20 #include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
21 #include "chrome/common/pref_names.h"
22 #include "chrome/common/url_constants.h"
23 #include "grit/chromium_strings.h"
24 #include "grit/generated_resources.h"
25 #include "ui/base/l10n/l10n_util.h"
26
27 SigninGlobalError::SigninGlobalError(Profile* profile)
28     : auth_error_(GoogleServiceAuthError::AuthErrorNone()), profile_(profile) {
29 }
30
31 SigninGlobalError::~SigninGlobalError() {
32   DCHECK(provider_set_.empty())
33       << "All AuthStatusProviders should be unregistered before"
34       << " SigninManager::Shutdown() is called";
35 }
36
37 void SigninGlobalError::AddProvider(const AuthStatusProvider* provider) {
38   DCHECK(provider_set_.find(provider) == provider_set_.end())
39       << "Adding same AuthStatusProvider multiple times";
40   provider_set_.insert(provider);
41   AuthStatusChanged();
42 }
43
44 void SigninGlobalError::RemoveProvider(const AuthStatusProvider* provider) {
45   std::set<const AuthStatusProvider*>::iterator iter =
46       provider_set_.find(provider);
47   DCHECK(iter != provider_set_.end())
48       << "Removing provider that was never added";
49   provider_set_.erase(iter);
50   AuthStatusChanged();
51 }
52
53 SigninGlobalError::AuthStatusProvider::AuthStatusProvider() {
54 }
55
56 SigninGlobalError::AuthStatusProvider::~AuthStatusProvider() {
57 }
58
59 void SigninGlobalError::AuthStatusChanged() {
60   // Walk all of the status providers and collect any error.
61   GoogleServiceAuthError current_error(GoogleServiceAuthError::AuthErrorNone());
62   std::string current_account_id;
63   for (std::set<const AuthStatusProvider*>::const_iterator it =
64            provider_set_.begin(); it != provider_set_.end(); ++it) {
65     current_account_id = (*it)->GetAccountId();
66     current_error = (*it)->GetAuthStatus();
67
68     // Break out if any provider reports an error (ignoring ordinary network
69     // errors, which are not surfaced to the user). This logic may eventually
70     // need to be extended to prioritize different auth errors, but for now
71     // all auth errors are treated the same.
72     if (current_error.state() != GoogleServiceAuthError::NONE &&
73         current_error.state() != GoogleServiceAuthError::CONNECTION_FAILED) {
74       break;
75     }
76   }
77   if (current_error.state() != auth_error_.state() ||
78       account_id_ != current_account_id) {
79     auth_error_ = current_error;
80     if (auth_error_.state() == GoogleServiceAuthError::NONE) {
81       account_id_.clear();
82     } else {
83       account_id_ = current_account_id;
84     }
85
86     GlobalErrorServiceFactory::GetForProfile(profile_)->NotifyErrorsChanged(
87         this);
88   }
89 }
90
91 bool SigninGlobalError::HasMenuItem() {
92   return !MenuItemLabel().empty();
93 }
94
95 int SigninGlobalError::MenuItemCommandID() {
96   return IDC_SHOW_SIGNIN_ERROR;
97 }
98
99 string16 SigninGlobalError::MenuItemLabel() {
100   if (account_id_.empty() ||
101       auth_error_.state() == GoogleServiceAuthError::NONE ||
102       auth_error_.state() == GoogleServiceAuthError::CONNECTION_FAILED) {
103     // If the user isn't signed in, or there's no auth error worth elevating to
104     // the user, don't display any menu item.
105     return string16();
106   } else {
107     // There's an auth error the user should know about - notify the user.
108     return l10n_util::GetStringUTF16(IDS_SYNC_SIGN_IN_ERROR_WRENCH_MENU_ITEM);
109   }
110 }
111
112 void SigninGlobalError::ExecuteMenuItem(Browser* browser) {
113 #if defined(OS_CHROMEOS)
114   if (auth_error_.state() != GoogleServiceAuthError::NONE) {
115     DLOG(INFO) << "Signing out the user to fix a sync error.";
116     // TODO(beng): seems like this could just call chrome::AttemptUserExit().
117     chrome::ExecuteCommand(browser, IDC_EXIT);
118     return;
119   }
120 #endif
121
122   // TODO(rogerta): what we do depends on which account is reporting an error.
123   // This will be needed once the account reconcilor is implemented.  The
124   // LoginUIService will support multi-login as well.
125
126   // Global errors don't show up in the wrench menu on android.
127 #if !defined(OS_ANDROID)
128   LoginUIService* login_ui = LoginUIServiceFactory::GetForProfile(profile_);
129   if (login_ui->current_login_ui()) {
130     login_ui->current_login_ui()->FocusUI();
131     return;
132   }
133   // Need to navigate to the settings page and display the UI.
134   chrome::ShowSettingsSubPage(browser, chrome::kSyncSetupSubPage);
135 #endif
136 }
137
138 bool SigninGlobalError::HasBubbleView() {
139   return !GetBubbleViewMessages().empty();
140 }
141
142 string16 SigninGlobalError::GetBubbleViewTitle() {
143   return l10n_util::GetStringUTF16(IDS_SIGNIN_ERROR_BUBBLE_VIEW_TITLE);
144 }
145
146 std::vector<string16> SigninGlobalError::GetBubbleViewMessages() {
147   std::vector<string16> messages;
148
149   // If the user isn't signed in, no need to display an error bubble.
150   SigninManagerBase* signin_manager =
151       SigninManagerFactory::GetForProfileIfExists(profile_);
152   if (signin_manager) {
153     std::string username = signin_manager->GetAuthenticatedUsername();
154     if (username.empty())
155       return messages;
156   }
157
158   switch (auth_error_.state()) {
159     // In the case of no error, or a simple network error, don't bother
160     // displaying a popup bubble.
161     case GoogleServiceAuthError::CONNECTION_FAILED:
162     case GoogleServiceAuthError::NONE:
163       return messages;
164
165     // TODO(rogerta): use account id in error messages.
166
167     // User credentials are invalid (bad acct, etc).
168     case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS:
169     case GoogleServiceAuthError::ACCOUNT_DELETED:
170     case GoogleServiceAuthError::ACCOUNT_DISABLED:
171       messages.push_back(l10n_util::GetStringFUTF16(
172           IDS_SYNC_SIGN_IN_ERROR_BUBBLE_VIEW_MESSAGE,
173           l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)));
174       break;
175
176     // Sync service is not available for this account's domain.
177     case GoogleServiceAuthError::SERVICE_UNAVAILABLE:
178       messages.push_back(l10n_util::GetStringFUTF16(
179           IDS_SYNC_UNAVAILABLE_ERROR_BUBBLE_VIEW_MESSAGE,
180           l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)));
181       break;
182
183     // Generic message for "other" errors.
184     default:
185       messages.push_back(l10n_util::GetStringFUTF16(
186           IDS_SYNC_OTHER_SIGN_IN_ERROR_BUBBLE_VIEW_MESSAGE,
187           l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)));
188   }
189   return messages;
190 }
191
192 string16 SigninGlobalError::GetBubbleViewAcceptButtonLabel() {
193   // If the service is unavailable, don't give the user the option to try
194   // signing in again.
195   if (auth_error_.state() == GoogleServiceAuthError::SERVICE_UNAVAILABLE) {
196     return l10n_util::GetStringUTF16(
197         IDS_SYNC_UNAVAILABLE_ERROR_BUBBLE_VIEW_ACCEPT);
198   } else {
199     return l10n_util::GetStringUTF16(IDS_SYNC_SIGN_IN_ERROR_BUBBLE_VIEW_ACCEPT);
200   }
201 }
202
203 string16 SigninGlobalError::GetBubbleViewCancelButtonLabel() {
204   return string16();
205 }
206
207 void SigninGlobalError::OnBubbleViewDidClose(Browser* browser) {
208 }
209
210 void SigninGlobalError::BubbleViewAcceptButtonPressed(Browser* browser) {
211   ExecuteMenuItem(browser);
212 }
213
214 void SigninGlobalError::BubbleViewCancelButtonPressed(Browser* browser) {
215   NOTREACHED();
216 }
217
218 // static
219 SigninGlobalError* SigninGlobalError::GetForProfile(Profile* profile) {
220   return ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->
221       signin_global_error();
222 }