1 // Copyright 2019 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 COMPONENTS_SIGNIN_PUBLIC_IDENTITY_MANAGER_IDENTITY_MUTATOR_H_
6 #define COMPONENTS_SIGNIN_PUBLIC_IDENTITY_MANAGER_IDENTITY_MUTATOR_H_
7 
8 #include <memory>
9 
10 #include "build/build_config.h"
11 #if defined(OS_ANDROID)
12 #include "base/android/jni_android.h"
13 #endif
14 
15 namespace signin {
16 class AccountsMutator;
17 class AccountsCookieMutator;
18 class PrimaryAccountMutator;
19 class DeviceAccountsSynchronizer;
20 
21 #if defined(OS_ANDROID)
22 class IdentityMutator;
23 
24 // This class is the JNI interface accessing IdentityMutator.
25 // This is created by IdentityMutator and can only be accessed by JNI generated
26 // code (IdentityMutator_jni.h), i.e. by IdentityMutator.java.
27 class JniIdentityMutator {
28  public:
29   // JniIdentityMutator is non-copyable, non-movable
30   JniIdentityMutator(IdentityMutator&& other) = delete;
31   JniIdentityMutator const& operator=(IdentityMutator&& other) = delete;
32 
33   JniIdentityMutator(const IdentityMutator& other) = delete;
34   JniIdentityMutator const& operator=(const IdentityMutator& other) = delete;
35 
36   // Called by java to mark the account with |account_id| as the primary
37   // account, and return whether the operation succeeded or not. To succeed,
38   // this requires that:
39   //   - the account is known by the IdentityManager.
40   //   - setting the primary account is allowed,
41   //   - the account username is allowed by policy,
42   //   - there is not already a primary account set.
43   bool SetPrimaryAccount(
44       JNIEnv* env,
45       const base::android::JavaParamRef<jobject>& primary_account_id,
46       jint consent_level);
47 
48   // Called by java to clear the primary account, and return whether the
49   // operation succeeded or not. Depending on |action|, the other accounts known
50   // to the IdentityManager may be deleted.
51   bool ClearPrimaryAccount(JNIEnv* env,
52                            jint action,
53                            jint source_metric,
54                            jint delete_metric);
55 
56   // Called by java to reload the accounts in the token service from the system
57   // accounts.
58   void ReloadAllAccountsFromSystemWithPrimaryAccount(
59       JNIEnv* env,
60       const base::android::JavaParamRef<jobject>& primary_account_id);
61 
62  private:
63   friend IdentityMutator;
64 
65   JniIdentityMutator(IdentityMutator* identity_mutator);
66 
67   IdentityMutator* identity_mutator_;
68 };
69 #endif
70 
71 // IdentityMutator is the mutating interface for IdentityManager.
72 class IdentityMutator {
73  public:
74   IdentityMutator(
75       std::unique_ptr<PrimaryAccountMutator> primary_account_mutator,
76       std::unique_ptr<AccountsMutator> accounts_mutator,
77       std::unique_ptr<AccountsCookieMutator> accounts_cookie_mutator,
78       std::unique_ptr<DeviceAccountsSynchronizer> device_accounts_synchronizer);
79 
80   virtual ~IdentityMutator();
81 
82   // IdentityMutator is non-copyable, non-moveable.
83   IdentityMutator(IdentityMutator&& other) = delete;
84   IdentityMutator const& operator=(IdentityMutator&& other) = delete;
85 
86   IdentityMutator(const IdentityMutator& other) = delete;
87   IdentityMutator const& operator=(const IdentityMutator& other) = delete;
88 
89 #if defined(OS_ANDROID)
90   // Get the reference on the java IdentityManager.
91   base::android::ScopedJavaLocalRef<jobject> GetJavaObject();
92 #endif
93 
94   // Returns pointer to the object used to change the signed-in state of the
95   // primary account, if supported on the current platform. Otherwise, returns
96   // null.
97   PrimaryAccountMutator* GetPrimaryAccountMutator();
98 
99   // Returns pointer to the object used to seed accounts and mutate state of
100   // accounts' refresh tokens, if supported on the current platform. Otherwise,
101   // returns null.
102   AccountsMutator* GetAccountsMutator();
103 
104   // Returns pointer to the object used to manipulate the cookies stored and the
105   // accounts associated with them. Guaranteed to be non-null.
106   AccountsCookieMutator* GetAccountsCookieMutator();
107 
108   // Returns pointer to the object used to seed accounts information from the
109   // device-level accounts. May be null if the system has no such notion.
110   DeviceAccountsSynchronizer* GetDeviceAccountsSynchronizer();
111 
112  private:
113 #if defined(OS_ANDROID)
114   // C++ endpoint for identity mutator calls originating from java.
115   std::unique_ptr<JniIdentityMutator> jni_identity_mutator_;
116 
117   // Java-side IdentityMutator object.
118   base::android::ScopedJavaGlobalRef<jobject> java_identity_mutator_;
119 #endif
120 
121   // PrimaryAccountMutator instance. May be null if mutation of the primary
122   // account state is not supported on the current platform.
123   std::unique_ptr<PrimaryAccountMutator> primary_account_mutator_;
124 
125   // AccountsMutator instance. May be null if mutation of accounts is not
126   // supported on the current platform.
127   std::unique_ptr<AccountsMutator> accounts_mutator_;
128 
129   // AccountsCookieMutator instance. Guaranteed to be non-null, as this
130   // functionality is supported on all platforms.
131   std::unique_ptr<AccountsCookieMutator> accounts_cookie_mutator_;
132 
133   // DeviceAccountsSynchronizer instance.
134   std::unique_ptr<DeviceAccountsSynchronizer> device_accounts_synchronizer_;
135 };
136 
137 }  // namespace signin
138 
139 #endif  // COMPONENTS_SIGNIN_PUBLIC_IDENTITY_MANAGER_IDENTITY_MUTATOR_H_
140