1 // Copyright 2015 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 #ifndef IOS_PUBLIC_PROVIDER_CHROME_BROWSER_SIGNIN_CHROME_IDENTITY_SERVICE_H_
6 #define IOS_PUBLIC_PROVIDER_CHROME_BROWSER_SIGNIN_CHROME_IDENTITY_SERVICE_H_
12 #include "base/macros.h"
13 #include "base/observer_list.h"
15 @
class ChromeIdentity
;
24 // Callback passed to method |SigninIdentity()|.
25 typedef void (^SigninIdentityCallback
)(ChromeIdentity
* identity
,
28 // Callback passed to method |GetAccessTokenForScopes()| that returns the
29 // information of the obtained access token to the caller.
30 typedef void (^AccessTokenCallback
)(NSString
* token
,
34 // Callback passed to method |ForgetIdentity()|. |error| is nil if the operation
35 // completed with success.
36 typedef void (^ForgetIdentityCallback
)(NSError
* error
);
38 // Callback passed to method |GetAvatarForIdentity()|.
39 typedef void (^GetAvatarCallback
)(UIImage
* avatar
);
41 // Describes the reason for an access token error.
42 enum class AccessTokenErrorReason
{ INVALID_GRANT
, UNKNOWN_ERROR
};
44 // ChromeIdentityService abstracts the signin flow on iOS.
45 class ChromeIdentityService
{
47 // Observer handling events related to the ChromeIdentityService.
51 virtual ~Observer() {}
53 // Handles identity list changed events.
54 virtual void OnIdentityListChanged() {}
56 // Handles access token refresh failed events.
57 // |identity| is the the identity for which the access token refresh failed.
58 virtual void OnAccessTokenRefreshFailed(ChromeIdentity
* identity
,
59 AccessTokenErrorReason error
) {}
61 // Called when profile information or the profile image is updated.
62 virtual void OnProfileUpdate(ChromeIdentity
* identity
) {}
65 DISALLOW_COPY_AND_ASSIGN(Observer
);
68 ChromeIdentityService();
69 virtual ~ChromeIdentityService();
71 // Returns YES if |identity| is valid and if the service has it in its list of
73 virtual bool IsValidIdentity(ChromeIdentity
* identity
) const;
75 // Returns the chrome identity having the email equal to |email| or |nil| if
76 // no matching identity is found.
77 virtual ChromeIdentity
* GetIdentityWithEmail(const std::string
& email
) const;
79 // Returns the chrome identity having the gaia ID equal to |gaia_id| or |nil|
80 // if no matching identity is found.
81 virtual ChromeIdentity
* GetIdentityWithGaiaID(
82 const std::string
& gaia_id
) const;
84 // Returns the canonicalized emails for all identities.
85 virtual std::vector
<std::string
> GetCanonicalizeEmailsForAllIdentities()
88 // Returns true if there is at least one identity.
89 virtual bool HasIdentities() const;
91 // Returns all ChromeIdentity objects in an array.
92 virtual NSArray
* GetAllIdentities() const;
94 // Returns all ChromeIdentity objects sorted by the ordering used in the
95 // account manager, which is typically based on the keychain ordering of
97 virtual NSArray
* GetAllIdentitiesSortedForDisplay() const;
99 // Forgets the given identity on the device. This method logs the user out.
100 // It is asynchronous because it needs to contact the server to revoke the
101 // authentication token.
102 // This may be called on an arbitrary thread, but callback will always be on
104 virtual void ForgetIdentity(ChromeIdentity
* identity
,
105 ForgetIdentityCallback callback
);
107 // Asynchronously retrieves access tokens for the given identity and scopes.
108 // Uses the default client id and client secret.
109 virtual void GetAccessToken(ChromeIdentity
* identity
,
110 const std::set
<std::string
>& scopes
,
111 const AccessTokenCallback
& callback
);
113 // Asynchronously retrieves access tokens for the given identity and scopes.
114 virtual void GetAccessToken(ChromeIdentity
* identity
,
115 const std::string
& client_id
,
116 const std::string
& client_secret
,
117 const std::set
<std::string
>& scopes
,
118 const AccessTokenCallback
& callback
);
120 // Allow the user to sign in with an identity already seen on this device.
121 virtual void SigninIdentity(ChromeIdentity
* identity
,
122 SigninIdentityCallback callback
);
124 // Fetches the profile avatar, from the cache or the network.
125 // For high resolution iPads, returns large images (200 x 200) to avoid
126 // pixelization. Calls back on the main thread.
127 virtual void GetAvatarForIdentity(ChromeIdentity
* identity
,
128 GetAvatarCallback callback
);
130 // Synchronously returns any cached avatar, or nil.
131 // GetAvatarForIdentity() should be generally used instead of this method.
132 virtual UIImage
* GetCachedAvatarForIdentity(ChromeIdentity
* identity
);
134 // Adds and removes observers.
135 void AddObserver(Observer
* observer
);
136 void RemoveObserver(Observer
* observer
);
139 // Fires |OnIdentityListChanged| on all observers.
140 void FireIdentityListChanged();
142 // Fires |OnAccessTokenRefreshFailed| on all observers, with the corresponding
143 // identity and error reason.
144 void FireAccessTokenRefreshFailed(ChromeIdentity
* identity
,
145 AccessTokenErrorReason error
);
147 // Fires |OnProfileUpdate| on all observers, with the corresponding identity.
148 void FireProfileDidUpdate(ChromeIdentity
* identity
);
151 base::ObserverList
<Observer
, true> observer_list_
;
153 DISALLOW_COPY_AND_ASSIGN(ChromeIdentityService
);
158 #endif // IOS_PUBLIC_PROVIDER_CHROME_BROWSER_SIGNIN_CHROME_IDENTITY_SERVICE_H_