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 if (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
]
167 << " email=" << emails
[i
];
168 if (!gaia_ids
[i
].empty() && !emails
[i
].empty())
169 tracker
->SeedAccountInfo(gaia_ids
[i
], emails
[i
]);
172 DVLOG(1) << "SigninManagerAndroid::OnSignInCompleted: missing ids/email"
173 << " ids=" << (void*) accountIds
174 << " emails=" << (void*) accountNames
;
177 SigninManagerFactory::GetForProfile(profile_
)->OnExternalSigninCompleted(
178 base::android::ConvertJavaStringToUTF8(env
, username
));
181 void SigninManagerAndroid::SignOut(JNIEnv
* env
, jobject obj
) {
182 SigninManagerFactory::GetForProfile(profile_
)->SignOut(
183 signin_metrics::USER_CLICKED_SIGNOUT_SETTINGS
);
186 base::android::ScopedJavaLocalRef
<jstring
>
187 SigninManagerAndroid::GetManagementDomain(JNIEnv
* env
, jobject obj
) {
188 base::android::ScopedJavaLocalRef
<jstring
> domain
;
190 #if defined(ENABLE_CONFIGURATION_POLICY)
191 policy::UserCloudPolicyManager
* manager
=
192 policy::UserCloudPolicyManagerFactory::GetForBrowserContext(profile_
);
193 policy::CloudPolicyStore
* store
= manager
->core()->store();
195 if (store
&& store
->is_managed() && store
->policy()->has_username()) {
197 base::android::ConvertUTF8ToJavaString(
198 env
, gaia::ExtractDomainName(store
->policy()->username())));
205 void SigninManagerAndroid::WipeProfileData(JNIEnv
* env
, jobject obj
) {
206 // The ProfileDataRemover deletes itself once done.
207 new ProfileDataRemover(
209 base::Bind(&SigninManagerAndroid::OnBrowsingDataRemoverDone
,
210 weak_factory_
.GetWeakPtr()));
213 #if defined(ENABLE_CONFIGURATION_POLICY)
215 void SigninManagerAndroid::OnPolicyRegisterDone(
216 const std::string
& dm_token
,
217 const std::string
& client_id
) {
218 dm_token_
= dm_token
;
219 client_id_
= client_id
;
221 JNIEnv
* env
= base::android::AttachCurrentThread();
222 base::android::ScopedJavaLocalRef
<jstring
> domain
;
223 if (!dm_token_
.empty()) {
224 DCHECK(!username_
.empty());
226 base::android::ConvertUTF8ToJavaString(
227 env
, gaia::ExtractDomainName(username_
)));
232 Java_SigninManager_onPolicyCheckedBeforeSignIn(env
,
233 java_signin_manager_
.obj(),
237 void SigninManagerAndroid::OnPolicyFetchDone(bool success
) {
238 Java_SigninManager_onPolicyFetchedBeforeSignIn(
239 base::android::AttachCurrentThread(),
240 java_signin_manager_
.obj());
245 void SigninManagerAndroid::OnBrowsingDataRemoverDone() {
246 BookmarkModel
* model
= BookmarkModelFactory::GetForProfile(profile_
);
247 model
->RemoveAllUserBookmarks();
249 // All the Profile data has been wiped. Clear the last signed in username as
250 // well, so that the next signin doesn't trigger the acount change dialog.
251 ClearLastSignedInUser();
253 Java_SigninManager_onProfileDataWiped(base::android::AttachCurrentThread(),
254 java_signin_manager_
.obj());
257 void SigninManagerAndroid::ClearLastSignedInUser(JNIEnv
* env
, jobject obj
) {
258 ClearLastSignedInUser();
261 void SigninManagerAndroid::ClearLastSignedInUser() {
262 profile_
->GetPrefs()->ClearPref(prefs::kGoogleServicesLastUsername
);
265 void SigninManagerAndroid::LogInSignedInUser(JNIEnv
* env
, jobject obj
) {
266 SigninManagerBase
* signin_manager
=
267 SigninManagerFactory::GetForProfile(profile_
);
268 // With the account consistency enabled let the account Reconcilor handles
270 ProfileOAuth2TokenService
* token_service
=
271 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_
);
272 const std::string
& primary_acct
= signin_manager
->GetAuthenticatedAccountId();
274 static_cast<OAuth2TokenServiceDelegateAndroid
*>(token_service
->GetDelegate())
275 ->ValidateAccounts(primary_acct
, true);
278 jboolean
SigninManagerAndroid::IsSigninAllowedByPolicy(JNIEnv
* env
,
280 return SigninManagerFactory::GetForProfile(profile_
)->IsSigninAllowed();
283 jboolean
SigninManagerAndroid::IsSignedInOnNative(JNIEnv
* env
, jobject obj
) {
284 return SigninManagerFactory::GetForProfile(profile_
)->IsAuthenticated();
287 void SigninManagerAndroid::OnSigninAllowedPrefChanged() {
288 Java_SigninManager_onSigninAllowedByPolicyChanged(
289 base::android::AttachCurrentThread(), java_signin_manager_
.obj(),
290 SigninManagerFactory::GetForProfile(profile_
)->IsSigninAllowed());
293 static jlong
Init(JNIEnv
* env
, jobject obj
) {
294 SigninManagerAndroid
* signin_manager_android
=
295 new SigninManagerAndroid(env
, obj
);
296 return reinterpret_cast<intptr_t>(signin_manager_android
);
299 static jboolean
ShouldLoadPolicyForUser(JNIEnv
* env
,
301 jstring j_username
) {
302 #if defined(ENABLE_CONFIGURATION_POLICY)
303 std::string username
=
304 base::android::ConvertJavaStringToUTF8(env
, j_username
);
305 return !policy::BrowserPolicyConnector::IsNonEnterpriseUser(username
);
312 bool SigninManagerAndroid::Register(JNIEnv
* env
) {
313 return RegisterNativesImpl(env
);