Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / android / signin / signin_manager_android.cc
blobd11e1adb0b409483ae392245111f4f2d5091b0ea
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 <vector>
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"
48 #endif
50 using bookmarks::BookmarkModel;
52 namespace {
54 // A BrowsingDataRemover::Observer that clears all Profile data and then
55 // invokes a callback and deletes itself.
56 class ProfileDataRemover : public BrowsingDataRemover::Observer {
57 public:
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);
74 private:
75 base::Closure callback_;
76 scoped_refptr<base::SingleThreadTaskRunner> origin_runner_;
77 BrowsingDataRemover* remover_;
79 DISALLOW_COPY_AND_ASSIGN(ProfileDataRemover);
82 } // namespace
84 SigninManagerAndroid::SigninManagerAndroid(JNIEnv* env, jobject obj)
85 : profile_(NULL),
86 weak_factory_(this) {
87 java_signin_manager_.Reset(env, obj);
88 profile_ = ProfileManager::GetActiveUserProfile();
89 DCHECK(profile_);
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,
100 jobject obj,
101 jstring username) {
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()));
110 #else
111 // This shouldn't be called when ShouldLoadPolicyForUser() is false.
112 NOTREACHED();
113 base::android::ScopedJavaLocalRef<jstring> domain;
114 Java_SigninManager_onPolicyCheckedBeforeSignIn(env,
115 java_signin_manager_.obj(),
116 domain.obj());
117 #endif
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(
126 username_,
127 dm_token_,
128 client_id_,
129 profile_->GetRequestContext(),
130 base::Bind(&SigninManagerAndroid::OnPolicyFetchDone,
131 weak_factory_.GetWeakPtr()));
132 dm_token_.clear();
133 client_id_.clear();
134 return;
136 #endif
137 // This shouldn't be called when ShouldLoadPolicyForUser() is false, or when
138 // CheckPolicyBeforeSignIn() failed.
139 NOTREACHED();
140 Java_SigninManager_onPolicyFetchedBeforeSignIn(env,
141 java_signin_manager_.obj());
144 void SigninManagerAndroid::OnSignInCompleted(JNIEnv* env,
145 jobject obj,
146 jstring username,
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,
155 &gaia_ids);
156 base::android::AppendJavaStringArrayToStringVector(env, accountNames,
157 &emails);
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]);
171 } else {
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()) {
196 domain.Reset(
197 base::android::ConvertUTF8ToJavaString(
198 env, gaia::ExtractDomainName(store->policy()->username())));
200 #endif
202 return domain;
205 void SigninManagerAndroid::WipeProfileData(JNIEnv* env, jobject obj) {
206 // The ProfileDataRemover deletes itself once done.
207 new ProfileDataRemover(
208 profile_,
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());
225 domain.Reset(
226 base::android::ConvertUTF8ToJavaString(
227 env, gaia::ExtractDomainName(username_)));
228 } else {
229 username_.clear();
232 Java_SigninManager_onPolicyCheckedBeforeSignIn(env,
233 java_signin_manager_.obj(),
234 domain.obj());
237 void SigninManagerAndroid::OnPolicyFetchDone(bool success) {
238 Java_SigninManager_onPolicyFetchedBeforeSignIn(
239 base::android::AttachCurrentThread(),
240 java_signin_manager_.obj());
243 #endif
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
269 // everything.
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,
279 jobject obj) {
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,
300 jobject obj,
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);
306 #else
307 return false;
308 #endif
311 // static
312 bool SigninManagerAndroid::Register(JNIEnv* env) {
313 return RegisterNativesImpl(env);