Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / signin / signin_ui_util.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_ui_util.h"
6
7 #include "base/strings/sys_string_conversions.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/signin/signin_global_error.h"
11 #include "chrome/browser/signin/signin_manager.h"
12 #include "chrome/browser/signin/signin_manager_factory.h"
13 #include "chrome/browser/sync/profile_sync_service.h"
14 #include "chrome/browser/sync/profile_sync_service_factory.h"
15 #include "chrome/browser/sync/sync_global_error.h"
16 #include "grit/chromium_strings.h"
17 #include "grit/generated_resources.h"
18 #include "ui/base/l10n/l10n_util.h"
19 #include "ui/gfx/font_list.h"
20 #include "ui/gfx/text_elider.h"
21
22 namespace {
23 // Maximum width of a username - we trim emails that are wider than this so
24 // the wrench menu doesn't get ridiculously wide.
25 const int kUsernameMaxWidth = 200;
26 }  // namespace
27
28 namespace signin_ui_util {
29
30 GlobalError* GetSignedInServiceError(Profile* profile) {
31   std::vector<GlobalError*> errors = GetSignedInServiceErrors(profile);
32   if (errors.empty())
33     return NULL;
34   return errors[0];
35 }
36
37 std::vector<GlobalError*> GetSignedInServiceErrors(Profile* profile) {
38   std::vector<GlobalError*> errors;
39
40   // Auth errors have the highest priority - after that, individual service
41   // errors.
42   SigninGlobalError* signin_error = SigninGlobalError::GetForProfile(profile);
43   if (signin_error && signin_error->HasMenuItem())
44     errors.push_back(signin_error);
45
46   // No auth error - now try other services. Currently the list is just hard-
47   // coded but in the future if we add more we can create some kind of
48   // registration framework.
49   if (profile->IsSyncAccessible()) {
50     ProfileSyncService* service =
51         ProfileSyncServiceFactory::GetForProfile(profile);
52     SyncGlobalError* error = service->sync_global_error();
53     if (error && error->HasMenuItem())
54       errors.push_back(error);
55   }
56
57   return errors;
58 }
59
60 base::string16 GetSigninMenuLabel(Profile* profile) {
61   GlobalError* error = signin_ui_util::GetSignedInServiceError(profile);
62   if (error)
63     return error->MenuItemLabel();
64
65   // No errors, so just display the signed in user, if any.
66   ProfileSyncService* service = profile->IsSyncAccessible() ?
67       ProfileSyncServiceFactory::GetForProfile(profile) : NULL;
68
69   // Even if the user is signed in, don't display the "signed in as..."
70   // label if we're still setting up sync.
71   if (!service || !service->FirstSetupInProgress()) {
72     std::string username;
73     SigninManagerBase* signin_manager =
74         SigninManagerFactory::GetForProfileIfExists(profile);
75     if (signin_manager)
76       username = signin_manager->GetAuthenticatedUsername();
77     if (!username.empty() && !signin_manager->AuthInProgress()) {
78       base::string16 elided_username = gfx::ElideEmail(
79           base::UTF8ToUTF16(username), gfx::FontList(), kUsernameMaxWidth);
80       return l10n_util::GetStringFUTF16(IDS_SYNC_MENU_SYNCED_LABEL,
81                                         elided_username);
82     }
83   }
84   return l10n_util::GetStringFUTF16(IDS_SYNC_MENU_PRE_SYNCED_LABEL,
85       l10n_util::GetStringUTF16(IDS_SHORT_PRODUCT_NAME));
86 }
87
88 // Given an authentication state this helper function returns various labels
89 // that can be used to display information about the state.
90 void GetStatusLabelsForAuthError(Profile* profile,
91                                  const SigninManagerBase& signin_manager,
92                                  base::string16* status_label,
93                                  base::string16* link_label) {
94   base::string16 username =
95       base::UTF8ToUTF16(signin_manager.GetAuthenticatedUsername());
96   base::string16 product_name = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME);
97   if (link_label)
98     link_label->assign(l10n_util::GetStringUTF16(IDS_SYNC_RELOGIN_LINK_LABEL));
99
100   const GoogleServiceAuthError::State state =
101       SigninGlobalError::GetForProfile(profile)->GetLastAuthError().state();
102   switch (state) {
103     case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS:
104     case GoogleServiceAuthError::SERVICE_ERROR:
105     case GoogleServiceAuthError::ACCOUNT_DELETED:
106     case GoogleServiceAuthError::ACCOUNT_DISABLED:
107       // If the user name is empty then the first login failed, otherwise the
108       // credentials are out-of-date.
109       if (username.empty()) {
110         if (status_label) {
111           status_label->assign(
112               l10n_util::GetStringUTF16(IDS_SYNC_INVALID_USER_CREDENTIALS));
113         }
114       } else {
115         if (status_label) {
116           status_label->assign(
117               l10n_util::GetStringUTF16(IDS_SYNC_LOGIN_INFO_OUT_OF_DATE));
118         }
119       }
120       break;
121     case GoogleServiceAuthError::SERVICE_UNAVAILABLE:
122       if (status_label) {
123         status_label->assign(
124             l10n_util::GetStringUTF16(IDS_SYNC_SERVICE_UNAVAILABLE));
125       }
126       if (link_label)
127         link_label->clear();
128       break;
129     case GoogleServiceAuthError::CONNECTION_FAILED:
130       if (status_label) {
131         status_label->assign(
132             l10n_util::GetStringFUTF16(IDS_SYNC_SERVER_IS_UNREACHABLE,
133                                        product_name));
134       }
135       // Note that there is little the user can do if the server is not
136       // reachable. Since attempting to re-connect is done automatically by
137       // the Syncer, we do not show the (re)login link.
138       if (link_label)
139         link_label->clear();
140       break;
141     default:
142       if (status_label) {
143         status_label->assign(l10n_util::GetStringUTF16(
144             IDS_SYNC_ERROR_SIGNING_IN));
145       }
146       break;
147   }
148 }
149
150 }  // namespace signin_ui_util