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_factory.h"
15 #include "chrome/browser/browser_process.h"
16 #include "chrome/browser/browsing_data/browsing_data_helper.h"
17 #include "chrome/browser/browsing_data/browsing_data_remover.h"
18 #include "chrome/browser/profiles/profile_manager.h"
19 #include "chrome/browser/signin/android_profile_oauth2_token_service.h"
20 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
21 #include "chrome/browser/signin/signin_manager_factory.h"
22 #include "chrome/common/pref_names.h"
23 #include "components/bookmarks/browser/bookmark_model.h"
24 #include "components/signin/core/browser/profile_oauth2_token_service.h"
25 #include "components/signin/core/browser/signin_manager.h"
26 #include "components/signin/core/browser/signin_metrics.h"
27 #include "components/signin/core/common/profile_management_switches.h"
28 #include "jni/SigninManager_jni.h"
30 #if defined(ENABLE_CONFIGURATION_POLICY)
31 #include "chrome/browser/policy/cloud/user_cloud_policy_manager_factory.h"
32 #include "chrome/browser/policy/cloud/user_policy_signin_service_factory.h"
33 #include "chrome/browser/policy/cloud/user_policy_signin_service_mobile.h"
34 #include "components/policy/core/browser/browser_policy_connector.h"
35 #include "components/policy/core/common/cloud/cloud_policy_core.h"
36 #include "components/policy/core/common/cloud/cloud_policy_store.h"
37 #include "components/policy/core/common/cloud/user_cloud_policy_manager.h"
38 #include "google_apis/gaia/gaia_auth_util.h"
39 #include "net/url_request/url_request_context_getter.h"
44 // A BrowsingDataRemover::Observer that clears all Profile data and then
45 // invokes a callback and deletes itself.
46 class ProfileDataRemover : public BrowsingDataRemover::Observer {
48 ProfileDataRemover(Profile* profile, const base::Closure& callback)
49 : callback_(callback),
50 origin_loop_(base::MessageLoopProxy::current()),
51 remover_(BrowsingDataRemover::CreateForUnboundedRange(profile)) {
52 remover_->AddObserver(this);
53 remover_->Remove(BrowsingDataRemover::REMOVE_ALL, BrowsingDataHelper::ALL);
56 virtual ~ProfileDataRemover() {}
58 virtual void OnBrowsingDataRemoverDone() OVERRIDE {
59 remover_->RemoveObserver(this);
60 origin_loop_->PostTask(FROM_HERE, callback_);
61 origin_loop_->DeleteSoon(FROM_HERE, this);
65 base::Closure callback_;
66 scoped_refptr<base::MessageLoopProxy> origin_loop_;
67 BrowsingDataRemover* remover_;
69 DISALLOW_COPY_AND_ASSIGN(ProfileDataRemover);
74 SigninManagerAndroid::SigninManagerAndroid(JNIEnv* env, jobject obj)
77 java_signin_manager_.Reset(env, obj);
78 profile_ = ProfileManager::GetActiveUserProfile();
82 SigninManagerAndroid::~SigninManagerAndroid() {}
84 void SigninManagerAndroid::CheckPolicyBeforeSignIn(JNIEnv* env,
87 #if defined(ENABLE_CONFIGURATION_POLICY)
88 username_ = base::android::ConvertJavaStringToUTF8(env, username);
89 policy::UserPolicySigninService* service =
90 policy::UserPolicySigninServiceFactory::GetForProfile(profile_);
91 service->RegisterForPolicy(
92 base::android::ConvertJavaStringToUTF8(env, username),
93 base::Bind(&SigninManagerAndroid::OnPolicyRegisterDone,
94 weak_factory_.GetWeakPtr()));
96 // This shouldn't be called when ShouldLoadPolicyForUser() is false.
98 base::android::ScopedJavaLocalRef<jstring> domain;
99 Java_SigninManager_onPolicyCheckedBeforeSignIn(env,
100 java_signin_manager_.obj(),
105 void SigninManagerAndroid::FetchPolicyBeforeSignIn(JNIEnv* env, jobject obj) {
106 #if defined(ENABLE_CONFIGURATION_POLICY)
107 if (!dm_token_.empty()) {
108 policy::UserPolicySigninService* service =
109 policy::UserPolicySigninServiceFactory::GetForProfile(profile_);
110 service->FetchPolicyForSignedInUser(
114 profile_->GetRequestContext(),
115 base::Bind(&SigninManagerAndroid::OnPolicyFetchDone,
116 weak_factory_.GetWeakPtr()));
122 // This shouldn't be called when ShouldLoadPolicyForUser() is false, or when
123 // CheckPolicyBeforeSignIn() failed.
125 Java_SigninManager_onPolicyFetchedBeforeSignIn(env,
126 java_signin_manager_.obj());
129 void SigninManagerAndroid::OnSignInCompleted(JNIEnv* env,
132 SigninManagerFactory::GetForProfile(profile_)->OnExternalSigninCompleted(
133 base::android::ConvertJavaStringToUTF8(env, username));
136 void SigninManagerAndroid::SignOut(JNIEnv* env, jobject obj) {
137 SigninManagerFactory::GetForProfile(profile_)->SignOut(
138 signin_metrics::USER_CLICKED_SIGNOUT_SETTINGS);
141 base::android::ScopedJavaLocalRef<jstring>
142 SigninManagerAndroid::GetManagementDomain(JNIEnv* env, jobject obj) {
143 base::android::ScopedJavaLocalRef<jstring> domain;
145 #if defined(ENABLE_CONFIGURATION_POLICY)
146 policy::UserCloudPolicyManager* manager =
147 policy::UserCloudPolicyManagerFactory::GetForBrowserContext(profile_);
148 policy::CloudPolicyStore* store = manager->core()->store();
150 if (store && store->is_managed() && store->policy()->has_username()) {
152 base::android::ConvertUTF8ToJavaString(
153 env, gaia::ExtractDomainName(store->policy()->username())));
160 void SigninManagerAndroid::WipeProfileData(JNIEnv* env, jobject obj) {
161 // The ProfileDataRemover deletes itself once done.
162 new ProfileDataRemover(
164 base::Bind(&SigninManagerAndroid::OnBrowsingDataRemoverDone,
165 weak_factory_.GetWeakPtr()));
168 #if defined(ENABLE_CONFIGURATION_POLICY)
170 void SigninManagerAndroid::OnPolicyRegisterDone(
171 const std::string& dm_token,
172 const std::string& client_id) {
173 dm_token_ = dm_token;
174 client_id_ = client_id;
176 JNIEnv* env = base::android::AttachCurrentThread();
177 base::android::ScopedJavaLocalRef<jstring> domain;
178 if (!dm_token_.empty()) {
179 DCHECK(!username_.empty());
181 base::android::ConvertUTF8ToJavaString(
182 env, gaia::ExtractDomainName(username_)));
187 Java_SigninManager_onPolicyCheckedBeforeSignIn(env,
188 java_signin_manager_.obj(),
192 void SigninManagerAndroid::OnPolicyFetchDone(bool success) {
193 Java_SigninManager_onPolicyFetchedBeforeSignIn(
194 base::android::AttachCurrentThread(),
195 java_signin_manager_.obj());
200 void SigninManagerAndroid::OnBrowsingDataRemoverDone() {
201 BookmarkModel* model = BookmarkModelFactory::GetForProfile(profile_);
202 model->RemoveAllUserBookmarks();
204 // All the Profile data has been wiped. Clear the last signed in username as
205 // well, so that the next signin doesn't trigger the acount change dialog.
206 ClearLastSignedInUser();
208 Java_SigninManager_onProfileDataWiped(base::android::AttachCurrentThread(),
209 java_signin_manager_.obj());
212 void SigninManagerAndroid::ClearLastSignedInUser(JNIEnv* env, jobject obj) {
213 ClearLastSignedInUser();
216 void SigninManagerAndroid::ClearLastSignedInUser() {
217 profile_->GetPrefs()->ClearPref(prefs::kGoogleServicesLastUsername);
220 void SigninManagerAndroid::MergeSessionCompleted(
221 const std::string& account_id,
222 const GoogleServiceAuthError& error) {
223 merge_session_helper_->RemoveObserver(this);
224 merge_session_helper_.reset();
227 void SigninManagerAndroid::LogInSignedInUser(JNIEnv* env, jobject obj) {
228 SigninManagerBase* signin_manager =
229 SigninManagerFactory::GetForProfile(profile_);
230 if (switches::IsNewProfileManagement()) {
231 // New Mirror code path that just fires the events and let the
232 // Account Reconcilor handles everything.
233 AndroidProfileOAuth2TokenService* token_service =
234 ProfileOAuth2TokenServiceFactory::GetPlatformSpecificForProfile(
236 const std::string& primary_acct =
237 signin_manager->GetAuthenticatedAccountId();
238 token_service->ValidateAccounts(primary_acct, true);
241 DVLOG(1) << "SigninManagerAndroid::LogInSignedInUser "
242 " Manually calling MergeSessionHelper";
243 // Old code path that doesn't depend on the new Account Reconcilor.
244 // We manually login.
246 ProfileOAuth2TokenService* token_service =
247 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_);
248 merge_session_helper_.reset(new MergeSessionHelper(
249 token_service, profile_->GetRequestContext(), this));
250 merge_session_helper_->LogIn(signin_manager->GetAuthenticatedAccountId());
254 static jlong Init(JNIEnv* env, jobject obj) {
255 SigninManagerAndroid* signin_manager_android =
256 new SigninManagerAndroid(env, obj);
257 return reinterpret_cast<intptr_t>(signin_manager_android);
260 static jboolean ShouldLoadPolicyForUser(JNIEnv* env,
262 jstring j_username) {
263 #if defined(ENABLE_CONFIGURATION_POLICY)
264 std::string username =
265 base::android::ConvertJavaStringToUTF8(env, j_username);
266 return !policy::BrowserPolicyConnector::IsNonEnterpriseUser(username);
272 static jboolean IsNewProfileManagementEnabled(JNIEnv* env, jclass clazz) {
273 return switches::IsNewProfileManagement();
277 bool SigninManagerAndroid::Register(JNIEnv* env) {
278 return RegisterNativesImpl(env);