1 // Copyright 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.
7 #include "chrome/browser/android/signin/signin_manager_android.h"
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_array.h"
11 #include "base/android/jni_string.h"
12 #include "base/bind.h"
13 #include "base/bind_helpers.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/prefs/pref_service.h"
16 #include "base/single_thread_task_runner.h"
17 #include "base/thread_task_runner_handle.h"
18 #include "base/thread_task_runner_handle.h"
19 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
20 #include "chrome/browser/browser_process.h"
21 #include "chrome/browser/browsing_data/browsing_data_helper.h"
22 #include "chrome/browser/browsing_data/browsing_data_remover.h"
23 #include "chrome/browser/profiles/profile_manager.h"
24 #include "chrome/browser/signin/account_tracker_service_factory.h"
25 #include "chrome/browser/signin/oauth2_token_service_delegate_android.h"
26 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
27 #include "chrome/browser/signin/signin_manager_factory.h"
28 #include "chrome/common/pref_names.h"
29 #include "components/bookmarks/browser/bookmark_model.h"
30 #include "components/signin/core/browser/account_tracker_service.h"
31 #include "components/signin/core/browser/profile_oauth2_token_service.h"
32 #include "components/signin/core/browser/signin_manager.h"
33 #include "components/signin/core/browser/signin_metrics.h"
34 #include "components/signin/core/common/profile_management_switches.h"
35 #include "google_apis/gaia/gaia_constants.h"
36 #include "jni/SigninManager_jni.h"
38 #if defined(ENABLE_CONFIGURATION_POLICY)
39 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
40 #include "chrome/browser/policy/cloud/user_policy_signin_service_factory.h"
41 #include "chrome/browser/policy/cloud/user_policy_signin_service_mobile.h"
42 #include "components/policy/core/browser/browser_policy_connector.h"
43 #include "components/policy/core/common/cloud/cloud_policy_core.h"
44 #include "components/policy/core/common/cloud/cloud_policy_store.h"
45 #include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
46 #include "google_apis/gaia/gaia_auth_util.h"
47 #include "net/url_request/url_request_context_getter.h"
50 using bookmarks::BookmarkModel
;
54 // A BrowsingDataRemover::Observer that clears all Profile data and then
55 // invokes a callback and deletes itself.
56 class ProfileDataRemover
: public BrowsingDataRemover::Observer
{
58 ProfileDataRemover(Profile
* profile
, const base::Closure
& callback
)
59 : callback_(callback
),
60 origin_runner_(base::ThreadTaskRunnerHandle::Get()),
61 remover_(BrowsingDataRemover::CreateForUnboundedRange(profile
)) {
62 remover_
->AddObserver(this);
63 remover_
->Remove(BrowsingDataRemover::REMOVE_ALL
, BrowsingDataHelper::ALL
);
66 ~ProfileDataRemover() override
{}
68 void OnBrowsingDataRemoverDone() override
{
69 remover_
->RemoveObserver(this);
70 origin_runner_
->PostTask(FROM_HERE
, callback_
);
71 origin_runner_
->DeleteSoon(FROM_HERE
, this);
75 base::Closure callback_
;
76 scoped_refptr
<base::SingleThreadTaskRunner
> origin_runner_
;
77 BrowsingDataRemover
* remover_
;
79 DISALLOW_COPY_AND_ASSIGN(ProfileDataRemover
);
84 SigninManagerAndroid::SigninManagerAndroid(JNIEnv
* env
, jobject obj
)
87 java_signin_manager_
.Reset(env
, obj
);
88 profile_
= ProfileManager::GetActiveUserProfile();
90 pref_change_registrar_
.Init(profile_
->GetPrefs());
91 pref_change_registrar_
.Add(
92 prefs::kSigninAllowed
,
93 base::Bind(&SigninManagerAndroid::OnSigninAllowedPrefChanged
,
94 base::Unretained(this)));
97 SigninManagerAndroid::~SigninManagerAndroid() {}
99 void SigninManagerAndroid::CheckPolicyBeforeSignIn(JNIEnv
* env
,
102 #if defined(ENABLE_CONFIGURATION_POLICY)
103 username_
= base::android::ConvertJavaStringToUTF8(env
, username
);
104 policy::UserPolicySigninService
* service
=
105 policy::UserPolicySigninServiceFactory::GetForProfile(profile_
);
106 service
->RegisterForPolicy(
107 base::android::ConvertJavaStringToUTF8(env
, username
),
108 base::Bind(&SigninManagerAndroid::OnPolicyRegisterDone
,
109 weak_factory_
.GetWeakPtr()));
111 // This shouldn't be called when ShouldLoadPolicyForUser() is false.
113 base::android::ScopedJavaLocalRef
<jstring
> domain
;
114 Java_SigninManager_onPolicyCheckedBeforeSignIn(env
,
115 java_signin_manager_
.obj(),
120 void SigninManagerAndroid::FetchPolicyBeforeSignIn(JNIEnv
* env
, jobject obj
) {
121 #if defined(ENABLE_CONFIGURATION_POLICY)
122 if (!dm_token_
.empty()) {
123 policy::UserPolicySigninService
* service
=
124 policy::UserPolicySigninServiceFactory::GetForProfile(profile_
);
125 service
->FetchPolicyForSignedInUser(
129 profile_
->GetRequestContext(),
130 base::Bind(&SigninManagerAndroid::OnPolicyFetchDone
,
131 weak_factory_
.GetWeakPtr()));
137 // This shouldn't be called when ShouldLoadPolicyForUser() is false, or when
138 // CheckPolicyBeforeSignIn() failed.
140 Java_SigninManager_onPolicyFetchedBeforeSignIn(env
,
141 java_signin_manager_
.obj());
144 void SigninManagerAndroid::OnSignInCompleted(JNIEnv
* env
,
147 jobjectArray accountIds
,
148 jobjectArray accountNames
) {
149 DVLOG(1) << "SigninManagerAndroid::OnSignInCompleted";
150 // Seed the account tracker with id/email information if provided.
151 DCHECK(accountIds
&& accountNames
);
152 std::vector
<std::string
> gaia_ids
;
153 std::vector
<std::string
> emails
;
154 base::android::AppendJavaStringArrayToStringVector(env
, accountIds
,
156 base::android::AppendJavaStringArrayToStringVector(env
, accountNames
,
158 DCHECK_EQ(emails
.size(), gaia_ids
.size());
159 DVLOG(1) << "SigninManagerAndroid::OnSignInCompleted: seeding "
160 << emails
.size() << " accounts";
162 AccountTrackerService
* tracker
=
163 AccountTrackerServiceFactory::GetForProfile(profile_
);
164 for (size_t i
= 0; i
< emails
.size(); ++i
) {
165 DVLOG(1) << "SigninManagerAndroid::OnSignInCompleted: seeding"
166 << " gaia_id=" << gaia_ids
[i
] << " email=" << emails
[i
];
167 if (!gaia_ids
[i
].empty() && !emails
[i
].empty())
168 tracker
->SeedAccountInfo(gaia_ids
[i
], emails
[i
]);
171 SigninManagerFactory::GetForProfile(profile_
)->OnExternalSigninCompleted(
172 base::android::ConvertJavaStringToUTF8(env
, username
));
175 void SigninManagerAndroid::SignOut(JNIEnv
* env
, jobject obj
) {
176 SigninManagerFactory::GetForProfile(profile_
)->SignOut(
177 signin_metrics::USER_CLICKED_SIGNOUT_SETTINGS
);
180 base::android::ScopedJavaLocalRef
<jstring
>
181 SigninManagerAndroid::GetManagementDomain(JNIEnv
* env
, jobject obj
) {
182 base::android::ScopedJavaLocalRef
<jstring
> domain
;
184 #if defined(ENABLE_CONFIGURATION_POLICY)
185 policy::UserCloudPolicyManager
* manager
=
186 policy::UserCloudPolicyManagerFactory::GetForBrowserContext(profile_
);
187 policy::CloudPolicyStore
* store
= manager
->core()->store();
189 if (store
&& store
->is_managed() && store
->policy()->has_username()) {
191 base::android::ConvertUTF8ToJavaString(
192 env
, gaia::ExtractDomainName(store
->policy()->username())));
199 void SigninManagerAndroid::WipeProfileData(JNIEnv
* env
, jobject obj
) {
200 // The ProfileDataRemover deletes itself once done.
201 new ProfileDataRemover(
203 base::Bind(&SigninManagerAndroid::OnBrowsingDataRemoverDone
,
204 weak_factory_
.GetWeakPtr()));
207 #if defined(ENABLE_CONFIGURATION_POLICY)
209 void SigninManagerAndroid::OnPolicyRegisterDone(
210 const std::string
& dm_token
,
211 const std::string
& client_id
) {
212 dm_token_
= dm_token
;
213 client_id_
= client_id
;
215 JNIEnv
* env
= base::android::AttachCurrentThread();
216 base::android::ScopedJavaLocalRef
<jstring
> domain
;
217 if (!dm_token_
.empty()) {
218 DCHECK(!username_
.empty());
220 base::android::ConvertUTF8ToJavaString(
221 env
, gaia::ExtractDomainName(username_
)));
226 Java_SigninManager_onPolicyCheckedBeforeSignIn(env
,
227 java_signin_manager_
.obj(),
231 void SigninManagerAndroid::OnPolicyFetchDone(bool success
) {
232 Java_SigninManager_onPolicyFetchedBeforeSignIn(
233 base::android::AttachCurrentThread(),
234 java_signin_manager_
.obj());
239 void SigninManagerAndroid::OnBrowsingDataRemoverDone() {
240 BookmarkModel
* model
= BookmarkModelFactory::GetForProfile(profile_
);
241 model
->RemoveAllUserBookmarks();
243 // All the Profile data has been wiped. Clear the last signed in username as
244 // well, so that the next signin doesn't trigger the acount change dialog.
245 ClearLastSignedInUser();
247 Java_SigninManager_onProfileDataWiped(base::android::AttachCurrentThread(),
248 java_signin_manager_
.obj());
251 void SigninManagerAndroid::ClearLastSignedInUser(JNIEnv
* env
, jobject obj
) {
252 ClearLastSignedInUser();
255 void SigninManagerAndroid::ClearLastSignedInUser() {
256 profile_
->GetPrefs()->ClearPref(prefs::kGoogleServicesLastUsername
);
259 void SigninManagerAndroid::LogInSignedInUser(JNIEnv
* env
, jobject obj
) {
260 SigninManagerBase
* signin_manager
=
261 SigninManagerFactory::GetForProfile(profile_
);
262 // With the account consistency enabled let the account Reconcilor handles
264 ProfileOAuth2TokenService
* token_service
=
265 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_
);
266 const std::string
& primary_acct
= signin_manager
->GetAuthenticatedAccountId();
268 static_cast<OAuth2TokenServiceDelegateAndroid
*>(token_service
->GetDelegate())
269 ->ValidateAccounts(primary_acct
, true);
272 jboolean
SigninManagerAndroid::IsSigninAllowedByPolicy(JNIEnv
* env
,
274 return SigninManagerFactory::GetForProfile(profile_
)->IsSigninAllowed();
277 jboolean
SigninManagerAndroid::IsSignedInOnNative(JNIEnv
* env
, jobject obj
) {
278 return SigninManagerFactory::GetForProfile(profile_
)->IsAuthenticated();
281 void SigninManagerAndroid::OnSigninAllowedPrefChanged() {
282 Java_SigninManager_onSigninAllowedByPolicyChanged(
283 base::android::AttachCurrentThread(), java_signin_manager_
.obj(),
284 SigninManagerFactory::GetForProfile(profile_
)->IsSigninAllowed());
287 static jlong
Init(JNIEnv
* env
, jobject obj
) {
288 SigninManagerAndroid
* signin_manager_android
=
289 new SigninManagerAndroid(env
, obj
);
290 return reinterpret_cast<intptr_t>(signin_manager_android
);
293 static jboolean
ShouldLoadPolicyForUser(JNIEnv
* env
,
295 jstring j_username
) {
296 #if defined(ENABLE_CONFIGURATION_POLICY)
297 std::string username
=
298 base::android::ConvertJavaStringToUTF8(env
, j_username
);
299 return !policy::BrowserPolicyConnector::IsNonEnterpriseUser(username
);
306 bool SigninManagerAndroid::Register(JNIEnv
* env
) {
307 return RegisterNativesImpl(env
);