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.
4
5#ifndef CHROME_BROWSER_ANDROID_SIGNIN_SIGNIN_MANAGER_ANDROID_H_
6#define CHROME_BROWSER_ANDROID_SIGNIN_SIGNIN_MANAGER_ANDROID_H_
7
8#include <jni.h>
9
10#include <string>
11
12#include "base/android/scoped_java_ref.h"
13#include "base/basictypes.h"
14#include "base/memory/scoped_ptr.h"
15#include "base/memory/weak_ptr.h"
16#include "base/prefs/pref_change_registrar.h"
17#include "google_apis/gaia/merge_session_helper.h"
18
19class GoogleServiceAuthError;
20class Profile;
21
22namespace policy {
23class CloudPolicyClient;
24}
25
26// Android wrapper of the SigninManager which provides access from the Java
27// layer. Note that on Android, there's only a single profile, and therefore
28// a single instance of this wrapper. The name of the Java class is
29// SigninManager.
30// This class should only be accessed from the UI thread.
31//
32// This class implements parts of the sign-in flow, to make sure that policy
33// is available before sign-in completes.
34class SigninManagerAndroid : public MergeSessionHelper::Observer {
35 public:
36  SigninManagerAndroid(JNIEnv* env, jobject obj);
37
38  // Registers the SigninManagerAndroid's native methods through JNI.
39  static bool Register(JNIEnv* env);
40
41  void CheckPolicyBeforeSignIn(JNIEnv* env, jobject obj, jstring username);
42
43  void FetchPolicyBeforeSignIn(JNIEnv* env, jobject obj);
44
45  void OnSignInCompleted(JNIEnv* env, jobject obj, jstring username);
46
47  void SignOut(JNIEnv* env, jobject obj);
48
49  base::android::ScopedJavaLocalRef<jstring> GetManagementDomain(JNIEnv* env,
50                                                                 jobject obj);
51
52  void WipeProfileData(JNIEnv* env, jobject obj);
53
54  void LogInSignedInUser(JNIEnv* env, jobject obj);
55
56  void ClearLastSignedInUser(JNIEnv* env, jobject obj);
57
58  jboolean IsSigninAllowedByPolicy(JNIEnv* env, jobject obj);
59
60 private:
61  virtual ~SigninManagerAndroid();
62
63#if defined(ENABLE_CONFIGURATION_POLICY)
64  void OnPolicyRegisterDone(const std::string& dm_token,
65                            const std::string& client_id);
66  void OnPolicyFetchDone(bool success);
67#endif
68
69  void OnBrowsingDataRemoverDone();
70
71  void ClearLastSignedInUser();
72
73  void OnSigninAllowedPrefChanged();
74
75  // MergeSessionHelper::Observer implementation.
76  virtual void MergeSessionCompleted(
77      const std::string& account_id,
78      const GoogleServiceAuthError& error) OVERRIDE;
79
80  Profile* profile_;
81
82  // Java-side SigninManager object.
83  base::android::ScopedJavaGlobalRef<jobject> java_signin_manager_;
84
85#if defined(ENABLE_CONFIGURATION_POLICY)
86  // CloudPolicy credentials stored during a pending sign-in, awaiting user
87  // confirmation before starting to fetch policies.
88  std::string dm_token_;
89  std::string client_id_;
90
91  // Username that is pending sign-in. This is used to extract the domain name
92  // for the policy dialog, when |username_| corresponds to a managed account.
93  std::string username_;
94#endif
95
96  // Helper to merge the signed into account into the cookie jar session.
97  scoped_ptr<MergeSessionHelper> merge_session_helper_;
98
99  PrefChangeRegistrar pref_change_registrar_;
100
101  base::WeakPtrFactory<SigninManagerAndroid> weak_factory_;
102
103  DISALLOW_COPY_AND_ASSIGN(SigninManagerAndroid);
104};
105
106#endif  // CHROME_BROWSER_ANDROID_SIGNIN_SIGNIN_MANAGER_ANDROID_H_
107