Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ios / public / provider / chrome / browser / signin / chrome_identity_service.h
blobc80cf2fdc5c29d0cffb8496ab7800e231d03e5fb
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_
8 #include <set>
9 #include <string>
10 #include <vector>
12 #include "base/macros.h"
13 #include "base/observer_list.h"
15 @class ChromeIdentity;
16 @class NSArray;
17 @class NSDate;
18 @class NSError;
19 @class NSString;
20 @class UIImage;
22 namespace ios {
24 // Callback passed to method |SigninIdentity()|.
25 typedef void (^SigninIdentityCallback)(ChromeIdentity* identity,
26 NSError* error);
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,
31 NSDate* expiration,
32 NSError* error);
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 {
46 public:
47 // Observer handling events related to the ChromeIdentityService.
48 class Observer {
49 public:
50 Observer() {}
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) {}
64 private:
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
72 // identitites.
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()
86 const;
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
96 // accounts.
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
103 // the main thread.
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);
138 protected:
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);
150 private:
151 base::ObserverList<Observer, true> observer_list_;
153 DISALLOW_COPY_AND_ASSIGN(ChromeIdentityService);
156 } // namespace ios
158 #endif // IOS_PUBLIC_PROVIDER_CHROME_BROWSER_SIGNIN_CHROME_IDENTITY_SERVICE_H_