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/message_loop/message_loop_proxy.h"
16 #include "base/prefs/pref_service.h"
17 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
18 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/browsing_data/browsing_data_helper.h"
20 #include "chrome/browser/browsing_data/browsing_data_remover.h"
21 #include "chrome/browser/profiles/profile_manager.h"
22 #include "chrome/browser/signin/account_tracker_service_factory.h"
23 #include "chrome/browser/signin/android_profile_oauth2_token_service.h"
24 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
25 #include "chrome/browser/signin/signin_manager_factory.h"
26 #include "chrome/common/pref_names.h"
27 #include "components/bookmarks/browser/bookmark_model.h"
28 #include "components/signin/core/browser/account_tracker_service.h"
29 #include "components/signin/core/browser/profile_oauth2_token_service.h"
30 #include "components/signin/core/browser/signin_manager.h"
31 #include "components/signin/core/browser/signin_metrics.h"
32 #include "components/signin/core/common/profile_management_switches.h"
33 #include "google_apis/gaia/gaia_constants.h"
34 #include "jni/SigninManager_jni.h"
36 #if defined(ENABLE_CONFIGURATION_POLICY)
37 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
38 #include "chrome/browser/policy/cloud/user_policy_signin_service_factory.h"
39 #include "chrome/browser/policy/cloud/user_policy_signin_service_mobile.h"
40 #include "components/policy/core/browser/browser_policy_connector.h"
41 #include "components/policy/core/common/cloud/cloud_policy_core.h"
42 #include "components/policy/core/common/cloud/cloud_policy_store.h"
43 #include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
44 #include "google_apis/gaia/gaia_auth_util.h"
45 #include "net/url_request/url_request_context_getter.h"
48 using bookmarks::BookmarkModel
;
52 // A BrowsingDataRemover::Observer that clears all Profile data and then
53 // invokes a callback and deletes itself.
54 class ProfileDataRemover
: public BrowsingDataRemover::Observer
{
56 ProfileDataRemover(Profile
* profile
, const base::Closure
& callback
)
57 : callback_(callback
),
58 origin_loop_(base::MessageLoopProxy::current()),
59 remover_(BrowsingDataRemover::CreateForUnboundedRange(profile
)) {
60 remover_
->AddObserver(this);
61 remover_
->Remove(BrowsingDataRemover::REMOVE_ALL
, BrowsingDataHelper::ALL
);
64 ~ProfileDataRemover() override
{}
66 void OnBrowsingDataRemoverDone() override
{
67 remover_
->RemoveObserver(this);
68 origin_loop_
->PostTask(FROM_HERE
, callback_
);
69 origin_loop_
->DeleteSoon(FROM_HERE
, this);
73 base::Closure callback_
;
74 scoped_refptr
<base::MessageLoopProxy
> origin_loop_
;
75 BrowsingDataRemover
* remover_
;
77 DISALLOW_COPY_AND_ASSIGN(ProfileDataRemover
);
82 SigninManagerAndroid::SigninManagerAndroid(JNIEnv
* env
, jobject obj
)
85 java_signin_manager_
.Reset(env
, obj
);
86 profile_
= ProfileManager::GetActiveUserProfile();
88 pref_change_registrar_
.Init(profile_
->GetPrefs());
89 pref_change_registrar_
.Add(
90 prefs::kSigninAllowed
,
91 base::Bind(&SigninManagerAndroid::OnSigninAllowedPrefChanged
,
92 base::Unretained(this)));
95 SigninManagerAndroid::~SigninManagerAndroid() {}
97 void SigninManagerAndroid::CheckPolicyBeforeSignIn(JNIEnv
* env
,
100 #if defined(ENABLE_CONFIGURATION_POLICY)
101 username_
= base::android::ConvertJavaStringToUTF8(env
, username
);
102 policy::UserPolicySigninService
* service
=
103 policy::UserPolicySigninServiceFactory::GetForProfile(profile_
);
104 service
->RegisterForPolicy(
105 base::android::ConvertJavaStringToUTF8(env
, username
),
106 base::Bind(&SigninManagerAndroid::OnPolicyRegisterDone
,
107 weak_factory_
.GetWeakPtr()));
109 // This shouldn't be called when ShouldLoadPolicyForUser() is false.
111 base::android::ScopedJavaLocalRef
<jstring
> domain
;
112 Java_SigninManager_onPolicyCheckedBeforeSignIn(env
,
113 java_signin_manager_
.obj(),
118 void SigninManagerAndroid::FetchPolicyBeforeSignIn(JNIEnv
* env
, jobject obj
) {
119 #if defined(ENABLE_CONFIGURATION_POLICY)
120 if (!dm_token_
.empty()) {
121 policy::UserPolicySigninService
* service
=
122 policy::UserPolicySigninServiceFactory::GetForProfile(profile_
);
123 service
->FetchPolicyForSignedInUser(
127 profile_
->GetRequestContext(),
128 base::Bind(&SigninManagerAndroid::OnPolicyFetchDone
,
129 weak_factory_
.GetWeakPtr()));
135 // This shouldn't be called when ShouldLoadPolicyForUser() is false, or when
136 // CheckPolicyBeforeSignIn() failed.
138 Java_SigninManager_onPolicyFetchedBeforeSignIn(env
,
139 java_signin_manager_
.obj());
142 void SigninManagerAndroid::OnSignInCompleted(JNIEnv
* env
,
145 jobjectArray accountIds
,
146 jobjectArray accountNames
) {
147 DVLOG(1) << "SigninManagerAndroid::OnSignInCompleted";
148 // Seed the account tracker with id/email information if provided.
149 if (accountIds
&& accountNames
) {
150 std::vector
<std::string
> gaia_ids
;
151 std::vector
<std::string
> emails
;
152 base::android::AppendJavaStringArrayToStringVector(env
, accountIds
,
154 base::android::AppendJavaStringArrayToStringVector(env
, accountNames
,
156 DCHECK_EQ(emails
.size(), gaia_ids
.size());
157 DVLOG(1) << "SigninManagerAndroid::OnSignInCompleted: seeding "
158 << emails
.size() << " accounts";
160 AccountTrackerService
* tracker
=
161 AccountTrackerServiceFactory::GetForProfile(profile_
);
162 for (size_t i
= 0; i
< emails
.size(); ++i
) {
163 DVLOG(1) << "SigninManagerAndroid::OnSignInCompleted: seeding"
164 << " gaia_id=" << gaia_ids
[i
]
165 << " email=" << emails
[i
];
166 if (!gaia_ids
[i
].empty() && !emails
[i
].empty())
167 tracker
->SeedAccountInfo(gaia_ids
[i
], emails
[i
]);
170 DVLOG(1) << "SigninManagerAndroid::OnSignInCompleted: missing ids/email"
171 << " ids=" << (void*) accountIds
172 << " emails=" << (void*) accountNames
;
175 SigninManagerFactory::GetForProfile(profile_
)->OnExternalSigninCompleted(
176 base::android::ConvertJavaStringToUTF8(env
, username
));
179 void SigninManagerAndroid::SignOut(JNIEnv
* env
, jobject obj
) {
180 SigninManagerFactory::GetForProfile(profile_
)->SignOut(
181 signin_metrics::USER_CLICKED_SIGNOUT_SETTINGS
);
184 base::android::ScopedJavaLocalRef
<jstring
>
185 SigninManagerAndroid::GetManagementDomain(JNIEnv
* env
, jobject obj
) {
186 base::android::ScopedJavaLocalRef
<jstring
> domain
;
188 #if defined(ENABLE_CONFIGURATION_POLICY)
189 policy::UserCloudPolicyManager
* manager
=
190 policy::UserCloudPolicyManagerFactory::GetForBrowserContext(profile_
);
191 policy::CloudPolicyStore
* store
= manager
->core()->store();
193 if (store
&& store
->is_managed() && store
->policy()->has_username()) {
195 base::android::ConvertUTF8ToJavaString(
196 env
, gaia::ExtractDomainName(store
->policy()->username())));
203 void SigninManagerAndroid::WipeProfileData(JNIEnv
* env
, jobject obj
) {
204 // The ProfileDataRemover deletes itself once done.
205 new ProfileDataRemover(
207 base::Bind(&SigninManagerAndroid::OnBrowsingDataRemoverDone
,
208 weak_factory_
.GetWeakPtr()));
211 #if defined(ENABLE_CONFIGURATION_POLICY)
213 void SigninManagerAndroid::OnPolicyRegisterDone(
214 const std::string
& dm_token
,
215 const std::string
& client_id
) {
216 dm_token_
= dm_token
;
217 client_id_
= client_id
;
219 JNIEnv
* env
= base::android::AttachCurrentThread();
220 base::android::ScopedJavaLocalRef
<jstring
> domain
;
221 if (!dm_token_
.empty()) {
222 DCHECK(!username_
.empty());
224 base::android::ConvertUTF8ToJavaString(
225 env
, gaia::ExtractDomainName(username_
)));
230 Java_SigninManager_onPolicyCheckedBeforeSignIn(env
,
231 java_signin_manager_
.obj(),
235 void SigninManagerAndroid::OnPolicyFetchDone(bool success
) {
236 Java_SigninManager_onPolicyFetchedBeforeSignIn(
237 base::android::AttachCurrentThread(),
238 java_signin_manager_
.obj());
243 void SigninManagerAndroid::OnBrowsingDataRemoverDone() {
244 BookmarkModel
* model
= BookmarkModelFactory::GetForProfile(profile_
);
245 model
->RemoveAllUserBookmarks();
247 // All the Profile data has been wiped. Clear the last signed in username as
248 // well, so that the next signin doesn't trigger the acount change dialog.
249 ClearLastSignedInUser();
251 Java_SigninManager_onProfileDataWiped(base::android::AttachCurrentThread(),
252 java_signin_manager_
.obj());
255 void SigninManagerAndroid::ClearLastSignedInUser(JNIEnv
* env
, jobject obj
) {
256 ClearLastSignedInUser();
259 void SigninManagerAndroid::ClearLastSignedInUser() {
260 profile_
->GetPrefs()->ClearPref(prefs::kGoogleServicesLastUsername
);
263 void SigninManagerAndroid::MergeSessionCompleted(
264 const std::string
& account_id
,
265 const GoogleServiceAuthError
& error
) {
266 merge_session_helper_
->RemoveObserver(this);
267 merge_session_helper_
.reset();
270 void SigninManagerAndroid::LogInSignedInUser(JNIEnv
* env
, jobject obj
) {
271 SigninManagerBase
* signin_manager
=
272 SigninManagerFactory::GetForProfile(profile_
);
273 // Just fire the events and let the Account Reconcilor handles everything.
274 AndroidProfileOAuth2TokenService
* token_service
=
275 ProfileOAuth2TokenServiceFactory::GetPlatformSpecificForProfile(
277 const std::string
& primary_acct
=
278 signin_manager
->GetAuthenticatedAccountId();
279 token_service
->ValidateAccounts(primary_acct
, true);
282 jboolean
SigninManagerAndroid::IsSigninAllowedByPolicy(JNIEnv
* env
,
284 return SigninManagerFactory::GetForProfile(profile_
)->IsSigninAllowed();
287 jboolean
SigninManagerAndroid::IsSignedInOnNative(JNIEnv
* env
, jobject obj
) {
288 return SigninManagerFactory::GetForProfile(profile_
)->IsAuthenticated();
291 void SigninManagerAndroid::OnSigninAllowedPrefChanged() {
292 Java_SigninManager_onSigninAllowedByPolicyChanged(
293 base::android::AttachCurrentThread(), java_signin_manager_
.obj(),
294 SigninManagerFactory::GetForProfile(profile_
)->IsSigninAllowed());
297 static jlong
Init(JNIEnv
* env
, jobject obj
) {
298 SigninManagerAndroid
* signin_manager_android
=
299 new SigninManagerAndroid(env
, obj
);
300 return reinterpret_cast<intptr_t>(signin_manager_android
);
303 static jboolean
ShouldLoadPolicyForUser(JNIEnv
* env
,
305 jstring j_username
) {
306 #if defined(ENABLE_CONFIGURATION_POLICY)
307 std::string username
=
308 base::android::ConvertJavaStringToUTF8(env
, j_username
);
309 return !policy::BrowserPolicyConnector::IsNonEnterpriseUser(username
);
316 bool SigninManagerAndroid::Register(JNIEnv
* env
) {
317 return RegisterNativesImpl(env
);