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.
5 #include "chrome/browser/android/signin/signin_manager_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
10 #include "base/bind_helpers.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/message_loop/message_loop_proxy.h"
13 #include "base/prefs/pref_service.h"
14 #include "chrome/browser/bookmarks/bookmark_model.h"
15 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/browsing_data/browsing_data_helper.h"
18 #include "chrome/browser/browsing_data/browsing_data_remover.h"
19 #include "chrome/browser/profiles/profile_manager.h"
20 #include "chrome/browser/signin/android_profile_oauth2_token_service.h"
21 #include "chrome/browser/signin/profile_oauth2_token_service.h"
22 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
23 #include "chrome/browser/signin/signin_manager.h"
24 #include "chrome/browser/signin/signin_manager_factory.h"
25 #include "chrome/common/pref_names.h"
26 #include "chrome/common/profile_management_switches.h"
27 #include "jni/SigninManager_jni.h"
29 #if defined(ENABLE_CONFIGURATION_POLICY)
30 #include "chrome/browser/policy/browser_policy_connector.h"
31 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
32 #include "chrome/browser/policy/cloud/user_policy_signin_service_android.h"
33 #include "chrome/browser/policy/cloud/user_policy_signin_service_factory.h"
34 #include "components/policy/core/common/cloud/cloud_policy_core.h"
35 #include "components/policy/core/common/cloud/cloud_policy_store.h"
36 #include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
37 #include "google_apis/gaia/gaia_auth_util.h"
38 #include "net/url_request/url_request_context_getter.h"
43 // A BrowsingDataRemover::Observer that clears all Profile data and then
44 // invokes a callback and deletes itself.
45 class ProfileDataRemover
: public BrowsingDataRemover::Observer
{
47 ProfileDataRemover(Profile
* profile
, const base::Closure
& callback
)
48 : callback_(callback
),
49 origin_loop_(base::MessageLoopProxy::current()),
50 remover_(BrowsingDataRemover::CreateForUnboundedRange(profile
)) {
51 remover_
->AddObserver(this);
52 remover_
->Remove(BrowsingDataRemover::REMOVE_ALL
, BrowsingDataHelper::ALL
);
55 virtual ~ProfileDataRemover() {}
57 virtual void OnBrowsingDataRemoverDone() OVERRIDE
{
58 remover_
->RemoveObserver(this);
59 origin_loop_
->PostTask(FROM_HERE
, callback_
);
60 origin_loop_
->DeleteSoon(FROM_HERE
, this);
64 base::Closure callback_
;
65 scoped_refptr
<base::MessageLoopProxy
> origin_loop_
;
66 BrowsingDataRemover
* remover_
;
68 DISALLOW_COPY_AND_ASSIGN(ProfileDataRemover
);
73 SigninManagerAndroid::SigninManagerAndroid(JNIEnv
* env
, jobject obj
)
76 java_signin_manager_
.Reset(env
, obj
);
77 profile_
= ProfileManager::GetActiveUserProfile();
81 SigninManagerAndroid::~SigninManagerAndroid() {}
83 void SigninManagerAndroid::CheckPolicyBeforeSignIn(JNIEnv
* env
,
86 #if defined(ENABLE_CONFIGURATION_POLICY)
87 username_
= base::android::ConvertJavaStringToUTF8(env
, username
);
88 policy::UserPolicySigninService
* service
=
89 policy::UserPolicySigninServiceFactory::GetForProfile(profile_
);
90 service
->RegisterForPolicy(
91 base::android::ConvertJavaStringToUTF8(env
, username
),
92 base::Bind(&SigninManagerAndroid::OnPolicyRegisterDone
,
93 weak_factory_
.GetWeakPtr()));
95 // This shouldn't be called when ShouldLoadPolicyForUser() is false.
97 base::android::ScopedJavaLocalRef
<jstring
> domain
;
98 Java_SigninManager_onPolicyCheckedBeforeSignIn(env
,
99 java_signin_manager_
.obj(),
104 void SigninManagerAndroid::FetchPolicyBeforeSignIn(JNIEnv
* env
, jobject obj
) {
105 #if defined(ENABLE_CONFIGURATION_POLICY)
106 if (!dm_token_
.empty()) {
107 policy::UserPolicySigninService
* service
=
108 policy::UserPolicySigninServiceFactory::GetForProfile(profile_
);
109 service
->FetchPolicyForSignedInUser(
113 profile_
->GetRequestContext(),
114 base::Bind(&SigninManagerAndroid::OnPolicyFetchDone
,
115 weak_factory_
.GetWeakPtr()));
121 // This shouldn't be called when ShouldLoadPolicyForUser() is false, or when
122 // CheckPolicyBeforeSignIn() failed.
124 Java_SigninManager_onPolicyFetchedBeforeSignIn(env
,
125 java_signin_manager_
.obj());
128 void SigninManagerAndroid::OnSignInCompleted(JNIEnv
* env
,
131 SigninManagerFactory::GetForProfile(profile_
)->OnExternalSigninCompleted(
132 base::android::ConvertJavaStringToUTF8(env
, username
));
135 void SigninManagerAndroid::SignOut(JNIEnv
* env
, jobject obj
) {
136 SigninManagerFactory::GetForProfile(profile_
)->SignOut();
139 base::android::ScopedJavaLocalRef
<jstring
>
140 SigninManagerAndroid::GetManagementDomain(JNIEnv
* env
, jobject obj
) {
141 base::android::ScopedJavaLocalRef
<jstring
> domain
;
143 #if defined(ENABLE_CONFIGURATION_POLICY)
144 policy::UserCloudPolicyManager
* manager
=
145 policy::UserCloudPolicyManagerFactory::GetForBrowserContext(profile_
);
146 policy::CloudPolicyStore
* store
= manager
->core()->store();
148 if (store
&& store
->is_managed() && store
->policy()->has_username()) {
150 base::android::ConvertUTF8ToJavaString(
151 env
, gaia::ExtractDomainName(store
->policy()->username())));
158 void SigninManagerAndroid::WipeProfileData(JNIEnv
* env
, jobject obj
) {
159 // The ProfileDataRemover deletes itself once done.
160 new ProfileDataRemover(
162 base::Bind(&SigninManagerAndroid::OnBrowsingDataRemoverDone
,
163 weak_factory_
.GetWeakPtr()));
166 #if defined(ENABLE_CONFIGURATION_POLICY)
168 void SigninManagerAndroid::OnPolicyRegisterDone(
169 const std::string
& dm_token
,
170 const std::string
& client_id
) {
171 dm_token_
= dm_token
;
172 client_id_
= client_id
;
174 JNIEnv
* env
= base::android::AttachCurrentThread();
175 base::android::ScopedJavaLocalRef
<jstring
> domain
;
176 if (!dm_token_
.empty()) {
177 DCHECK(!username_
.empty());
179 base::android::ConvertUTF8ToJavaString(
180 env
, gaia::ExtractDomainName(username_
)));
185 Java_SigninManager_onPolicyCheckedBeforeSignIn(env
,
186 java_signin_manager_
.obj(),
190 void SigninManagerAndroid::OnPolicyFetchDone(bool success
) {
191 Java_SigninManager_onPolicyFetchedBeforeSignIn(
192 base::android::AttachCurrentThread(),
193 java_signin_manager_
.obj());
198 void SigninManagerAndroid::OnBrowsingDataRemoverDone() {
199 BookmarkModel
* model
= BookmarkModelFactory::GetForProfile(profile_
);
202 // All the Profile data has been wiped. Clear the last signed in username as
203 // well, so that the next signin doesn't trigger the acount change dialog.
204 profile_
->GetPrefs()->ClearPref(prefs::kGoogleServicesLastUsername
);
206 Java_SigninManager_onProfileDataWiped(base::android::AttachCurrentThread(),
207 java_signin_manager_
.obj());
210 void SigninManagerAndroid::MergeSessionCompleted(
211 const std::string
& account_id
,
212 const GoogleServiceAuthError
& error
) {
213 merge_session_helper_
->RemoveObserver(this);
214 merge_session_helper_
.reset();
217 void SigninManagerAndroid::LogInSignedInUser(JNIEnv
* env
, jobject obj
) {
218 if (switches::IsNewProfileManagement()) {
219 // New Mirror code path that just fires the events and let the
220 // Account Reconcilor handles everything.
221 AndroidProfileOAuth2TokenService
* token_service
=
222 ProfileOAuth2TokenServiceFactory::GetPlatformSpecificForProfile(
224 const std::string
& primary_acct
= token_service
->GetPrimaryAccountId();
225 const std::vector
<std::string
>& ids
= token_service
->GetAccounts();
226 token_service
->ValidateAccounts(primary_acct
, ids
);
229 DVLOG(1) << "SigninManagerAndroid::LogInSignedInUser "
230 " Manually calling GoogleAutoLoginHelper";
231 // Old code path that doesn't depend on the new Account Reconcilor.
232 // We manually login.
234 ProfileOAuth2TokenService
* token_service
=
235 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_
);
236 merge_session_helper_
.reset(new GoogleAutoLoginHelper(profile_
, this));
237 merge_session_helper_
->LogIn(token_service
->GetPrimaryAccountId());
241 static jlong
Init(JNIEnv
* env
, jobject obj
) {
242 SigninManagerAndroid
* signin_manager_android
=
243 new SigninManagerAndroid(env
, obj
);
244 return reinterpret_cast<intptr_t>(signin_manager_android
);
247 static jboolean
ShouldLoadPolicyForUser(JNIEnv
* env
,
249 jstring j_username
) {
250 #if defined(ENABLE_CONFIGURATION_POLICY)
251 std::string username
=
252 base::android::ConvertJavaStringToUTF8(env
, j_username
);
253 return !policy::BrowserPolicyConnector::IsNonEnterpriseUser(username
);
260 bool SigninManagerAndroid::Register(JNIEnv
* env
) {
261 return RegisterNativesImpl(env
);